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(La youtKind.Sequen tial)]
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.ReadInt 32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
// use the array of structs...
foreach(msg_fil e_s s in sa)
{
// use structure data
int x = s.mb_buffer.Get UpperBound(0);
int y = s.mb_buffer.Get UpperBound(1);
...
}
}
}
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message
news:6E******** *************** ***********@mic rosoft.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********@dis cussions.micros oft.comwrote in message
news:13******** *************** ***********@mic rosoft.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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
5 3794
See Inline
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message
news:F6******** *************** ***********@mic rosoft.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(La youtKind.Sequen tial)] 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.ReadInt 32(pBuf, 0); // copy the MM data to a managed array byte[] data = new byte[len]; Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0, len); // deserialize the managed byte[] to the struct representation BinaryFormatter bf = new BinaryFormatter (); MemoryStream ms = new MemoryStream(da ta); msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms); // use the array of structs... foreach(msg_fil e_s s in sa) { // use structure data int x = s.mb_buffer.Get UpperBound(0); int y = s.mb_buffer.Get UpperBound(1); ... }
} }
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message news:6E******* *************** ************@mi crosoft.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********@dis cussions.micros oft.comwrote in message news:13******* *************** ************@mi crosoft.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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
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. SerializationEx ception' 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********@dis cussions.micros oft.comwrote in message
news:F6******** *************** ***********@mic rosoft.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(La youtKind.Sequen tial)]
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.ReadInt 32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0,
len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
// use the array of structs...
foreach(msg_fil e_s s in sa)
{
// use structure data
int x = s.mb_buffer.Get UpperBound(0);
int y = s.mb_buffer.Get UpperBound(1);
...
}
}
}
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message
news:6E******** *************** ***********@mic rosoft.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********@dis cussions.micros oft.comwrote in message
news:13******** *************** ***********@mic rosoft.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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
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(s truct msg_file_s **kernel_ram_pt r)
{
hKernel = CreateFileMappi ng ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));
if (hKernel != NULL)
{
switch(GetLastE rror())
{
case ERROR_ALREADY_E XISTS : 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++,fi le_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(La youtKind.Sequen tial)]
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 = CreateFileMappi ng(InvalidHandl eValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show ("hFileMap olmadı");
return IntPtr.Zero;
}
address = MapViewOfFile(h FileMap, 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(pB uf, data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize (ms);
// just a test to show
int x = sa[0].mb_buffer.GetU pperBound(0);
MessageBox.Show (x.ToString());
}
// test access memory mapped file
private void button1_Click(o bject 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. SerializationEx ception' 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********@dis cussions.micros oft.comwrote in message
news:F6******** *************** ***********@mic rosoft.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(La youtKind.Sequen tial)]
> 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.ReadInt 32(pBuf, 0);
> // copy the MM data to a managed array
> byte[] data = new byte[len];
> Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0,
>len);
> // deserialize the managed byte[] to the struct representation
> BinaryFormatter bf = new BinaryFormatter ();
> MemoryStream ms = new MemoryStream(da ta);
> msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
> // use the array of structs...
> foreach(msg_fil e_s s in sa)
> {
> // use structure data
> int x = s.mb_buffer.Get UpperBound(0);
> int y = s.mb_buffer.Get UpperBound(1);
> ...
> }
>>
> }
> }
>>
>>
>Willy.
>>
>>
>"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message
>news:6E******* *************** ************@mi crosoft.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********@dis cussions.micros oft.comwrote in message
>news:13******* *************** ************@mi crosoft.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 = CreateFileMappi ng ( (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_pt r);
}
>
/*************** *************** *************** *************** *************** */
>
C# implementation:
>
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
>>
>>
>>
>>
>>
>>
>>
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********@dis cussions.micros oft.comwrote in message
news:9F******** *************** ***********@mic rosoft.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(s truct msg_file_s **kernel_ram_pt r)
{
hKernel = CreateFileMappi ng ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));
if (hKernel != NULL)
{
switch(GetLastE rror())
{
case ERROR_ALREADY_E XISTS : 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++,fi le_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(La youtKind.Sequen tial)]
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 = CreateFileMappi ng(InvalidHandl eValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show ("hFileMap olmadı");
return IntPtr.Zero;
}
address = MapViewOfFile(h FileMap, 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(pB uf, data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize (ms);
// just a test to show
int x = sa[0].mb_buffer.GetU pperBound(0);
MessageBox.Show (x.ToString());
}
// test access memory mapped file
private void button1_Click(o bject 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.
Deserializatio n Error: An unhandled exception of type 'System.Runtim e.Serialization .SerializationE xception' 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 serializatio n and deserialization .
Thank you very much for your valuable effort. Best Regards, Aykut
"Willy Denoyette [MVP]" wrote:
See Inline
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message
news:F6******** *************** ***********@mic rosoft.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(La youtKind.Sequen tial)] 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.ReadInt 32(pBuf, 0); // copy the MM data to a managed array byte[] data = new byte[len]; Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0, len); // deserialize the managed byte[] to the struct representati on BinaryFormatter bf = new BinaryFormatter (); MemoryStream ms = new MemoryStream(da ta); msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms); // use the array of structs... foreach(msg_fil e_s s in sa) { // use structure data int x = s.mb_buffer.Get UpperBound(0); int y = s.mb_buffer.Get UpperBound(1); ... }
} }
Willy.
"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in message news:6E******* *************** ************@mi crosoft.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********@dis cussions.micros oft.comwrote in message news:13******* *************** ************@mi crosoft.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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType=
UnmanagedType.U 1,
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.
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********@dis cussions.micros oft.comwrote in message
news:9F******** *************** ***********@mic rosoft.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(s truct msg_file_s **kernel_ram_pt r)
{
hKernel = CreateFileMappi ng ((HANDLE) 0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof( struct msg_file_s ),
_T("TEST"));
if (hKernel != NULL)
{
switch(GetLastE rror())
{
case ERROR_ALREADY_E XISTS : 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++,fi le_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(La youtKind.Sequen tial)]
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 = CreateFileMappi ng(InvalidHandl eValue, IntPtr.Zero,
PAGE_READWRITE, 0, 100, "TEST");
if (hFileMap == IntPtr.Zero)
{
MessageBox.Show ("hFileMap olmadı");
return IntPtr.Zero;
}
address = MapViewOfFile(h FileMap, 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(pB uf, data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[])bf.Deserialize (ms);
// just a test to show
int x = sa[0].mb_buffer.GetU pperBound(0);
MessageBox.Show (x.ToString());
}
// test access memory mapped file
private void button1_Click(o bject 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. SerializationEx ception' 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********@dis cussions.micros oft.comwrote in message
news:F6******** *************** ***********@mic rosoft.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(La youtKind.Sequen tial)]
> 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.ReadInt 32(pBuf, 0);
> // copy the MM data to a managed array
> byte[] data = new byte[len];
> Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0,
>len);
> // deserialize the managed byte[] to the struct
>representati on
> BinaryFormatter bf = new BinaryFormatter ();
> MemoryStream ms = new MemoryStream(da ta);
> msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
> // use the array of structs...
> foreach(msg_fil e_s s in sa)
> {
> // use structure data
> int x = s.mb_buffer.Get UpperBound(0);
> int y = s.mb_buffer.Get UpperBound(1);
> ...
> }
>>
> }
> }
>>
>>
>Willy.
>>
>>
>"Aykut Ergin" <Ay********@dis cussions.micros oft.comwrote in
>message
>news:6E******* *************** ************@mi crosoft.com...
Hi,
>
My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};
>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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:
/*.....*/
TCHAR msg
func(msg);
/*.....*/
|
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
(paragraph 1 of section 6.5.3.4).
For instance, I've routinely done things like this:
struct foo {...
|
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 6.19
(http://www.eskimo.com/~scs/C-faq/q6.19.html).
Thanks.
|
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# 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:
| |
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 multiple arrays,etc)
I do not understand how to define the structure at run time.i.e.what
fields it will contain.
|
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 help do the most basic
things.
One of the "basic" things I haven't been able to find out how to do is how
to delete an item from a multidimensional array object and resize it
afterwards.
It seems so easy to conceive of the code to delete...
|
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 get an error:
----------------
An unhandled exception of type 'System.NullReferenceException' occured in
|
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)
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |