I'm actually learning C#/WPF and I wrote a library in C++. And I have a function that have an output parameter in LPSTR
type, the test work well in C++ and I'm actually getting the right value I wanted with the right size. But in C# after importing the dll and set the format to unicode/ansi, either I have a empty value or bad value.
C++ project build in myfunction.dll win32.
C++ export
/*
* getting the right value in outString
* c++ test
*
*/
API_DLLEXPORT long GetSomeString (LPSTR outString ,long * outNbChar);
for (long iChar = 0; iChar < *outNbofchar; ++iChar)
{
outsLibClass[iChar] = (char)finder.GetFileName()[iChar];
}
outsLibClass[*outNbofchar] = '\0';
break;
C# import
[DllImport("myfunction.dll", CharSet = CharSet.Unicode)]
public static extern Int32 GetSomeString (StringBuilder outString, ref Int32 outNbChar);
StringBuilder out = new StringBuilder(256) ;
Int32 nbchar = new Int32() ;
int result = GetSomeString (out, ref nbchar ) ;
Console.WriteLine(out.ToString());
// I get the right nbchar
But I'm not getting the right outstring result:
㠴0휐ࠦ휠ࠦ鏺瓑휬ࠦ찞瑻쳁촀
And of course I try also to set the CharSet
to Ansi
but the result was empty.
So if anyone could help me that would be a great pleasure.
Thx to @john link in the comments, i resolve it : i was making a copy and not actualy referencing the address of outString.
and in c#