bool vs BOOL
I am trying to convert my console app. to a Windows app. but I am a little bit confused as to the difference between these two lines:
The first line everything complies without errors but the second line gives the following error. (Obviously that the 4th param. is wrong but I dont understand why!)
BOOL CALLBACK InputProc(HWND, UINT, WPARAM, LPARAM);
bool CALLBACK InputProc(HWND, UINT, WPARAM, LPARAM);
error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'bool (struct HWND__ *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(struct HWND__*,unsigned int,unsigned int,long)'
Why are bool and BOOL different in this example?
I am currently running MSV C on an XP pro machine.
bool is a built-in C++ variable type. Like long, short, double etc.
BOOL, on the other hand is typedefed to int in <windows.h>.
In MSVC, sizeof(bool) == 1, while sizeof(BOOL) == sizeof(int) == 4. Therefore these types are not interchangeable. Also, a bool can only hold true or false while a BOOL has traditionally been used to hold arbitary int values, where 0 is false and any other value true. Also in the past, BOOL has been used to pass pointers and other other data types.
Microsoft API prototypes will generally not use the bool datatype as it is not available in C.
Note that the correct prototype for a dialog box procedure is now:
This is beacause a BOOL can not hold a pointer in Win64.
INT_PTR CALLBACK DialogProc(
>> (Obviously that the 4th param. is wrong but I dont understand why!) <<
The error is not saying that the fourth argument of the function prototype is wrong, it is saying the the fourth argument to DialogBoxParam() is wrong. The fourth argument is the function pointer and the function pointer that you are passing does not match.
Do it this way:
That should work :-)
LRESULT CALLBACK InputProc(HWND, UINT, WPARAM, LPARAM);
DialogBox(hInstance, dialog_name, parent, (DLGPROC) InputProc);