|
-
Aug 3rd, 2003, 12:31 PM
#1
Thread Starter
New Member
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.
-
Aug 3rd, 2003, 12:44 PM
#2
I searched our forums for printer AND monitor and while a lot of the hits were off-topic here is an article from user MerrionComputin that should get you going.
-
Aug 3rd, 2003, 03:05 PM
#3
Thread Starter
New Member
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'
-
Aug 3rd, 2003, 03:59 PM
#4
Fanatic Member
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:
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
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
Private Const REG_SZ = 1 ' Unicode nul terminated string
Private Const REG_DWORD = 4 ' 32-bit number
Private Const ERROR_SUCCESS = 0&
Public Enum pvpHK
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
End Enum
Private Const pvpRunHKey = "Software\Microsoft\Windows\CurrentVersion\Run"
Private Sub savestring(ByVal Hkey As Long, strPath As String, strValue As String, strData As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
If r = 87 Then
'Tom streng! -> må slettes
DeleteValue Hkey, strPath, strValue
End If
r = RegCloseKey(keyhand)
End Sub
Private Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
Dim r As Long
r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function
Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, sAppName
End Function
'usage
Private Sub Command1_Click()
RunAtStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
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.
-
Aug 3rd, 2003, 04:24 PM
#5
Thread Starter
New Member
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.
-
Aug 8th, 2003, 01:27 PM
#6
Fanatic Member
if your thanks is directed at me, no problem. Glad i cud have helped
good luck still
-
Nov 19th, 2003, 08:28 AM
#7
Addicted Member
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:
Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, sAppName
What I may be doing wrong? Thank you!
-
Nov 19th, 2003, 09:08 AM
#8
it's a minor typo, I think it should have been:
VB Code:
Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, [b]strsAppName[/b]
-
Nov 19th, 2003, 09:20 AM
#9
Addicted Member
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:
Public Function DoNotRunAtStartup(sAppTitle As String, strsAppName As String)
deletestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
Private Sub Command2_Click()
DoNotRunAtStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
End Sub
Appreciate your help.
-
Nov 19th, 2003, 09:29 AM
#10
I think you want to use the DeleteValue function that was provided 
how's this?
VB Code:
Public Function DoNotRunAtStartup(sAppTitle As String)
DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle
End Function
-
Nov 19th, 2003, 09:51 AM
#11
Addicted Member
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:
Public Function DoNotRunAtStartup(sAppTitle As String)
DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
End Function
Private Sub Command2_Click()
DoNotRunAtStartup pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
End Sub
-
Nov 19th, 2003, 10:18 AM
#12
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:
Public Function DoNotRunAtStartup(sAppTitle As String)
DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle
End Function
Private Sub Command2_Click()
DoNotRunAtStartup App.Title
End Sub
-
Nov 19th, 2003, 10:31 AM
#13
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|