Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow direct cast of get_proc_address result #95

Merged
merged 2 commits into from
Aug 26, 2024

Conversation

Flamefire
Copy link
Contributor

@Flamefire Flamefire commented Aug 25, 2024

GetProcAddress returns a pointer to some function. It can return pointers to different functions, so it has to return something that is suitable to store any pointer to function. Microsoft chose FARPROC, which is int (WINAPI *)() on 32-bit Windows. The user is supposed to know the signature of the function he requests and perform a cast (which is a nop on this platform). The result is a pointer to function with the required signature, which is bitwise equal to what GetProcAddress returned.
However, gcc >= 8 warns about that.

(See boostorg/thread#405)

Add a template parameter to do the cast inside the function and suppress the warning there.

This allows to do the following replacement at the usage site without breaking backwards compatibility:

-        GetFileInformationByHandleEx_t* get_file_information_by_handle_ex = (GetFileInformationByHandleEx_t*)get_proc_address(h, "GetFileInformationByHandleEx");
+        GetFileInformationByHandleEx_t* get_file_information_by_handle_ex = get_proc_address<GetFileInformationByHandleEx_t*>(h, "GetFileInformationByHandleEx");

GetProcAddress returns a pointer to some function. It can return
pointers to different functions, so it has to return something that is
suitable to store any pointer to function. Microsoft chose FARPROC,
which is int (WINAPI *)() on 32-bit Windows. The user is supposed to
know the signature of the function he requests and perform a cast
(which is a nop on this platform). The result is a pointer to function
with the required signature, which is bitwise equal to what
GetProcAddress returned.
However, gcc >= 8 warns about that.

Add a template parameter to do the cast inside the function and suppress
the warning there.
@Lastique
Copy link
Member

I don't think all libraries that depend on Boost.WinAPI have switched to C++11 or later, so let's keep Boost.WinAPI compatible with C++03 for now.

@Flamefire
Copy link
Contributor Author

Oh you are right, default method template params are C++11, been coding to long in at least C++11 by now. Thanks!

Fixed by adding the templated method and calling the non-templated one from there and doing only the cast.

@Lastique Lastique merged commit 3ce8d53 into boostorg:develop Aug 26, 2024
@Flamefire Flamefire deleted the get_proc_add-templ branch August 26, 2024 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants