字符数组到LPCTSTR转换 [英] char array to LPCTSTR conversion in c
问题描述
有没有人知道如何将字符数组转换为LPC中的LPCTSTR?
编辑:
为了更多的参考,我需要添加一个整数到一个字符串,然后将该字符串转换为LPCTSTR为windows函数CreateFile()的第一个参数。
这是硬编码示例,我目前使用,但我需要能够传递任何数字作为端口号。
CreateFile (_T(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ COM11
$ b 这里有几个我已经尝试过的,我相信包括以下建议为这篇文章的下两个答案。他们不工作不幸的是,如果任何人可以指出我的东西
所有这些例子假设portNum是一个已经赋值为有效值的int
1
char portName [12] = {0};
sprintf_s(portName,sizeof(portName),\\\\\\\\\\\\\%%%%%%%%%%%% ...%
CreateFile(portName ...
有一个LPCSTR案例为它的价值...
2
LPCSTR SomeFunction(LPCSTR aString){
return aString;
}
main(){
char portName [12] = {0};
sprintf_s(portName,sizeof(portName),\\\\.\\COM%i,portNum);
LPCSTR lpPortName = SomeFunction ;
CreateFile(lpPortName ...
3
const char * portName =;
sprintf_s(portName,sizeof(portName),\\\\。 \\COM%i,portNum);
LPCSTR lpPortName = portName;
CreateFile(lpPortName ...
解决方案可以隐式地将一个字符数组转换为 LPCSTR
没有任何转型:
void SomeFunction(LPCSTR aString);
...
char myArray [ ] =hello,world!;
SomeFunction(myArray);
一个 LPCSTR
是Windows typedef长指针指向一个常量字符串。回到Win16编程的黑暗时代,有不同类型的指针: 指针和远指针,有时也被称为 short long 指针。近指针只能指向由x86段寄存器之一确定的64KB内存段。远指针可以指向任何东西。现在在Win32中使用虚拟内存,没有必要使用near指针 - 所有指针都很长。
所以,一个 LPSTR
是 char *
的typedef,或指向字符串的指针。 LPCSTR
是 const
版本,即它是 const char * / code>。在C中,数组衰减为指向其第一个元素的指针,因此 char []
衰减为 char *
。最后,任何类型的指针到T(对于任何类型T)可以被隐式地转换为指向const T的指针。因此,结合这三个事实,我们看到我们可以隐式地将 char []
转换为 LPCSTR
p>
响应你的编辑,我猜你正在编译一个Unicode应用程序。如果您仔细查看 CreateFile()
,你会注意到文件名参数实际上是一个 LPCTSTR
,而不是 LPCSTR
(注意 T
)。
几乎每个Win32函数一些字符串类型的参数(可能间接地,即作为作为参数传递的结构的成员),实际上有该函数的两个版本:一个采用8位ANSI字符串,一个采用16位宽字符串,字符串。要获取实际的函数名,请在函数名后面附加 A
或 W
。因此, CreateFile()
的ANSI版本命名为 CreateFileA()
,宽字符版本命名为 CreateFileW()
。根据是否使用Unicode编译(即是否定义了预处理符号 _UNICODE
),符号 CreateFile
是 #define
d至 CreateFileA
或 CreateFileW
同样,类型 TCHAR
/ code>是 typedef
编辑到 char
或 wchar_t
,取决于是否启用Unicode,并且 LPCTSTR
是 typedef
ed到指向 const TCHAR
。
因此,为了使代码正确,您应该使用 TCHAR
字符串,并使用类型通用版本 sprintf_s
, _stprintf_s
:
TCHAR portName [32];
_stprintf_s(portName,sizeof(portName)/ sizeof(TCHAR),_T(\\\\.\\\\%\\),
CreateFile(portName,...);
或者,您可以显式地使用ANSI或宽字符版本的一切:
//使用ANSI
char portName [32];
sprintf_s(portName,sizeof(portName),\\\\\\\\COM%d,portNum);
CreateFileA(portName,...);
//使用宽字符
wchar_t portName [32];
swprintf_s(portName,sizeof(portName)/ sizeof(wchar_t),L\\\\\\\\\\\\\\\\%%%%%%%%%% ...'
CreateFileW(portName,...);
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...
解决方案 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 #define
d 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 typedef
ed to either char
or wchar_t
, depending on whether Unicode is enabled, and LPCTSTR
is typedef
ed 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, ...);
这篇关于字符数组到LPCTSTR转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!