Solved

Calling unmanaged DLL from vb.net

Posted on 2014-10-31
6
228 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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