cadena Al pasar de C# para C++ debería ser sencillo. PInvoke administrará la conversión por usted.
Obteniendo una cadena de C++ a C# se puede hacer usando un StringBuilder. Necesita obtener la longitud de la cadena para crear un búfer del tamaño correcto.
Éstos son dos ejemplos de una API de Win32 conocido:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string GetText(IntPtr hWnd)
{
// Allocate correct string length first
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
SetWindowText(Process.GetCurrentProcess().MainWindowHandle, "Amazing!");
Su uso de 'const' es al revés. –
@Ben Voigt: gracias, lo arreglaron. – sithereal
Estos ejemplos explotaron con excepciones de pila en VisStudio 2012 hasta que agregué cdecl tanto al C# como al C .... extern "C" __declspec (dllexport) int __cdecl SetString (... y luego ... [DllImport (" YourLib.dlll ", CallingConvention = CallingConvention.Cdecl)] ... – user922020