Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to define struct with array in C#?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Polaris - 13 Mar 2008 06:11 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.