473,326 Members | 2,108 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,326 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 2606

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
0
by: Stefan | last post by:
Hi, Could anyone share some light on this and how I can work around it. If I have 2 managed c++ dlls (A and B) and B tries to use a native class in A this fails to link. It is easily...
5
by: Ninan | last post by:
given a struct typedef struct { int a; int b; } Logstruct; How can I add a new member to the struct typedef struct {
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
6
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I have a class hierarchy distributed over 3 native C++ dlls. The base class has a .NET Windows.Form for status output via a gcroot<>. The gcroot is declared private - the sub classes only...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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: 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.