I have this C++ code :

const int ANYSIZE_ARRAY = 1;
    struct LUID {DWORD LowPart; WORD HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; DWORD Attributes;};
    struct TOKEN_PRIVILEGES {DWORD PrivilegeCount;   LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];};

I want to convert this code into C# syntax.

I tried this one, but don't know how to place array in a struct.

const int ANYSIZE_ARRAY = 1;
    struct LUID {Int32 LowPart; Int16 HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; Int32 Attributes;};
    struct TOKEN_PRIVILEGES {Int32 PriviligeCount; /*WHAT TO WRITE HERE??*/};

Help me please.

Dani AI

Generated

Short answer: you cannot declare a C-style variable-length inline array in a safe C# struct the same way you do in C/C++. The typical pattern for Windows structs like TOKEN_PRIVILEGES is to declare the native header in C# with a one-element inline array (so the managed layout matches the native header) and then allocate a contiguous unmanaged block large enough for N entries and marshal the elements yourself before calling the native API. This preserves layout for P/Invoke while letting you pass any number of privileges. (learn.microsoft.com)

Why drjr01's "fixed" hint only goes so far: C# fixed-size buffers work, but only for primitive element types (int, byte, float, char, etc.) and require unsafe code and /unsafe compilation. That makes them unsuitable for embedding a struct-array element type (like LUID_AND_ATTRIBUTES) directly. Use fixed only when the element type is one of the allowed primitives. (learn.microsoft.com)

A practical managed mapping (safe code) is to declare the structs with sequential layout and mark the trailing array with MarshalAs(ByValArray, SizeConst = 1). ANYSIZE_ARRAY is defined as 1 in the Windows headers, so SizeConst = 1 matches the header; you then compute the total buffer size = sizeof(header) + (count - 1) * sizeof(element), AllocHGlobal that many bytes, write the header at the start, then write each element in sequence (Marshal.StructureToPtr) before passing the pointer to the native call. Use Marshal.OffsetOf to find where the inline array starts. Free the unmanaged block when done. (learn.microsoft.com)

One important correctness note: the Windows LUID uses a 32-bit signed HighPart (LONG) and a 32-bit unsigned LowPart (DWORD). Make sure your C# LUID fields match the native types (use uint/int as appropriate) so sizes and sign match the native API. (learn.microsoft.com)

Troubleshooting checklist: declare [StructLayout(LayoutKind.Sequential)], verify field sizes with Marshal.SizeOf, compute total size using Marshal.SizeOf and (count-1) adjustment, pin or use unmanaged allocation as shown above, and always free the unmanaged memory. If you prefer not to hand-marshal, use a wrapper (pinvoke.net has tested signatures) or write a tiny native shim that accepts a managed-friendly layout. (pinvoke.net)

Code examples in replies can show the exact C# structs and the AllocHGlobal/StructureToPtr loop if needed.

Recommended Answers

All 3 Replies

Hi

Would it not be something like;

LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];


for the above i have assumed that, you want an array of LUID_AND_ATTRIBUTES type struct , which you have defined before, as comprising of LUID Luid; int32 Attributes.
If my assunption is wrong, then please accept my apologies.

HTH

Thanks

I have this C++ code :

const int ANYSIZE_ARRAY = 1;
    struct LUID {DWORD LowPart; WORD HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; DWORD Attributes;};
    struct TOKEN_PRIVILEGES {DWORD PrivilegeCount;   LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];};

I want to convert this code into C# syntax.

I tried this one, but don't know how to place array in a struct.

const int ANYSIZE_ARRAY = 1;
    struct LUID {Int32 LowPart; Int16 HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; Int32 Attributes;};
    struct TOKEN_PRIVILEGES {Int32 PriviligeCount; /*WHAT TO WRITE HERE??*/};

Help me please.

Would it not be something like;
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];

Given error :

Error    1    Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.    C:\Prog\SAVE\C#\Shutdown\Main.cs    22    86    Shutdown
Error    2    Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)    C:\Prog\SAVE\C#\Shutdown\Main.cs    22    87    Shutdown

for the above i have assumed that, you want an array of LUID_AND_ATTRIBUTES type struct , which you have defined before, as comprising of LUID Luid; int32 Attributes.

Exactly. What I try to do is place an array into a struct. I couldn't find an example not even in google.

This one doesn't work :

struct Animals { float Bird; int Rabbit[20]; Int64 Elephant; };

Error 1 Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. C:\Prog\SAVE\C#\Shutdown\ 35 42 Shutdown
Error 2 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Prog\SAVE\C#\Shutdown\ 35 43 Shutdown

This one too :

struct Animals { float Bird; int[20] Rabbit; Int64 Elephant; };

Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Prog\SAVE\C#\Shutdown\ 35 36 Shutdown

Another one :

struct Animals { float Bird; int Rabbit[]; Int64 Elephant; };

Error 1 Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. C:\Prog\SAVE\C#\Shutdown\ 35 42 Shutdown

This one works :

struct Animals { float Bird; int[] Rabbit; Int64 Elephant; };

But now I am not able to specify the size of the array:sad:


Any suggestions?

The answer is in the error message.

struct Animals { float Bird; int Rabbit[20]; Int64 Elephant; };

The previous code should be replaced with the following:

struct Animals { float Bird; fixed int Rabbit[20]; Int64 Elephant; };

Notice the addition of the keyword "fixed" which is place before the variable declaration.
See http://en.wikibooks.org/wiki/C_Sharp_Programming/Keywords/fixed for more details about the "fixed" keyword.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.