# Building DLLs

You can write your own DLL's using Arity/Prolog32. This section is a guide.

## Exported names

A Prolog predicate has two names, one for Prolog, one for C:

:- public pred/3:system(det=predname(int,int,int)).

The name pred_3 is used by Prolog calls. To call it from 'C', _predname@12 is used.

Then in your .DEF file you can have:

EXPORTS
predname
pred_3          CONSTANT

Win32 will figure out that predname really refers to _predname@12

Every Prolog program must call initprolog during initialization and endprolog prior to termination. (Multiple threaded programs call PrologInitProcess and PrologEndProcess for process initialization and termination and PrologInitThread and PrologEndThread for thread initialization and termination.) These calls manage the memory areas use to execute Prolog code. We call these areas the Prolog stacks.

Initialization and termination of Prolog can be done either by the executable module or by a DLL. It is usually done by the executable.

It is now possible to have visible declarations in DLLs. These declarations will be added when the DLL is loaded (either at run-time or load-time) and will be removed when the DLL is unloaded. Furthermore they may be added and removed by the predicate dll_visi/2.

In order for this to work, the DLL's must be built as follows:

• If your DLL does use 'C', compile and link in the module INITDLL.C. You must compile with -DTARG_WIN32 for Win32.

You can change INITDLL.C to do other initialization tasks. To enable visible autoloading you must call at initialization time

DllVisibles(1)

and call at termination time

DllVisibles(0).

You should never do one without the other. Otherwise, the program may experience a protection fault.

Note that you cannot execute any Prolog code at initialization time because you cannot be sure that PrologInitProcess has been called.

• Note that you do not need to export any names that are visible - you only need to export a name if it is going to be called by compiled code.
• If you wish to use the dll_visi/2 predicate with this DLL you must have an export as follows:
EXPORTS
VISITABLE = visihdr_start CONSTANT

## "Visible only" DLL's

Note that the predicates declared visible in a DLL need not be defined in a DLL. Therefore, it is possible to have a DLL that is comprised solely of visible declarations of predicates defined elsewhere (either in ARITY32.DLL or in other DLL's).

One possible use for this is for easy debugging: have the additional visibles necessary for debugging in a separate DLL.