Groups | Blog | Home
all groups > dotnet interop > september 2004 >

dotnet interop : Marshal char str[32], DllImport


forloop
9/6/2004 9:05:07 AM
The C DLL has the following declaration:

ExternC int4 STDCALL func( char * ); // -> array of 32 byte strings

I can call it from C++ code using [this works!]:

char str[32] = "My String";
int4 result = func( str );

Now, I need to do this in C#. I tried something like this, but it won't
compile ("Specified unmanaged type is only valid on fields."):

[ DllImport( "capi.dll", EntryPoint = "func" ) ]
public static extern int CSharpFunc( [MarshalAs (UnmanagedType.ByValTStr,
SizeConst=32)] string s );

I realize that this isn't the way it should be done, but those of you with
experience of marshalling different types of string representations should
Mattias Sjögren
9/6/2004 11:48:31 PM

This should work, you don't need the MarshalAs attribute here

[ DllImport( "capi.dll", EntryPoint = "func" ) ]
public static extern int CSharpFunc(string s);

Just make sure the string you pass in is long enough if it's actually
required to be 32 chars.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
forloop
9/7/2004 2:03:02 AM
Thanks Mattias!

I'm not getting this to work, but I suspect it might be another issue
altogether... Good to know this is not the cause of it.


[quoted text, click to view]
AddThis Social Bookmark Button