Re: Marshal object as a byte array.
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Thu, 05 Jul 2007 09:05:56 +0200
repekto.argosoft@xxxxxxxxx wrote:
Hi,
I'm working with Marshalling. I got an idea to pass an object to the
Native side as a byte array i.e. as void* in terms of C.
For example:
I have a .NET structure Point that contains two fields X and Y that
means that the byte array representation will look in the memory like
this: "XXXXYYYY" First four bytes are X and second for bytes are Y.
Then I marshall this array to the C-style function. I declare this
function in C# code this way:
[DllImport( "ImageProcessor.dll",
CharSet = CharSet.Auto,
EntryPoint = "Uninitialize" )]
private static extern int PassPoint_native(byte[] serializedPoint);
This function is declared on the native part this way:
int PassPoint_native(CPoint* point);
where CPoint class is following:
struct CPoint
{
int X;
int Y;
}
The byte array passed to the function PassPoint will be mapped to the
class CPoint and I get the object in terms of C/C++.
This is a simple example, but I think it can be used in more complex
cases.
Is it possible to represent the C# object as a raw byte array?
Kind regards,
Eugene.
Yes, it's possible, but:
:: You have to pin the object in memory, so that the garbage collector can't move it around.
:: The object can only contain value type members. It can't contain references to other objects.
:: The members are not always stored back to back within the object. There may be padding between the members to align them to suitable offsets. I'm not sure, but I think that the compiler might also rearrange members to store them better.
--
Göran Andersson
_____
http://www.guffa.com
.
- Follow-Ups:
- Re: Marshal object as a byte array.
- From: repekto . argosoft
- Re: Marshal object as a byte array.
- From: repekto . argosoft
- Re: Marshal object as a byte array.
- References:
- Marshal object as a byte array.
- From: repekto . argosoft
- Marshal object as a byte array.
- Prev by Date: Re: Why are C# conversion operators static?
- Next by Date: Re: whats faster, initialize component, or form load?
- Previous by thread: Marshal object as a byte array.
- Next by thread: Re: Marshal object as a byte array.
- Index(es):
Relevant Pages
|