472,969 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,969 software developers and data experts.

Re: share a structure array containing multidimensional char array

Hi Willy,

Thank you very much for your work.

C++ code doesnot make any serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"

I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.

Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:
Oh, but 256*256 is only 64.000 bytes!

You can declare your structure like this:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}

The "writer" has to serialize the structure data and provide the size of the
total number of bytes in the shared memory segment to the "reader", for
instance as an int value at the start of the MM segment. Without this length
info there is no way to know the size and number of the individual
structures stored in the MM segment.

Then, the reader needs to deserialize the memory buffer as follows:

// pBuf is the pointer that points to the shared memory buffer returned
by the MapViewOfFile Win32 API.

static void GetObjectData2(IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
// use the array of structs...
foreach(msg_file_s s in sa)
{
// use structure data
int x = s.mb_buffer.GetUpperBound(0);
int y = s.mb_buffer.GetUpperBound(1);
...
}

}
}
Willy.
"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
Hi,

My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};

struct msg_file_s msg_file[16];

This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.

Best Regards
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:
"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:13**********************************@microsof t.com...
I want to share a structure array containing multi dimensional char
array
between C# and C++, by using memory mapped file.

I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.

I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below structure
array
).
2. How should I define pointer to structure array in C# ( for the below
structure array ).

Any help related to this would be highly appreciated.

C++ implementation:

struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;
HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,

NULL,

PAGE_READWRITE,
0,

sizeof( struct msg_file_s ),

_T("TEST"));

*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,

FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0
);
return(hKernel);
}
map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}

/************************************************** **************************/

C# implementation:

[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType= UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer


Are you sure you want to pass such a huge amount of data through Shared
memory (MM file)?
I see in your declaration that one element is already 1000000 bytes, how
large are the other elements? and how large is the array of structures
you
want to share?

Note that passing data through Shared Memory requires a lot more memory
because you'll have to marshal the data from unmanaged memory to managed
memory before you will be able to access the managed presentation of the
unmanaged data. Note that the extra marshaling will reduce the
performance
advantage of the shared memory "transfer" significantly, if not
completely.

Willy.



Jun 27 '08 #1
5 3718
See Inline

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:F6**********************************@microsof t.com...
Hi Willy,

Thank you very much for your work.

C++ code doesnot make any serialization.
Mapping a datastructure, whatever it's type, in a serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"
What error do you get? Note that when mapping a structure to shared memory,
you have to consider the alignment and packing requirements of both readers
and writers if both are wriiten using different languages or compiler tools.
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.

Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:
>Oh, but 256*256 is only 64.000 bytes!

You can declare your structure like this:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}

The "writer" has to serialize the structure data and provide the size of
the
total number of bytes in the shared memory segment to the "reader", for
instance as an int value at the start of the MM segment. Without this
length
info there is no way to know the size and number of the individual
structures stored in the MM segment.

Then, the reader needs to deserialize the memory buffer as follows:

// pBuf is the pointer that points to the shared memory buffer
returned
by the MapViewOfFile Win32 API.

static void GetObjectData2(IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0,
len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
// use the array of structs...
foreach(msg_file_s s in sa)
{
// use structure data
int x = s.mb_buffer.GetUpperBound(0);
int y = s.mb_buffer.GetUpperBound(1);
...
}

}
}
Willy.
"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:6E**********************************@microso ft.com...
Hi,

My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};

struct msg_file_s msg_file[16];

This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects
for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.

Best Regards
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:13**********************************@microso ft.com...
I want to share a structure array containing multi dimensional char
array
between C# and C++, by using memory mapped file.

I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.

I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below
structure
array
).
2. How should I define pointer to structure array in C# ( for the
below
structure array ).

Any help related to this would be highly appreciated.

C++ implementation:

struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;
HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,

NULL,

PAGE_READWRITE,

0,

sizeof( struct msg_file_s ),

_T("TEST"));

*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,

FILE_MAP_READ | FILE_MAP_WRITE,

0,

0,

0
);
return(hKernel);
}
map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}

/************************************************** **************************/

C# implementation:

[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType= UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer


Are you sure you want to pass such a huge amount of data through
Shared
memory (MM file)?
I see in your declaration that one element is already 1000000 bytes,
how
large are the other elements? and how large is the array of structures
you
want to share?

Note that passing data through Shared Memory requires a lot more
memory
because you'll have to marshal the data from unmanaged memory to
managed
memory before you will be able to access the managed presentation of
the
unmanaged data. Note that the extra marshaling will reduce the
performance
advantage of the shared memory "transfer" significantly, if not
completely.

Willy.




Jun 27 '08 #2
Hi Willy,

I am using C++ VS2005 and C# VS2005 compilers.
I write DLL with C++ VS2005. Struct member alignment=1byte.
I write application with C# VS2005.
I am not experienced at C# and dont know how to set structure alignment and
pack size.

Deserialization Error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in mscorlib.dll
Additional information: Binary stream '0' does not contain a valid
BinaryHeader.
Possible causes are invalid stream or object version change between
serialization and deserialization.

Thank you very much for your valuable effort.
Best Regards,
Aykut
"Willy Denoyette [MVP]" wrote:
See Inline

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:F6**********************************@microsof t.com...
Hi Willy,

Thank you very much for your work.

C++ code doesnot make any serialization.

Mapping a datastructure, whatever it's type, in a serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"
What error do you get? Note that when mapping a structure to shared memory,
you have to consider the alignment and packing requirements of both readers
and writers if both are wriiten using different languages or compiler tools.
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.

Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:
Oh, but 256*256 is only 64.000 bytes!

You can declare your structure like this:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}

The "writer" has to serialize the structure data and provide the size of
the
total number of bytes in the shared memory segment to the "reader", for
instance as an int value at the start of the MM segment. Without this
length
info there is no way to know the size and number of the individual
structures stored in the MM segment.

Then, the reader needs to deserialize the memory buffer as follows:

// pBuf is the pointer that points to the shared memory buffer
returned
by the MapViewOfFile Win32 API.

static void GetObjectData2(IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0,
len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
// use the array of structs...
foreach(msg_file_s s in sa)
{
// use structure data
int x = s.mb_buffer.GetUpperBound(0);
int y = s.mb_buffer.GetUpperBound(1);
...
}

}
}
Willy.
"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
Hi,

My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};

struct msg_file_s msg_file[16];

This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects
for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.

Best Regards
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:13**********************************@microsof t.com...
I want to share a structure array containing multi dimensional char
array
between C# and C++, by using memory mapped file.

I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.

I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below
structure
array
).
2. How should I define pointer to structure array in C# ( for the
below
structure array ).

Any help related to this would be highly appreciated.

C++ implementation:

struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;
HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,

NULL,

PAGE_READWRITE,

0,

sizeof( struct msg_file_s ),

_T("TEST"));

*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,

FILE_MAP_READ | FILE_MAP_WRITE,

0,

0,

0
);
return(hKernel);
}
map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}

/************************************************** **************************/

C# implementation:

