Boost logo

Boost :

Subject: Re: [boost] [Style] Import a C function with wrong signature on purpose
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2016-05-08 12:11:57


On Sunday, 8 May 2016 19:05:45 MSK Klemens Morgenstern wrote:
>
> I missed the most obvious solution (I am using GetLastError because it's
> shorter):
>
> inline HANDLE_ kernel32_handle()
> {
> //it's a system dll, so I don't really need to clean up
> static HANDLE_ h = get_module_handle("Kernel32.dll");
> return h;
> }
>
> inline DWORD_ get_last_error()
> {
> typedef DWORD_ WINAPI (*GetLastError_t)();
> static GetLastError_t p =
> reinterpret_cast<GetLastError_t>
> (get_proc_address(kernel32_handle(), "GetLastError"));
> return (*p)();
> }
>
> I tested that directly with the winapi, the loaded address is correct.
>
> That way I could load any function and override the enum type. I would
> have few static handles here, but since they all point to system-apis I
> don't think it's a problem to not clean them up (though that could be
> done via unique_ptr). That would also be pure C++03, but of course
> violate the current winapi style. Would that be acceptable for the
> winapi by any chance?

In C++03 that is not thread safe (i.e. p may not be initialized on first use). You do have to
use call_once or something like it. Also, this is not strictly equivalent to just using the
function directly because it involves the library loader and may result in deadlocks if
called in DllMain. That said, I don't know if the function in question is safe in this regard in
its own right.

I think, the GetProcAddress solution might be ok in the context of your library, but
probably not in Boost.WinAPI.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk