本文所述情况是我在写测试代码时发现和解决问题的过程,见以下这段测试代码,在DEV-C++用32位TDM-GCC编译可无错无警告通过;但用64位编译却出错了。
#include <windows.h> #include <string> using namespace std; LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { static int i=0; static HWND bHwnd[15],sHwnd; static HFONT hFont = CreateFont( 30, 20, 0, 0, 700, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, TEXT("仿宋") ); switch(Message) { case WM_CREATE: for (auto b:bHwnd){ b = CreateWindow( "BUTTON", "", //先不设标题,可以创建后添加 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_FLAT | BS_PUSHBUTTON, 70 + (i>=10 ? 360:(i>=5 ? 180:0)) , 110 + i*60 - (i>=10 ? 600:(i>=5 ? 300:0)) , 120, 50, hwnd, (HMENU)i, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL); SendMessage(b, WM_SETTEXT, 0, (LPARAM)to_string(1+i++).c_str()); //添加标题 } sHwnd = CreateWindow( TEXT("STATIC"),NULL,WS_CHILD | WS_VISIBLE, 70, 30, 480, 50, hwnd, NULL, ((LPCREATESTRUCT) lParam)->hInstance,NULL); SendMessage(sHwnd, WM_SETFONT, (WPARAM)hFont, lParam); SetWindowLong(sHwnd, GWL_STYLE, GetWindowLong(sHwnd, GWL_STYLE)|SS_CENTER|SS_CENTERIMAGE); break; case WM_COMMAND: SetWindowText(sHwnd, to_string(LOWORD(wParam)+1).data()); break; case WM_MOUSEMOVE: SetWindowText(sHwnd, to_string((int)GetFocus()).data()); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG msg; memset(&wc,0,sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL,NULL,hInstance,NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } while(GetMessage(&msg, NULL, 0, 0) > 0) { if(!IsDialogMessage(hwnd, &msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; }
错误在39行代码:SetWindowText(sHwnd, to_string((int)GetFocus()).data());
错误信息:[Error] cast from 'HWND' to 'int' loses precision [-fpermissive]
分析错误所在,先看SetWindowText()原型:
#define SetWindowText __MINGW_NAME_AW(SetWindowText) WINUSERAPI WINBOOL WINAPI SetWindowTextA(HWND hWnd,LPCSTR lpString); WINUSERAPI WINBOOL WINAPI SetWindowTextW(HWND hWnd,LPCWSTR lpString);
再看LPCSTR、LPCWSTR:
typedef CONST CHAR *LPCSTR,*PCSTR; typedef CONST WCHAR *LPCWSTR,*PCWSTR; typedef LPCSTR PCTSTR,LPCTSTR,PCUTSTR,LPCUTSTR; typedef LPCWSTR PCTSTR,LPCTSTR;
to_string((int)GetFocus()).data()中,.data()等价于.c_str()没问题,所以只剩下GetFocus()的返回值强制转int这个可能了。
GetFocus()原型:
WINUSERAPI HWND WINAPI GetFocus(VOID);
它的返回值是HWND,它是窗口句柄Handle of Window,相当于一个指向窗口的指针。
#ifndef WIN_INTERNAL DECLARE_HANDLE (HWND); DECLARE_HANDLE (HHOOK); #ifdef WINABLE DECLARE_HANDLE (HEVENT); #endif #endif ...... struct HWND__ { int unused ; }; typedef struct HWND__ *HWND;
估计问题在32位系统里HWND 和 int 是同字节的可以转换,但在64位系统中int的字节宽度就不够所以出错了。
马上写代码验证:
#include <iostream> #include <windows.h> using namespace std; BOOL IsWow64() { //此函数好像取决于编译器的是32-bit还是64-bit,而不是操作系统的位数 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); LPFN_ISWOW64PROCESS fnIsWow64Process; BOOL bIsWow64 = FALSE; fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle("kernel32"),"IsWow64Process"); if (NULL != fnIsWow64Process) fnIsWow64Process(GetCurrentProcess(),&bIsWow64); return bIsWow64; } int main(void) { cout<<sizeof(HWND)<<endl; cout<<sizeof(int)<<endl; cout<<sizeof(long)<<endl; cout<<sizeof(long long)<<endl; cout<<IsWow64()<<endl; return 0; } /* 8 4 4 8 0 //或1,取决于编译器的是32-bit还是64-bit -------------------------------- Process exited after 0.8295 seconds with return value 0 请按任意键继续. . . */
把 SetWindowText(sHwnd, to_string((int)GetFocus()).data()) 中红字部分改为(long long)就通过编译了。
后来发现只要是8位字节的数据类型都行(__int64)、(intptr_t)、(uintptr_t)、(ptrdiff_t),甚至这些类型:(LPARAM)、(WPARAM)、(LRESULT)。
看它们在库函数中的定义:
#ifndef _INTPTR_T_DEFINED #define _INTPTR_T_DEFINED #ifndef __intptr_t_defined #define __intptr_t_defined #undef intptr_t #ifdef _WIN64 __MINGW_EXTENSION typedef __int64 intptr_t; #else typedef int intptr_t; #endif /* _WIN64 */ #endif /* __intptr_t_defined */ #endif /* _INTPTR_T_DEFINED */ #ifndef _UINTPTR_T_DEFINED #define _UINTPTR_T_DEFINED #ifndef __uintptr_t_defined #define __uintptr_t_defined #undef uintptr_t #ifdef _WIN64 __MINGW_EXTENSION typedef unsigned __int64 uintptr_t; #else typedef unsigned int uintptr_t; #endif /* _WIN64 */ #endif /* __uintptr_t_defined */ #endif /* _UINTPTR_T_DEFINED */ #ifndef _PTRDIFF_T_DEFINED #define _PTRDIFF_T_DEFINED #ifndef _PTRDIFF_T_ #define _PTRDIFF_T_ #undef ptrdiff_t #ifdef _WIN64 __MINGW_EXTENSION typedef __int64 ptrdiff_t; #else typedef int ptrdiff_t; #endif /* _WIN64 */ #endif /* _PTRDIFF_T_ */ #endif /* _PTRDIFF_T_DEFINED */
typedef UINT_PTR WPARAM; typedef LONG_PTR LPARAM; typedef LONG_PTR LRESULT; ... ... #ifdef _WIN64 __MINGW_EXTENSION typedef __int64 INT_PTR,*PINT_PTR; __MINGW_EXTENSION typedef unsigned __int64 UINT_PTR,*PUINT_PTR; __MINGW_EXTENSION typedef __int64 LONG_PTR,*PLONG_PTR; __MINGW_EXTENSION typedef unsigned __int64 ULONG_PTR,*PULONG_PTR; #define __int3264 __int64 #else typedef int INT_PTR,*PINT_PTR; typedef unsigned int UINT_PTR,*PUINT_PTR; typedef long LONG_PTR,*PLONG_PTR; typedef unsigned long ULONG_PTR,*PULONG_PTR; #define __int3264 int #endif
果然这些数据在两类系统中就是int和__int64的不同而已,所以把39行代码改为以下几行,就不出错了。
#ifdef _WIN64 SetWindowText(sHwnd, to_string((__int64)GetFocus()).data()); #else SetWindowText(sHwnd, to_string((int)GetFocus()).data()); #endif /* _WIN64 */
随后再次编译,第23行代码又出现警告: [Warning] cast to pointer from integer of different size [-Wint-to-pointer-cast]
估计是差不多情形,把(HMENU)i 改成 (HMENU)(WPARAM)i,代码通过无警告无错误编译;并且用 (WPARAM) 强制转换相当于C++代为条件编译了 。