L prefix for strings in C++

前端 未结 5 1599
遇见更好的自我
遇见更好的自我 2021-01-05 10:45

I have a static library. This library have the following function defined

int WriteData(LPTSTR s)

The sample to call the function is

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 11:16

    I think you're confused, as your function should work just fine:

    int TestFun(LPTSTR lpData)
    {
       return  WriteData(lpData); // Should be happy
    }
    

    But when you call your function, you'll have to be careful:

    TestFun((LPTSTR) L"ABC"); // Will work fine
    TestFun((LPTSTR) "ABC");  // Will not work
    

    This is because "ABC" and L"ABC" are two different things. If you look at them in memory:

    "ABC"  | 65 66 67 00
    L"ABC" | 65 00 66 00 67 00 00 00
    

    Edited to add:

    There is nothing like L prefix in .Net

    This is just wrong. I just opened "New Project->C++->CLR Console" in VisualStudio, and the first line is:

    Console::WriteLine(L"Hello World");
    

提交回复
热议问题