help reading a key from the register: RAPI

R

rocio

I wrote a key to the registry of my PPC using the OpenNETCF.Win32 utility (this is a VB.NET program that runs in the PPC itself). This key is but the Device ID that I also get using RAPI in VB.NET. I store this id in \HKEY_LOCAL_MACHINE\HARDWARE\UUID\DeviceID=123-456-789

where the value for DeviceID is the actual id of my PPC.
I verified that this entry is indeed in the registry by reading the subkey "HARDWARE\UUID" with OpenNETCF.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\UUID").GetValue("DeviceID") .

Now from a desktop application (aka vb6 now), I need to read this value to identify the PPC I'm conecting to. I'm using the RAPI functions: CeRegOpenKey and CeRegQueryValueEx.

CeRegOpenKey opens the registry at HKEY_LOCAL_MACHINE and returns a handle that is used by CeRegQueryValueEx. CeRegQueryValueEx runs successfully, I can confirm that it reads a string value of size 72 bytes ....... but the actual value it reads is empty:

Public Declare Function CeRegQueryValueEx Lib "rapi.dll" ( _
ByVal hkey As Long, _
ByVal lpValueName As Long, _
ByVal lpReserved As Long, _
lptype As Long, _
ByVal lpData As Long, _
lpcbData As Long) As Long

Public Function GetCERegistryValue(ByRef hkey As Long, ByRef value As String) As Long
Dim subkey As String
Dim lpData(1000) As Byte
Dim cbData As Long
Dim lptype As Long

subkey = "DeviceID"
GetCERegistryValue = CeRegQueryValueEx(hkey, StrPtr(subkey), 0, lptype, lpData(0), cbData)
If GetCERegistryValue <> ERROR_SUCCESS Then
value = UnicodeToAnsiByte(lpData)
Exit Function
Else
value = UnicodeToAnsiByte(lpData)
End If

End Function

Private Function UnicodeToAnsiByte(bString() As Byte) As String
Dim index As Integer
Dim stemp As String
stemp = ""
While Not (bString(index) = 0 And bString(index + 1) = 0)
stemp = stemp & Chr(bString(index))
index = index + 2
Wend

UnicodeToAnsiByte = stemp
End Function
I put a breakdown at the red line to see what is stored in the variables lptype, lpData, and cbData.

lpData should store the value of DeviceID, but it does not. It returns an empty array. However, lptype is set to 1, which means it read a "null terminated string", and cbdata is set to 72, or 72 bytes for whatever it read (this is the actual value of the ID = E60A00E6-105F-E135-0038-0050BFE45CE5)

what am I doing wrong here? any ideas?
 
C

Chris Tacke, eMVP

If I recall correctly, in the query call:

CeRegQueryValueEx(hkey, StrPtr(subkey), 0, lptype, lpData(0), cbData)

cbData must be initialized to the size of the buffer pointed to by lpData. In your code it's not set, so will be initialized to zero. My bet is a call to CeGetLastError will return 234, or "more data exists".

set cbData to 1000 *the size of your byte array) before the call.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


I wrote a key to the registry of my PPC using the OpenNETCF.Win32 utility (this is a VB.NET program that runs in the PPC itself). This key is but the Device ID that I also get using RAPI in VB.NET. I store this id in \HKEY_LOCAL_MACHINE\HARDWARE\UUID\DeviceID=123-456-789

where the value for DeviceID is the actual id of my PPC.
I verified that this entry is indeed in the registry by reading the subkey "HARDWARE\UUID" with OpenNETCF.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\UUID").GetValue("DeviceID") .

Now from a desktop application (aka vb6 now), I need to read this value to identify the PPC I'm conecting to. I'm using the RAPI functions: CeRegOpenKey and CeRegQueryValueEx.

CeRegOpenKey opens the registry at HKEY_LOCAL_MACHINE and returns a handle that is used by CeRegQueryValueEx. CeRegQueryValueEx runs successfully, I can confirm that it reads a string value of size 72 bytes ....... but the actual value it reads is empty:

Public Declare Function CeRegQueryValueEx Lib "rapi.dll" ( _
ByVal hkey As Long, _
ByVal lpValueName As Long, _
ByVal lpReserved As Long, _
lptype As Long, _
ByVal lpData As Long, _
lpcbData As Long) As Long

Public Function GetCERegistryValue(ByRef hkey As Long, ByRef value As String) As Long
Dim subkey As String
Dim lpData(1000) As Byte
Dim cbData As Long
Dim lptype As Long

subkey = "DeviceID"
GetCERegistryValue = CeRegQueryValueEx(hkey, StrPtr(subkey), 0, lptype, lpData(0), cbData)
If GetCERegistryValue <> ERROR_SUCCESS Then
value = UnicodeToAnsiByte(lpData)
Exit Function
Else
value = UnicodeToAnsiByte(lpData)
End If

End Function

Private Function UnicodeToAnsiByte(bString() As Byte) As String
Dim index As Integer
Dim stemp As String
stemp = ""
While Not (bString(index) = 0 And bString(index + 1) = 0)
stemp = stemp & Chr(bString(index))
index = index + 2
Wend

UnicodeToAnsiByte = stemp
End Function
I put a breakdown at the red line to see what is stored in the variables lptype, lpData, and cbData.

lpData should store the value of DeviceID, but it does not. It returns an empty array. However, lptype is set to 1, which means it read a "null terminated string", and cbdata is set to 72, or 72 bytes for whatever it read (this is the actual value of the ID = E60A00E6-105F-E135-0038-0050BFE45CE5)

what am I doing wrong here? any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top