Why RegQueryValueEx function work incorrect?

G

Guest

I want to get some infomation in the smartphone registry ,so I use the
RegQueryValueEx function to access the registry value ,but a question occur
,the function alway fail in the first time call,but will get the correct
Value in the second time.I'm a VB.net programmer ,I declare the function as
the follow cpde:
<DllImport("coredll.dll")> _
Public Shared Function RegQueryValueEx(ByVal hKey As Int32, _
ByVal lpValueName As String, _
ByVal lpReserved As Int32, _
ByRef lpType As Int32, _
ByVal lpData As Text.StringBuilder, _
ByRef lpcbData As Int32 _
) As Int32 ' Note that if you
declare the lpData parameter as String, you must pass it By Value.

End Function

and use it as following:

Public Function fnRegQueryValueToStringBuilder(ByVal ni_strValueName As
String, Optional ByRef ni_i32ValueType As Int32 = 0) As Text.StringBuilder
REM å–得指定注册表键的值
Dim ti32tempa As Int32
Dim ti32ValueType As Int32, ti32Datalength As Int32
Dim tobjValueData As New Text.StringBuilder(255)


'Error occur here.Below line alway fail,even I give the tobjValueData
enought space!
ti32tempa = RegQueryValueEx(Me.InnerRegKeyHandle, ni_strValueName, 0,
ni_i32ValueType, tobjValueData, ti32Datalength)

使用第一次读得的数æ®é•¿åº¦å†è¯»ä¸€æ¬¡å¯ä»¥ç¡®ä¿å¾—到正确的结果,但ä¸æ˜Žç™½ä¸ºä»€ä¹ˆç¬¬ä¸€æ¬¡è¯»æ— è®ºå¦‚何总是出错
tobjValueData.Capacity = ti32Datalength
'now ,this time the code will work perfectly .why ?
ti32tempa = RegQueryValueEx(Me.InnerRegKeyHandle, ni_strValueName, 0,
ni_i32ValueType, tobjValueData, ti32Datalength)


If ti32tempa = 0 Then
'tobjValueData.Length = ti32Datalength
Return (tobjValueData)
Else
Return Nothing
End If


End Function

Who can tell me the reason why I get this error ,thanks a lot!
 
M

MissileCat

Daniel Moth" <[email protected]> Thanks a lot .
But why I do the same thing in the Net Framework is different ,I call the
the RegQueryValue just one time and it already get the correct result .I n
the SDK help ,the document tell me :"If the buffer specified by lpData
parameter is not large enough to hold the data, the function returns the
value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into
the variable pointed to by lpcbData." But I already give the lpdata enough
space in this case ,the function behaviour is so odd .

The second thing is I want to know what's the meaning of the SDF ?
Thanks again !

MissileCat missilecat@hotmail. com

============================================================================
===================
 
D

Daniel Moth

You are using registry pinvokes for the full framework as well?

The .NET Framework offers the Microsoft.Win32.Registry class which you
should be using for this task. The .NET Compact Framework does not have this
class. The Smart Device Framework (SDF) offers OpenNETCF.Win32.Registry
which exposes an identical interface for you to use (and hence not worry
about dllimports)

Click on the link I sent previously for the registry class

Click on this one for the SDF page:
http://www.opennetcf.org/PermaLink.aspx?guid=3a013afd-791e-45ef-802a-4c1dbe1cfef9

Cheers
Daniel
 

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