字符数组LPCTSTR在C转换 [英] char array to LPCTSTR conversion in c
问题描述
有谁知道如何将一个字符数组转换为LPCTSTR在C?
编辑:
有关更多的参考,我需要一个整数添加到字符串再转换该字符串LPCTSTR为Windows函数CreateFile的第一个参数()。
这是我目前使用硬codeD的例子,但我需要能够在任何数量的传递为一个端口号来使用。
的CreateFile(_T(\\\\\\\\。\\\\ COM11)... //硬codeD COM端口11
和这里有几件事我已经试过,我相信包括这个职位的未来2回答以下建议。它们不不幸工作。如果任何人都可以指出一些我做错了,并可能解决我的问题,我想AP preciate它。
所有这些例子都假定端口编号是已经分配的一个有效的值的int
1
字符PORTNAME [12] = {0};sprintf_s(PORTNAME,sizeof的(PORTNAME),\\\\\\\\ \\\\ COM%I,端口编号);的CreateFile(PORTNAME ...
我也试了#1与LPCSTR情况下这是非常值得...
2
LPCSTR SomeFunction(LPCSTR ASTRING){
返回ASTRING;
}主(){CHAR PORTNAME [12] = {0};
sprintf_s(PORTNAME,sizeof的(PORTNAME),\\\\\\\\ \\\\ COM%I,端口编号);LPCSTR lpPortName = SomeFunction(PORTNAME);的CreateFile(lpPortName ...
3
为const char * PORTNAME =;
sprintf_s(PORTNAME,sizeof的(PORTNAME),\\\\\\\\ \\\\ COM%I,端口编号);LPCSTR lpPortName = PORTNAME;的CreateFile(lpPortName ...
您可以隐式字符数组转换为 LPCSTR
无任何强制转换:
无效SomeFunction(LPCSTR ASTRING);
...
CHAR myArray的[] =你好,世界!;
SomeFunction(myarray的);
这是 LPCSTR
是Windows的typedef久指向一个常量字符串。早在Win16的编程的黑暗日子里,有不同类型的指针:附近的指针和的据的三分球,有时也被称为的短和长的分别指针。邻近指针只能指向由86段寄存器中的一个来确定的存储器的64KB段。远指针可能指向任何东西。如今在Win32中使用虚拟内存,没有必要近指针 - 所有的指针很长
所以,一个 LPSTR
是一个的char *
,或指向字符串的指针的typedef。一个 LPCSTR
是常量
版本,也就是说,它是一个为const char的typedef *
。在C语言中,数组衰变为指针,以他们的第一要素,所以的char []
衰变成的char *
。最后,任何类型的指针T(任何类型T)的可以隐式转换为指针为const T。因此,结合这三个事实,我们看到,我们可以隐式转换的char []
到 LPCSTR
。
在回答您的编辑,我要去猜,你编译一个统一code应用程序。如果您在文档,仔细寻找的CreateFile()
,你会发现文件名参数实际上是 LPCTSTR
,而不是 LPCSTR
(注意 T
)。
有关pretty多少每个Win32函数,需要一些字符串类型的参数(也许是间接的,即是作为参数传递结构的成员),居然有该函数的两个版本:一个这需要8比特ANSI字符串,和其中一个需要16位宽字符串。为了获得实际的函数名,你追加一个 A
或是W
函数名。因此,ANSI版本的的CreateFile()
被命名为 CreateFileA的()
,以及宽字符版本被命名为 CreateFileW()
。根据您是否正在与统一code编译启用(即preprocessor符号 _UNI code
是否定义),符号的CreateFile
是的#define
D为 CreateFileA的
或 CreateFileW
适当的,同样的,有一个ANSI和宽字符版本。所有其它功能
按照同样的思路,类型 TCHAR
是的typedef
编辑为字符
或 wchar_t的
,根据统一code是否启用,而 LPCTSTR
是的typedef
ED的指针到常量TCHAR
。
因此,为了使您的code正确,应更换您使用的是与 TCHAR
字符串字符串,并使用的类型,通用版 sprintf_s
, _stprintf_s
:
TCHAR PORTNAME [32];
_stprintf_s(PORTNAME,sizeof的(PORTNAME)/的sizeof(TCHAR),_T(\\\\\\\\。\\\\ COM%d个。),端口编号);
的CreateFile(PORTNAME,...);
另外,你可以明确地使用一切的ANSI或宽字符版本:
//使用ANSI
焦炭PORTNAME [32];
sprintf_s(PORTNAME,sizeof的(PORTNAME),\\\\\\\\ \\\\ COM%d个,端口编号);
CreateFileA的(PORTNAME,...);//使用宽字符
wchar_t的PORTNAME [32];
swprintf_s(PORTNAME,sizeof的(PORTNAME)/的sizeof(wchar_t的),L\\\\\\\\。\\\\ COM%d个,端口编号);
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在C转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!