Using Visual Prolog DLL’s from other languages

This directory contains several examples showing how to use Visual Prolog DLL’s from other languages (VB, C, Delphi, Java).

All examples uses the same Visual Prolog DLL’s that are in the DLLS directory, so these have to be compiled before any of these examples can run.

Common notes for all languages

If a string (or other term stored in Prolog GStack) is returned from a DLL to the calling program then the calling program should release GStack memory. If, for example, a program calls getString predicate in a DLL, which returns a string, then the calling program should have the following structure:

dll_mark_gstack(Stack);
getString(String);
... // saving String in variable of calling program
dll_release_gstack(Stack);

See memory management.

Examples

The examples are organized in subdirectories corresponding to the programming languages:

Borland Delphi main programs using Visual Prolog DLL's.

NONGUI: Delphi project uses a non-GUI Visual Prolog DLL.

USEVPI: Delphi project uses VPI based Visual Prolog DLLs.

Java (Microsoft and Sun) main programs using a non-GUI Visual Prolog DLL.

MSVJ example: Microsoft Visual Java program, which uses a non-GUI Visual Prolog DLL.

SunJava example: Sun Java program, which uses a non-GUI Visual Prolog DLL.

Microsoft Visual Basic main programs using Visual Prolog DLL's

NONGUI: Microsoft Visual Basic project uses a non-GUI Visual Prolog DLL.

USEVPI: Microsoft Visual Basic project uses VPI based Visual Prolog DLLs.

Microsoft Visual C++ main program using Visual Prolog DLL's

NONGUI: Visual Prolog project with MSVC as Main Program, which uses a non-GUI Visual Prolog DLL.

USEVPI: Microsoft Visual C++ MFC project using VPI based Visual Prolog DLLs.

ActiveX: Microsoft Visual C++ MFC ActiveX project using VPI based Visual Prolog DLLs.

 

Notes for DLLs

No fail and no errors in exported predicates

Predicate, which is exported from a Prolog DLL should be declared as procedure. All possible run time errors should be trapped by trap predicate.

Exporting predicates

The predicates to be called from outside a DLL must be defined as GLOBAL PREDICATES. The predicate names MUST be added to the EXPORT section of the DLL's module-definition (.DEF) file. The following language calling conventions should be used:

         DLL Platform            Calling Convention
 
         32-bit Windows          language stdcall
         16-bit Windows          language pascal

Managing the DLL's Global Stack

Releasing of the DLL's GStack is done by mem_MarkGStack and mem_ReleaseGStack predicates inside DLL. In an application releasing of the DLL's GStack should be done by dll_mark_gstack and dll_release_gstack functions that should surround each call of DLL's predicates, which return output variables allocated on DLL's GStack. Memory management predicates should be included into DLL if there is no output argument with memory allocation.

In Visual Prolog DLLs these GStack releasing predicates are defined like this:

global predicates
dll_mark_gstack(stackmark) - procedure (o) language stdcall
dll_release_gstack(stackmark) - procedure (i) language stdcall
clauses
dll_mark_gstack(STACKMARK):- STACKMARK=mem_MarkGStack().
dll_release_gstack(STACKMARK):-mem_ReleaseGStack(STACKMARK).

Predicates mem_MarkGStack and mem_ReleaseGStack are parts of the Prolog run-time library. They are declared in PDCRUNT.PRE. It will be included into your project if you select PDCRUNT in the Application Expert's Other Tools dialog.

Link the DLL

Visual Prolog DLLs can be linked to a project via import library (statically) or can be loaded dynamically in run time.