I am using MinGW-w64 and trying to dynamically import a function from a DLL using GetProcAddress. The function I want to import has the following signature:
__declspec(dllexport) double __cdecl func(double t, unsigned int n);
To retrieve the function, I am using GetProcAddress from WIN32 API as follows:
typedef double (__cdecl *TYPE_FUNCTION)(double, unsigned int);
TYPE_FUNCTION func_ptr = (TYPE_FUNCTION) GetProcAddress(hinstLib, "func");
However, I receive the following:
warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'double (*)(double, unsigned int)'
Is this warning something I should be concerned about? If so, how can I safely cast or call the function? Would the cast cause any potential issues at runtime?
use
__declspec(dllimport) double __cdecl func(double t, unsigned int n);
and
PVOID __imp_func;
(in case c++ declare both as EXTERN_C
or extern "C"
)
and finally
__imp_func = GetProcAddress(hinstLib, "func");
so we not need any typedef
and cast
explanation:
when we declare function func
with __declspec(dllimport)
attribute, compiler declare variable extern PVOID __imp_func;
(add __imp_
) prefix to decorated function func
name. and when you call func
compiler generate code call __imp_func
; so all what we need - implement PVOID __imp_func;
. in case static link - the __imp_func
will be inside some lib file. if you use dynamic linking - simply implement PVOID __imp_func;
by self (without extern
) and assign it value from GetProcAddress
(or LdrGetProcedureAddress
). note - this is for x64 mangling.
for x86 this will be PVOID _imp__func
(because __cdecl
- func
will be transformed to _func
and variable x
transformed to _x
so PVOID _imp__func
generate __imp__func
symbol in x86)
if use __stdcall
- will be PVOID _imp__func@12
(__imp__func@12
symbol). we can not declare
PVOID _imp__func@12;
already. but we can use asm
for this. or if use CL (more known as msvc) compiler (latest versions) we can use
__identifier
keyword with
#pragma warning(disable: 4483) // Allow use of __identifier
so code in case x86 and if api was __stdcall
#pragma warning(disable: 4483) // Allow use of __identifier
__declspec(dllimport) double __stdcall func(double t, unsigned int n);
#define __imp_func __identifier("_imp__func@12")
PVOID __imp_func; // = GetProcAddress(hinstLib, "func");