Given the code link in your question, it seems the problem is here:
Serial::Serial(char *portName)
{
...
this->hSerial = CreateFile(portName, // <--- ERROR
CreateFile
is a Win32 API that expects an LPCTSTR
as first string parameter .
LPCTSTR
is a Win32 typedef, which is expanded to:
const char*
in ANSI/MBCS builds
const wchar_t*
in Unicode builds (which have been the default since VS2005)
Since you are using VS2010, probably you are in the default Unicode build mode.
Actually, there is no "physical" CreateFile
API exposed, but there are two distinct functions: CreateFileA
and CreateFileW
. The former takes a const char*
input string, the latter takes a const wchar_t*
.
In Unicode builds, CreateFile
is a preprocessor macro expanded to CreateFileW
; in ANSI/MBCS builds, CreateFile
is expanded to CreateFileA
.
So, if you are in Unicode build mode, your CreateFile call is expanded to CreateFileW(const wchar_t*, ...)
. Since portName
is defined as a char*
, there is a mismatch between wchar_t*
and char*
, and you get a compiler error.
To fix that, you have some options.
For example, you could be explicit in your code, and just call CreateFileA()
instead of CreateFile()
. In this way, you will be using the ANSI/MBCS version of the function (i.e., the one taking a const char*
), independently from the actual ANSI/MBCS/Unicode settings in Visual Studio.
Another option would be to change your current build settings from the default Unicode mode to ANSI/MBCS. To do that, you can follow the path:
Project Properties | Configuration Properties | General | Character Set
and select "Use Multi-Byte Character Set", as showed in the following screenshot:
