PDA

Click to See Complete Forum and Search --> : Reg_dword


aatwell
Mar 7th, 2001, 10:21 AM
Folks,

With the following Module how do I add a Hexadecimal value? When I change REG_SZ to REG_DWORD, I get a binary value. I need to have a hex value. Please help

aatwell
--------------------------------------------------------

Add to a Module


code:--------------------------------------------------------------------------------
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, 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 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 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 REG_SZ = 1

Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)
Dim lResult As Long
Dim lValueType As Long
Dim strBuf As String
Dim lDataBufSize As Long

On Error GoTo 0
lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
RegQueryStringValue = StripTerminator(strBuf)
End If
End If
End If
End Function

Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
Dim KeyHand&
Dim datatype&
Call RegOpenKey(HKEY, sPath, KeyHand&)
GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
Call RegCloseKey(KeyHand&)
End Function

Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer

intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function

Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
Dim KeyHand As Long
Call RegCreateKey(HKEY, sPath, KeyHand)
Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))
Call RegCloseKey(KeyHand&)
End Sub
--------------------------------------------------------------------------------


Usage:

code:--------------------------------------------------------------------------------
'Save a Value
SaveSettingEx HKEY_CURRENT_USER, "Software\Myapp", "Testing", "Hello"


'Get a value
Retval = GetSettingEx(HKEY_CURRENT_USER, "Software\Myapp", "Testing")
Print Retval

Mar 7th, 2001, 02:47 PM
Make sure you have the constant right as well. Form what I see, you do not have the constant declared, nor is it changed in the code.

aatwell
Mar 7th, 2001, 09:23 PM
Megatron,

Even when I do declare it, it doesn't work. What I am
doing wrong? What value should the declaration take?
I change every occurance of REG_SZ to REG_DWORD with
a global replace and it doesn't work.

code:--------------------------------------------------------------------------------
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 REG_DWORD = 1: 'what value should this be?


And what should the usage be?

SaveSettingEx HKEY_LOCAL_MACHINE, "Software\MyApp", "MyVal", 0

Will this line add a hex value equal to zero into the reg?

Please help me?

aatwell

Mar 8th, 2001, 02:35 PM
you got the constant wrong; it should be:

Const REG_DWORD = 4

aatwell
Mar 9th, 2001, 07:21 AM
I tried using Public Const REG_DWORD = 4

but it gives me a REG_DWORD entry with a binary value and it tells me it has an invalid DWORD value.

I have tried the following usages:

SaveSettingEx HKEY_LOCAL_MACHINE, "Software\MyApp", "MyVal", 0

SaveSettingEx HKEY_LOCAL_MACHINE, "Software\MyApp", "MyVal", myval:' where i have tried declaring myval as (double,integer, long)

SaveSettingEx HKEY_LOCAL_MACHINE, "Software\MyApp", "MyVal", "0"

I am seriously exhausted with this Hexidecimal Registry Thing. Please help me find a solution. I am sure the problem is my own ignorance. But I'm trying hard to learn.

aatwell

Mar 9th, 2001, 02:23 PM
Sorry about that..Change the constant to 0 instead of 4.

aatwell
Mar 9th, 2001, 02:27 PM
I tried 0, 1, 2, 3, and 4 as the value for the constant and none worked.

parksie
Mar 9th, 2001, 02:40 PM
Binary = Decimal = Hex

It all depends on how you look at it...

However, here's an example project: http://www.parksie.net/registry.zip

aatwell
Mar 9th, 2001, 03:09 PM
Try adding a binary value to the Windows 98 registry at:

HKEY_LOCAL_MACHINE
Network
Logon
MustBeValidated

it doesn't like a binary value. The moment I put a hex value it works.

Hence my dilemna. I must complete this program by April 18, 2001 for my employer.

aatwell

Mar 9th, 2001, 05:27 PM
Hmmm..I tried it when I changed the constant to 0 and it showed up as a dword value, e.g:

84 58 49 32

parksie
Mar 9th, 2001, 05:29 PM
That's binary...it should show up as 0x00000000 (0) or similar.

aatwell
Mar 9th, 2001, 06:09 PM
Yup.

I get the same kind of invalid value.
It has to be 0x00000000 (0) in order
for this to work, hence the dilemna.

aatwell

aatwell
Mar 9th, 2001, 08:16 PM
I made some progress....

I am getting a hex value now....

I changed the following lines in the module

From: Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
To: Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As Long)

and

From: Call RegSetValueEx(KeyHand&, sValue, 4, REG_DWORD, ByVal sData, Len(sData))
To: Call RegSetValueEx(KeyHand&, sValue, 4, REG_DWORD, ByVal sData, Len(sData))

Then I used the usage

SaveSettingEx HKEY_LOCAL_MACHINE, "Software\MyApp", "MyVal", 1

only I got a strange hex value of

6500c90f

But at least it isn't a binary value and the machine recognizes it as a hex.

Any clues on the rest of this puzzle.

parksie
Mar 10th, 2001, 04:09 PM
Have a look at the code in my project...that works for strings & numbers.

aatwell
Mar 12th, 2001, 01:12 PM
Thanks a lot. That code did the trick. I am now able to add valid HEX values to the registry.

Not only is my hat off to you... but my head as well.


:) Thnks.