Help, Calling c++ dll from vb.net

X

xin.huang.xh

Hi All:

Please help me out here. I try to call a C++ dll from my VB.NET code.
I follow the examples given on the web, but it does not work for
me!!! It drives me crazy, so please help me out. Here is what I did


Create an empty VC++ dll project from Visual Studio 2003. Create a
test.cpp file and add the following


long _stdcall test(long a) {
return a;



}


Create a test.def file and add the following

LIBRARY "test"


EXPORTS
test


Create a VB windows application. Here is what is given in the file


Public Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function test Lib "location of the dll in my
computer" (ByVal b As Long) as Long


Private Sub Button1_Click( ..... ) Handles Button1.Click
MsgBox(test(1000))
End Sub


End Class


When I click the button, it returns a number that is very large (so
it
is NOT 1000).


I also try the same thing in VS2005 on another machine in my lab.
This time I get an error


PInvokeStackImbalance was detected


I just don't know what is going on. It just a very simple example and
just cannot get it work.


So, any suggestions will be much appreciated,


Thanks alot


Xin
 
G

Guest

In .NET, the size of a long is 8 bytes. I'm guessing that the architecture
for your machine a long in C++ is 4 bytes. That would cause a stack
imbalance. Try changing your .NET declartion to an Integer instead of a long.

In addition, you can always try playing around with the DllImport Attribute
instead:

Imports System.Runtime.InteropServices

Public Class Form1

<DllImport("C:\MyDll.dll",
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function Test(<MarshalAs(UnmanagedType.I4)> ByVal b As
Integer) As Integer
End Function


End Class
 
X

xin.huang.xh

In .NET, the size of a long is 8 bytes. I'm guessing that the architecture
for your machine a long in C++ is 4 bytes. That would cause a stack
imbalance. Try changing your .NET declartion to an Integer instead of a long.

In addition, you can always try playing around with the DllImport Attribute
instead:

Imports System.Runtime.InteropServices

Public Class Form1

<DllImport("C:\MyDll.dll",
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function Test(<MarshalAs(UnmanagedType.I4)> ByVal b As
Integer) As Integer
End Function

End Class



















- Show quoted text -

Hi Stanimir and rmacias

Thank you very much for your suggestions. I incoporate both
suggestions and use the DllImport. It works NOW. :D

Xin
 
P

Phoenix

In .NET, the size of a long is 8 bytes. I'm guessing that the architecture
for your machine a long in C++ is 4 bytes. That would cause a stack
imbalance. Try changing your .NET declartion to an Integer instead of a long.

In addition, you can always try playing around with the DllImport Attribute
instead:

Imports System.Runtime.InteropServices

Public Class Form1

<DllImport("C:\MyDll.dll",
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function Test(<MarshalAs(UnmanagedType.I4)> ByVal b As
Integer) As Integer
End Function

End Class

Hi Friend ,

Could you please have a look at following :

http://groups.google.co.in/group/mi...fa303/b7cadcdcc47ac223?hl=en#b7cadcdcc47ac223

Any suggestions would be highly appreciated :)

Sincerely,
Sudhansu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top