468,537 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

sharing a structure containing char array using memory mapped file

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

Nov 16 '05 #1
2 10932
Hi,

"vikas" <vi***@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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


Nov 16 '05 #2
Hi,

"vikas" <vi***@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
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" wrote:
Hi,

"vikas" <vi***@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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



Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Srijit Kumar Bhadra | last post: by
1 post views Thread by Jan | last post: by
6 posts views Thread by Eric Smith | last post: by
reply views Thread by Ville Pirhonen | last post: by
6 posts views Thread by Divick | last post: by
3 posts views Thread by wakun | last post: by
1 post views Thread by Carl Mackey | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.