I am trying to pass a pointer to a HANDLE (HANDLE*) to a VC++ DLL from VB.NET. I can do it in VB6, but all the changes in .NET are giving me a headache trying to figure it out.
Here is the VC++ prototype I am calling:
I have this function declared:
And here is my calling code:
As you can see, in the declare, the first one that is commented out is trying to Marshal the data AsAny. Since a HANDLE is really just void*, I tried using this example from MS to help me: (http://msdn.microsoft.com/library/en-us/cpguide/html/cpconvoidsample.asp)
However, it is giving this error when I run the code:
So my question is: How do I pass the HANDLE* to VC++ from VB.NET? Any help or links would be appreciated, I have been trying to figure this out for way too long.
Here is the VC++ prototype I am calling:
Code:
F32API F32STATUS WINAPI F32Open(DWORD dwDevice, HANDLE* cyHandle);
I have this function declared:
Code:
Public Declare Function F32Open Lib "F32.dll" (ByVal dwDevice As Long, _
<MarshalAs(UnmanagedType.AsAny)> ByRef cyHandle As Object) As Integer
Public Declare Function F32Open Lib "F32.dll" (ByVal dwDevice As Long, _
ByRef cyHandle As IntPtr) As Integer
And here is my calling code:
Code:
Private Sub LoadButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LoadButton.Click
Dim Status
Dim hDevice
Status = F32Open(0, hDevice)
If Status <> F32SUCCESS Then
MsgBox("Error opening device: 0")
End If
End Sub
As you can see, in the declare, the first one that is commented out is trying to Marshal the data AsAny. Since a HANDLE is really just void*, I tried using this example from MS to help me: (http://msdn.microsoft.com/library/en-us/cpguide/html/cpconvoidsample.asp)
However, it is giving this error when I run the code:
An unhandled exception of type
System.Runtime.InteropServices.MarshalDirectiveException
occurred in F32.exe
Additional information: Can not marshal parameter #2:
The AsAny type can not be marshaled as byref or as an
unmanaged-to-managed parameter.
So my question is: How do I pass the HANDLE* to VC++ from VB.NET? Any help or links would be appreciated, I have been trying to figure this out for way too long.