[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType= UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer


Are you sure you want to pass such a huge amount of data through
Shared
memory (MM file)?
I see in your declaration that one element is already 1000000 bytes,
how
large are the other elements? and how large is the array of structures
you
want to share?

Note that passing data through Shared Memory requires a lot more
memory
because you'll have to marshal the data from unmanaged memory to
managed
memory before you will be able to access the managed presentation of
the
unmanaged data. Note that the extra marshaling will reduce the
performance
advantage of the shared memory "transfer" significantly, if not
completely.

Willy.




Jun 27 '08 #3
My original test code is shown below
I may have several errors,

hoping for your help.
Thanks in advance
Aykut

C++ code asis

HANDLE hKernel;
HANDLE hFile;

struct msg_file_s
{
unsigned char mb_buffer[10][10];
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;

HANDLE gfnInitMemory(struct msg_file_s **kernel_ram_ptr)
{
hKernel = CreateFileMapping ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));

if (hKernel != NULL)
{
switch(GetLastError())
{
case ERROR_ALREADY_EXISTS : return(FALSE);
case 0 : break;
}
}
else return(NULL);

*kernel_ram_ptr = (struct msg_file_s *)MapViewOfFile( hKernel,
FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0 );

return(hKernel);
}
void init_struct()
{
struct msg_file_s *file_ptr;
file_ptr = &msg_file[0];
for(BYTE i=0;i<16;i++,file_ptr++)
{
for(BYTE i=0;i<10;i++)
for(BYTE j=0;j<10;j++)
file_ptr->mb_buffer[i][j] = 0x55;
}
}
// create and fill the memory mapped file
test_function()
{
msg_file_ptr = &msg_file[0];
hKernel = NULL;
hKernel = gfnInitMemory(&msg_file_ptr);

if (hKernel == NULL)
{
exit(0);
}

init_struct();
}

/************************************************** ************************************************** ***/
/************************************************** ************************************************** ***/
C# Code asis

[Serializable]
[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
public byte[,] mb_buffer;
}

unsafe msg_file_s[] msg_file = new msg_file_s[16];
IntPtr hFileMap;
IntPtr address;
unsafe IntPtr map_memory_file()
{
hFileMap = CreateFileMapping(InvalidHandleValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show("hFileMap olmadı");
return IntPtr.Zero;
}

address = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, IntPtr.Zero);
if (address == IntPtr.Zero)
{
MessageBox.Show("address olmadı");
return IntPtr.Zero;
}

return address;
}
unsafe static void GetObjectData2(IntPtr pBuf)
{
// set size of data bytes in MM segment manually
int len = 100;

// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(pBuf, data, 0, len);

// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize(ms);

// just a test to show
int x = sa[0].mb_buffer.GetUpperBound(0);
MessageBox.Show(x.ToString());

}

// test access memory mapped file
private void button1_Click(object sender, EventArgs e)
{
address = map_memory_file();
GetObjectData2(address);
}
"Aykut Ergin" wrote:
Hi Willy,

I am using C++ VS2005 and C# VS2005 compilers.
I write DLL with C++ VS2005. Struct member alignment=1byte.
I write application with C# VS2005.
I am not experienced at C# and dont know how to set structure alignment and
pack size.

Deserialization Error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in mscorlib.dll
Additional information: Binary stream '0' does not contain a valid
BinaryHeader.
Possible causes are invalid stream or object version change between
serialization and deserialization.

