Help, Calling c++ dll from vb.net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xin.huang.xh@gmail.com

    #1

    Help, Calling c++ dll from vb.net

    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(100 0))
    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


    PInvokeStackImb alance 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

  • Stanimir Stoyanov

    #2
    Re: Help, Calling c++ dll from vb.net

    <xin.huang.xh@g mail.comwrote in message
    news:1188569508 .477279.194980@ w3g2000hsg.goog legroups.com...
    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!!!
    Hello Xin,

    You have to specify "C" linkage as your DLL is currently export C++-style
    functions whose names are mangled.

    See http://msdn2.microsoft.com/en-us/library/ms857730.aspx.

    Best Regards,
    Stanimir Stoyanov
    www.stoyanoff.info | www.aeroxp.org

    Comment

    • =?Utf-8?B?cm1hY2lhcw==?=

      #3
      RE: Help, Calling c++ dll from vb.net

      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",
      CallingConventi on:=CallingConv ention.StdCall) _
      Private Shared Function Test(<MarshalAs (UnmanagedType. I4)ByVal b As
      Integer) As Integer
      End Function


      End Class

      "xin.huang.xh@g mail.com" wrote:
      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(100 0))
      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
      >
      >
      PInvokeStackImb alance 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
      >
      >

      Comment

      • xin.huang.xh@gmail.com

        #4
        Re: Help, Calling c++ dll from vb.net

        On Aug 31, 11:28 am, rmacias <rmac...@newsgr oup.nospamwrote :
        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",
        CallingConventi on:=CallingConv ention.StdCall) _
        Private Shared Function Test(<MarshalAs (UnmanagedType. I4)ByVal b As
        Integer) As Integer
        End Function
        >
        End Class
        >
        >
        >
        "xin.huang...@g mail.com" wrote:
        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(100 0))
        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
        >
        PInvokeStackImb alance 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- Hide quoted text -
        >
        - 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

        Comment

        • Phoenix

          #5
          Re: Help, Calling c++ dll from vb.net

          On Aug 31, 8:28 pm, rmacias <rmac...@newsgr oup.nospamwrote :
          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",
          CallingConventi on:=CallingConv ention.StdCall) _
          Private Shared Function Test(<MarshalAs (UnmanagedType. I4)ByVal b As
          Integer) As Integer
          End Function
          >
          End Class
          >
          "xin.huang...@g mail.com" wrote:
          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(100 0))
          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
          >
          PInvokeStackImb alance 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
          Hi Friend ,

          Could you please have a look at following :



          Any suggestions would be highly appreciated :)

          Sincerely,
          Sudhansu

          Comment

          Working...