Building a DLL
PureBasic allows to create standard Microsoft Windows DLL (Dynamic Linked Library). The DLL code is like a PureBasic code except than no real code should be written outside of procedure. When writting a DLL, all the code is done inside procedures. When a procedure should be public (ie: accessible by third programs which will use the DLL), the keyword ProcedureDLL is used instead of Procedure. This is the only change to do to a program. When this is done, select 'Shared DLL' as output format ('Compiler Option' window in the purebasic editor or /DLL switch in command line) and a DLL called PureBasic.dll will be created in the PureBasic\Compilers\ directory. All public procedure names will be begin with an underscore '_' when called in a third program.
Example :
ProcedureDLL MyFunction() MessageRequester("Hello", "This is a PureBasic DLL !", 0) EndProcedure ; Now the client program, which use the DLL ; If OpenLibrary(0, "PureBasic.dll") CallFunction(0, "_MyFunction") CloseLibrary(0) EndIf
For advanced user only: there is 4 special procedures which are called automatically by Windows when one of the following events happen:
- DLL is attached to a new process
- DLL is detached from a process
- DLL is attached to a new thread
- DLL is detached from a thread
To handle that, it's possible to declare 4 special procedures called: AttachProcess(), DetachProcess(), AttachThread() and DetachThread(). This means than
these 4 procedures names are reserved and can't be used by the programmer for other purposes.
Example :
DLLSample.pb