C++ 将字符串转换为用于 CreateFile() 的 LPCWSTR 以寻址串行端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3783842/
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
Converting a string to LPCWSTR for CreateFile() to address a serial port
提问by John
I seem to be having a bit of a TEXT / UNICODE problem when using the windows CreateFile function for addressing a serial port. Can someone please help point out my error?
使用 windows CreateFile 函数寻址串行端口时,我似乎遇到了一些 TEXT / UNICODE 问题。有人可以帮忙指出我的错误吗?
I'm writing a Win32 console application in VC++ using VS 2008.
我正在使用 VS 2008 在 VC++ 中编写 Win32 控制台应用程序。
I can create a handle to address the serial port like this:
我可以创建一个句柄来寻址串行端口,如下所示:
#include <iostream>
#include <windows.h>
#include <string>
int main()
{
HANDLE hSerial;
hSerial = CreateFile( L"\\.\COM20",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);`
return 0;
}
That works just fine (the \\\\.\\
bit is required for comports greater than COM9 and works for those up to COM9 also). The problem is that my comport will not always be COM20, so I'd like to have the user specify what it is.
这工作得很好(该\\\\.\\
位对于大于 COM9 的端口是必需的,并且也适用于那些高达 COM9 的端口)。问题是我的comport 并不总是COM20,所以我想让用户指定它是什么。
Here are some things I've tried:
以下是我尝试过的一些事情:
#include <iostream>
#include <windows.h>
#include <string>
int main()
{
std::string comNum;
std::cout << "\n\nEnter the port (ex: COM20): ";
std::cin >> comNum;
std::string comPrefix = "\\.\";
std::string comID = comPrefix+comNum;
HANDLE hSerial;
hSerial = CreateFile( comID,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);`
return 0;
}
This does not compile and returns the error: error C2664: 'CreateFileW' : cannot convert parameter 1 from 'std::string' to 'LPCWSTR'
这不会编译并返回错误:错误 C2664:'CreateFileW':无法将参数 1 从 'std::string' 转换为 'LPCWSTR'
I thought maybe specifying CreateFileA would work then, but that gave basically the same error.
我想也许指定 CreateFileA 会起作用,但这给出了基本相同的错误。
I also tried :
我也试过:
/*
everything else the same
*/
hSerial = CreateFile( TEXT(comID),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);`
which also does not compile and returns: error C2065: 'LcomID' : undeclared identifier
它也不会编译并返回:错误 C2065:'LcomID':未声明的标识符
I am not much of an expert but I have been working on this for a while now. Can someone tell me how to replace L"\\\\.\\COM20"
in such a way that the user can specify the comport and so that CreateFile will still work? Thanks!
我不是什么专家,但我已经在这方面工作了一段时间。有人可以告诉我如何以L"\\\\.\\COM20"
用户可以指定comport并且CreateFile仍然可以工作的方式替换吗?谢谢!
回答by André Caron
You can either use std::wstring
and std::wcin
, std::wcout
to perform input directly in "unicode strings", or you can look into Microsoft's conversion functions.
您可以使用std::wstring
和std::wcin
,std::wcout
直接在“Unicode字符串”执行输入,或者你可以看看微软的转换功能。
If you go for the 1st option (recommended), you still need to use the c_str()
function to gain access to a LPCWSTR
value (pointer to const wchar_t
).
如果您选择第一个选项(推荐),您仍然需要使用该c_str()
函数来访问LPCWSTR
值(指向 的指针const wchar_t
)。
Sample solution (also not use of CreateFileW
syntax, to prevent issues with UNICODE
macro):
示例解决方案(也不使用CreateFileW
语法,以防止UNICODE
宏出现问题):
#include <iostream>
#include <windows.h>
#include <string>
int main()
{
std::wstring comNum;
std::wcout << L"\n\nEnter the port (ex: COM20): ";
std::wcin >> comNum;
std::wstring comPrefix = L"\\.\";
std::wstring comID = comPrefix+comNum;
HANDLE hSerial;
hSerial = CreateFileW( comID.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);`
return 0;
}
回答by Remy Lebeau
If you want to continue using std::string
, then use its c_str()
method when calling CreateFileA()
, eg:
如果您想继续使用std::string
,则c_str()
在调用 时使用其方法CreateFileA()
,例如:
hSerial = CreateFileA( comID.c_str(), ...);
回答by AngryWhenHungry
General info on string conversions here: http://www.codeproject.com/KB/string/cppstringguide2.aspx
此处有关字符串转换的一般信息:http: //www.codeproject.com/KB/string/cppstringguide2.aspx
回答by Nick
As an additional reference, have a look at the ATL conversions macros (eg: CA2CW, etc.). These (if used properly) can handle pretty much all simple conversion cases inline, with little fuss.
作为附加参考,请查看 ATL 转换宏(例如:CA2CW 等)。这些(如果使用得当)可以处理几乎所有简单的内联转换情况,而无需大惊小怪。
In your example, you can use std::string, then use:
在您的示例中,您可以使用 std::string,然后使用:
CreateFile( CA2CT( comID.c_str() ), ... );
回答by myeviltacos
If (for some reason) you decide to keep working with ANSI strings, then take a look at the MultiByteToWideChar
function. But ANSI strings are pretty much obsolete.
如果(出于某种原因)您决定继续使用 ANSI 字符串,请查看该MultiByteToWideChar
函数。但是 ANSI 字符串已经过时了。
回答by Paul Nathan
I ran into this recently. I simply disabled Unicode, as Unicode was wholly out of scope for this application.
我最近遇到了这个。我只是禁用了 Unicode,因为 Unicode 完全超出了这个应用程序的范围。