DLL Marshalling : How do I treat a char FAR * HUGE * FAR * in C# ?

G

Guest

I am calling a C/C++ DLL from C# and I am marshalling the parameters to the
API call by doing a type conversion for each parameter.

For example, here is my C++ API method :

short int XENO_API XcDatabaseCodes
(
HWND hwnd,
char FAR * pszDatabase,
char FAR * HUGE * FAR * pszItemCategories
)

The char FAR * HUGE * FAR * parameter is the address to return huge array of
null terminated strings i.e. a list

Here is my C# marshalling definition for this API :

[DllImport("Xenoc32.dll")]
public static extern int XcDatabaseCodes(
string hWindowHandle ,
string pszDatabase,
ref string [] pszItemCategories );

and here is my API call from C#

const string pszDatabase = "\\\\NETWORK\\DATABASE";
string [] pszItemCategories = {};
int shReturnCode = XcDatabaseCodes(
null,
pszDatabase,
ref pszItemCategories );

The call returns ok but the trouble is I only get one item back.

How should I declare and pass pszItemCategories to get the list.
 
N

Nicholas Paldino [.NET/C# MVP]

Reynardine,

The P/Invoke layer doesn't know how to marsal arrays back to the managed
world. The reason for this is that it doesn't know what the size of the
array is (after all, it's only a pointer).

Because of this, you will have to marshal it as an IntPtr (or a ref
IntPtr) and then dereference the data yourself (you can use the methods on
the Marshal class to do this).

Also, are you supplying the memory where the strings are written to? If
not, then you need to make sure you dispose of the memory allocated by the
function correctly (whomever wrote the API should have provided a function
to do so).

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top