Multimobile Development: Building Applications for any Smartphone
how can I use interop to get an array of string back from a dll ?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.


bg
GOOD ANSWER
hi all,

I have a function in a DLL exported like this "DllExport char *DSGetthingList(void)" - the return value (the char*) is an array of strings seperated by nulls with two nulls at the end. how can I get this back intact ?

I have tried StringBuilder and String and String[] but I only ever get the last string in the array back.

i.e
[DllImport("C:/build/server/asd/asd.dll")]
internal static extern StringBuilder DSGetProjectList();

[DllImport("C:/build/server/asd/asd.dll")]
internal static extern String DSGetProjectList();

[DllImport("C:/build/server/asd/asd.dll")]
internal static extern String[] DSGetProjectList();

TIA

bg

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Mattias Sjögren
GOOD ANSWER
bg,

Try it like this:

internal static extern IntPtr DSGetProjectList();

---

IntPtr p1, p2;
string s = null;

p1 = p2 = DSGetProjectList();
do {
s = Marshal.PtrToStringAnsi( p2 );
Console.WriteLine( s );
p2 = unchecked((IntPtr)((int)p2 + s.Length + 1));
} while ( s.Length > 0 );
// Free p1 here if needed.

Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
bg
GOOD ANSWER
thanks that got the data over then I wrote this to strip the strings out (assuming ansi)

private ArrayList GetStringArrayFromStupidApi(IntPtr sapi)
{
ArrayList arr = new ArrayList();
int idx = 0;
bool endFound = false;
StringBuilder curr = new StringBuilder();
do
{
byte byt = Marshal.ReadByte(sapi,idx++);
char c = (char)byt;
if(byt!='\0')
{
curr.Append((char)byt);
}
else
{
arr.Add(curr.ToString());
curr = new StringBuilder();
byte end = Marshal.ReadByte(sapi,idx);
if(end=='\0') endFound = true;
}

} while (!endFound);
return arr;
}

"Mattias Sjögren" <Click here to reveal e-mail address> wrote in message news:uzEK2PGxBHA.2324@tkmsftngp03...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Collections.ArrayList
System.Console
System.IntPtr
System.Runtime.InteropServices.Marshal
System.Text.StringBuilder




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734