2013-08-04 29 views
1

下面的錯誤是@的GetFullPathName()函數不兼容的變種類型

1 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR" 
2 IntelliSense: argument of type "char *" is incompatible with parameter of type "LPWSTR" 

我不斷收到上面的錯誤,當我嘗試運行我的程序。這些變量是適當的類型,但它一直說不。任何想法,爲什麼這是?我不認爲有必要輸入他們。

#include "stdafx.h" 

using namespace std; 

int main(int argc, _TCHAR* argv[]) 
{ 
    /* Get path of DLL we're loading */ 
    string name; 
    cin >> name; 

    const char* DLL_NAME = name.c_str(); 

    HWND handle = FindWindow(0, L"Calculator"); 

    if(handle == NULL){ 
     cout << "Couldn't find process window!" << endl; 
    }else{ 
     cout << "Process window found, continuing..." << endl; 

     DWORD id; 
     GetWindowThreadProcessId(handle, &id); 

     char DLL_LOCATION[MAX_PATH] = {0}; 
     GetFullPathName(DLL_NAME, MAX_PATH, DLL_LOCATION, NULL); 

    } 

    return 0; 
} 

回答

4

的變量是合適的類型,但它一直否則說什麼?

不,他們不是。 LPCWSTRLPWSTR分別是const wchar_t*wchar_t*的別名。您必須使用std::wstring而不是std::string。他們的意思

擊穿:

  • LPCWSTR:長指針爲const寬字符串
  • LPWSTR:長指針,寬字符串

另外,您可以編譯您的項目unicode(通過將字符集更改爲Multi-Byte IIRC),這樣Windows API將會期望「常規」字符串。

編輯:我要指出,就像串有廣泛的模擬這麼做std::coutstd::cinstd::wcoutstd::wcin形式。

+0

好的,只是將var類型更改爲寬字符串。直到第二天,我才忘記了獲勝功能,所以我試圖弄清楚爲什麼我無法獲得用戶輸入。感謝您的快速回答。 :) – Justin

+0

@Justin很高興能有所幫助。 – Borgleader

2

你正在編譯一個Unicode版本,所以所有的Windows API函數都會期待Unicode字符串。

您可以:

  • 改變您的項目設置做多字節建立
  • 開關採用寬弦(std::wstring等)
  • 調用ANSI API函數明確(GetFullPathNameA等)
2

LPCWSTR是const wchar_t *。你最好也切換到wchar_t,因爲所有的windows API本身都與wchar_t一起工作。