sharing a structure containing char array using memory mapped file

G

Guest

I have following structure in c++.
typedef struct MMF_result_struct
{
int action;
char text[1024];
int cols,rows;
int month,day,year;
} MMF_result;
Now this structure is shared between C++ and C# using memory mapped file. We already have C++ code for handling memory mapped file. I am working on converting code for memory mapped file in C#. Now I have to pass pointer to the above structure. I converted this structure to C# as follows:
[StructLayout(LayoutKind.Sequential)]
public struct MMF_result_struct
{
public int action;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public char[] a1;
public int cols;
public int rows;
public int month;
public int day;
public int year;
}
I am not able to determine size of the structure using sizeof operator. Also I am not able to typecast the void * pointer returned from memory mapped file back to above structure pointer. It says "Cannot take the address or size of a variable of a managed type ('MMF_result_struct') ". I dont know why when a char array is included in structure it starts treating structure as managed type. If we exclude the character array it works fine.

Any help related to this would be highly appreciated.

Vikas
 
B

BMermuys

Hi,

vikas said:
I have following structure in c++.
typedef struct MMF_result_struct
{
int action;
char text[1024];
int cols,rows;
int month,day,year;
} MMF_result;
Now this structure is shared between C++ and C# using memory mapped file.
We already have C++ code for handling memory mapped file. I am working on
converting code for memory mapped file in C#. Now I have to pass pointer to
the above structure. I converted this structure to C# as follows:
[StructLayout(LayoutKind.Sequential)]
public struct MMF_result_struct
{
public int action;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public char[] a1;
public int cols;
public int rows;
public int month;
public int day;
public int year;
}
I am not able to determine size of the structure using sizeof operator.
Also I am not able to typecast the void * pointer returned from memory
mapped file back to above structure pointer.

No you can't. The layout managed vs unmanaged isn't the same. Marshalling
must happen, the only way I know is using MarshalPtrToStruct :

MMF_result_struct res = (MMF_result_struct) Marshal.PtrToStruct( p,
typeof(MMF_result_struct) );
It says "Cannot take the address or size of a variable of a managed type
('MMF_result_struct') ". I dont know why when a char

To know the unmanaged size, use Marshal.SizeOf .

I also recommend using CharSet on the structure since you're working with
chars. If the char array is a null-terminated string, you can also use
string and UnmanagedType.ByValTStr with SizeConst.

If you need to write the struct back, you can use Marshal.StructToPtr.

HTH,
greetings
array is included in structure it starts treating structure as managed
type. If we exclude the character array it works fine.
 
B

BMermuys

Hi,

vikas said:
Thanks a lot for your reply. I have some issues. I changed the structure
definition as recommented by you to following
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct MMF_result_struct
{
public int action;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public char[] a1;
public int cols;
public int rows;
public int month;
public int day;
public int year;
}


When i use Marahal.SizeOf(MMF_result_struct) I get an error saying

// use this for null-terminated strings
// padding will occure with '\0' characters if the string is smaller then
SizeConst
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string a1;

// use this for non-null-terminated strings
// use default encoder to convert to and from string
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public char[] a1;


HTH,
Greetings
"Type TestMeditech.MMF_request_struct can not be marshaled as an unmanaged
structure; no meaningful size or offset can be computed."
Do I need to do some thing else before calling sizeof on structure.

Thanks

BMermuys said:
Hi,

vikas said:
I have following structure in c++.
typedef struct MMF_result_struct
{
int action;
char text[1024];
int cols,rows;
int month,day,year;
} MMF_result;
Now this structure is shared between C++ and C# using memory mapped
file.
We already have C++ code for handling memory mapped file. I am working on
converting code for memory mapped file in C#. Now I have to pass pointer to
the above structure. I converted this structure to C# as follows:
[StructLayout(LayoutKind.Sequential)]
public struct MMF_result_struct
{
public int action;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public char[] a1;
public int cols;
public int rows;
public int month;
public int day;
public int year;
}
I am not able to determine size of the structure using sizeof
operator.
Also I am not able to typecast the void * pointer returned from memory
mapped file back to above structure pointer.

No you can't. The layout managed vs unmanaged isn't the same. Marshalling
must happen, the only way I know is using MarshalPtrToStruct :

MMF_result_struct res = (MMF_result_struct) Marshal.PtrToStruct( p,
typeof(MMF_result_struct) );
It says "Cannot take the address or size of a variable of a managed
type
('MMF_result_struct') ". I dont know why when a char

To know the unmanaged size, use Marshal.SizeOf .

I also recommend using CharSet on the structure since you're working with
chars. If the char array is a null-terminated string, you can also use
string and UnmanagedType.ByValTStr with SizeConst.

If you need to write the struct back, you can use Marshal.StructToPtr.

HTH,
greetings
array is included in structure it starts treating structure as managed
type. If we exclude the character array it works fine.
Any help related to this would be highly appreciated.

Vikas
 

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