-
Windows Startup Toughie!
Hello,
First things first, this is not my code I'm working with, the credit goes to some genius who spent his time writing it. Congrats.
I have been struggling on the issue of editing/manipulating the windows startup order. I have a program that, when loaded, places itself in the windows startup routine, (these programs are viewed by running "msconfig"), and when this program is unloaded, it removes itself from this startup routine. Everything works very nicely except for one thing, the user still has the power to maneuver around in windows for some time before my program runs. I know this is because it is too far down in the order, (other programs are loaded first), so now to my question. How do I get my program to load first, how can I say "as well as putting yourself on the startup menu, run yourself first thing!"
If anyone can change my current code to make this happen, or tell me how to change it, I would be very grateful.
Here is what I have, I call the procedure "MakeStartUp( My Program Name )" to put it in, and "DeleteFromStartup( My Program Name )" to remove it.
This goes in a module.
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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
Public 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
Public Const REG_SZ = 1
Public Const REG_DWORD = 4
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&
Const UNIT_NAME = "UTILITY"
Public Sub MakeStartUp(FileName As String)
Dim Counter As Integer
Dim MarkPos As Integer
Dim Application As String
Application = GetFileName(FileName)
Application = Left(Application, (Len(Application) - 4)) 'Replace(Application, ".exe", "", , , vbTextCompare) & "~@#"
Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", Application, FileName)
End Sub
Public Sub SaveKey(Hkey As Long, strPath As String)
Dim KeyHand&
Dim r As Long
r = RegCreateKey(Hkey, strPath, KeyHand&)
r = RegCloseKey(KeyHand&)
End Sub
Public Function GetString(Hkey As Long, strPath As String, strValue As String)
Dim KeyHand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim r As Long
Dim lValueType As Long
r = RegOpenKey(Hkey, strPath, KeyHand)
lResult = RegQueryValueEx(KeyHand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(KeyHand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left(strBuf, intZeroPos - 1)
Else
GetString = strBuf
End If
End If
End If
End Function
Public Sub SaveString(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))
r = RegCloseKey(KeyHand)
End Sub
Function GetDWord(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim KeyHand As Long
r = RegOpenKey(Hkey, strPath, KeyHand)
lDataBufSize = 4
lResult = RegQueryValueEx(KeyHand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetDWord = lBuf
End If
End If
r = RegCloseKey(KeyHand)
End Function
Function SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim lResult As Long
Dim KeyHand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, KeyHand)
lResult = RegSetValueEx(KeyHand, strValueName, 0&, REG_DWORD, lData, 4)
r = RegCloseKey(KeyHand)
End Function
Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
Dim r As Long
r = RegDeleteKey(Hkey, strKey)
End Function
Public 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 Sub DeleteFromStartup(FileName As String)
Dim Counter As Integer
Dim MarkPos As Integer
Dim Application As String
Application = GetFileName(FileName)
Application = Left(Application, (Len(Application) - 4)) 'Replace(Application, ".exe", "", , , vbTextCompare) & "~@#"
Call DeleteValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", Application)
End Sub
Public Function GetFileName(Path As String) As String
Dim Counter As Integer
Dim LastPos As Integer
LastPos = 1
For Counter = 1 To Len(Path)
If Mid(Path, Counter, 1) = "\" Then
LastPos = Counter
End If
Next Counter
GetFileName = Mid(Path, (LastPos + 1), Len(Path))
End Function
Then I call these two procedures at my leisure.
Call DeleteFromStartup(App.Path & "\" & App.EXEName & ".exe" & vbNullChar)
Call MakeStartUp(App.Path & "\" & App.EXEName & ".exe" & vbNullChar)
Thanks a lot - good luck!!!
-
According to MSDN documentation(Article ID: Q179365) all the values in the Run key are run asynchronously, that is all at once, so there is no way to make sure your application will start first in the Run list.
If this is crucial to your application, I'd suggest replacing the Shell with your application, this will cause your application to be loaded instead of the standard Windows Explorer interface, then when your application has finished starting up, you can load the normal Shell via your app, ensuring your application has time to load and be functional before anything else.
Here's a link where I described something similar and gave some example code: Shell Replacement
-
Thank you for replying. I know you probably think this is pretty lame of me, but is there any way you could guide me a little more about how to do this. It seems pretty complicated to me.
Thanks again!