Hi
I'm trying to call a native C++ dll using the following code (both C# and
C++ code segments included). When I make the call to the method
(AddTwoDoubles) that has no return value all is fine. When I try and call a
function that returns a double (AddTwoDoubles2) I get the following message:
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'tempModule!<Module>::AddTwoDoubles2'
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.
I can't see what I'm doing wrong.
Thanks for any help that anyone can provide.
Marek
================================================== ===================
C# code follows.
================================================== ===================
//...Set up the return value:
//Type returnType = null; //...Function (AddTwoDoubles) with void return
works
Type returnType = typeof(double); //...Function (AddTwoDoubles2) does not
work
//...Set up the parameters:
Type[] parameterTypes = new Type[2];
object[] parameterValues = new object[2];
parameterTypes[0] = typeof(double);
parameterValues[0] = 2.0;
parameterTypes[1] = typeof(double);
parameterValues[1] = 2.0;
// Create a dynamic assembly and a dynamic module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("tempModule");
// Dynamically construct a global PInvoke signature using the input
information
MethodBuilder methodBuilder =
moduleBuilder.DefinePInvokeMethod(entryPointName, dllName,
MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl, CallingConventions.Standard, returnType,
parameterTypes, CallingConvention.Winapi, CharSet.Ansi);
moduleBuilder.CreateGlobalFunctions();
// Get a MethodInfo for the PInvoke method
MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);
// Invoke the static method and return whatever it returns
ParameterInfo[] parameterInfo = methodInfo.GetParameters();
try
{
Object returnValue = methodInfo.Invoke(null, parameterValues);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
================================================== ===================
C++ code follows
================================================== ===================
extern "C"
{
__declspec( dllexport ) void WINAPI AddTwoDoubles(double a, double b)
{
CString str;
str.Format(_T("%lf + %lf = %lf"), a, b, a+b);
AfxMessageBox(str);
}
__declspec( dllexport ) double WINAPI AddTwoDoubles2(double a, double b)
{
return a+b;
}
} 2 2404
Not sure where the stack imbalance comes from, but IMO you are missing:
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
before you call:
| moduleBuilder.CreateGlobalFunctions();
Willy.
"Marek" <no*****@dnv.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
| Hi
| I'm trying to call a native C++ dll using the following code (both C# and
| C++ code segments included). When I make the call to the method
| (AddTwoDoubles) that has no return value all is fine. When I try and call
a
| function that returns a double (AddTwoDoubles2) I get the following
message:
|
|
| PInvokeStackImbalance was detected
| Message: A call to PInvoke function 'tempModule!<Module>::AddTwoDoubles2'
| 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.
|
| I can't see what I'm doing wrong.
|
| Thanks for any help that anyone can provide.
|
| Marek
| ================================================== ===================
| C# code follows.
| ================================================== ===================
| //...Set up the return value:
| //Type returnType = null; //...Function (AddTwoDoubles) with void return
| works
| Type returnType = typeof(double); //...Function (AddTwoDoubles2) does not
| work
|
| //...Set up the parameters:
| Type[] parameterTypes = new Type[2];
| object[] parameterValues = new object[2];
|
| parameterTypes[0] = typeof(double);
| parameterValues[0] = 2.0;
| parameterTypes[1] = typeof(double);
| parameterValues[1] = 2.0;
|
| // Create a dynamic assembly and a dynamic module
| AssemblyName assemblyName = new AssemblyName();
| assemblyName.Name = "tempDll";
| AssemblyBuilder assemblyBuilder =
| AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
| AssemblyBuilderAccess.Run);
| ModuleBuilder moduleBuilder =
| assemblyBuilder.DefineDynamicModule("tempModule");
|
| // Dynamically construct a global PInvoke signature using the input
| information
| MethodBuilder methodBuilder =
| moduleBuilder.DefinePInvokeMethod(entryPointName, dllName,
| MethodAttributes.Static | MethodAttributes.Public |
| MethodAttributes.PinvokeImpl, CallingConventions.Standard, returnType,
| parameterTypes, CallingConvention.Winapi, CharSet.Ansi);
|
| moduleBuilder.CreateGlobalFunctions();
|
| // Get a MethodInfo for the PInvoke method
| MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);
|
| // Invoke the static method and return whatever it returns
| ParameterInfo[] parameterInfo = methodInfo.GetParameters();
|
| try
| {
| Object returnValue = methodInfo.Invoke(null, parameterValues);
| }
| catch (Exception ex)
| {
| Console.Write(ex.Message);
| }
|
| ================================================== ===================
| C++ code follows
| ================================================== ===================
|
| extern "C"
| {
| __declspec( dllexport ) void WINAPI AddTwoDoubles(double a, double b)
| {
|
| CString str;
| str.Format(_T("%lf + %lf = %lf"), a, b, a+b);
| AfxMessageBox(str);
| }
| __declspec( dllexport ) double WINAPI AddTwoDoubles2(double a, double b)
| {
| return a+b;
| }
| }
|
|
Hi Willy
That certainly did the trick. Thank you.
Have you any experience of what changes I need to make to the code to pass
arrays and return values (byref as opposed to byval) as well as the good old
passing and retrieval of string parameters?
Thanks again.
Marek
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl... Not sure where the stack imbalance comes from, but IMO you are missing:
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig); before you call: | moduleBuilder.CreateGlobalFunctions();
Willy. "Marek" <no*****@dnv.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl... | Hi | I'm trying to call a native C++ dll using the following code (both C# and | C++ code segments included). When I make the call to the method | (AddTwoDoubles) that has no return value all is fine. When I try and call a | function that returns a double (AddTwoDoubles2) I get the following message: | | | PInvokeStackImbalance was detected | Message: A call to PInvoke function 'tempModule!<Module>::AddTwoDoubles2' | 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. | | I can't see what I'm doing wrong. | | Thanks for any help that anyone can provide. | | Marek | ================================================== =================== | C# code follows. | ================================================== =================== | //...Set up the return value: | //Type returnType = null; //...Function (AddTwoDoubles) with void return | works | Type returnType = typeof(double); //...Function (AddTwoDoubles2) does not | work | | //...Set up the parameters: | Type[] parameterTypes = new Type[2]; | object[] parameterValues = new object[2]; | | parameterTypes[0] = typeof(double); | parameterValues[0] = 2.0; | parameterTypes[1] = typeof(double); | parameterValues[1] = 2.0; | | // Create a dynamic assembly and a dynamic module | AssemblyName assemblyName = new AssemblyName(); | assemblyName.Name = "tempDll"; | AssemblyBuilder assemblyBuilder = | AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName, | AssemblyBuilderAccess.Run); | ModuleBuilder moduleBuilder = | assemblyBuilder.DefineDynamicModule("tempModule"); | | // Dynamically construct a global PInvoke signature using the input | information | MethodBuilder methodBuilder = | moduleBuilder.DefinePInvokeMethod(entryPointName, dllName, | MethodAttributes.Static | MethodAttributes.Public | | MethodAttributes.PinvokeImpl, CallingConventions.Standard, returnType, | parameterTypes, CallingConvention.Winapi, CharSet.Ansi); | | moduleBuilder.CreateGlobalFunctions(); | | // Get a MethodInfo for the PInvoke method | MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName); | | // Invoke the static method and return whatever it returns | ParameterInfo[] parameterInfo = methodInfo.GetParameters(); | | try | { | Object returnValue = methodInfo.Invoke(null, parameterValues); | } | catch (Exception ex) | { | Console.Write(ex.Message); | } | | ================================================== =================== | C++ code follows | ================================================== =================== | | extern "C" | { | __declspec( dllexport ) void WINAPI AddTwoDoubles(double a, double b) | { | | CString str; | str.Format(_T("%lf + %lf = %lf"), a, b, a+b); | AfxMessageBox(str); | } | __declspec( dllexport ) double WINAPI AddTwoDoubles2(double a, double b) | { | return a+b; | } | } | |
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matthias Klöpper |
last post by:
Hi,
I have a little problem with supplying a mixed set of byVal and byRef
parameters to a dynamically created PInvoke method.
I have the following message signature which works perfectly when...
|
by: BBFrost |
last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net
2003. At first things were looking ok, now problems are starting to
appear.
So far ...
(1) ...
|
by: Claire |
last post by:
I've now found out how to approach calling the functions of dynamically
loaded dlls. Ive never looked at interop so I don't understand a thing I'm
doing but I am trying to adapt someone elses...
|
by: Alpha |
last post by:
I was told that Unix API can only be called using C++, ATL and MFC. However,
I was also told that C# can do that through Pinvoke to a DLL that interfaces
with the Unix API. Can someone direct me...
|
by: Marek |
last post by:
Hi
I need to call a native function in a C++ dll from my C#. The C++ function
declaration is shown below.
The C# code that I have been trying to perform the call is also shown below.
All it is...
|
by: Samuel R. Neff |
last post by:
When is it appropriate to use "volatile" keyword? The docs simply
state:
"
The volatile modifier is usually used for a field that is accessed by
multiple threads without using the lock...
|
by: John M. Gamble |
last post by:
I'm getting this message in Visual Studio 2005:
PInvokeStackImbalance was detected
Message: A call to PInvoke function
'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack.
...
|
by: linuxfedora |
last post by:
I wrote a class for playing wav file, the code:
class SoundPlayer
{
// flag values for SoundFlags argument on PlaySound
public int SND_SYNC = 0x0000; // play synchronously
(default)
public...
|
by: news.microsoft.com |
last post by:
I am getting the following error on the return from a function call.It
specically happens on the assignment of the return value from the function
call. No error happens inside the function. The...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |