# How to use the Ghostscript Dynamic Link Library (DLL)

## Table of contents

- What is the Ghostscript DLL?
- Platform-independent DLL functions
  - `gsdll_revision()`
  - `gsdll_init()`
  - `gsdll_execute_begin()`
  - `gsdll_execute_cont()`
  - `gsdll_execute_end()`
  - `gsdll_exit()`
  - `gsdll_lock_device()`
- Callback function
- Ghostscript DLL device for OS/2
  - `gsdll_get_bitmap()`
  - Example DLL usage for OS/2
- Ghostscript DLL device for MS Windows
  - `gsdll_copy_dib()`
  - `gsdll_copy_palette()`
  - `gsdll_draw()`
  - `gsdll_get_bitmap_row()`
- Ghostscript DLL Device for 16-bit MS Windows

For other information, see the Ghostscript overview.

WARNING: The API described in this document is obsolete and will be removed in the future. The current Ghostscript Interpreter API is described in API.htm.

## What is the Ghostscript DLL?

For the OS/2, Win16 and Win32 platforms, Ghostscript is built as a dynamic link library (DLL), and to provide the interface described in the usage documentation, a smaller independent executable (`.EXE`) loads this DLL, which provides all the interaction with the windowing system, including image windows and, if necessary, a text window. This document describes the DLL interface, which consists of eight main functions, seven platform-independent ones provided by the DLL and one, the callback function, provided by the caller. The DLL provides some other platform-specific functions for display devices.

The DLL's name and characteristics differ among the three platforms:

- The OS/2 DLL `GSDLL2.DLL` has MULTIPLE NONSHARED data segments and can be called by multiple programs simultaneously.
- The Win32 DLL `GSDLL32.DLL` has MULTIPLE NONSHARED data segments. Under Win32s it can be used by only one program at a time, but under Windows 95/98 or Windows NT it can be called by multiple programs simultaneously.
- The Win16 DLL `GSDLL16.DLL` is a large-memory model DLL with far static data. Due to the limitations of 16-bit MS Windows -- Windows 3.n -- the DLL can be used by only one program at a time.

The source for the executable is in `dp`*.* (OS/2) and `dw`*.* (Windows). See these source files for examples of how to use the DLL.

## Platform-independent DLL functions

The seven functions provided by the DLL are

- `int GSDLLAPI gsdll_revision(char **product, char **copyright, long *gs_revision, long *gs_revisiondate)`
- `int GSDLLAPI gsdll_init(GSDLL_CALLBACK callback, HWND hwnd, int argc, char *argv[]);`
- `int GSDLLAPI gsdll_execute_begin(void);`
- `int GSDLLAPI gsdll_execute_cont(const char *str, int len);`
- `int GSDLLAPI gsdll_execute_end(void);`
- `int GSDLLAPI gsdll_exit(void);`
- `int GSDLLAPI gsdll_lock_device(unsigned char *device, int flag);`

  where `GSDLLAPI` is defined under OS/2 as

  > `#define GSDLLAPI`

  and under MS Windows as

  > `#define GSDLLAPI CALLBACK _export`

### `gsdll_revision()`

> This function returns the revision numbers and strings of the Ghostscript DLL; you should call it before `gsdll_init()` to make sure that the correct version of the Ghostscript DLL has been loaded. For example

> >     char *product;
> >     char *copyright;
> >     long revision;
> >     long revisiondate;
> >     gsdll_revision(&product, &copyright, &revision, &revisiondate);

> You may use `NULL` pointers if you do not want a particular value.

### `gsdll_init()`

> `gsdll_init()` must be called after loading the DLL and before executing any Ghostscript commands. The arguments are the address of the callback function, a parent window handle, the count of arguments and an array of pointers to the arguments. For example

> >     char *argv[5];
> >     argv[0] = "gswin.exe";
> >     argv[1] = "-Ic:\\gs;c:\gs\\fonts";
> >     argv[2] = "-dNOPAUSE",
> >     argv[3] = "-sDEVICE=djet500",
> >     argv[4] = NULL;
> >     argc = 4;

> >     code = gsdll_init(gsdll_callback, hwnd, argc, argv);

> `hwnd` is used as the parent window handle for any windows created by Ghostscript. `hwnd` may be `NULL` if the caller has no windows, but if it is `NULL`, you should avoid using devices which may open windows.

> A return code of 0 (zero) indicates no errors, and the code may now call `gsdll_execute_begin()` or `gsdll_exit()`. If the return value is non-zero then `gsdll_exit()` must not be called.

> A return value of `GSDLL_INIT_QUIT` indicates that one of the command line files or arguments called "`quit`", or that Ghostscript was reading stdin and reached end-of-file. This is not an error. `gsdll_exit()` must not be called.

