Auto Run Registry Entry

Submitted on: 1/11/2015 11:32:00 AM
By: Dean lancaster (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: VB 6.0
Views: 482
     If you have ever needed to have your program start at the same time as windows starts without putting the program into the start up menu but have been unable to figure out how to do it then just follow the following text.

				What you have to do to use this code is:
In your project (for example, form_load())
insert the following text somewhere that would be useful to your project: 

Call SetStringValue(HKEY_LOCAL_MACHINE, "Software\microsoft\windows\currentversion\run", "Currency", App.Path + "\" + App.EXEName + ".exe")

This code will put the current project path and the current project name in the windows auto run section in the windows registry.
The benfit of this is so that you can have your program start as windows starts.
Add to your project a module

project -> add module

and insert the following code

Public Exist As Boolean

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&
Public Const REG_SZ = 1

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long

Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long

Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

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

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

After this has been added also add the following code to your module file

Public Sub SetStringValue(Hkey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim i As Long
 'Create the key
 i = RegCreateKey(Hkey, strPath, keyhand)
 'Set the value
 i = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
 'Close the key
 i = RegCloseKey(keyhand)
End Sub

This then should allow the setstringvalue to be accessed and the information you need be put into the windows auto run registry.
I must thank T. L. Phillips for the code for the module form, that is where i optained it.
Hope you like the code! :)
if you need help with it (although you shouldn't) mail me :)
(As for the Link to mr phillips's website, that is beyond my control and the link that is on his source submissions)

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.