Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I put an application in the startup????

Status
Not open for further replies.

nitika

Programmer
Mar 22, 2002
21
0
0
IN
Hi

How can we put an application in the startup through VB code so that It gets run Automatically as soon as the windows starts.
Also Can We put A part of the application in the startup Say only one form in the startup.
If possible then plz tell me.

Any help appreciated



 
Hi nitika,

just create a registry key at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

or

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

bye
LauDse
 
Hi Laudse

Can u tell me exactly how can I create a registry key as I have never worked with registries.
Does It ned working with any API' or what???

thanx in advance

Nitika
 
Hi nitika,

here is the code for creating a registry key:

Const ERROR_SUCCESS = 0&

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

Function RegCreateKey(ByVal hKey As Long, ByVal SubKey As String, hResult As Long) As Boolean
SubKey = SubKey & vbNullChar
RegCreateKey = APIRegCreateKey(hKey, SubKey, hResult) = ERROR_SUCCESS
End Function

bye
LauDse
 
Hi nitika!

sorry, here is the whole example;)

The sub Test at the end creates an entry in the registry.
This should start the notepad one time after reboot!

Const ERROR_SUCCESS = 0&
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Private Declare Function Reg_OpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function Reg_SetValueEx 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 Declare Function Reg_CloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long

Function MakeNullTerminated(ByVal str As String) As String
If Len(str) > 0 Then
If Right(str, 1) <> vbNullChar Then MakeNullTerminated = str & vbNullChar
Else
MakeNullTerminated = vbNullChar
End If
End Function

Function RegOpenKey(ByVal hKey As Long, ByVal SubKey As String, hResult As Long) As Boolean
SubKey = MakeNullTerminated(SubKey)
RegOpenKey = Reg_OpenKey(hKey, SubKey, hResult) = ERROR_SUCCESS
End Function

Function RegSetValueEx(ByVal hKey As Long, ByVal ValueName As String, ByVal StrValue As String) As Boolean
Dim StrLength As String

ValueName = MakeNullTerminated(ValueName)
StrValue = MakeNullTerminated(StrValue)
StrLength = Len(StrValue)
RegSetValueEx = Reg_SetValueEx(hKey, ValueName, 0, REG_SZ, ByVal StrValue, StrLength) = ERROR_SUCCESS
End Function

Public Function RegCloseKey(ByVal hKey As Long) As Boolean
RegCloseKey = Reg_CloseKey(hKey) = ERROR_SUCCESS
End Function

Sub Test()
Dim hKey As Long

If RegOpenKey(HKEY_LOCAL_MACHINE, &quot;SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce&quot;, hKey) Then
RegSetValueEx hKey, &quot;Test&quot;, &quot;notepad.exe&quot;
RegCloseKey hKey
End If
End Sub

bye
LauDse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top