Link to home
Start Free TrialLog in
Avatar of Sid Price
Sid PriceFlag for United States of America

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:
AVRPODS_API IDBE_ERROR avr_init(AVR_POD_OPTIONS *lpsEepromOptions);

Open in new window

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 ;

Open in new window


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

Open in new window

Thanks,
Sid.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sid Price

ASKER

Thank you, that is a very comprehensive article however I am having trouble translating it to VB.NET because it would appear that my code should work. The only issues I thought it had were with the return type and the string pointer in the structure so I changed them to:
    <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.LPWStr)> Public eepromFilename As String
    End Structure
    '
    <DllImport("avr_pods", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function avr_init(ByRef eepromOptions As podOptions) As Integer
    End Function

Open in new window


I still got the same exception, so I must still be doing something wrong here.
Sid.
It may depend on platform. For x64 pointer should be IntPtr, not Integer
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.
I have resolved the problem by adding a "CallingConvention" attribute to the "DllImport" statement:

    
<DllImport("avr_pods", SetLastError:=True, CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function avr_init(ByRef eepromOptions As podOptions) As Integer
End Function

Open in new window


Sid.
This article while not directly applicable to VB.NET did lead me to question my code and ultimately to the answer.
Sid.