Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling unmanaged DLL from vb.net

Posted on 2014-10-31
6
Medium Priority
?
266 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 1500 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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