Solved

Calling unmanaged DLL from vb.net

Posted on 2014-10-31
6
247 Views
Last Modified: 2014-11-01
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.
0
Comment
Question by:Sid Price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40416573
Have you tried:
<StructLayout(LayoutKind.Sequential)> _
Public Structure podOptions
	Public iPodType As UInteger
	Public ucClockDelay As Char
	Public eEEPROMControl As UInteger
	<MarshalAs(UnmanagedType.LPStr)> Public eepromFilename As String
End Structure

Open in new window


Here is a great article on Marshaling:

http://www.codeproject.com/Articles/66245/Marshaling-with-Csharp-Chapter-1-Introducing-Marsh.aspx

-saige-
0
 

Author Comment

by:Sid Price
ID: 40416635
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.
0
 
LVL 28

Expert Comment

by:Ark
ID: 40417046
It may depend on platform. For x64 pointer should be IntPtr, not Integer
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Sid Price
ID: 40417137
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.
0
 

Author Comment

by:Sid Price
ID: 40417192
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.
0
 

Author Closing Comment

by:Sid Price
ID: 40417194
This article while not directly applicable to VB.NET did lead me to question my code and ultimately to the answer.
Sid.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question