Thank you very much for your valuable effort.
Best Regards,
Aykut
"Willy Denoyette [MVP]" wrote:
See Inline

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:F6**********************************@microsof t.com...
Hi Willy,
>
Thank you very much for your work.
>
C++ code doesnot make any serialization.
Mapping a datastructure, whatever it's type, in a serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"
>
What error do you get? Note that when mapping a structure to shared memory,
you have to consider the alignment and packing requirements of both readers
and writers if both are wriiten using different languages or compiler tools.
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.
>
Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer
>
>
>
"Willy Denoyette [MVP]" wrote:
>
>Oh, but 256*256 is only 64.000 bytes!
>>
>You can declare your structure like this:
>>
> [Serializable]
> [StructLayout(LayoutKind.Sequential)]
> public struct msg_file_s
> {
> public byte[,] mb_buffer;
> public byte[] other;
> }
>>
>The "writer" has to serialize the structure data and provide the size of
>the
>total number of bytes in the shared memory segment to the "reader", for
>instance as an int value at the start of the MM segment. Without this
>length
>info there is no way to know the size and number of the individual
>structures stored in the MM segment.
>>
>Then, the reader needs to deserialize the memory buffer as follows:
>>
> // pBuf is the pointer that points to the shared memory buffer
>returned
>by the MapViewOfFile Win32 API.
>>
> static void GetObjectData2(IntPtr pBuf)
> {
> // get size of data bytes in MM segment
> int len = Marshal.ReadInt32(pBuf, 0);
> // copy the MM data to a managed array
> byte[] data = new byte[len];
> Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0,
>len);
> // deserialize the managed byte[] to the struct representation
> BinaryFormatter bf = new BinaryFormatter();
> MemoryStream ms = new MemoryStream(data);
> msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
> // use the array of structs...
> foreach(msg_file_s s in sa)
> {
> // use structure data
> int x = s.mb_buffer.GetUpperBound(0);
> int y = s.mb_buffer.GetUpperBound(1);
> ...
> }
>>
> }
> }
>>
>>
>Willy.
>>
>>
>"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
>news:6E**********************************@microso ft.com...
Hi,
>
My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};
>
struct msg_file_s msg_file[16];
>
This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects
for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.
>
Best Regards
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer
>
>
>
"Willy Denoyette [MVP]" wrote:
>
>"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
>news:13**********************************@microso ft.com...
>I want to share a structure array containing multi dimensional char
>array
between C# and C++, by using memory mapped file.
>
I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.
>
I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below
structure
array
).
2. How should I define pointer to structure array in C# ( for the
below
structure array ).
>
Any help related to this would be highly appreciated.
>
C++ implementation:
>
struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};
>
struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;
>
>
HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,
>
NULL,
>
PAGE_READWRITE,
>
0,
>
sizeof( struct msg_file_s ),
>
_T("TEST"));
>
*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,
>
FILE_MAP_READ | FILE_MAP_WRITE,
>
0,
>
0,
>
0
);
return(hKernel);
}
>
>
map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}
>
/************************************************** **************************/
>
C# implementation:
>
[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType= UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}
>
>
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer
>
>>
>>
>>
>Are you sure you want to pass such a huge amount of data through
>Shared
>memory (MM file)?
>I see in your declaration that one element is already 1000000 bytes,
>how
>large are the other elements? and how large is the array of structures
>you
>want to share?
>>
>Note that passing data through Shared Memory requires a lot more
>memory
>because you'll have to marshal the data from unmanaged memory to
>managed
>memory before you will be able to access the managed presentation of
>the
>unmanaged data. Note that the extra marshaling will reduce the
>performance
>advantage of the shared memory "transfer" significantly, if not
>completely.
>>
>Willy.
>>
>>
>>
>>
>>
>>
>>
Jun 27 '08 #4
Oh I see, you aren't 'serializing' the structure to shared memory, I guess I
wasn't clear when I said "the writer has to serialize...".

When you want to deserialize/serialize a managed type to/from unmanaged
memory, you have to use the same serializer/deserializer at both sides, that
means you also need to use .NET to serialize/deserialize at the C++ side.
For this you can use C++/CLI (VS2006 or VS2008) and compile your C++ program
using /clr.

If you don't want to use .NET at the C++ side, you'll have to "custom"
serialize, that means you'll have to copy the array of structures to the
buffer obtained by MapViewOfFile. Besides the structures, you also have to
store the number of structures serialized to the shared memory buffer and
the size of the individual structure members, without this info it's
impossible for the reader to determine the number of array members (number
of structs) and the size of the structure members.

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:9F**********************************@microsof t.com...
My original test code is shown below
I may have several errors,

hoping for your help.
Thanks in advance
Aykut

C++ code asis

HANDLE hKernel;
HANDLE hFile;

struct msg_file_s
{
unsigned char mb_buffer[10][10];
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;

HANDLE gfnInitMemory(struct msg_file_s **kernel_ram_ptr)
{
hKernel = CreateFileMapping ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));

if (hKernel != NULL)
{
switch(GetLastError())
{
case ERROR_ALREADY_EXISTS : return(FALSE);
case 0 : break;
}
}
else return(NULL);

*kernel_ram_ptr = (struct msg_file_s *)MapViewOfFile( hKernel,
FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0 );

