Results 1 to 13 of 13

Thread: Running VB App in Background

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    3

    Running VB App in Background

    I am creating a program to capture the quantity of prints being produced by a printer. Is it possible to create a VB program that continously runs in the background to detect when the printer initiates? How? It seems like it would have to be an App that initiates when windows loads.

    Any and all help appreciated.

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    3
    Thanks Marty,

    That is very helpful to get me started. I am very much lacking in my API, and Registry knowledge though. Do you think if I read up on them more that they could show me how to create a program that:

    1.Loads up when the computer Operating System Loads
    2.Constantly monitors a peripherals status'

  4. #4
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    DAM! I had just the code for you to load a program on startup! And its been deleted because I never used it!

    That will teach me not to delete stuff in case i need it later...

    but heres what i found:

    VB Code:
    1. Option Explicit
    2. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
    3. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    4. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
    5. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
    6. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    7. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    8. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    9. Private Const REG_SZ = 1                         ' Unicode nul terminated string
    10. Private Const REG_DWORD = 4                      ' 32-bit number
    11. Private Const ERROR_SUCCESS = 0&
    12. Public Enum pvpHK
    13.     HKEY_CLASSES_ROOT = &H80000000
    14.     HKEY_CURRENT_USER = &H80000001
    15.     HKEY_LOCAL_MACHINE = &H80000002
    16.     HKEY_USERS = &H80000003
    17.     HKEY_PERFORMANCE_DATA = &H80000004
    18. End Enum
    19. Private Const pvpRunHKey = "Software\Microsoft\Windows\CurrentVersion\Run"
    20. Private Sub savestring(ByVal Hkey As Long, strPath As String, strValue As String, strData As String)
    21.     Dim keyhand As Long
    22.     Dim r As Long
    23.     r = RegCreateKey(Hkey, strPath, keyhand)
    24.     r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    25.     If r = 87 Then
    26.         'Tom streng! -> må slettes
    27.         DeleteValue Hkey, strPath, strValue
    28.     End If
    29.     r = RegCloseKey(keyhand)
    30. End Sub
    31. Private Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
    32.     Dim keyhand As Long
    33.     Dim r As Long
    34.     r = RegOpenKey(Hkey, strPath, keyhand)
    35.     r = RegDeleteValue(keyhand, strValue)
    36.     r = RegCloseKey(keyhand)
    37. End Function
    38. Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
    39.          savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, sAppName
    40. End Function
    41. 'usage
    42. Private Sub Command1_Click()
    43.     RunAtStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
    44. End Sub

    Thanks to 'Peet' who posted this somewhere on the forum. It followed exactly the same method as the code I had. Only difference is order of code and a few quotes lol so no difference in usage.

    That should make your program run when windows starts/user logs in

    Good luck with the next bit

    Edit: Oh yeah, and as for the running in background thing, its really easy. All you do is set your form to Invisible in properties. Its best you do that, because if you use code OnLoad then you see the form for a split second while it makes iteslf invisible

    If you want to do that, it would be

    Form1.Visible = False

    That way you will never see the form. But i still think you should change the Visible to False on the properties of the form instead of doing it at run time

    Code should run fine, i use it all the time for things in the background even sometimes for internet apps (which may sound malicious but it aint luckily lol)

    Good luck again hope my code and tip helped
    Last edited by LITHIA; Aug 3rd, 2003 at 04:03 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    3
    Thank you very much. Currently I have 1 Small VB and 2 Website(Flash) projects. So I am trying to handle them all at one time. It will take me a while to understand everything going on in that code but, if it loads programs to run at OS start it will be exactly what I need.

    So once again thanks.

  6. #6
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    if your thanks is directed at me, no problem. Glad i cud have helped

    good luck still

  7. #7
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252
    Hi there,

    To test LITHIA's code I copied it to a new Project. As it needed a Command1 button, I added one too.

    When i run the form, it says Variable not defined for sAppName

    VB Code:
    1. Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
    2.          savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, sAppName

    What I may be doing wrong? Thank you!

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    it's a minor typo, I think it should have been:

    VB Code:
    1. Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
    2.          savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, [b]strsAppName[/b]

  9. #9
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252
    Exactly mate! It worked like a charm after that.
    Yeah you were correct, the String was actually declared as strsAppName. didn't see that before lol.

    Checked it in msconfig and the Project1 has been added to Startup.

    One more thing to make this Thread complete and informative::

    How can we undo the operation?

    (something like this one. Note: this is doesn't actually work)
    VB Code:
    1. Public Function DoNotRunAtStartup(sAppTitle As String, strsAppName As String)
    2.          deletestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
    3.  
    4. Private Sub Command2_Click()
    5.     DoNotRunAtStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
    6. End Sub

    Appreciate your help.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I think you want to use the DeleteValue function that was provided

    how's this?
    VB Code:
    1. Public Function DoNotRunAtStartup(sAppTitle As String)
    2.          DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle
    3. End Function

  11. #11
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252
    What I was doing until now was trying different things mate, but now it gives Wrong Number of arguments. We are almost there.

    VB Code:
    1. Public Function DoNotRunAtStartup(sAppTitle As String)
    2.          DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
    3. End Function
    4.  
    5. Private Sub Command2_Click()
    6.  DoNotRunAtStartup pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
    7. End Sub

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    yep, I can see it would

    each function has a specific number of parameters, and you must call it with the same amount (and they must be the correct data types too).

    SaveString has these parameters:
    (ByVal Hkey As Long, strPath As String, strValue As String, strData As String)

    and DeleteValue has these:
    (ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)

    ignore the ByVal's (they just mean that if the function changes the values then the caller wont see those changes), you can see that DeleteValue does not need the final value - so for a start your DoNotRunAtStartup function has one parameter too many.

    Your code for Command2_click also calls this function with too many parameters (it should only have one), and doesn't pass the correct one either (it should send the application title).

    The code should be:
    VB Code:
    1. Public Function DoNotRunAtStartup(sAppTitle As String)
    2.    DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle
    3. End Function
    4.  
    5. Private Sub Command2_Click()
    6.    DoNotRunAtStartup App.Title
    7. End Sub

  13. #13
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252
    you da man!

    I will learn this lesson thoroughly tonight. thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width