> A return value of `GSDLL_INIT_IN_USE` indicates that the DLL is in use by another application (Windows 3.1 only). The DLL should be immediately unloaded (or the caller terminated). `gsdll_exit()` must not be called.

### `gsdll_execute_begin()`

> This must be called after `gsdll_init()` and before `gsdll_execute_cont()`.

### `gsdll_execute_cont()`

> After successfully calling `gsdll_init()` and `gsdll_execute_begin()`, commands may be given to Ghostscript with `gsdll_execute_cont()`. Examples are:

> >     char *command = "1 2 add == flush\n";
> >     code = gsdll_execute_cont(command, strlen(command));
> >     command = "qu"
> >     code = gsdll_execute_cont(command, strlen(command));
> >     command = "it\n"
> >     code = gsdll_execute_cont(command, strlen(command));

> > Return codes from `gsdll_execute_cont()`
> > Code |  | Status
> > 0 |  | No errors
> > < 0 |  | Error
> > <= -100 |  | "`quit`" has been executed, or fatal error. `gsdll_exit()` must then be called: do not call `gsdll_execute_end()`.

> `gsdll_execute_cont()` does not flush stdio, so if you want to see output from Ghostscript you must do this explicitly as shown in the example above.

> When executing a string with `gsdll_execute_cont()`, `currentfile` is the input from `gsdll_execute_cont()`. Reading from `%stdin` uses the callback.

> Please note that there is a 64 KB length limit on the passed string. If you have more than 65535 bytes of input to process at a given time, it must be split into separate smaller buffers, each passed on a separate call.

### `gsdll_execute_end()`

> If `gsdll_execute_cont()` did not return an error, then `gsdll_execute_end()` must be called after `gsdll_execute_cont()` and before `gsdll_exit()`.

### `gsdll_exit()`

> Call gsdll_exit() to terminate the Ghostscript DLL. It must be called if a fatal error has occurred; see the return value of `gsdll_execute_cont()`. After calling `gsdll_exit()`, there are two options:
> - Unload the DLL, either by terminating the application or by calling `DosFreeModule` (OS/2) or `FreeLibrary` (MS Windows).
> - Call `gsdll_init()` again to restart Ghostscript.

### `gsdll_lock_device()`

> Since the caller may be multithreaded, a lock is needed to control access to the display device, and `gsdll_lock_device()` provides that locking.

> >     int gsdll_lock_device(unsigned char *device, int flag);
> >      /* Lock the device if flag = TRUE */
> >      /* Unlock the device if flag = FALSE */
> >      /* device is a pointer to Ghostscript os2dll or mswindll device */
> >      /* from GSDLL_DEVICE message. */
> >      /* Return value is the lock count. */

> > Locking and unlocking devices
> > To lock a device |  | `gsdll_lock_device(device, 1)`;
> > To unlock a device |  | `gsdll_lock_device(device, 0)`;

> This function is typically used to lock the device while repainting a window or copying the device bitmap to the clipboard. Ghostscript may draw into the device bitmap or update the palette entries while the device is locked by the caller, but locking the device prevents the Ghostscript DLL from closing the device or changing its size or depth.

> Under OS/2, Windows 95/98 and Windows NT, this lock is implemented using a mutual exclusion semaphore (mutex). The return value is the lock count, which is either 0 ("unlocked") or 1 ("locked"). The function blocks until the device is locked by the caller.

> Under Win16 or Win32s, `gsdll_lock_device()` always returns immediately with a lock count as its return value. A lock count of 2 or more is definite indication of an error, probably calling the function twice. Access to the device should be controlled by checking the Windows message queue only when the bitmap is not being accessed.

## Callback function

> As an argument to `gsdll_init()` the caller must provide a callback function which the DLL invokes for stdio and to notify the caller about device events. The function provided by the caller has this prototype:

> > `int gsdll_callback(int message, char *str, unsigned long count);`

> The Pascal calling convention is not used. An example callback function is:

> >     int
> >     gsdll_callback(int message, char *str, unsigned long count)
> >     {
> >     char *p;
> >         switch (message) {
> >             case GSDLL_STDIN:
> >                 p = fgets(str, count, stdin);
> >                 if (p)
> >                     return strlen(str);
> >                 else
> >                     return 0;
> >             case GSDLL_STDOUT:
> >                 if (str != (char *)NULL)
> >                     fwrite(str, 1, count, stdout);
> >                 return count;
> >             case GSDLL_DEVICE:
> >                 fprintf(stdout,"Callback: DEVICE %p %s\n", str,
> >                     count ? "open" : "close");
> >                 break;
> >             case GSDLL_SYNC:
> >                 fprintf(stdout,"Callback: SYNC %p\n", str);
> >                 break;
> >             case GSDLL_PAGE:
> >                 fprintf(stdout,"Callback: PAGE %p\n", str);
> >                 break;
> >             case GSDLL_SIZE:
> >                 fprintf(stdout,"Callback: SIZE %p width=%d height=%d\n", str,
> >                     (int)(count & 0xffff), (int)((count>>16) & 0xffff) );
> >                 break;
> >             case GSDLL_POLL:
> >                 return 0; /* no error */
> >             default:
> >                 fprintf(stdout,"Callback: Unknown message=%d\n",message);
> >                 break;
> >         }
> >         return 0;
> >     }

> > Messages used by callback
> > Symbol |  |  |  | Use
> > `GSDLL_STDIN` |  | 1 |  | get `count` characters to `str` from stdin, return number of characters read
> > `GSDLL_STDOUT` |  | 2 |  | put `count` characters from `str` to stdout, return number of characters written
> > `GSDLL_DEVICE` |  | 3 |  | device `str` has been opened if `count` = 1, closed if `count` = 0
> > `GSDLL_SYNC` |  | 4 |  | sync_output for device `str`
> > `GSDLL_PAGE` |  | 5 |  | output_page for device `str`
> > `GSDLL_SIZE` |  | 6 |  | resize for device `str`: LOWORD(`count`) is new `xsize`, HIWORD(`count`) is new `ysize`
> > `GSDLL_POLL` |  | 7 |  | Called from `gp_check_interrupt()` Can be used by the caller to poll the message queue. Normally returns 0. To abort `gsdll_execute_cont()`, return a non-zero error code until `gsdll_execute_cont()` returns.

## Ghostscript DLL device for OS/2

The `os2dll` device is provided in the Ghostscript DLL for use by the caller. No drawing facilities are provided by the DLL because the DLL may be loaded by a text-only (non-PM) application. The caller is notified via the `gsdll_callback()` when a new `os2dll` device is opened or closed (`GSDLL_DEVICE`), when the window should be redrawn (`GSDLL_SYNC` or `GSDLL_PAGE`) or when the bitmap size changes (`GSDLL_SIZE`). Note that more than one `os2dll` device may be opened.

### `gsdll_get_bitmap()`

> `gsdll_get_bitmap()` returns a pointer to a bitmap in BMP format. The `os2dll` device draws into this bitmap.

> >     unsigned long gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap);
> >      /* return in pbitmap the address of the bitmap */
> >      /* device is a pointer to Ghostscript os2dll device from GSDLL_DEVICE message */

> The caller can then display the bitmap however it likes, but should lock the bitmap with `gsdll_lock_device()` before painting from it, and unlock it afterwards. The bitmap address does not change until the `os2dll` device is closed; however the bitmap size and palette may change whenever the bitmap is not locked.

### Example DLL usage for OS/2

The example here shows a minimal usage of the Ghostscript DLL under OS/2. The sample callback function above is needed.

>     #define INCL_DOS
>     #include <os2.h>
>     #include <stdio.h>
>     #include "gsdll.h"

>     PFN_gsdll_init pgsdll_init;
>     PFN_gsdll_execute_begin pgsdll_execute_begin;
>     PFN_gsdll_execute_cont pgsdll_execute_cont;
>     PFN_gsdll_execute_end pgsdll_execute_end;
>     PFN_gsdll_exit pgsdll_exit;

>     HMODULE hmodule_gsdll;
>     char buf[256];

>     int
>     main(int argc, char *argv[])
>     {
>     int code;
>     APIRET rc;
>         if (!DosLoadModule(buf, sizeof(buf), "GSDLL2", &hmodule_gsdll)) {
>             fprintf(stderr, "Loaded GSDLL2\n");
>             DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_init", (PFN *)(&pgsdll_init));
>             DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_begin", (PFN *)(&pgsdll_execute_begin));
>             DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_cont", (PFN *)(&pgsdll_execute_cont));
>             DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_end", (PFN *)(&pgsdll_execute_end));
>             DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_exit", (PFN *)(&pgsdll_exit));
>         }
>         else {
>             fprintf(stderr, "Can't load GSDLL2\n");
>         }

>         code = (*pgsdll_init)(gsdll_callback, NULL, argc, argv);
>         fprintf(stdout,"gsdll_init returns %d\n", code);
>         code = (*pgsdll_execute_begin)();
>         if (code==0) {
>             while (fgets(buf, sizeof(buf), stdin)) {
>                 code = (*pgsdll_execute_cont)(buf, strlen(buf));
>                 fprintf(stdout,"gsdll_execute returns %d\n", code);
>                 if (code < 0)
>                    break;
>             }
>             if (!code)
>                 code = (*pgsdll_execute_end)();
>             code = (*pgsdll_exit)();
>             fprintf(stdout,"gsdll_exit returns %d\n", code);
>         }
>         rc = DosFreeModule(hmodule_gsdll);
>         fprintf(stdout,"DosFreeModule returns %d\n", rc);
>         return 0;
>     }

