[.net] sizeof a managed struct/class
Alright for some I can't do the following in C#.
ManagedEventArgs d = new ManagedEventArgs();
d.ID = ID;
d.value = 3;
IntPtr pManPtr = Marshal.AllocHGlobal(sizeof(d));
Marshal.StructureToPtr(d, pManPtr, true);
I bolded the problem code.
ManagedEventArgs is a struct define in Managed C++ and included into my c# project. I have no problem creating an instance of it, but as soon as I try to get the size of it I get:
"
error CS0246: The type or namespace name 'd' could not be found (are you missing a using directive or an assembly reference?)
"
any idea why??
[EDIT]
also, i can use Marshal.Sizeof(); but I get a runtime exception.
"Type 'Controller.ManagedEventArgs' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
What does ManagedEventArgs look like? This isn't C++, you can't just compute the size of any old object. Chances are, ManagedEventArgs is defined in such a way that it cannot be automatically marshalled like that.
nothing special, at least that I know of.
public ref struct ManagedEventArgs
{
int ID;
int value;
};
public ref struct ManagedEventArgs
{
int ID;
int value;
};
sizeof can only be used on value types not reference types. You made yours; public ref struct ManagedEventArgs, into a reference type.
At least I think that is it, can't test it right now.
theTroll
At least I think that is it, can't test it right now.
theTroll
alright, great, that worked, theTroll. thanks. though now it throws an exception when i try to convert the pointer back to a structure using Marshal.PtrToStructure(); saying that it requires the structure to "not be a value class" is there something similar to & that i could use to pass the reference?
heres the code
ManagedEventArgs args = new ManagedEventArgs();
args.ID = 0;
args.value = 0;
Marshal.PtrToStructure(data, args);
"
The structure must not be a value class.
"
I'm going to go ahead and try the StructLayout thing, but I wanted to post this ASAP.
thanks guys!!
[Edited by - maxdub on November 4, 2007 6:09:34 PM]
heres the code
ManagedEventArgs args = new ManagedEventArgs();
args.ID = 0;
args.value = 0;
Marshal.PtrToStructure(data, args);
"
The structure must not be a value class.
"
I'm going to go ahead and try the StructLayout thing, but I wanted to post this ASAP.
thanks guys!!
[Edited by - maxdub on November 4, 2007 6:09:34 PM]
Hiya,
You want to decorate your struct with the following attribute:
[StructLayout(LayoutKind.Sequential, Pack = 1)] (well, the equivalent in C++/CLI syntax)
I've you're converting between data/bytes as generated by an unmanaged app, you'll want to use Marshal.SizeOf(): this gives different results to sizeof() for some primitive types (I think bool in unmanaged was 1 byte, but 4 in .NET, but don't quote me on that).
On your failing methods, e.g. StructureToPtr, look for overloads where you can pass a type in, i.e. typeof(ManagedEventArgs).
Let us know how you get on!
Cheers,
gary
You want to decorate your struct with the following attribute:
[StructLayout(LayoutKind.Sequential, Pack = 1)] (well, the equivalent in C++/CLI syntax)
I've you're converting between data/bytes as generated by an unmanaged app, you'll want to use Marshal.SizeOf(): this gives different results to sizeof() for some primitive types (I think bool in unmanaged was 1 byte, but 4 in .NET, but don't quote me on that).
On your failing methods, e.g. StructureToPtr, look for overloads where you can pass a type in, i.e. typeof(ManagedEventArgs).
Let us know how you get on!
Cheers,
gary
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement