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!

Registry API with windows XP

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
0
0
US
I have a VB6 application which is developed on Windows Me that works just fine. Below are the API functions:

************

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 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

*****************

My code goes like this:

*****************

retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey2, keyID2)


If retValue = 0 Then

(Do somthing)

*****************

Problem is, that API call does not work WHEN RUNNING ON WINDOWS XP (retValue = 161 instead of 0 -- which it should be if successful). It works just fine on Windows Me. I suspect that there is an updated API that I should use instead of RegCreateKey.

Any help would be much appreciated.

Also any recommendations for books on API functions with windows XP would be met with gratitude.

Ortho




 
Quick suggestion:

try adding a '\' to the beginning of the regkey variable.

i.e.
Code:
regkey = "\" & regkey

MSDN suggests that this is a requirement for RegCreateKeyEx but is silent on the matter wrt RegCreateKey

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
could it be a permissions thing on XP? I'm not too sure about permissions on ME but with XP you have to have admin rights to touch parts of the registry.

regards

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
I have more information. First of all I found out that error 161 is "Bad File Path"

So I probably need to show you the variable values for RegKey and RegKey2, and they are:

RegKey = "\FirstSubKey\Item1"
RegKey2 = "\SOFTWARE\SecondSubKey\Item2"

So the problem is that these appear to be a VALID paths on Windows ME but not on XP.

Can anyone help

Ortho [cry]
 
I FIGURED IT OUT MYSELF! MattKnight was on the right track. Turns out that it was the backslash starting the middle variable which I called RegKey and RegKey2, that is "\SubKey...." was the problem.

Once I removed the "\", it worked like a charm.

MORAL OF THE STORY:

When trying to get this line of code to work:

retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)

In Windows ME you can get away with
regKey = "\SubKey1\Item1", etc

In Windows XP you CANNOT have the initial backslash, so the correct syntax is:

regKey = "SubKey1\Item1", etc.

So Matt, you were on the right track, just backwards. Thanks anyway -- your comment helped me to notice this in the MSDN library.

Ortho [2thumbsup]
 
Remove the backslash:
RegKey = "\FirstSubKey\Item1"
RegKey2 = "\SOFTWARE\SecondSubKey\Item2"


Change to:
RegKey = "FirstSubKey\Item1"
RegKey2 = "SOFTWARE\SecondSubKey\Item2"

And you cannot create a new Subkey directly under HKLM

RegKey = "FirstSubKey\Item1" Won't work.
 
LOL Looks like you typed at the same time I did.

For XP you should use this.
Public 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



I don't see you closing the handle.

Public Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top