Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Start program when system boots

Posted on 1999-06-27
4
Medium Priority
?
132 Views
Last Modified: 2010-05-02
How can I make my program run every time when Windows is started?
0
Comment
Question by:SiriusP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:KDivad
ID: 1520039
Add a shortcut to the start menu, or add your program to the registry key: HKEY_CURRENT_USERS\Software\Microsoft\Windows\CurrentVersion\Run
0
 
LVL 1

Author Comment

by:SiriusP
ID: 1520040
ok, nothing may happen manually, it has got to be in the code.
and how do I add the key?
0
 
LVL 5

Accepted Solution

by:
KDivad earned 60 total points
ID: 1520041
Declaration section stuff:

Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Boolean
End Type

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, 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 RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition 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
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

'Registry data types
Global Const REG_BINARY = 3
Global Const REG_DWORD = 4
Global Const REG_DWORD_LITTLE_ENDIAN = 4
Global Const REG_DWORD_BIG_ENDIAN = 5
Global Const REG_EXPAND_SZ = 2
Global Const REG_LINK = 6
Global Const REG_MULTI_SZ = 7
Global Const REG_NONE = 0
Global Const REG_RESOURCE_LIST = 8
Global Const REG_SZ = 1

'Registry root keys
Global Const HKCU = &H80000001
Global Const HKLM = &H80000002
Global Const HKU = &H80000003
Global Const HKCC = &H80000005
Global Const HKDD = &H80000006

'Registry access level
Global Const KEY_ALL_ACCESS = &HF003F
Global Const KEY_CREATE_LINK = &H20
Global Const KEY_CREATE_SUB_KEY = &H4
Global Const KEY_ENUMERATE_SUB_KEYS = &H8
Global Const KEY_EXECUTE = &H20019
Global Const KEY_NOTIFY = &H10
Global Const KEY_QUERY_VALUE = &H1
Global Const KEY_READ = &H20019
Global Const KEY_SET_VALUE = &H2
Global Const KEY_WRITE = &H20006


Code:

x = RegOpenKeyEx(HKCU, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_READ, hRegKey)
If Result <> 0 Then
    MsgBox "Error opening key!"
    Exit Sub
End If
Dim ReturnValue As Long
x = RegSetValueExString(hRegKey, "My program to start", 0, REG_SZ, "C:\PathTo\My\Program.exe", Len("C:\PathTo\My\Program.exe"))
If x <> 0 Then
    MsgBox "Error writing value!"
    Exit Sub
End If
x = RegCloseKey(hRegKey)


This should do it, any questions?
0
 
LVL 5

Expert Comment

by:KDivad
ID: 1520042
Oops, two typos:

x = RegOpenKeyEx(HKCU, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_READ, hRegKey)
If Result <> 0 Then

should read:

x = RegOpenKeyEx(HKCU, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, hRegKey)
If x <> 0 Then

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question