c中的char数组到LPCTSTR的转换 [英] char array to LPCTSTR conversion in c

查看:38
本文介绍了c中的char数组到LPCTSTR的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何在 c 中将 char 数组转换为 LPCTSTR?

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

为了获得更多参考,我需要将一个整数添加到一个字符串中,然后将该字符串转换为 LPCTSTR 作为 windows 函数 CreateFile() 的第一个参数.

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

这是我尝试过的几件事,我相信其中包括对本文接下来的 2 个答案的以下建议.不幸的是,它们不起作用.如果有人能指出我做错了什么并可能解决我的问题,我将不胜感激.

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.

所有这些示例都假定 portNum 是一个已分配有效值的 int

All of these examples assume that portNum is an int that is already assigned a valid value

char portName[12] = { 0 };

sprintf_s( portName, sizeof( portName ), "\\.\COM%i", portNum );

CreateFile(portName...

我还尝试了使用 LPCSTR 案例的 #1 来证明它的价值...

I've also tried #1 with a LPCSTR case for what it's worth...

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...

推荐答案

您可以将 char 数组隐式转换为 LPCSTR 而无需任何强制转换:

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

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

LPCSTR 是 Windows 类型定义,用于指向常量字符串的长指针.早在 Win16 编程的黑暗时期,就有不同类型的指针:near 指针和 far 指针,有时也称为 shortlong 指针.Near 指针只能指向由 x86 段寄存器之一确定的 64KB 内存段.远指针可以指向任何东西.现在在带有虚拟内存的 Win32 中,不需要近指针——所有指针都是长指针.

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.

因此,LPSTRchar * 的 typedef,或指向字符串的指针.LPCSTRconst 版本,即它是 const char * 的 typedef.在 C 中,数组衰减为指向其第一个元素的指针,因此 char[] 衰减为 char*.最后,任何类型的指向 T 的指针"(对于任何类型 T)都可以隐式转换为指向 const T 的指针".因此,结合这三个事实,我们可以看到我们可以将 char[] 隐式转换为 LPCSTR.

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.

作为对您的编辑的回应,我猜您正在编译一个 Unicode 应用程序.如果您仔细查看 CreateFile() 的文档,你会注意到文件名参数实际上是一个 LPCTSTR,而不是 LPCSTR(注意 T).

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).

对于几乎所有接受某种字符串类型参数的 Win32 函数(可能是间接的,即作为作为参数传递的结构的成员),该函数实际上有两个版本:一个接受 8 位 ANSI字符串,一种采用 16 位宽字符串.要获得实际的函数名称,您需要在函数名称后附加 AW.因此,CreateFile() 的 ANSI 版本被命名为 CreateFileA(),而宽字符版本被命名为 CreateFileW().根据您是否在启用 Unicode 的情况下进行编译(即是否定义了预处理器符号 _UNICODE),符号 CreateFile#defined 到 CreateFileACreateFileW 视情况而定,同样适用于具有 ANSI 和宽字符版本的所有其他函数.

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.

同理,类型 TCHARtypedef 编辑为 charwchar_t,具体取决于是否启用了 Unicode,并且 LPCTSTRtypedef 指向一个指向 const TCHAR 的指针.

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.

因此,为了使您的代码正确,您应该将您正在使用的字符串替换为 TCHAR 字符串,并使用 sprintf_s 的类型通用版本,_stprintf_s:

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, ...);

或者,您可以显式使用所有内容的 ANSI 或宽字符版本:

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, ...);

这篇关于c中的char数组到LPCTSTR的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