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 LPCTSTR for the first parameter of the windows function CreateFile().
This is the hardcoded example which I am currently using, but I need to be able to pass in any number to use as a port number.
CreateFile(_T("\\\\.\\COM11")... //hardcoded for com port 11
and here are several things I have tried, which I believe include the following suggestions for the next 2 answers of this post. They don't work unfortunately. If anyone could point out something I've done wrong and could possibly solve my problem, I'd appreciate it.
All of these examples assume that portNum is an int that is already assigned a valid value
1
char portName[12] = { 0 };
sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );
CreateFile(portName...
I've also tried #1 with a LPCSTR case for what it's worth…
2
LPCSTR SomeFunction(LPCSTR aString) {
return aString;
}
main() {
char portName[12] = { 0 };
sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );
LPCSTR lpPortName = SomeFunction(portName);
CreateFile(lpPortName...
3
const char * portName = "";
sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );
LPCSTR lpPortName = portName;
CreateFile(lpPortName...
Best Answer
You can implicitly convert a char array to an
LPCSTR
without any casts: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 achar *
, or pointer to a string. AnLPCSTR
is theconst
version, i.e. it is a typedef for aconst char *
. In C, arrays decay into pointers to their first elements, so achar[]
decays into achar*
. 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 achar[]
into anLPCSTR
.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 anLPCTSTR
, not anLPCSTR
(note theT
).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 aW
to the function name. So, the ANSI version ofCreateFile()
is namedCreateFileA()
, and the wide-character version is namedCreateFileW()
. Depending on whether or not you're compiling with Unicode enabled (i.e. whether the preprocessor symbol_UNICODE
is defined), the symbolCreateFile
is#define
d to eitherCreateFileA
orCreateFileW
as appropriate, and likewise for every other function that has an ANSI and a wide-character version.Along the same lines, the type
TCHAR
istypedef
ed to eitherchar
orwchar_t
, depending on whether Unicode is enabled, andLPCTSTR
istypedef
ed to a pointer to aconst TCHAR
.Thus, to make your code correct, you should replace the strings you're using with
TCHAR
strings, and use the type-generic version ofsprintf_s
,_stprintf_s
:Alternatively, you can explicitly use the ANSI or wide-character versions of everything: