鑑於你的問題的代碼鏈接,看來問題就在這裏:
Serial::Serial(char *portName)
{
...
this->hSerial = CreateFile(portName, // <--- ERROR
CreateFile
is a Win32 API that expects an LPCTSTR
as first string parameter。
LPCTSTR
是Win32的typedef,它擴展爲:
- 在ANSI/MBCS
const char*
構建 以Unicode
const wchar_t*
構建(其已自VS2005默認值)
由於您正在使用VS2010,可能您處於默認的Unicode構建模式。
實際上,沒有「物理」CreateFile
API暴露,但有兩個截然不同的函數:CreateFileA
和CreateFileW
。前者採用const char*
輸入字符串,後者採用const wchar_t*
。
在Unicode構建中,CreateFile
是一個預處理宏,擴展爲CreateFileW
;在ANSI/MBCS版本中,CreateFile
擴展爲CreateFileA
。
因此,如果您處於Unicode構建模式,則您的CreateFile調用將擴展爲CreateFileW(const wchar_t*, ...)
。由於portName
定義爲char*
,因此wchar_t*
和char*
之間存在不匹配,並且您收到編譯器錯誤。
要解決這個問題,你有一些選擇。
例如,你可以明確在你的代碼,而只是調用CreateFileA()
,而不是CreateFile()
。這樣,您將使用該函數的ANSI/MBCS版本(即採用const char*
的版本),與Visual Studio中的實際ANSI/MBCS/Unicode設置無關。
另一種選擇是將當前的編譯設置從默認的Unicode模式更改爲ANSI/MBCS。要做到這一點,你可以按照以下路徑:
Project Properties | Configuration Properties | General | Character Set
,並選擇「使用多字節字符集」,作爲顯示在下面的截圖:
![Setting Multi-Byte Character Set in VS2010 IDE](https://i.stack.imgur.com/aE54Y.png)
的可能重複[什麼是LPCWSTR代表以及應如何與如何處理?(http://stackoverflow.com/questions/2230758/what-does-lpcwstr-stand-for-以及如何處理 - 與) –