In VB 'PInvoke cannot return varients' message wrapping C++ librar

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to wrap an unmanaged DLL. The function I am currently trying to
wrap is defined as this:
KPDCStatus KPDCAllocCameraIterator( KPDCLibMgrRef inManagerRef,
KPDCCameraIterRef *outRef);

The types they are defined this way:
typedef void* KPDCOpaqueRef;
typedef KPDCOpaqueRef KPDCLibMgrRef;
typedef KPDCIteratorRef KPDCCameraIterRef;

So, basically the function takes 2 IntPtrs, one of the ByReference. So I
wrote my VB code like this:
Declare Auto Function KAllocCamera Lib "DCSPro4SLR.dll" Alias
"KPDCAllocCameraIterator" (ByVal inManagerRef As IntPtr, ByRef outRef As
IntPtr)

Private Sub FireItUp()
Dim myLibMgr As IntPtr = Marshal.AllocHGlobal(1)
Dim myCamIter As IntPtr = Marshal.AllocHGlobal(1)

....
'Some code that gets passed myLibMgr and fills it
....

myStatus = myPro4.KAllocCamera(myLibMgr, myCamIter)
End Sub

But I get this error:
System.Runtime.InteropServices.MarshalDirectiveException: PInvoke
restriction: can not return variants.

What's it all mean? The MarshalDirectiveException?
 
So, basically the function takes 2 IntPtrs, one of the ByReference. So I
wrote my VB code like this:
Declare Auto Function KAllocCamera Lib "DCSPro4SLR.dll" Alias
"KPDCAllocCameraIterator" (ByVal inManagerRef As IntPtr, ByRef outRef As
IntPtr)

You forgot to specify a return type, so it defaults to Object which
marshals as a VARIANT by default.

The solution is to
1) Turn on Option Strict so this never happens again
2) Specify the correct return type




Mattias
 
I am also experiencing the same problem. The DLL is returning a VARIANT. I
have turned on "Option Strict", but the exception still occurs. Can anyone
help?
 
Back
Top