## Ghostscript DLL device for MS Windows

The `mswindll` device is provided in the Ghostscript DLL for use by the caller. The caller is notified via the `gsdll_callback()` when a new `mswindll` device is opened or closed (`GSDLL_DEVICE`), when the window should be redrawn (`GSDLL_SYNC` or `GSDLL_PAGE`) or when the bitmap size changes (`GSDLL_SIZE`). Note that more than one `mswindll` device may be opened.

Four DLL functions are available to use the `mswindll` device.

### `gsdll_copy_dib()`

> Copy the `mswindll` bitmap to the clipboard.

> >     HGLOBAL GSDLLAPI gsdll_copy_dib(unsigned char *device);
> >      /* make a copy of the device bitmap and return shared memory handle to it */
> >      /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */

### `gsdll_copy_palette()`

> Copy the `mswindll` palette to the clipboard.

> >     HPALETTE GSDLLAPI gsdll_copy_palette(unsigned char *device);
> >     /* make a copy of the device palette and return a handle to it */
> >     /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */

### `gsdll_draw()`

> Display output from the `mswindll` device. The caller should create a window and call `gsdll_draw()` in response to the `WM_PAINT` message. The device context `hdc` must be for a device because `SetDIBitsToDevice()` is used.

> >     void GSDLLAPI gsdll_draw(unsigned char *device, HDC hdc,
> >                                       LPRECT dest, LPRECT src);
> >     /* copy the rectangle src from the device bitmap */
> >     /* to the rectangle dest on the device given by hdc */
> >     /* hdc must be a device context for a device (NOT a bitmap) */
> >     /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */

### `gsdll_get_bitmap_row()`

> Get a BMP header, a palette, and a pointer to a row in the bitmap. This function exists to allow the bitmap to be copied to a file or structured storage without the overhead of having two copies of the bitmap in memory at the same time.

> Ghostscript can change the palette while the device is locked. Do not call this function while Ghostscript is busy.

> >     int GSDLLAPI gsdll_get_bitmap_row(unsigned char *device, LPBITMAPINFOHEADER pbmih,
> >         LPRGBQUAD prgbquad, LPBYTE *ppbyte, unsigned int row)
> >     /* If pbmih nonzero, copy the BITMAPINFOHEADER.
> >      * If prgbquad nonzero, copy the palette.
> >      *   number of entries copied is given by pbmih->biClrUsed
> >      * If ppbyte nonzero, return pointer to row.
> >      *   pointer is only valid while device is locked
> >      */

## Ghostscript DLL Device for 16-bit MS Windows

This platform has the most problems of the three. Support for it may be dropped in future.

The Win16 DLL `GSDLL16.DLL` is a large-memory model DLL with far static data. Due to the limitations of 16-bit MS Windows, the DLL can be used by only one program at a time.

However, `GSDLL16` is marked as having SINGLE SHARED data segments, allowing multiple applications to load it with no error indication. (The DLL wouldn't load at all if MULTIPLE NONSHARED was used). Nonetheless, it cannot be used by more than one application at a time, so applications loading `GSDLL16` should check the return value of `gsdll_init()`: if this value is non-zero, then `GSDLL16` is already in use by another application and should not be used: `GSDLL16` should be unloaded immediately using `FreeLibrary()`, or the calling program should quit without attempting to use the library..

The segmented architecture of the Intel 80286 causes the usual amount of grief when using `GSDLL16`. Because the callback is called from the DLL, which is using a different data segment, the callback must be declared as `_far _export`:

>     int _far _export gsdll_callback(int message, char *str, unsigned long count);

Instead of giving `gsdll_init()` the address of `gsdll_callback()`, it should instead be given the address of a thunk created by `MakeProcInstance`. This thunk changes the data segment back to that used by the caller:

>     FARPROC lpfnCallback;
>     lpfnCallback = (FARPROC)MakeProcInstance((FARPROC)gsdll_callback, hInstance);
>     code = (*pgsdll_init)((GSDLL_CALLBACK)lpfnCallback, NULL, argc, argv);
>     if (!code) {
>         fprintf(stderr, "GSDLL16 is already in use\n");
>         return -1;
>     }

Copyright © 2000-2006 Artifex Software, Inc. All rights reserved.

This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of that license. Refer to licensing information at http://www.artifex.com/ or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.

Ghostscript version 9.06, 8 August 2012