return(hKernel);
}
void init_struct()
{
struct msg_file_s *file_ptr;
file_ptr = &msg_file[0];
for(BYTE i=0;i<16;i++,file_ptr++)
{
for(BYTE i=0;i<10;i++)
for(BYTE j=0;j<10;j++)
file_ptr->mb_buffer[i][j] = 0x55;
}
}
// create and fill the memory mapped file
test_function()
{
msg_file_ptr = &msg_file[0];
hKernel = NULL;
hKernel = gfnInitMemory(&msg_file_ptr);

if (hKernel == NULL)
{
exit(0);
}

init_struct();
}

/************************************************** ************************************************** ***/
/************************************************** ************************************************** ***/
C# Code asis

[Serializable]
[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
public byte[,] mb_buffer;
}

unsafe msg_file_s[] msg_file = new msg_file_s[16];
IntPtr hFileMap;
IntPtr address;
unsafe IntPtr map_memory_file()
{
hFileMap = CreateFileMapping(InvalidHandleValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show("hFileMap olmadı");
return IntPtr.Zero;
}

address = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, IntPtr.Zero);
if (address == IntPtr.Zero)
{
MessageBox.Show("address olmadı");
return IntPtr.Zero;
}

return address;
}
unsafe static void GetObjectData2(IntPtr pBuf)
{
// set size of data bytes in MM segment manually
int len = 100;

// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(pBuf, data, 0, len);

// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize(ms);

// just a test to show
int x = sa[0].mb_buffer.GetUpperBound(0);
MessageBox.Show(x.ToString());

}

// test access memory mapped file
private void button1_Click(object sender, EventArgs e)
{
address = map_memory_file();
GetObjectData2(address);
}
"Aykut Ergin" wrote:
>Hi Willy,

I am using C++ VS2005 and C# VS2005 compilers.
I write DLL with C++ VS2005. Struct member alignment=1byte.
I write application with C# VS2005.
I am not experienced at C# and dont know how to set structure alignment
and
pack size.

Deserialization Error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcept ion' occurred in
mscorlib.dll
Additional information: Binary stream '0' does not contain a valid
BinaryHeader.
Possible causes are invalid stream or object version change between
serialization and deserialization.

Thank you very much for your valuable effort.
Best Regards,
Aykut
"Willy Denoyette [MVP]" wrote:
See Inline

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:F6**********************************@microsof t.com...
Hi Willy,

Thank you very much for your work.

C++ code doesnot make any serialization.

Mapping a datastructure, whatever it's type, in a serialization.

So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"

What error do you get? Note that when mapping a structure to shared
memory,
you have to consider the alignment and packing requirements of both
readers
and writers if both are wriiten using different languages or compiler
tools.

I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and
C++.
I am looking other ways to share memory between C++ and C#.

Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:

Oh, but 256*256 is only 64.000 bytes!

You can declare your structure like this:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}

The "writer" has to serialize the structure data and provide the
size of
the
total number of bytes in the shared memory segment to the "reader",
for
instance as an int value at the start of the MM segment. Without
this
length
info there is no way to know the size and number of the individual
structures stored in the MM segment.

Then, the reader needs to deserialize the memory buffer as follows:

// pBuf is the pointer that points to the shared memory buffer
returned
by the MapViewOfFile Win32 API.

static void GetObjectData2(IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0,
len);
// deserialize the managed byte[] to the struct
representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
// use the array of structs...
foreach(msg_file_s s in sa)
{
// use structure data
int x = s.mb_buffer.GetUpperBound(0);
int y = s.mb_buffer.GetUpperBound(1);
...
}

}
}
Willy.
"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in
message
news:6E**********************************@microso ft.com...
Hi,

My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};

struct msg_file_s msg_file[16];

This structure will execute on communication dll ( written with
C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several
projects
for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003
/ XP.
We do not have any problem.

Best Regards
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer

"Willy Denoyette [MVP]" wrote:

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in
message
news:13**********************************@microso ft.com...
I want to share a structure array containing multi dimensional
char
array
between C# and C++, by using memory mapped file.

I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for
C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.

I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below
structure
array
).
2. How should I define pointer to structure array in C# ( for
the
below
structure array ).

