Sid Price
asked on
Calling unmanaged DLL from vb.net
I need to consume an unmanaged (WIN32) DLL with my VB.NET application. I have read several articles and gone in circles in the MS documentation about doing this. All my attempts result in a "stack imbalance" exception. The first method I need to access takes a pointer to a structure as a parameter and returns a value from an enumeration:
My last attempt wit the VB.NET code looks like this:
Sid.
AVRPODS_API IDBE_ERROR avr_init(AVR_POD_OPTIONS *lpsEepromOptions);
The structure looks like this:typedef struct tagPodOptions {
int iPodType ;
unsigned char ucJTAGClockDelay ;
AVR_EEPROM_CONTROL eEepromControl ;
wchar_t wcsEepromFilename[MAX_PATH] ;
} AVR_POD_OPTIONS ;
My last attempt wit the VB.NET code looks like this:
<StructLayout(LayoutKind.Sequential)> _
Public Structure podOptions
<MarshalAs(UnmanagedType.U4)> Public iPodType As Integer
<MarshalAs(UnmanagedType.U1)> Public ucClockDelay As Char
<MarshalAs(UnmanagedType.U4)> Public eEEPROMControl As Integer
<MarshalAs(UnmanagedType.LPStr)> Public eepromFilename As String
End Structure
'
Public Enum IDBE_ERROR
eOK ' just a dummy for now
End Enum
<DllImport("avr_pods", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function avr_init(ByRef eepromOptions As podOptions) As IDBE_ERROR
End Function
Thanks,Sid.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It may depend on platform. For x64 pointer should be IntPtr, not Integer
ASKER
Ark, I don't understand what you wrote ... the only pointer is the parameter "eepromOptions", I did not use "Integer" as a pointer. The return value is an enum value.
Sid.
Sid.
ASKER
I have resolved the problem by adding a "CallingConvention" attribute to the "DllImport" statement:
Sid.
<DllImport("avr_pods", SetLastError:=True, CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function avr_init(ByRef eepromOptions As podOptions) As Integer
End Function
Sid.
ASKER
This article while not directly applicable to VB.NET did lead me to question my code and ultimately to the answer.
Sid.
Sid.
ASKER
Open in new window
I still got the same exception, so I must still be doing something wrong here.
Sid.