Solved

Error Calling a C++ DLL from VB 2010 .NET 4

Posted on 2011-03-21
3
614 Views
Last Modified: 2012-08-13
Hi Folks,

I've got a c++ DLL (written by a well knownforum member on here!). I've been using it fine with vb.net 2005, now I've upgraded to VB 2010 I get this:

A call to PInvoke function 'xxxxxxxxxx!xxxxxxxxxxx.PUX::ExecuteScript' 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.

All processes are 32 bit, which is what the dll was compiled for. I can see the *32 in process manager.

I've tried recompiling the DLL with Visual Studio 2010 - that doesnt fix it.
This is windows 2008 R2. I've seen it working with .NET 2 on Windows 2003 R2 64 bit version.

So I think its something about .net 4.

Here's the declarations:

in c++

      int ExecuteScript(BSTR Script, BSTR OutputFile, BSTR Host, BSTR User, BSTR Pass, BSTR SUUser, BSTR SUPass, unsigned int unAuthTimeout, unsigned int unFlags);


in vb:

    Private Declare Auto Function ExecuteScript _
        Lib "UXDiscovery.dll" _
        (ByVal sScript As String, ByVal sOutputFile As String, ByVal sHost As String, _
        ByVal sUser As String, ByVal sPass As String, _
        ByVal sRootUser As String, ByVal sRootPass As String, ByVal lTimeout As UInt32, ByVal lFlags As UInt32) As Int32

        Dim iReturn As Integer = ExecuteScript(sScript, sPath, IPAddress, msUserName, msPassword, "", "", lTimeout * CType(1000, UInt32), mlFlags)

These all started "As Integer", the UInt32 is just me trying different things, but nothing yet works..

thanks
Paul





0
Comment
Question by:plq
[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
3 Comments
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35182823
Change the Auto to Unicode and then pass Nothing instead of "" to the function. You can also use Integer it would just become converted to unsigned integer on the c++ end so that shouldn't make a difference. It's the BSTR strings that is most likely the problem. If the above doesn't work you may need to marshal each parameter as <bstr> using DllImport attribute.
Dim bstrNotUsed1 as String = Nothing
Dim bstrNotUsed2 as String = Nothing

' Calculate lTimeout here
Dim iReturn As Integer = ExecuteScript(sScript, sPath, IPAddress, msUserName, msPassword, bstrNotUsed1 ,bstrNotUsed2 , lTimeout , mlFlags)

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35183256
Another potential issue is a Calling Convention mismatch.  Declare uses CallingConvention.StdCall and C++ has CallingConvention.Cdecl as the default.  To change the calling convention, you need to replace Declare by DllImpot as follows:

<DllImport("UXDiscovery.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function ExecuteScript _
	(ByVal sScript As String, ByVal sOutputFile As String, ByVal sHost As String, _
	ByVal sUser As String, ByVal sPass As String, _
	ByVal sRootUser As String, ByVal sRootPass As String, ByVal lTimeout As UInt32, ByVal lFlags As UInt32) As Int32
End Function

Open in new window


I hope this helps.
0
 
LVL 8

Author Closing Comment

by:plq
ID: 35187897
Spot on - thank you
0

Featured Post

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

615 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