Any help related to this would be highly appreciated.

C++ implementation:

struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;
HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,

NULL,

PAGE_READWRITE,

0,

sizeof( struct msg_file_s ),

_T("TEST"));

*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,

FILE_MAP_READ | FILE_MAP_WRITE,

0,

0,

0
);
return(hKernel);
}
map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}

/************************************************** **************************/

C# implementation:

[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType=
UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}
--
Aykut ERGİN
Nortel Netaş
Software Design Engineer


Are you sure you want to pass such a huge amount of data through
Shared
memory (MM file)?
I see in your declaration that one element is already 1000000
bytes,
how
large are the other elements? and how large is the array of
structures
you
want to share?

Note that passing data through Shared Memory requires a lot more
memory
because you'll have to marshal the data from unmanaged memory to
managed
memory before you will be able to access the managed presentation
of
the
unmanaged data. Note that the extra marshaling will reduce the
performance
advantage of the shared memory "transfer" significantly, if not
completely.

Willy.




Jun 27 '08 #5
Hi Willy,

Thank you very much for your help.
I will try your solution for C++.

Kind Regards,
Aykut
"Willy Denoyette [MVP]" wrote:
Oh I see, you aren't 'serializing' the structure to shared memory, I guess I
wasn't clear when I said "the writer has to serialize...".

When you want to deserialize/serialize a managed type to/from unmanaged
memory, you have to use the same serializer/deserializer at both sides, that
means you also need to use .NET to serialize/deserialize at the C++ side.
For this you can use C++/CLI (VS2006 or VS2008) and compile your C++ program
using /clr.

If you don't want to use .NET at the C++ side, you'll have to "custom"
serialize, that means you'll have to copy the array of structures to the
buffer obtained by MapViewOfFile. Besides the structures, you also have to
store the number of structures serialized to the shared memory buffer and
the size of the individual structure members, without this info it's
impossible for the reader to determine the number of array members (number
of structs) and the size of the structure members.

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:9F**********************************@microsof t.com...
My original test code is shown below
I may have several errors,

hoping for your help.
Thanks in advance
Aykut

C++ code asis

HANDLE hKernel;
HANDLE hFile;

struct msg_file_s
{
unsigned char mb_buffer[10][10];
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;

HANDLE gfnInitMemory(struct msg_file_s **kernel_ram_ptr)
{
hKernel = CreateFileMapping ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));

if (hKernel != NULL)
{
switch(GetLastError())
{
case ERROR_ALREADY_EXISTS : return(FALSE);
case 0 : break;
}
}
else return(NULL);

*kernel_ram_ptr = (struct msg_file_s *)MapViewOfFile( hKernel,
FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0 );

return(hKernel);
}
void init_struct()
{
struct msg_file_s *file_ptr;
file_ptr = &msg_file[0];
for(BYTE i=0;i<16;i++,file_ptr++)
{
for(BYTE i=0;i<10;i++)
for(BYTE j=0;j<10;j++)
file_ptr->mb_buffer[i][j] = 0x55;
}
}
// create and fill the memory mapped file
test_function()
{
msg_file_ptr = &msg_file[0];
hKernel = NULL;
hKernel = gfnInitMemory(&msg_file_ptr);

if (hKernel == NULL)
{
exit(0);
}

init_struct();
}

/************************************************** ************************************************** ***/
/************************************************** ************************************************** ***/
C# Code asis

[Serializable]
[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
public byte[,] mb_buffer;
}

unsafe msg_file_s[] msg_file = new msg_file_s[16];
IntPtr hFileMap;
IntPtr address;
unsafe IntPtr map_memory_file()
{
hFileMap = CreateFileMapping(InvalidHandleValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show("hFileMap olmadı");
return IntPtr.Zero;
}

address = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, IntPtr.Zero);
if (address == IntPtr.Zero)
{
MessageBox.Show("address olmadı");
return IntPtr.Zero;
}

