[2.0] getting the first byte of a structure? HELP!
Hi all!
I'm using COM.... I have a function from a COM object that takes a byte pointer, pointing to the first byte in a structure. My question is, how can i get a byte pointer that would point to the first byte in my structure? :confused:
any help is mucho appreciated!:afrog:
Re: [2.0] getting the first byte of a structure? HELP!
You could either use unsafe code if you really must have a genuine pointer, or else the Marshal.StructureToPtr method should do the trick.
Re: [2.0] getting the first byte of a structure? HELP!
Quote:
Originally Posted by jmcilhinney
You could either use unsafe code if you really must have a genuine pointer, or else the Marshal.StructureToPtr method should do the trick.
Hi! thanks man! :)
I looked at that marshal function,
i kinda lied actually, i needed a "ref byte" not a byte pointer, so I passed in the first field in the structure and it works fine....
curious though, in an unsafe context how would you convert the structure to a byte* ?
Re: [2.0] getting the first byte of a structure? HELP!
I've actually never written any unsafe code in C# but I would expect it would be something like:That would do the trick in C++, although C# may have an issue with converting the address of one type to a pointer of another type. You may have to cast like this:
CSharp Code:
byte* p = (byte*)&myStructure;
Also, I'm not 100% sure that the addressof operator (&) from C/C++ has carried over, but I'm guessing it has.
Edit: Just checked and that second code snippet does the trick.
Re: [2.0] getting the first byte of a structure? HELP!
Quote:
Originally Posted by jmcilhinney
I've actually never written any unsafe code in C# but I would expect it would be something like:
That would do the trick in C++, although C# may have an issue with converting the address of one type to a pointer of another type. You may have to cast like this:
CSharp Code:
byte* p = (byte*)&myStructure;
Also, I'm not 100% sure that the addressof operator (&) from C/C++ has carried over, but I'm guessing it has.
Edit: Just checked and that second code snippet does the trick.
and why did i never think of that? :D
thanks