473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Cannot successfully can C++ function from C#

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::MTGetEventCodeDesc ription' 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());
}
Apr 14 '06 #1
3 1504
Mike wrote:
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::MTGetEventCodeDesc ription' 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
Apr 14 '06 #2
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
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:e%****************@TK2MSFTNGP04.phx.gbl...
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::MTGetEventCodeDesc ription' 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());
}

Apr 14 '06 #3
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.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:e%****************@TK2MSFTNGP04.phx.gbl...
|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::MTGetEventCodeDesc ription' 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());
| }
|
|
Apr 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Krista | last post by:
Hi, I hope you guys can help me out~~~ when I run it localhost/config.php, it cannot display any message such as connect successfully or failed. Do u guys have any idea? config.php <html>...
1
by: Krista | last post by:
Hi guys, i have a problem about inserting data into the table. I dont know why the output always said cannot be added to the database! Anyone has idea for me? i check the connect is ok, but dont...
8
by: Alex Ang | last post by:
I have written the following VBScript program. It is stored into a file "map_drive.vbs". It successfully mapped to a network drive \\server1\data. Dim WshNetwork Set WshNetwork =...
5
by: Giles Brown | last post by:
I'm feeling quite dumb this morning. I'm trying to build a COM server DLL using py2exe and it ain't working. Here's what ain't working... setup_dll.py based on py2exe sample: """from...
3
by: Laax | last post by:
Hi, I am trying to compile a .cc file and get the following error. I have translated the error message from original japanese text. So, it may not look exactly like standard(English) error...
0
by: Yannick Turgeon | last post by:
Hello all, Back-end: SS2000 SP2 + NT4 Front-end: A97 + XP We are currently trying to add a Domain Server to our network. We added our SQL Server to the domain and added a client too....
5
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
0
by: DWoood | last post by:
Background: We have been developing a Web Services Project (let's call it "MyServices") for several months and have had no problems with it. In fact other members of my team are still...
0
DressageRider
by: DressageRider | last post by:
I need your help. Desperately. Someone has to be able to fix the utter balls up I've made after dinking around in Flash for the last four days. I have no prior knowledge of anything other than...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.