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 / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

Marshalling structs with fixed length arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vince Gatto - 12 Apr 2004 00:46 GMT
I have a C++ struct like this:

struct TESTSTRUCT
{
   DWORD dwTest;
   WCHAR wszTest[ 256 ];
};

used in an interface like this:

virtual HRESULT Test(TESTSTRUCT *test) = 0;

The C# COM server interface is defined as:

void Test(ref TESTSTRUCT test);

and the struct is defined as:

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct TESTSTRUCT
{
   public UInt32 dwTest;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public String
sTest;
}

According to Microsoft, this is the correct way to handle fixed length
arrays in structures, but it does not seem to work.  I'm trying to use
this as an in/out parameter, and values are making it into the C#
server just fine, but any modifications that are made there are not
reflected in the C++ client.  If I remove the array from the struct,
everything works fine, so assume my interface definition is ok, it's
just my structure that is somehow wrong.  When I look at the type
library for the C# assembly, the struct definition looks identical to
the C++ version, yet it still will not marshal correctly.

Any ideas?
Idael Cardoso - 12 Apr 2004 10:50 GMT
Apply In and Out attributes to your C# definition:

void Test([In, Out] ref TESTSTRUCT test);

It must work, the other think you can do is define it as:

void Test(IntPtr test);

and use Marshal.PtrToStructure and Marshal.StructureToPrt. I agree that's is
not a prety way but it can be used as a last resource.
Regards,
Idael

> I have a C++ struct like this:
>
[quoted text clipped - 33 lines]
>
> Any ideas?
Vince Gatto - 13 Apr 2004 06:51 GMT
Yep, that's it.  I need to put "Out" in the method signature.  I
figured it was something simple.  Thanks.

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.