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?
I'm writing a Win32 console application in VC++ using VS 2008.
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.
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'
I thought maybe specifying CreateFileA would work then, but that gave basically the same error.
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
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!