473,402 Members | 2,046 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,402 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 11913
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.