Marshelling help

  • Thread starter Thread starter colin
  • Start date Start date
C

colin

Hi,
well ive written some code wich iterates through all the fields in
my data structures wich I need to format to/from a file
(wich has a complicated format)
and I can identify some simple structures that are packed and sequenctial
that I can
just binary copy the whole structure,

however I cant figure out how to get round the problems imposed in c#:-

I pass the structure to the function as an object ref,
but i cant then take the address of an object as it complains,
so I cant just simply copy it with pointers in an unsafe+fixed code section.

but when I use the marsheling services such as
Masrhal.PtrToStructure(fileSourceIntPtr,destObj)
it complains that it cant do this on destObj as it is a value type.
it wont let me pass it as a ref either.

it all seems to work ok for clases but that is too much of a limitation.

so is there any way to get the binary data from a file
copied into a packed struct when you only have a object ref to it ?

ive been looking for ages at the marshelling stuff and ive started to get
crossed eyes lol

thanks
Colin =^.^=
 
but when I use the marsheling services such as
Masrhal.PtrToStructure(fileSourceIntPtr,destObj)
it complains that it cant do this on destObj as it is a value type.
it wont let me pass it as a ref either.

Use the other PtrToStructure overload

destObj = Marshal.PtrToStructure(fileSourceIntPtr,
typeof(YourStruct));


Mattias
 
Mattias Sjögren said:
Use the other PtrToStructure overload

destObj = Marshal.PtrToStructure(fileSourceIntPtr,
typeof(YourStruct));

wow cool thanks, i missed that one somehow.
does it make a newly alocated block of memory then copy it to the structure?
or is it cool enough to just use the destination ?

itl prob be probably ok either way.

Colin =^.^=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top