Hi,
I am using
RegEnumValue API with the following declaration in a Visual Basic 6 project and it is working as desired. Following is the declaration I used in VB6:
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Now I am converting this Visual Basic 6 application to Visual basic 2008. I used Visual Studio 2008 conversion wizard to convert my application. The wizard in result converts the API declaration as follows:
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA"(ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpValueName As String, ByRef lpcbValueName As Integer, ByVal lpReserved As Integer, ByRef lpType As Integer, ByRef lpData As Any, ByRef lpcbData As Integer) As Integer
After conversion in the
RegEnumValue API the type
Any, used for parameter
lpData, is no longer supported. Convertor now pass the
lpData as
ByRef, which is not mentioned in VB6 declaration.
To solve the problem I use
String type instead of
Any as described as a solution, which is mentioned in the following URL.
http://msdn.microsoft.com/en-us/library/aa260644%28VS.60%29.aspx
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpValueName As String, ByRef lpcbValueName As Integer, ByVal lpReserved As Integer, ByRef lpType As Integer, ByRef lpData As String, ByRef lpcbData As Integer) As Integer
Then when I execute the program I get the following exception message from the API call:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
After doing a lot of surfing on internet I found out that the problem is with using String data type in the API declaration and must be passed as
ByVal. This solves the problem and API returns the result accurately. My net surfing includes following links:
http://www.developer.com/net/vb/article.php/10926_1537091_1/Visual- Basic-6-Win32-API-Tutorial.htm
http://www.vb6.us/tutorials/using-win32-api#Calls
http://www.codingdomain.com/visualbasic/win32api/strings/
http://www.vbmonster.com/Uwe/Forum.aspx/vb/4576/RegEnumValue-XP-MSDN-Jan-2003-Q173009-FIX-Run-Time-Error-87-Using-RegEnumValue
Now I need explanation for this behavior, as why
ByRef is not working and only with the string?
Regards,
Hassan