how to marshal a struct with a char arrray in VC++ CLR

Joined
May 30, 2007
Messages
1
Reaction score
0
Hi,
i am new to .NET framework so not that knowledgable about managed and unmanaged codes.

The problem i am facing is i need to call a function say fun_test(struct test * A) which is present in an unmanaged dll, from my CLR class library.

The structure 'test' is present in unmanaged C file as

struct test{
int x;
char info[256];
};
The fuction fun_test just populates some value in to the reference structure.


I declared the structure and marshalled the textinfo as shown

[StructLayout(LayoutKind::Sequential)]
ref struct test{
int x;

[System::Runtime::InteropServices::MarshalAsAttribute(System::Runtime::InteropServices::UnmanagedType::ByValTStr,SizeConst=5)] System::String^ textinfo;
};
//I imported the fuction as below

[DllImport("test.dll",EntryPoint="test_struct_fun")]
static int fun_test( test^ struct_instance);

I get no compilation or runtime error but the structure whose reference i passed is not getting updated at all.

This is how i call fun_test from my managed code.
int classA::wrapper(){


classA::test_struct^ struct_instance1 = gcnew test_struct();

fun_test((test_struct^)struct_instance1);


if ( struct_instance1->x == 2){
return 3;
}

return 0;
}

NOTE: I am able to call this function succesfully with the structure getting populated if i removed the char[5] member from the structure.
I suspect it has to do something with the marshalling of char to string^ or its initialization.

I was able to call this function succesfully with C# and VB where char Info[5] is marshalled as String.

Thanks in advance
Rocky
 

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