We help IT Professionals succeed at work.

RegSetValueEx problem

rutledgj
rutledgj asked
on
I'm trying to set a value in the registry using this winapi call:

Private Declare Function RegSetValueEx Lib "advapi32" 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


hKey=HKEY_CURRENT_USER

lpValueName =  "SOFTWARE\Microsoft\Office\9.0\PowerPoint\Security\Level"


dwType = REG_DWORD

the call is:

rc = RegSetValueEx(hKey, lpValueName, _
                       0, REG_DWORD, _
                       "1", 4)

I'm trying to change the current value to 1. When done manually, the value is hex. When I do it with this call it changes it to 0x00000031

Apparently I'm not sending the value correctly. I also tried sending it as a long.

Any suggestions?
Comment
Watch Question

Commented:
Almost correctly, but value "1" should have the type Long.

hKey=HKEY_CURRENT_USER
lpValueName = "SOFTWARE\Microsoft\Office\9.0\PowerPoint\Security\Level"
dwType = REG_DWORD
the call is:
rc = RegSetValueEx (hKey, lpValueName, _
0, REG_DWORD, _
Clng (1), 4)
Hi,

Change your declaration to

Declare Function apiRegSetValueEx 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

Then, your call to:

lResult = apiRegSetValueEx(hKey, sValue, Reserved, REG_DWORD, ByVal lData, 4)

Where lData is a long value, it's coerced by VB to the correct one, i'de have to run it through the checker to see what value it goes into, but aint got time for that yet.

Hope it helps.

Grtz.©

D.
A comment on the coercion, according to some research the API itself treats it as a unsigned char * lpdata. Thus, a pointer to a memory location; nothing more nothing less. The one call to a function that only takes a key handle, a string name and a long value does exactly this:

- _vbaStrToUnicode
- _vbaStrToAnsi
- DLLFunctionCall
- _vbaFreeStr
- RegSetValueExA
- _vbaStrCopy
- _vbaSetSystemError

What this tells, well, nothing realy, just thought I'de be interesting.. No, in fact, one would almost say that the Any is a string type variable. But i'm not sure on that, i base it on the fact that I give one string argument, and end up with two internal string conversions. Perhaps another expert can provide us with more detailed views.

Grtz.©

D.
HI,
Please help me to do the same in C#...
I am not able to set the value for REG_DWORD. How can we pass the correct paramter,
Thanks
Nebu

Explore More ContentExplore courses, solutions, and other research materials related to this topic.