Cannot successfully can C++ function from C#

M

Mike

I have been banging my head againt my desk for a couple days now and nothing
I try seems to work. I know I am missing something small and it's killing
me.

This is the error I am getting with this code:

A call to PInvoke function
'SmartworksTest!SmartworksTest::MTGetEventCodeDescription' 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 have also tried to use char arrays and IntPtr instead of the string. The
char array give me a memory error and I can't get any useful information
from the IntPtr.

Thanks in advance for you help.


Here is the C++ definition I need to call
void MTGetEventCodeDescription(
const ULONG EventCode,
const PCHAR pDescription,
const int nLength
);

Here is my C# code:

[DllImport("NtiDrv.dll", EntryPoint = "MTGetEventCodeDescription", CharSet =
CharSet.Auto)]
public static extern void MTGetEventCodeDescription(uint returnCode, out
string pDescription,out short nLength);

public static string GetEventCodeDescription(uint eventCode)
{
uint ret;
string desc;
short size = 0;

MTGetEventCodeDescription(eventCode, out desc, out size);
return (desc.ToString());
}
 
C

Chris

Mike said:
I have been banging my head againt my desk for a couple days now and nothing
I try seems to work. I know I am missing something small and it's killing
me.

This is the error I am getting with this code:

A call to PInvoke function
'SmartworksTest!SmartworksTest::MTGetEventCodeDescription' 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 have also tried to use char arrays and IntPtr instead of the string. The
char array give me a memory error and I can't get any useful information
from the IntPtr.

Thanks in advance for you help.


Here is the C++ definition I need to call
void MTGetEventCodeDescription(
const ULONG EventCode,
const PCHAR pDescription,
const int nLength
);

Here is my C# code:

[DllImport("NtiDrv.dll", EntryPoint = "MTGetEventCodeDescription", CharSet =
CharSet.Auto)]
public static extern void MTGetEventCodeDescription(uint returnCode, out
string pDescription,out short nLength);

public static string GetEventCodeDescription(uint eventCode)
{
uint ret;
string desc;
short size = 0;

MTGetEventCodeDescription(eventCode, out desc, out size);
return (desc.ToString());
}

When I got this error it was a reference vs value passing problem in my
definition.

Chris
 
S

sb

Try this instead...

using System.Text; // if you don't already have it listed
....
[DllImport("NtiDrv.dll", EntryPoint = "MTGetEventCodeDescription", CharSet =
CharSet.Auto)]
public static extern void MTGetEventCodeDescription(uint returnCode,
StringBuilder pDescription, int nLength);

public static string GetEventCodeDescription(uint eventCode)
{
StringBuilder desc = new StringBuilder(512); //or whatever buffer size
you want to send in

MTGetEventCodeDescription(eventCode, desc, desc.Capacity);
return (desc.ToString());
}

-sb
 
W

Willy Denoyette [MVP]

The unbalanced stack comes from the fact that the last argument is declared
as a short, while it should be an int.
Note that the second arg. must be a StringBuilder, but this doesn't result
in an unbalance.

Willy.


|I have been banging my head againt my desk for a couple days now and
nothing
| I try seems to work. I know I am missing something small and it's killing
| me.
|
| This is the error I am getting with this code:
|
| A call to PInvoke function
| 'SmartworksTest!SmartworksTest::MTGetEventCodeDescription' 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 have also tried to use char arrays and IntPtr instead of the string.
The
| char array give me a memory error and I can't get any useful information
| from the IntPtr.
|
| Thanks in advance for you help.
|
|
| Here is the C++ definition I need to call
| void MTGetEventCodeDescription(
| const ULONG EventCode,
| const PCHAR pDescription,
| const int nLength
| );
|
| Here is my C# code:
|
| [DllImport("NtiDrv.dll", EntryPoint = "MTGetEventCodeDescription", CharSet
=
| CharSet.Auto)]
| public static extern void MTGetEventCodeDescription(uint returnCode, out
| string pDescription,out short nLength);
|
| public static string GetEventCodeDescription(uint eventCode)
| {
| uint ret;
| string desc;
| short size = 0;
|
| MTGetEventCodeDescription(eventCode, out desc, out size);
| return (desc.ToString());
| }
|
|
 

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