Hi Experts:
In my C# program I need to use a Win32 DLL which leads to a question: how to
define Win32/C++ struct with array in C#. For example, I have a C++ struct:
struct MY_STRUCT
{
int nCities[128];
};
How to define it in C#?
Thanks in advance!
Polaris
Jon Skeet [C# MVP] - 13 Mar 2008 08:50 GMT
> In my C# program I need to use a Win32 DLL which leads to a question: how to
> define Win32/C++ struct with array in C#. For example, I have a C++ struct:
[quoted text clipped - 5 lines]
>
> How to define it in C#?
The problem isn't declaring an array - it's declaring an array with a
fixed size. In C# 2 you can declare it in unsafe code with a fixed-size
buffer:
struct MY_STRUCT
{
fixed int nCitires[128];
}
However, in my experience it's not worth it - look at marshalling
options instead to get the marshaller to copy stuff for you. I don't
know a lot about interop like this I'm afraid, but the interop
newsgroup might be able to help you.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Willy Denoyette [MVP] - 13 Mar 2008 13:22 GMT
> Hi Experts:
>
[quoted text clipped - 11 lines]
> Thanks in advance!
> Polaris
internal struct MY_STRUCT
{
internal const int nrOfCities= 128;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = nrOfCities)]
internal int nCities[];
};
For more info search MSDN for "Marshaling Data with Platform Invoke"
Willy.