|
| 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
|
|
|
| |
|
| |
| |
| 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/
|
|
|
| |
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|