Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 Arduino cpp Error: argument of type "char *" is incompatible with parameter of type "LPCWSTR"

I'm trying to set up an arduino uno for serial port communication with a C++ program in visual studio 2010. I'm working from the code found here: http://playground.arduino.cc/Interfacing/CPPWindows

Unfortunately, the .cpp file gives me the following message for line 9 for the variable 'portName':

Error: argument of type "char *" is incompatible with parameter of type "LPCWSTR"

I don't understand this error message, and have tried a few different things to fix it. Any help would be greatly appreciated!

like image 987
X__ Avatar asked Dec 08 '22 03:12

X__


2 Answers

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:

Setting Multi-Byte Character Set in VS2010 IDE

like image 136
Mr.C64 Avatar answered Dec 31 '22 14:12

Mr.C64


Your settings in Visual Studio are probably set to Unicode but the code you're compiling expects ASCII.

Go to Project Properties -> Configuration Properties -> General -> Character Set and choose "Use Multi-Byte Character Set".

-Surenthar

like image 40
Surenthar Avatar answered Dec 31 '22 14:12

Surenthar