0

I have this method on a C++ project

long CPPProject::CPPMethod(long lValue, LPCTSTR Data){
    do something...
}

But, when I consume the method via a C# project, sometimes the value of Data is '?' For example:

C# code:

String sText1 = "u0110 \u0110"; //Đ
object.CPPMethod(lValue, sText1);

In this case, the 'Data' variable received the value correctly (Đ).

But on the other hand, if the code is:

C# code:

String sText2 = "u8162 \u8162";//腢 
object.CPPMethod(lValue, sText2);

The value of 'Data' is '?'

I write the value of Data on a log file.

Why is this behavior? What do I need to fix?

Ferrus
  • 15
  • 6
  • 5
    What does the *declaration* of `object.CPPMethod()` look like on the C# side? And why are you using `LPCTSTR` on the C++ side? You should be using `LPCWSTR` instead. `LPCTSTR` is based on `TCHAR` (`char` or `wchar_t` based on compiler configuration). Also, `CPPProject::CPPMethod()` should probably be using the `__stdcall` calling convention on the C++ side, depending on the declaration on the C# side. Plase provide a [mcve]. – Remy Lebeau Jun 16 '22 at 19:39
  • Hmm, surely Data resembles "u8162 ?". An encoding problem that could exist anywhere, could be the viewer you use to look at the log file. But CharSet.Ansi on the pinvoke declaration would be the first place it goes wrong, Đ is character code 0xD0 in code page 1252 so could survive. Use LPWSTR and CharSet.Unicode. – Hans Passant Jun 16 '22 at 20:16

1 Answers1

0

I replaced from LPCTSTR to LPCWSTR

Basically is because; LPCWSTR is a const WCHAR *, and LPCTSTR const of TCHAR *, in addition in the DISP_FUNCTION, was a necessary change VTS_BSTR to VTS_WBSTR.

After making this, the value was received without bugs

Ferrus
  • 15
  • 6
  • Why? What's the difference? – Peter Bruins Jan 27 '23 at 13:12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 14:17