Problem with calling a managed C++ function from managed C#

A

Andre

Hi,

I have a problem with calling a managed C++ function from C#.

The function in C++ looks like Class::Func(char* Text)

this function is compiled to an DLL and this DLL is referenced in my
C# project and I can see that I could call this function like
Func(sbyte* ).

I created a sbyte[] mytext = new sbyte[20];

So how can I pass this to the C++ function?

The compiler always give me an compileerror that it cannot convert
from sbyte* and sbyte[].

Thanks

André Betz
http://www.andrebetz.de
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

The problem is that you are delcaring the method in C++ as a pointer,
and not a managed array (the concepts are different in the managed world).

You want to declare your function like this:

Class::Func(char Text __gc[])

This syntax will change with the next version of .NET (at least, in
C++). Also, if you want to pass a string, why not use the string class,
like so:

Class::Func(String *Text)

Hope this helps.
 
W

Willy Denoyette [MVP]

Not sure why you called this a "managed" function, but char* is not a
managed type.
A char* is a pointer to whatever 'char' may be (ANSI or UNICODE), so you
need to pass a pinned pointer to an array of bytes (or shorts).
byte[] mytext = new byte[20];fixed (byte* pmyText= mytext )
{
// Call your function here...// Note that the sbyte array will be pinned
for the duration of the call! Func(pmyText);
}Are you sure this is what you expected from your managed C++ function, I
 

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