Re: Marshal object as a byte array.

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



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
.



Relevant Pages

  • Re: visibility matter on converting object to array
    ... i'll get an array having keys that from member variable's name ... i guessed "i can get public members but not protected, private, static ... Is it right way to access object members?(by converting to ...
    (comp.lang.php)
  • Re: Array Design
    ... ITOH it's pretty sure you are referencing an element in the array that you ... > When the application starts I initialise a state class, ... > I ask the user how many members to create. ... >>> have exceptions cropping up. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: extract items from list in single field
    ... Try using the Splitfunction to change the delimited string into an array. ... You would then loop through the members of the array and write one record to ...
    (microsoft.public.access.modulesdaovba)
  • Re: how to make a variable based on array length and member value
    ... I have an array full_arrayof an arbitrary number of transaction ... From the array I have created a new array years.uniq! ... what id like to do is take the new arrays members and use them as ... will fail, because you HAVE NOT YET WRITTEN THE CODE. ...
    (comp.lang.ruby)
  • RE: Xml serialization, arrays, XmlAnyAttribute
    ... an array type property/member. ... The use case I was trying to implement involved adding an xml attribute to ... .NET class's static members, correct? ... the .NET Xmlserializer based xml serialization ...
    (microsoft.public.dotnet.framework)