471,627 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Consuming C++ dlls in C# - struct definition

Raj
I have an unmanaged C++ dll which Iam trying to consume in C#.The code is as
below

//Unmanaged dll definition
typedef struct {
float a;
bool b;
char * c;
int d;
unsigned e;
unsigned char * f;
} sample;

//Managed C# dll definiton
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct sample
{
public float a;
public bool b;
public String c;
public int d;
public int e;
public int f;
}

[DllImport("sample.dll", EntryPoint="sendMessage")]
public static extern int sendMessage([In, Out] ref sample sampleStruct);

//Invoke method
sample sampleStruct = new sample();
sample.a = 2.00F;
sample.b = false;
sample.c = "A";
sample.d = 1;
sample.e = 3;
sample.f = 5;

response = sendMessage(ref sampleStruct);

Iam able to invoke and get the response but the invoked C++ method couldnt
read the structure passed.
Not sure whether my definitions are right. I have tried using
MarshalAs(UnmanagedType.LPStr) and SizeConstant but no luck

Any thoughts?Thanks
Feb 12 '06 #1
3 2549

Raj wrote:
I have an unmanaged C++ dll which Iam trying to consume in C#.The code is as
below

//Unmanaged dll definition
typedef struct {
float a;
bool b;
char * c;
int d;
unsigned e;
unsigned char * f;
} sample;
.... Any thoughts?Thanks


1) You probably will need a MarshalAs for the string ptr - though I
can't find a sample right off hand...

2) Double check marshalling for the "bool". By default .NET marshals
PInvoke bools as WIN32 bools {32 bit} with values equal to WIN32 TRUE
and FALSE #defines - which is not good for C++ {8 bit} bool type...
You can marshal the bool as a char if there is no usable MarshalAs
override...

Feb 12 '06 #2
Raj
Thanks for your feedback, Crash.
I did try [MarshalAs(UnmanagedType.LPStr)] for the string but that didnt
make any difference.I also changed the bool to char but that also didnt make
any difference :-(

Iam able to retrieve all the response codes coming back in my managed code
but inovked C++ code couldnt read any values in the structure passed. Iam
sure its my definition. Not sure what it is though :-(
Thanks
Raj

"Crash" wrote:

Raj wrote:
I have an unmanaged C++ dll which Iam trying to consume in C#.The code is as
below

//Unmanaged dll definition
typedef struct {
float a;
bool b;
char * c;
int d;
unsigned e;
unsigned char * f;
} sample;

....
Any thoughts?Thanks


1) You probably will need a MarshalAs for the string ptr - though I
can't find a sample right off hand...

2) Double check marshalling for the "bool". By default .NET marshals
PInvoke bools as WIN32 bools {32 bit} with values equal to WIN32 TRUE
and FALSE #defines - which is not good for C++ {8 bit} bool type...
You can marshal the bool as a char if there is no usable MarshalAs
override...

Feb 13 '06 #3
Crash,

By default, .NET marshals bool as the BOOL type (32-bits).

Also, the char type in .NET is 16 bits, not 8, so you can't use that
instead.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Crash" <so*********@san.rr.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...

Raj wrote:
I have an unmanaged C++ dll which Iam trying to consume in C#.The code is
as
below

//Unmanaged dll definition
typedef struct {
float a;
bool b;
char * c;
int d;
unsigned e;
unsigned char * f;
} sample;

...
Any thoughts?Thanks


1) You probably will need a MarshalAs for the string ptr - though I
can't find a sample right off hand...

2) Double check marshalling for the "bool". By default .NET marshals
PInvoke bools as WIN32 bools {32 bit} with values equal to WIN32 TRUE
and FALSE #defines - which is not good for C++ {8 bit} bool type...
You can marshal the bool as a char if there is no usable MarshalAs
override...

Feb 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Van Altena via .NET 247 | last post: by
2 posts views Thread by Neil McPhail | last post: by
5 posts views Thread by Ninan | last post: by
20 posts views Thread by ma0001yu | last post: by
14 posts views Thread by Lane Straatman | last post: by
2 posts views Thread by Laurent Deniau | last post: by
6 posts views Thread by =?Utf-8?B?RmFiaWFu?= | last post: by
reply views Thread by leo001 | last post: by

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.