passing char[] / byte[] arguments between C# and C++ dll

R

raludamian

Hello!

Sorry if I bring up again a problem that perhaps has already been
discussed...

I don't really understand how to pass strings as parameters between a
native C++ dll and a managed C# application.

The C++ code looks like this:

extern "C" void __declspec(dllexport) __stdcall
UTIL_OGRWriteDraw(double x, double y, char szName[33]);

and the C# code:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, byte[]
bMessage);

and I try to use it like this:

....
byte[] bMessage = ASCIIEncoding.ASCII.GetBytes(message);
UTIL_OGRWriteDraw((double)xMouse, (double)yMouse, bMessage);
....

The C# app should send a message to the C++ dll but the problem is
that I only get the first letter of the message... The same happens if
I use char[] instead of byte[]... And I understand that .NET CF does
not support [MarshalAs...]

Any suggestions? I really would appreciate any help or advice you
could give me...

Thank you!

Raluca
 
R

raludamian

It's the same problem.... I can access only the first character of
the string....

Neil Cowburn a scris:
Try this:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, string
bMessage);

CF 2.0 does support the MarshalAs attribute, but CF 1.0 does not.

--
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC.

Managed Code in the Embedded World

http://www.opennetcf.com/
http://www.smartdeviceframework.com/


Hello!

Sorry if I bring up again a problem that perhaps has already been
discussed...

I don't really understand how to pass strings as parameters between a
native C++ dll and a managed C# application.

The C++ code looks like this:

extern "C" void __declspec(dllexport) __stdcall
UTIL_OGRWriteDraw(double x, double y, char szName[33]);

and the C# code:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, byte[]
bMessage);

and I try to use it like this:

...
byte[] bMessage = ASCIIEncoding.ASCII.GetBytes(message);
UTIL_OGRWriteDraw((double)xMouse, (double)yMouse, bMessage);
...

The C# app should send a message to the C++ dll but the problem is
that I only get the first letter of the message... The same happens if
I use char[] instead of byte[]... And I understand that .NET CF does
not support [MarshalAs...]

Any suggestions? I really would appreciate any help or advice you
could give me...

Thank you!

Raluca
 
N

Neil Cowburn

That's because the string is being marshalled as a pointer to a string.
Ideally, you'd want to change your native method signature to this:

extern "C" void __declspec(dllexport) __stdcall UTIL_OGRWriteDraw(double x,
double y, char * pszName, DWORD cbName);

Alternatively, try passing a StringBuilder instance instead of a string to
your native method.

--
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC.

Managed Code in the Embedded World

http://www.opennetcf.com/
http://www.smartdeviceframework.com/


It's the same problem.... I can access only the first character of
the string....

Neil Cowburn a scris:
Try this:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, string
bMessage);

CF 2.0 does support the MarshalAs attribute, but CF 1.0 does not.

--
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC.

Managed Code in the Embedded World

http://www.opennetcf.com/
http://www.smartdeviceframework.com/


Hello!

Sorry if I bring up again a problem that perhaps has already been
discussed...

I don't really understand how to pass strings as parameters between a
native C++ dll and a managed C# application.

The C++ code looks like this:

extern "C" void __declspec(dllexport) __stdcall
UTIL_OGRWriteDraw(double x, double y, char szName[33]);

and the C# code:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, byte[]
bMessage);

and I try to use it like this:

...
byte[] bMessage = ASCIIEncoding.ASCII.GetBytes(message);
UTIL_OGRWriteDraw((double)xMouse, (double)yMouse, bMessage);
...

The C# app should send a message to the C++ dll but the problem is
that I only get the first letter of the message... The same happens if
I use char[] instead of byte[]... And I understand that .NET CF does
not support [MarshalAs...]

Any suggestions? I really would appreciate any help or advice you
could give me...

Thank you!

Raluca
 
G

Guest

I'd like to know more about the "WriteDraw" function. Your byte[] to a char
array would likely work, but are you sure it's using the data as char and
not wchar_t? CE is all Unicode, so if it in turn uses that data for some
drawing operation then it's got to convert it to Unicode. That means you
convert from Unicode to cross the P/Invoke boundary only to convert it back
again, which really isn't very efficient.
 
R

raludamian

I have made UTIL_OGRWriteDraw(double x, double y, char szName[33]) in
order to use other other functions from a C++ library (GDAL).

My function uses the char[] like this: poFeature->SetField( "Name",
szName ) and that function is SetField( const char *pszFName, const
char * pszValue)...

So I don't think that the char is actually wchar_t....

I have used byte[] because I have read that this is the way i should
do transmit string arguments... Perhaps I misunderstood that
explication, though...


I'd like to know more about the "WriteDraw" function. Your byte[] to a char
array would likely work, but are you sure it's using the data as char and
not wchar_t? CE is all Unicode, so if it in turn uses that data for some
drawing operation then it's got to convert it to Unicode. That means you
convert from Unicode to cross the P/Invoke boundary only to convert it back
again, which really isn't very efficient.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello!

Sorry if I bring up again a problem that perhaps has already been
discussed...

I don't really understand how to pass strings as parameters between a
native C++ dll and a managed C# application.

The C++ code looks like this:

extern "C" void __declspec(dllexport) __stdcall
UTIL_OGRWriteDraw(double x, double y, char szName[33]);

and the C# code:

[DllImport("gdalce.dll")]
public static extern void UTIL_OGRWriteDraw(double x, double y, byte[]
bMessage);

and I try to use it like this:

...
byte[] bMessage = ASCIIEncoding.ASCII.GetBytes(message);
UTIL_OGRWriteDraw((double)xMouse, (double)yMouse, bMessage);
...

The C# app should send a message to the C++ dll but the problem is
that I only get the first letter of the message... The same happens if
I use char[] instead of byte[]... And I understand that .NET CF does
not support [MarshalAs...]

Any suggestions? I really would appreciate any help or advice you
could give me...

Thank you!

Raluca
 

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