Marshal object as a byte array.

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.

.



Relevant Pages

  • Re: Marshalling Question: Array of Complex Structs?
    ... I get back an array of length 1 (I've only got one radio so this ... but that one struct contains only the initialization values I ... You can just tell the marshalling layer to embed the string into the ... public UInt32 Features; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Marshalling Question: Array of Complex Structs?
    ... I've used no managed types within the struct. ... the following is the code used to call the DLL function. ... C style arrays don't have any information about the length of the array, ... byte buffers to fill with character data while the marshalling code ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Marshalling Question: Array of Complex Structs?
    ... C style arrays don't have any information about the length of the array, ... but that one struct contains only the initialization values I ... You can just tell the marshalling layer to embed the string into the ... public UInt32 Features; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with Array from C# to COM
    ... It sounds likely that the array is not being correctly marshalled. ... you may have to modify your interop assembly usiing ... The marshalling in the IL ... > Set broadcast = app.get_broadcast ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Marshal object as a byte array.
    ... I'm working with Marshalling. ... Native side as a byte array i.e. as void* in terms of C. ... where CPoint class is following: ... :: The members are not always stored back to back within the object. ...
    (microsoft.public.dotnet.languages.csharp)