How to DllImport function with "char**" parameter

  • Thread starter Thread starter Dennis
  • Start date Start date
D

Dennis

Excuse me!!

I have a DLL file named "TESTDLL.DLL".
In TESTDLL.DLL
export function :
void GetPChar(char** pstr);

In .NET C# ,
How to call "GetPChar(char** pstr)" function ?

Thanks!!
 
Dennis,
Excuse me!!

I have a DLL file named "TESTDLL.DLL".
In TESTDLL.DLL
export function :
void GetPChar(char** pstr);

In .NET C# ,
How to call "GetPChar(char** pstr)" function ?

What does the function do? If it returns a single string pointer, try
it like this

[DllImport("testdll.dll")]
static extern void GetPChar(out IntPtr pstr);

---
IntPtr pstr;
GetPChar(out pstr);
string s = Marshal.PtrToStringAnsi(pstr);

Then you might have to free the returned buffer somehow, depending on
how its allocated by the DLL.



Mattias
 
Dear Mattias Sjögren :

Thanks for your answer.

But "char** pstr" is a string array.
Such as
pstr[0] ---> string a
pstr[1] ---> string b
pstr[2] ---> string c

In C# ,
How to declare and call it ?

Thanks a lot!


Mattias Sjögren said:
Dennis,
Excuse me!!

I have a DLL file named "TESTDLL.DLL".
In TESTDLL.DLL
export function :
void GetPChar(char** pstr);

In .NET C# ,
How to call "GetPChar(char** pstr)" function ?

What does the function do? If it returns a single string pointer, try
it like this

[DllImport("testdll.dll")]
static extern void GetPChar(out IntPtr pstr);

---
IntPtr pstr;
GetPChar(out pstr);
string s = Marshal.PtrToStringAnsi(pstr);

Then you might have to free the returned buffer somehow, depending on
how its allocated by the DLL.



Mattias
 
Dennis said:
But "char** pstr" is a string array.
Such as
pstr[0] ---> string a
pstr[1] ---> string b
pstr[2] ---> string c
In C# ,
How to declare and call it ?

Here's how I would do it [warning: haven't tested the code below]

[DllImport("testdll.dll")]
static extern void GetPChar(out IntPtr pstr);

string[] ret = new string[count];
IntPtr str;
GetPChar(str);
for(int i = 0; i < count; i++) {
ret = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(str, i *
IntPtr.Size));
}

The above code asumes you know the number of items in the returned array
['count'] in advance.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
 
Back
Top