windows WCHAR 到 LPCWSTR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3164509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:44:58  来源:igfitidea点击:

WCHAR to LPCWSTR

c++windowswinapiqt

提问by user198725878

when i use CreateFile function like below ...it gives me valid handle

当我使用如下所示的 CreateFile 函数时......它给了我有效的句柄

HANDLE hDevice = CreateFile (TEXT("\\.\G:"),
                 0,FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                 NULL, OPEN_EXISTING, 0, NULL);

if( hDevice == INVALID_HANDLE_VALUE )
{
  qDebug()<<"In valid handle";
}
else
{
  qDebug()<<"valid handle";
}

when i use like below ...it gives me invalid handle..

当我像下面这样使用时......它给了我无效的句柄..

WCHAR Drive[4];

qDebug ()<<QString::fromWCharArray ( Drive ); 

The above prints like "G:\"

HANDLE hDevice = CreateFile ( Drive,
                 0,FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                 NULL, OPEN_EXISTING, 0, NULL);

if( hDevice == INVALID_HANDLE_VALUE )
{
  qDebug()<<"In valid handle";
}
else
{
  qDebug()<<"valid handle";
}

How can i change the wchar to LPCWSTR

如何将 wchar 更改为 LPCWSTR

Thank you

谢谢

回答by humbagumba

The problem is not the conversion of the string, but the contents of the string. You can't open a volume (I guess that's what you're trying to do) with "G:\". It needs to be in the same format as you used in the first example. From MSDN:

问题不是字符串的转换,而是字符串的内容。你不能用“G:\”打开一个卷(我猜这就是你想要做的)。它需要与您在第一个示例中使用的格式相同。来自 MSDN:

When opening a volume or floppy drive, the lpFileName string should be the following form: \\.\X:. Do not use a trailing backslash, which indicates the root directory of a drive.

打开卷或软盘驱动器时,lpFileName 字符串应为以下形式:\\.\X:。不要使用尾部反斜杠,它表示驱动器的根目录。

Hint: Always use GetLastError()after API functions fail to get the reason for the failure.

提示:GetLastError()API 函数失败后始终使用,获取失败原因。

Update: MSDN Link

更新: MSDN 链接

回答by the_void

You can either use the toWCharArrayfunction or try something like this:

您可以使用toWCharArray函数或尝试以下操作:

handle = CreateFile((LPCWSTR) fileName.constData(), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

or this:

或这个:

handle = CreateFile((LPCWSTR) fileName.utf16(), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

回答by TheUndeadFish

LPCWSTRis a pointer (LP) to a constant (C) wide character (W) string (STR). In other words, this is a const WCHAR*

LPCWSTR是指向常量 (C) 宽字符 (W) 字符串 (STR) 的指针 (LP)。换句话说,这是一个const WCHAR*

WCHAR Drive[4];is a wide character array, which can also be called a wide character string.

WCHAR Drive[4];是一个宽字符数组,也可以称为宽字符串。

Any array of a certain type can implicitly convert to a pointer to that same type. Furthermore, a pointer of a certain type can implicitly convert to a constant pointer of the same type, especially in the case of a function call.

任何特定类型的数组都可以隐式转换为指向相同类型的指针。此外,某种类型的指针可以隐式转换为相同类型的常量指针,尤其是在函数调用的情况下。

Thus passing Driveto that function implicitly converts to LPCWSTR.

因此传递Drive给该函数隐式转换为LPCWSTR.

Your problem in not in that conversion. Your problem is most likely in the contentsof your strings, as humbagumba's answer already explained.

你的问题不在于那个转换。您的问题很可能出在字符串的内容中,正如 humbagumba 的回答已经解释的那样。