Knowledge Base Nr: 00293 dllarray.cs - http://www.swe-kaiser.de

c#: .net: win32-dll aufruf mit rückgabe von string array

  
##### c:

char* parser(int* count, const char* file)
{
char* pArray[5];

pArray[0] = dupstr("lulli");
pArray[1] = dupstr("fritz");
pArray[2] = dupstr("hugo");
pArray[3] = dupstr("jupp");
pArray[4] = dupstr("zupp");

count = 5;

return pArray;
}

##### c#

[DllImport("parser.dll")]
private static extern int parser(ref int count, string file);

private void button1_Click(object sender, System.EventArgs e)
{
int count = 0;
string file = "eepromContent.txt";
int array = 0;

string outs = "calling parser:\r\n";

try
{
array = parser(ref count, file);

outs += "count = " + count + "\r\n";

IntPtr pa = (IntPtr)array;

for (int i=0; i<count; i++)
{
IntPtr ptr = Marshal.ReadIntPtr(pa, i*IntPtr.Size);

string line = Marshal.PtrToStringAnsi(ptr);
Console.WriteLine(line);
outs += "line: " + i + ": <" + line + ">\r\n";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
outs += ex.Message;
}

textBox1.Text = outs;
}