简体   繁体   中英

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!

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 (ie, 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:

在VS2010 IDE中设置多字节字符集

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

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".

You should also remove UNICODE or _UNICODE from C++ -> Preprocessor -> Preprocessor definitions, if they are defined there.

This will make your code call the ASCII versions of the Windows API functions, which accept char strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM