Discussions related to Visual Prolog
alfonsgp
Posts: 5
Joined: 19 Aug 2023 21:05

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

Unread post 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);         } }
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

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

Unread post 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. ...
Regards Thomas Linder Puls
PDC
alfonsgp
Posts: 5
Joined: 19 Aug 2023 21:05

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

Unread post 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.
alfonsgp
Posts: 5
Joined: 19 Aug 2023 21:05

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

Unread post 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
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

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

Unread post by Thomas Linder Puls »

Something as useful (forgot it before):

Code: Select all

class export     predicates     exportedPredicate : () language stdcall as "exportedPredicate".   end class export
Regards Thomas Linder Puls
PDC
alfonsgp
Posts: 5
Joined: 19 Aug 2023 21:05

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

Unread post 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!
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

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

Unread post by Thomas Linder Puls »

Regards Thomas Linder Puls
PDC
alfonsgp
Posts: 5
Joined: 19 Aug 2023 21:05

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

Unread post by alfonsgp »

Really cool.
Thank you very much, Mr Puls!
Post Reply