英文:
E0167 argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
问题
Expecting No Error for this line // CreateFile(port.c_str()
E0167 argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
英文:
#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> serialCOM::list_serial_ports()
{
std::vector<std::string> ports;
// Enumerate all available COM ports
for (int i = 1; i <= 256; i++) {
std::string port = "COM" + std::to_string(i);
HANDLE hComm = CreateFile(port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
}
return ports;
}
Expecting No Error for this line // CreateFile(port.c_str()
E0167 argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
答案1
得分: 1
您正在使用 CreateFileW
,它需要一个 const wchar_t*
(这就是 LPCWSTR
的 typedef
)。port.c_str()
返回一个 const char*
,这些指针不兼容。
要么改用 CreateFileA
(它需要一个 const char*
/ LPCSTR
),要么将 port
改为 std::wstring
并继续使用 CreateFileW
:
std::wstring port = L"COM" + std::to_wstring(i);
HANDLE hComm = CreateFileW(port.c_str(), ...
英文:
You are using CreateFileW
which expects a const wchar_t*
(which is what LPCWSTR
is a typedef
for). port.c_str()
returns a const char*
and these pointers are not compatible.
Either use CreateFileA
instead (which expects a const char*
/ LPCSTR
) or make port
a std::wstring
and continue to use CreateFileW
:
std::wstring port = L"COM" + std::to_wstring(i);
HANDLE hComm = CreateFileW(port.c_str(), ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论