return address;
}
unsafe static void GetObjectData2(IntPtr pBuf)
{
// set size of data bytes in MM segment manually
int len = 100;

// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(pBuf, data, 0, len);

// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize(ms);

// just a test to show
int x = sa[0].mb_buffer.GetUpperBound(0);
MessageBox.Show(x.ToString());

}

// test access memory mapped file
private void button1_Click(object sender, EventArgs e)
{
address = map_memory_file();
GetObjectData2(address);
}
"Aykut Ergin" wrote:
Hi Willy,

I am using C++ VS2005 and C# VS2005 compilers.
I write DLL with C++ VS2005. Struct member alignment=1byte.
I write application with C# VS2005.
I am not experienced at C# and dont know how to set structure alignment
and
pack size.

Deserialization Error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll
Additional information: Binary stream '0' does not contain a valid
BinaryHeader.
Possible causes are invalid stream or object version change between
serialization and deserialization.

Thank you very much for your valuable effort.
Best Regards,
Aykut
"Willy Denoyette [MVP]" wrote:

See Inline

Willy.

"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in message
news:F6**********************************@microsof t.com...
Hi Willy,
>
Thank you very much for your work.
>
C++ code doesnot make any serialization.

Mapping a datastructure, whatever it's type, in a serialization.

So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);"
>
What error do you get? Note that when mapping a structure to shared
memory,
you have to consider the alignment and packing requirements of both
readers
and writers if both are wriiten using different languages or compiler
tools.

I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and
C++.
I am looking other ways to share memory between C++ and C#.
>
Best Regards,
Aykut ERGİN
Nortel Netaş
Software Design Engineer
>
>
>
"Willy Denoyette [MVP]" wrote:
>
>Oh, but 256*256 is only 64.000 bytes!
>>
>You can declare your structure like this:
>>
> [Serializable]
> [StructLayout(LayoutKind.Sequential)]
> public struct msg_file_s
> {
> public byte[,] mb_buffer;
> public byte[] other;
> }
>>
>The "writer" has to serialize the structure data and provide the
>size of
>the
>total number of bytes in the shared memory segment to the "reader",
>for
>instance as an int value at the start of the MM segment. Without
>this
>length
>info there is no way to know the size and number of the individual
>structures stored in the MM segment.
>>
>Then, the reader needs to deserialize the memory buffer as follows:
>>
> // pBuf is the pointer that points to the shared memory buffer
>returned
>by the MapViewOfFile Win32 API.
>>
> static void GetObjectData2(IntPtr pBuf)
> {
> // get size of data bytes in MM segment
> int len = Marshal.ReadInt32(pBuf, 0);
> // copy the MM data to a managed array
> byte[] data = new byte[len];
> Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0,
>len);
> // deserialize the managed byte[] to the struct
>representation
> BinaryFormatter bf = new BinaryFormatter();
> MemoryStream ms = new MemoryStream(data);
> msg_file_s[] sa = (msg_file_s[]) bf.Deserialize(ms);
> // use the array of structs...
> foreach(msg_file_s s in sa)
> {
> // use structure data
> int x = s.mb_buffer.GetUpperBound(0);
> int y = s.mb_buffer.GetUpperBound(1);
> ...
> }
>>
> }
> }
>>
>>
>Willy.
>>
>>
>"Aykut Ergin" <Ay********@discussions.microsoft.comwrote in
>message
>news:6E**********************************@microso ft.com...
Hi,
>
My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};
>
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: M.Siler | last post by:
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT> <!-- var factor_val = new Array(8,7) factor_val = 68.8 factor_val = 55
2
by: shane | last post by:
Ive searched a fair bit for the answer, but nothing has come up that matches what i want to do. I'm having an issue with passing and assigning pointers to multidimensional arrays. The code: ...
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...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
2
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.