E0167错误:类型为”const char *”的参数与”type为”LPCWSTR”的参数不兼容。

huangapple go评论109阅读模式
英文:

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*(这就是 LPCWSTRtypedef)。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(), ...

huangapple
  • 本文由 发表于 2023年3月7日 03:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定