Wednesday, 3 June 2009

Passing parameters of char* data types from C# or VB code to a C++ function

Some times you may need to use some C++ functions in your managed C# or VB code.
According to Frank Boyne, there are three ways to do this, if you choose the third method (which is easier and better, I think), you may have some problems passing variables of char* (or other pointer types) from your C# or VB code to your C++ code (in this case the wrapper class).
Suppose that you have something like this in your C++ wrapper class:

static char* foo(char *name)

the problem is this: How can I pass the value of the parameter name and how can I use the value returned by the function.

To pass the value of parameter name, you can simply use:


in your C# code.

And to get the returning value of the function, you can use this code:

string s =
new string(