List Info

Thread: C-Sharp (C#) Group: Unmanaged Code and Structures




C-Sharp (C#) Group: Unmanaged Code and Structures
user name
2006-05-03 07:25:55
When creating a C# representation of a native C structure,
do you think
its a good idea to embed constants inside that structure?
Like for some
Windows API functions you need to pass structures as
arguments, and for
some ODD reason those structures have members that denote
the size of
the structure itself (for instance WNDCLASSEX). So my real
question is
that when you re-create those structures in C# can you just
add a
constant member to it that already holds the size of
structure?

Example:

public struct WNDCLASSEX {
    ...
    public const int Size =
Marshal.SizeOf(WNDCLASSEX.GetType());
}

P.S> I'm sorry I'm really bad at wording my questions,
but if someone
could help me out with this, I'd be REALLY thankful.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Unmanaged Code and Structures
user name
2006-05-03 09:09:30
Embedding constants inside structs is not a great idea as the constant will be re-initilised for each instance (since structs are handled as value types not reference types) but realistically if you are just dealing with a few primitives the performance issues are going to be non existing (unless you are talking about many thousands of instances of the struct floating about).

As for the sizeof you will be OK as long as you don't have any reference type members at all defined in the struct. Just one point though, the size is not a constant it is variable based on what the struct contains so you might want something like:

    ;   public struct WNDCLASSEX
        {
            public int Size
            {
                get
 ;               {
                    return Marshal.SizeOf(this);
                }
            }
        }

instead.

Liam

On 5/3/06, R. Aiken <gmail.com">rodger.aikengmail.com> wrote:

When creating a C# representation of a native C structure, do you think
its a good idea to embed constants inside that structure? Like for some
Windows API functions you need to pass structures as arguments, and for
some ODD reason those structures have members that denote the size of
the structure itself (for instance WNDCLASSEX). So my real question is
that when you re-create those structures in C# can you just add a
constant member to it that already holds the size of structure?

Example:

public struct WNDCLASSEX {
 &nbsp; &nbsp;...
&nbsp; &nbsp; public const int Size = Marshal.SizeOf(WNDCLASSEX.GetType());
}

P.S> I'm sorry I'm really bad at wording my questions, but if someone
could help me out with this, I'd be REALLY thankful.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )