Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I cast the result of GetProcAddress to a function pointer without -fpermissive on mingw?

When I use -fpermissive I can just write something like this:

void (*NtSetTimerResolution)(ULONG, bool, PULONG) = 0;

int main()
{
    NtSetTimerResolution = GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetTimerResolution");
    ULONG pointless;
    NtSetTimerResolution(0x1388, 1, &pointless);
    return 0;
}

and it compiles and runs just fine with no runtime errors.
How can I re-write this code to not include fpermissive?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need an explicit cast:

NtSetTimerResolution = reinterpret_cast <void (*)(ULONG, bool, PULONG)> (GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetTimerResolution"));

You might still be violating strict aliasing rules here, but you can use -fno-strict-aliasing to get round that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading