Why does MAKEINTRESOURCE() work?

后端 未结 3 1546
清歌不尽
清歌不尽 2020-12-03 14:00

The macro is defined as:

#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))
         


        
3条回答
  •  情话喂你
    2020-12-03 14:07

    MAKEINTRESOURCE macro just makes casting between numeric parameter and string pointer. The resulting string pointer is invalid and cannot be dereferenced as resource name. However, resource handling API detect such pointers by their absolute value and treat them as resource ID and not resource name. Since C-style API doesn't support overloading, they cannot define two functions like:

    HICON LoadIcon(HINSTANCE hInstance,LPCTSTR lpIconName);
    HICON LoadIcon(HINSTANCE hInstance,UINT resourceId);
    

    So, API developers decided to use the same function for both cases, providing MAKEINTRESOURCE macro for API users. I believe that two different functions could look better:

    HICON LoadIconByName(HINSTANCE hInstance,LPCTSTR lpIconName);
    HICON LoadIconById(HINSTANCE hInstance,UINT resourceId);
    

    But this is not the way Windows API is implemented. Valid resource ID is always less than minimal possible pointer value. Resource name parameter is passed to API without this macro, and its value is not restricted.

提交回复
热议问题