Jump to content
  • Advertisement
Sign in to follow this  
maxdub

[.net] sizeof a managed struct/class

This topic is 3998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
nothing special, at least that I know of.

public ref struct ManagedEventArgs
{
int ID;
int value;
};

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!