char array to LPCTSTR conversion in c

前端 未结 8 796

Does anyone know how to convert a char array to a LPCTSTR in c?

Edit:

For more reference, I need to add an integer to a string then convert that string to LP

相关标签:
8条回答
  • 2021-01-02 03:28

    "Does anyone know how to convert a char array to a LPCSTR in c?"

    You don't have to do anything at all. It automatically converts to that type (except in initializers and sizeof).

    "CreateFile(portName..."

    Perhaps you should tell us the error message that VC++ gives you at compile time?

    Perhaps you should also tell us what error message VC++ gave you when Adam Rosenfield's whcar_t version didn't work for you?

    0 讨论(0)
  • 2021-01-02 03:30

    You have string functions for TCHAR. You can use, for example, stprintf_s accepting TCHAR. This way, you make the code "independent" of unicode or multi-byte character set.

    Your code (variant 1) becomes:

    TCHAR portName[12] = { 0 };
    
    stprintf_s( portName, sizeof( portName ) / sizeof(TCHAR), _T("\\\\.\\COM%i"), portNum );
    
    CreateFile(portName...
    
    0 讨论(0)
  • 2021-01-02 03:32

    You can implicitly convert a char array to an LPCSTR without any casts:

    void SomeFunction(LPCSTR aString);
    ...
    char myArray[] = "hello, world!";
    SomeFunction(myArray);
    

    An LPCSTR is a Windows typedef for a long pointer to a constant string. Back in the dark days of Win16 programming, there were different types of pointers: near pointers and far pointers, sometimes also known as short and long pointers respectively. Near pointers could only point to a 64KB segment of memory determined by one of the x86 segment registers. Far pointers could point to anything. Nowadays in Win32 with virtual memory, there is no need for near pointers -- all pointers are long.

    So, an LPSTR is a typedef for a char *, or pointer to a string. An LPCSTR is the const version, i.e. it is a typedef for a const char *. In C, arrays decay into pointers to their first elements, so a char[] decays into a char*. Finally, any type of "pointer to T" (for any type T) can be implicitly converted into a "pointer to const T". Thus, combining these three facts, we see that we can implicitly convert a char[] into an LPCSTR.


    In response to your edit, I'm going to guess that you're compiling a Unicode application. If you look carefully at the documentation for CreateFile(), you'll notice that the filename parameter is actually an LPCTSTR, not an LPCSTR (note the T).

    For pretty much every Win32 function that takes an argument of some string type (perhaps indirectly, i.e. as a member of a structure passed as a parameter), there are actually two versions of that function: one which takes 8-bit ANSI strings, and one which takes 16-bit wide-character strings. To get the actual function names, you append an A or a W to the function name. So, the ANSI version of CreateFile() is named CreateFileA(), and the wide-character version is named CreateFileW(). Depending on whether or not you're compiling with Unicode enabled (i.e. whether the preprocessor symbol _UNICODE is defined), the symbol CreateFile is #defined to either CreateFileA or CreateFileW as appropriate, and likewise for every other function that has an ANSI and a wide-character version.

    Along the same lines, the type TCHAR is typedefed to either char or wchar_t, depending on whether Unicode is enabled, and LPCTSTR is typedefed to a pointer to a const TCHAR.

    Thus, to make your code correct, you should replace the strings you're using with TCHAR strings, and use the type-generic version of sprintf_s, _stprintf_s:

    TCHAR portName[32];
    _stprintf_s(portName, sizeof(portName)/sizeof(TCHAR), _T("\\\\.\\COM%d"), portNum);
    CreateFile(portName, ...);
    

    Alternatively, you can explicitly use the ANSI or wide-character versions of everything:

    // Use ANSI
    char portName[32];
    sprintf_s(portName, sizeof(portName), "\\\\.\\COM%d", portNum);
    CreateFileA(portName, ...);
    
    // Use wide-characters
    wchar_t portName[32];
    swprintf_s(portName, sizeof(portName)/sizeof(wchar_t), L"\\\\.\\COM%d", portNum);
    CreateFileW(portName, ...);
    
    0 讨论(0)
  • 2021-01-02 03:39
    char* filename;
    LPCTSTR ime = CA2W(filename);
    

    This is String Conversion Macro and works on my VS12

    0 讨论(0)
  • 2021-01-02 03:42

    In what format is your char array?

    Is it a const char[] or a non-const?

    LPCSTR is just the (somewhat) confusing Microsoft name for "Long Pointer to Constant String".

    LPCSTR bar = "hello";
    const char *foo = bar;
    
    const char *evil = "hello";
    LPCSTR sauron = evil;
    

    If you need to get a non-const version, you either cast away the constness, or you copy to a new array. I would probably prefer the latter. Variables are often const for a reason, and changing them is almost always bad practice.

    0 讨论(0)
  • 2021-01-02 03:47

    try like this.........

    TCHAR *pcCommPort = TEXT("COM1");
    HANDLE h = CreateFile(pcCommPort,other arguments);
    
    0 讨论(0)
提交回复
热议问题