Hi everyone,
I have an issue with this conversion problem.
From a C# component I need to call a C++ Dll method:
C++
extern "C"
{
__declspec(dllexport) bool getVal(LPTSTR& ab);
}
In c# I define a string 'cd.
C#
[DllImport("...dll")]
public static extern bool getVal(???);
private void Testbutton_Click(object sender, EventArgs e)
{
string cd????
getVal(??cd???)
);
Question:
How do I call, from C#, the method so that the string defined in the C++ LPTSTR variable is copied into the C# string variable?
I don't know how big the returned string can be.
** UPDATE**
This is the C++ method:
C++
extern "C"
{
__declspec(dllexport) bool getVal(LPTSTR& retVal);
}
this is how retVal is set:
extern bool getVal(LPTSTR& retVal)
{
.
.
.
String^ val = sr->ReadLine();
retVal = (LPTSTR)(Marshal::StringToHGlobalAnsi(val)).ToPointer();
.
.
.
return true;
}
I need help calling this method from C#.
Currently I have:
C#
[DllImport("myLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool getVal([MarshalAsAttribute(UnmanagedType.LPTStr)] out String retVal);
private void Testbutton_Click(object sender, EventArgs e)
{
string myVal;
getVal(out myVal)
);
what I get in myVal is garbage, like chinese chars (ANSI/Unicode issue??), not the value I can print out and read if, debugging the dll, I access the val C++ variable.
Maybe the conversion from String^ to LPTSTR is not correct?