读书人

GetWindowText函数有关问题

发布时间: 2012-01-22 22:38:43 作者: rapoo

GetWindowText函数问题.

C/C++ code
#include <iostream>#include <Windows.h>#include <string>using namespace std;void main(){    HWND hwnd;    hwnd=::FindWindow(TEXT("CabinetWClass"),TEXT("我的电脑"));    string s="";    ::GetWindowText(hwnd,s,255);    cout<<s;}


错误1error C2664: “GetWindowTextW”: 不能将参数 2 从“std::string”转换为“LPWSTR”c:\Documents and Settings\MoBin\My Documents\Visual Studio 2008\Projects\c\c\z.cpp11c


[解决办法]
GetWindowText是一个宏,根据项目所使用的字符集决定使用GetWindowTextA和GetWindowTextW其中的一个。因为你的项目使用的是Unicode字符集,所以如果后面没有A,表示使用GetWindowTextW,相应的变量都要使用宽字符。因为你要用string类型,所以要搭配GetWindowTextA;如果用GetWindowText,要把char改成wchar_t,把string改成wstring。

读书人网 >VC

热点推荐