DLL Question
-
Hi, I wrote a DLL which works fine if I just include the header file and the lib file in the code that uses this DLL. However, when I try to call functions from the DLL the program crash and complain about calling conventions that do not match. Here is the bare minimum: ******* DLL Code **** Header:
typedef void (__stdcall *PF_SPCMCOMM_CALLBACK)(char *,pack_data *); SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK pfn);
CPP file:SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK p_cbfn) { ..... }
*** Client Code *** Header file:static void __cdecl SpcmCallBack (char *payload,pack_data *pd);
CPP file:HISNT spcm_hInst =LoadLibrary (__TEXT("SpcmComm.dll"));//load the dll if (spcm_hInst) { PF_SPCMCOMMINIT initspcm;//pointer to the function initspcm = (PF_SPCMCOMMINIT) GetProcAddress spcm_hInst, "InitilizeConnection"); if (initspcm) { //*****this is where the code crash ***** int result=initspcm ("TestAlertUUT",(PF_SPCMCOMM_CALLBACK)SpcmCallBack); }
-
Hi, I wrote a DLL which works fine if I just include the header file and the lib file in the code that uses this DLL. However, when I try to call functions from the DLL the program crash and complain about calling conventions that do not match. Here is the bare minimum: ******* DLL Code **** Header:
typedef void (__stdcall *PF_SPCMCOMM_CALLBACK)(char *,pack_data *); SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK pfn);
CPP file:SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK p_cbfn) { ..... }
*** Client Code *** Header file:static void __cdecl SpcmCallBack (char *payload,pack_data *pd);
CPP file:HISNT spcm_hInst =LoadLibrary (__TEXT("SpcmComm.dll"));//load the dll if (spcm_hInst) { PF_SPCMCOMMINIT initspcm;//pointer to the function initspcm = (PF_SPCMCOMMINIT) GetProcAddress spcm_hInst, "InitilizeConnection"); if (initspcm) { //*****this is where the code crash ***** int result=initspcm ("TestAlertUUT",(PF_SPCMCOMM_CALLBACK)SpcmCallBack); }
-
does the library implementation have something like this in it ?
extern "C"
{
}This needs to be around the prototype of InitilizeConnection in the header file and around its implementation in the .CPP file.
Thanks, I will try that and let you know. In any case, what is the logic behind it and why does it work when I use the dll implicitly.
-
Hi, I wrote a DLL which works fine if I just include the header file and the lib file in the code that uses this DLL. However, when I try to call functions from the DLL the program crash and complain about calling conventions that do not match. Here is the bare minimum: ******* DLL Code **** Header:
typedef void (__stdcall *PF_SPCMCOMM_CALLBACK)(char *,pack_data *); SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK pfn);
CPP file:SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK p_cbfn) { ..... }
*** Client Code *** Header file:static void __cdecl SpcmCallBack (char *payload,pack_data *pd);
CPP file:HISNT spcm_hInst =LoadLibrary (__TEXT("SpcmComm.dll"));//load the dll if (spcm_hInst) { PF_SPCMCOMMINIT initspcm;//pointer to the function initspcm = (PF_SPCMCOMMINIT) GetProcAddress spcm_hInst, "InitilizeConnection"); if (initspcm) { //*****this is where the code crash ***** int result=initspcm ("TestAlertUUT",(PF_SPCMCOMM_CALLBACK)SpcmCallBack); }
//int result=initspcm ("TestAlertUUT",(PF_SPCMCOMM_CALLBACK)SpcmCallBack);
aha! this is a callback function so the calling convention of both function has to be same but ",(PF_SPCMCOMM_CALLBACK)SpcmCallBack is _cedel where as initspcm is _stdcall. never say die -
Hi, I wrote a DLL which works fine if I just include the header file and the lib file in the code that uses this DLL. However, when I try to call functions from the DLL the program crash and complain about calling conventions that do not match. Here is the bare minimum: ******* DLL Code **** Header:
typedef void (__stdcall *PF_SPCMCOMM_CALLBACK)(char *,pack_data *); SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK pfn);
CPP file:SPCMCOMM_API int InitilizeConnection(char *appname,PF_SPCMCOMM_CALLBACK p_cbfn) { ..... }
*** Client Code *** Header file:static void __cdecl SpcmCallBack (char *payload,pack_data *pd);
CPP file:HISNT spcm_hInst =LoadLibrary (__TEXT("SpcmComm.dll"));//load the dll if (spcm_hInst) { PF_SPCMCOMMINIT initspcm;//pointer to the function initspcm = (PF_SPCMCOMMINIT) GetProcAddress spcm_hInst, "InitilizeConnection"); if (initspcm) { //*****this is where the code crash ***** int result=initspcm ("TestAlertUUT",(PF_SPCMCOMM_CALLBACK)SpcmCallBack); }
Shay Harel wrote:
typedef void (__stdcall *PF_SPCMCOMM_CALLBACK)(char *,pack_data *);
Shay Harel wrote:
static void __cdecl SpcmCallBack (char *payload,pack_data *pd);
These two must have the same calling convention -
__stdcall
and__cdecl
are not compatible.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"