Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Mattias Sjögren
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...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
bg
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...
 
 
 
System.Collections.ArrayList
System.Console
System.IntPtr
System.Runtime.InteropServices.Marshal
System.Text.StringBuilder




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734