473,238 Members | 1,930 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,238 software developers and data experts.

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

Similar topics

1
by: Srijit Kumar Bhadra | last post by:
Hello, I see that it is possible to use mmapfile.pyd of win32all. The same is mentioned in http://www.python.org/windows/win32/#mmapfile. Unfortunately I could not trace any example using...
1
by: Jan | last post by:
Hi, I don't seem to find any c# methods for memory mapped file. Does C# have any? Or it is using different name? Thanks. Jan
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
0
by: Ville Pirhonen | last post by:
Hi ! I'm doing application with memory mapped file handling. I use mmf to change data between applications. Is dllimport the only way to use those mmf functions in VB.Net? In the application I...
1
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a...
6
by: Divick | last post by:
Is there a way in C++ to have a file stream which is directly mapped to memory(virtual memory) without actually having to create a file and then memory mapping that file using system dependent...
3
by: wakun | last post by:
Hi there, I am seeking a fastest way to load a BIG string and parse it as a given format. I have a extern function which return a (char *)string in BIG size. Now, I am going to parse it with a...
1
by: Carl Mackey | last post by:
hi, i'm new to this list and new to python as well. i have a question on the memory mapped file ability python has. when i use a mmap on a file, will it copy the whole thing to ram or just...
2
by: ARAVINDRNR | last post by:
How to change an unsigned char array to bit map file in 'C' langauge
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...

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.