passing a string as a const char * to an external c++ dll

  • Thread starter Thread starter Max Adams
  • Start date Start date
M

Max Adams

All,
I have a third-party DLL which takes a const char * as a paramenter. What
datatype do I need to use to pass a value to this successfully?

MA
 
Short answer: you need to use managed data types (such as String).

Some details:
You need to use System.Runtime.InteropServices namespace.
You need to use the DllImport attribute
You can use either a string or a StringBuilder type, as far as I know.

Most examples show a String type used for a char*, but here's an example
from some of my code where I use StringBuilder:

[DllImport("c_language.dll", EntryPoint="my_C_function")]
public unsafe static extern RetvalFlag myImportedCFunction(double x, int
y, CalcFlag iflag, double* ptrD, StringBuilder sberr);

private StringBuilder sberr = new StringBuilder("empty string", 2*256);

You'll find a lot more info on this topic if you look under pinvoke or
P/Invoke or Platform Invoke.

Hope that helps!
 
Back
Top