It seems I have a problem with TEXT / UNICODE when using the CreateFile function to address the serial port. Can someone please indicate my mistake?
I am writing a Win32 console application in VC ++ using VS 2008.
I can create a descriptor for the serial port as follows:
#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;
}
This works very well (a bit \\\\.\\is required to build more than COM9 and works for those prior to COM9 as well). The problem is that my comport will not always be COM20, so I would like the user to specify what it is.
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;
}
: C2664: "CreateFileW": 1 "std::string" "LPCWSTR"
, , , , CreateFileA , .
:
/*
everything else the same
*/
hSerial = CreateFile( TEXT(comID),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);`
: C2065: "LcomID":
, . - , L"\\\\.\\COM20" , comport CreateFile ? !