Page 1 of 1

Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 19 Aug 2023 21:16
by alfonsgp
Hi, I am using this code from Visual C++, LoadLibrary does load the library well, GetProcAddress finds the function inside the DLL, but when I call to the pointer-to-function, the application crashes.

What am I doing wrong? Typedef declaration maybe? Is there any similar working sample for Visual C++?

Here is the code:

Code: Select all

typedef void (__stdcall * LPFNEXPORTEDPREDICATE)(void);   void CMFCApplication3View::OnBnClickedLoadPrologDll() {         HMODULE hMod = 0x0000;         wchar_t wszDLLPath[256];         char szFunctionName[100];         LPFNEXPORTEDPREDICATE lpfnExportedPredicate = NULL;           memset(wszDLLPath, 0, sizeof(wszDLLPath));         wcscpy(wszDLLPath, _T("libProlog.dll"));         strcpy(szFunctionName, "exportedPredicate");         hMod = LoadLibrary(wszDLLPath);         if (hMod)         {                 lpfnExportedPredicate = (LPFNEXPORTEDPREDICATE)GetProcAddress(hMod, (LPCSTR)szFunctionName);                 if (lpfnExportedPredicate)                 {                         lpfnExportedPredicate(); //Crashes here                 }                 FreeLibrary(hMod);         } }

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 21 Aug 2023 21:24
by Thomas Linder Puls
In that case the Visual Prolog predicate should be declared with 'language stdcall':

Code: Select all

class something   predicates     exportedPredicate : () language stdcall. ...

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 23 Aug 2023 23:01
by alfonsgp
I have changed the export.cl file with stdcall, but now the GetProcAddress function stopped working from the client side. Cannot find "exportedPredicate" entry point.
It seems like there is a kind of "decorated names" aka "mangling" maybe using the class name as a prefix.

The export.cl file is as follows:

Code: Select all

class export     predicates     exportedPredicate : () language stdcall.   end class export
I have supressed also the "open core" line.

From the C language caller (client part) I have tried GetProcAddress with function names like "export.exportedPredicate" and export::exportedPredicate" but without success.

My question is: Which is the right function name that I have to search for?

Thanks.

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 23 Aug 2023 23:19
by alfonsgp
I've found the way: I opened the binary file of the DLL and browsed for "exportedPredicate and found the real name as "?exportedPredicate@export@@SGXXZ"

The source code now works and it is like this:

Code: Select all

void CMFCApplication3View::OnBnClickedLoadPrologDll() {         HMODULE hMod = 0x0000;         wchar_t wszDLLPath[256];         char szFunctionName[100];         LPFNEXPORTEDPREDICATE lpfnExportedPredicate = NULL;           memset(wszDLLPath, 0, sizeof(wszDLLPath));         wcscpy(wszDLLPath, _T("libTestProlog.dll"));         strcpy(szFunctionName, "?exportedPredicate@export@@SGXXZ");         hMod = LoadLibrary(wszDLLPath);         if (hMod)         {                 lpfnExportedPredicate = (LPFNEXPORTEDPREDICATE)GetProcAddress(hMod, (LPCSTR)szFunctionName);                 if (lpfnExportedPredicate)                 {                         lpfnExportedPredicate();                 }                 FreeLibrary(hMod);         } }
Is there any way to use the equivalent of extern "C" to avoid the mangling

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 24 Aug 2023 14:51
by Thomas Linder Puls
Something as useful (forgot it before):

Code: Select all

class export     predicates     exportedPredicate : () language stdcall as "exportedPredicate".   end class export

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 24 Aug 2023 19:59
by alfonsgp
Great. Now it works perfectly!

Is there any documentation somewhere to specify prototypes of pointer-to-function to pass string, integer, float parameters from C to Prolog DLL?

Thank you very much!

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 25 Aug 2023 7:11
by Thomas Linder Puls

Re: Problem calling from VisualC++ 32 bits to a Visual Prolog DLL

Posted: 26 Aug 2023 17:55
by alfonsgp
Really cool.
Thank you very much, Mr Puls!