Reputation: 367
Hello everyone, I have some confusion regarding some Win32 API data types and macros that we use-:
Firstly-: Regarding the WM_NOTIFY message. The lparam contains the pointer to the NMHDR structure. So if its a pointer why is it illegal to access the NMHDR structure like this-:
(*lparam)->idFrom
I mean if its a pointer then I could just use an indirection operator to get to the structure. LPARAM is itself typedefed from LONG_PTR. Why do I have to write the code like this-:
((LPNMHDR)lparam)->idFrom
What is LPNMHDR? LPNMHDR is typedefed in the following way-:
typedef NMHDR FAR * LPNMHDR;
FAR* is again defined in the following way-:
#define FAR _far
What is _far and why to I have to use LPNMHDR cast to access the NMHDR structure from LPARAM?
Secondly-: What does the MAKEINTRESOURCE macro do? I have seen a lot of authors use a plain string when specifying resources to functions. For example-:
CreateDialog(hInst,"Dialog Box",
hwnd,(DLGPROC)DialogFunc);
But in modern compilers we use-:
CreateDialog(hInst,MAKEINTRESOURCE(DIALOG_BOX),
hwnd,(DLGPROC)DialogFunc);
I know that if we use a string identifier in the resource file instead of a number then we can omit this macro so does this macro convert a number to a string. For example does it convert 23 into "23"?? Because if it did then I would be able to use -:
CreateDialog(hInst,"23",
hwnd,(DLGPROC)DialogFunc);
if my dialog box resource was defined with 23. But this doesn't work.
So I want to know what is the outcome after this macro processes an identifier? How does it work? What would I have to do in order to print the value of the MAKEINTRESOURCE in a message box because I am facing problems while copying the value to a string using the the sprintf function. But I know that MAKEINTRESOURCE outputs a LPSTR because it is defined in the following way-:
#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
I have not yet found any documentation on how this macro works. The msdn states that MAKEINTRESOURCE is a macro which 'converts an integer value to a resource type compatible with the resource-management functions'. Thank You.
Upvotes: 1
Views: 2641
Reputation: 92261
lParam
is not a pointer, it is an integer type parameter, so you have to cast it to the proper pointer type before using it.
LPNMHDR
is just a typedef used for historical reason. You can just as well use NMHDR*
. The FAR and _far qualifiers were used with 16-bit Windows, and are just noise left over from that time.
The MAKEINTRESOURCE
is a trick used by the Windows C language interface, to simulate what an overloaded function would do in C++. This way you can call the CreateDialog
with either a pointer (to string) parameter or an integer parameter. This uses knowledge that strings will never be allocated at very low addresses, so these can be reserved as an alias for the resource IDs. A rather ugly hack!
Upvotes: 4