CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Question Calling external DLL function

    In my application I'm trying to invoke a function from an external DLL (this DLL is compiled in C++)

    I have this code to declare the DLL function:
    Code:
    <DllImport("{DLLPATH}", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function functionName(<MarshalAs(UnmanagedType.VBByRefStr)> ByRef Name1 As String, <MarshalAs(UnmanagedType.VBByRefStr)> ByRef Name2 As String) As Short
    End Function
    (where {DLLPATH} is the DLL file name)

    then into my code I call the function with this:

    Code:
    functionName("String1","String2")
    If I compile the code with .NET 2, it work perfectly; if I compile it with .NET 4 it guves me the following error:

    A call to PInvoke function 'TestVB!TestVB.Form1::functionName' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
    Can someone help me to make the code work in .NET 4??

    Thank you in advance.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Calling external DLL function

    did you need to set the callingConvention?
    like in
    Code:
    DllImport("{DLLPATH}", CallingConvention.Cdecl, CharSet:=CharSet.Auto, SetLastError:=True)> _
    ie:
    http://stackoverflow.com/questions/2...ented-function

    you should also use a couple of variables, instead of inline string
    Code:
    dim s1 as string="String1"
    dim s2 as string ="String2"
    
    functionName(s1,s2)
    Also this might help
    http://stackoverflow.com/questions/2...-pointer-strin
    Last edited by Cimperiali; March 21st, 2012 at 11:50 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Talking Re: Calling external DLL function

    Quote Originally Posted by Cimperiali View Post
    did you need to set the callingConvention?
    That worked perfectly. Thank you very much!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured