473,520 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11932
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
3069
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 mmapfile. Any example or link to an example will be of help. I am interested to learn how to achieve efficient sharing of data between separate...
1
1543
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
4179
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 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types...
0
1861
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 have multiple threads which all map mmf, is there anyway to "lock" mmf to prevent simultanous writes? I don't know if it is wise to use mmf in .Net...
1
4539
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 struct like (from another posting in this group): <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _ Private Structure...
6
11137
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 calls? I need this because I don't want to create a regular file on disk (reasons for which are many , say directory permissions ), but being a file...
3
7542
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 iterator as following char *str = return_a_big_size_str(); istringstream ss(string(str), istringstream::in); istreambuf_iterator<char> bit(ss), eit;...
1
2166
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 whatever part of it i'm working on? basically, i'm wondering if it would be ok for me to have multiple mmap's open on very large files as i read or write...
2
3602
by: ARAVINDRNR | last post by:
How to change an unsigned char array to bit map file in 'C' langauge
0
7206
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7444
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5749
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4790
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3284
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1652
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
510
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.