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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About RegOpenKeyEx with VB6 1

Status
Not open for further replies.

Ajay011

Vendor
Dec 21, 2010
16
NP
I was using VBScript method of registrywriting which is more than easy. But, when I am trying to write to registry through RegOpenKeyEx, I am having problem(s).
Here is what I tried:
'_____________________________________________________
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hkey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" _
(ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, ByRef phkResult 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 HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ As Long = 1
Private Const KEY_ALL_ACCESS = &H2003F

Private Sub Form_Load()
Dim hkey As Long, ret As Long, rett As Long
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_ALL_ACCESS, hkey)
rett = RegSetValueEx(hkey, App.EXEName, 0, REG_SZ, "e:\myprog.exe" & vbNullChar, Len("e:\myprog.exe") + 1)
RegCloseKey (hkey)
End Sub
'_____________________________________________________

It does make the SubKey but the value is something like:

,qT instead of e:\myprog.exe
If you have a sample code then please post one...
Thanks.
 
RegSetValueEx function has lpData parameter declared by reference.

When passing a string to this function, you need to pass it by value, otherwise a pointer to the pointer of string is passed to the function. That is why you see ,qT or something gibberish in the registry.

Just modify the call to RegSetValueEx as follows.

[tt]rett = RegSetValueEx(hkey, App.EXEName, 0, REG_SZ, ByVal "e:\myprog.exe" & vbNullChar, Len("e:\myprog.exe") + 1)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top