Click here to Skip to main content
15,883,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
tiggerc 11-Aug-10 3:23am    
The string maybe in protected memory space, but thats just a guess

Scrap the VB6 interop code and just re-write it using the Registry class built into .NET. You'll probably end up writing code that's 1/4th size of the VB6 equivilent.
 
Share this answer
 
I also posted this question on MSDN and there i found the answer:

http://social.msdn.microsoft.com/Forums/en-US/vbinterop/thread/3ae5e246-24b5-4a84-894b-881254a70605[^]

Thanks anyways for help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900