Have you tried
UnmanagedType.ByValTStr
It is supposed to be used for struct fields.
--------------------
[quoted text, click to view] >Content-Class: urn:content-classes:message
>From: "Christoph Schittko [MVP XML]" <christophdotnetINVALID@austin.rr.com>
>Sender: "Christoph Schittko [MVP XML]"
<christophdotnetINVALID@austin.rr.com>
>Subject: Struct with array of strings
>Date: Mon, 28 Jul 2003 14:36:32 -0700
>Lines: 67
>Message-ID: <058801c35550$501194e0$a101280a@phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcNVUFAPuSO9go40T3KnwLEA5ZHhNw==
>Newsgroups: microsoft.public.dotnet.framework.interop
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:17551
>NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
>X-Tomcat-NG: microsoft.public.dotnet.framework.interop
>
>All,
>
>I am trying to write a P/invoke wrapper for a method that
>takes a pointer to a struct that has an array of char*
>strings.
>
>It goes something like this:
>
>Unmanaged Method:
>
>int unmanaged_foo(
> Struct1 * s1,
> const char* class_name,
> StringSet * instances
>)
>{
> // ...
>}
>
>The managed definition is:
>[DllImport( LIB_PATH, CharSet=CharSet.Ansi )]
>public static extern int
> unmanaged_foo(
> ref Struct1 s1,
> String class_name,
> ref StringSet instances );
>
>
>where the unmanaged definition of StringSet is:
>struct StringSet
>{
> int size;
> char ** elements;
>}
>
>Now I thought I should come up with the correct
>parameters for the MarshalAs attribute on the elments
>field, but so far that's been pretty much wishful
>thinking. I've tried passing various UnmanagedTypes
>values, LPArray, ByValArray and none whatsoever, but as
>soon as I attach MarshalAs I get an exception:
>
>Cannot marshal field elements of type foo: This type
>cannot be marshaled as a structure field.
>
>Now I found some postings on google that seem to suggest
>that it is very much possible to do this. In fact, the
>FrameworkSDK docs show an example as well.
>
>If I don't attach a MarshalAs then my method call
>actually makes it into the unmanaged method without
>causing an exception. The int field of the StringSet
>struct has the correct value, but the elements pointer
>that's supposed to point to the first string in the array
>points to laa-laa land.
>
>I know that I will run into more issues marshalling the
>string array back from the unamanaged method, but first I
>would like to be able to call that method without getting
>an exception AND the string array making it in there.
>
>Can anybody point out to me what I am missing?
>
>Any help would be greatly appreciated.
>TIA,
>Christoph Schittko
>[MS MVP XML .NET]
>