/* Copyright (C) 1994-1996, Russell Lang. All rights reserved. This file is part of Aladdin Ghostscript. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or distributor accepts any responsibility for the consequences of using it, or for whether it serves any particular purpose or works at all, unless he or she says so in writing. Refer to the Aladdin Ghostscript Free Public License (the "License") for full details. Every copy of Aladdin Ghostscript must include a copy of the License, normally in a plain ASCII text file named PUBLIC. The License grants you the right to copy, modify and redistribute Aladdin Ghostscript, but only under certain conditions described in the License. Among other things, the License requires that the copyright notice and this notice be preserved on all copies. */ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This file, dll.txt, describes how to use the Ghostscript Dynamic Link Library. For an overview of Ghostscript and a list of the documentation files, see README. ================================== DLL.TXT 1996-09-05 ================================== For the OS/2, Win16 and Win32 platforms, Ghostscript is compiled as a dynamic link library. To provide the interface described in use.txt, a smaller EXE loads this Ghostscript DLL. This EXE provides image windows and if necessary, a text window. The source for the EXE is in dp*.* for OS/2 and dw*.* for Windows. Refer to these source files for examples of DLL usage. This document document describes the DLL interface. 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. The Win32 DLL (GSDLL32.DLL) has MULTIPLE NONSHARED data segments. Under Win32s it can be used by only one program at a time. Under Windows 95 or NT it can be called by multiple programs. The OS/2 DLL (GSDLL2.DLL) has MULTIPLE NONSHARED data segments and can be called by multiple programs. The interface to the DLL consists of 8 main functions, 1 provided by the caller and the other 7 by the DLL. Some other platform dependant functions are provided by the DLL for display devices. ============= 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); For OS/2, GSDLLAPI is defined as #define GSDLLAPI For Win32 and Win16, GSDLLAPI is defined as #define GSDLLAPI CALLBACK _export ---------------- gsdll_revision() ---------------- This returns the revision numbers and strings of the Ghostscript DLL. This function may be called before gsdll_init(). An example: char *product; char *copyright; long revision; long revisiondate; gsdll_revision(&product, ©right, &revision, &revisiondate); NULL pointers may be used if you do not want a particular value. It is recommended that this function be called before gsdll_init() to make sure that the correct version of the Ghostscript DLL has been loaded. ------------ gsdll_init() ------------ The function 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 does not have any windows, but if NULL you should avoid using devices which may open windows. If the return code is zero then there is no error. gsdll_execute_begin() or gsdll_exit() may now be called. 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 GS was reading stdin and reached EOF. 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 code is zero if there are no errors. return code is less than zero if an error has occured. return code is less than or equal to -100 if "quit" has been executed or a fatal error has occured. gsdll_exit() must then be called - do not call gsdll_execute_end(). gsdll_execute_cont does not flush stdio - 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 will use the callback. ------------------- 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 ---------- To terminate the Ghostscript DLL, gsdll_exit() is called. This must be called if a fatal error has occured (see return value of gsdll_execute_cont). After calling gsdll_exit(), there are two options: 1. Unload the DLL, either by terminating the application or by calling DosFreeModule (OS/2) or FreeLibrary (MS-Windows). 2. 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. This is accessed via the following function. 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. */ To lock the device use gsdll_lock_device(device, 1); To unlock the device use gsdll_lock_device(device, 0); Locking the device prevents the Ghostscript DLL from closing the device or changing the device size or depth. Ghostscript may draw into the device bitmap or update the palette entries while the device is locked by the caller. This function is typically used to lock the device while repainting a window, or copying the device bitmap to the clipboard. Under OS/2, Win95 and WinNT, this lock is implemented using a mutual exclusion semaphore (mutex). The return value is the lock count, which will be either 0 or 1, for unlocked and locked respectively. This function will block until the device is locked by the caller. Under Win16 or Win32s, gsdll_lock_device will always return immediately, giving a lock count as the return value. If a lock value of 2 or more is returned, beware! Access to the device should be controlled by checking the windows message queue only when the bitmap is not being accessed. ================= Callback function ================= A callback function must be provided by the caller and given as an argument to gsdll_init(). The callback function is called by the DLL for stdio and to notify the caller about device events. The function provided by the caller has the following 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; } The messages used by the callback are: #define GSDLL_STDIN 1 /* get count characters to str from stdin */ /* return number of characters read */ #define GSDLL_STDOUT 2 /* put count characters from str to stdout*/ /* return number of characters written */ #define GSDLL_DEVICE 3 /* device = str has been opened if count=1 */ /* or closed if count=0 */ #define GSDLL_SYNC 4 /* sync_output for device str */ #define GSDLL_PAGE 5 /* output_page for device str */ #define GSDLL_SIZE 6 /* resize for device str */ /* LOWORD(count) is new xsize */ /* HIWORD(count) is new ysize */ #define GSDLL_POLL 7 /* Called from gp_check_interrupt */ /* Can be used by 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 */ ========================== Example DLL usage for OS/2 ========================== The following example shows a minimal usage of the Ghostscript DLL. The example callback function above is needed. #define INCL_DOS #include #include #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 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 will be 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. One DLL function is available for accessing the os2dll device: ---------------- gsdll_get_bitmap ---------------- The following function 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 will not change until the os2dll device is closed, however the bitmap size and palette may change at any time the bitmap is not locked. ===================================== Ghostscript DLL device for MS-Windows ===================================== The mswindll device is provided in the Ghostscript DLL for use by the caller. The caller will be 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 extra DLL functions are available for accessing the mswindll device: -------------- gsdll_copy_dib -------------- This function is commonly used when copying the mswindll bitmap to the clipboard. /* 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 */ HGLOBAL GSDLLAPI gsdll_copy_dib(unsigned char *device); ------------------ gsdll_copy_palette ------------------ This function can be used when copying the mswindll palette to the clipboard. /* make a copy of the device palette and return a handle to it */ /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */ HPALETTE GSDLLAPI gsdll_copy_palette(unsigned char *device); ---------- gsdll_draw ---------- This function is used for displaying 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. /* 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 */ void GSDLLAPI gsdll_draw(unsigned char *device, HDC hdc, LPRECT dest, LPRECT src); -------------------- gsdll_get_bitmap_row -------------------- The following function returns a BMP header, a palette and a pointer to a row in the bitmap. int GSDLLAPI gsdll_get_bitmap_row(unsigned char *device, LPBITMAPINFOHEADER pbmih, LPRGBQUAD prgbquad, LPBYTE *ppbyte, unsigned int row) /* Copy bitmap * 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 * GS can change the palette while the device is locked. * Do not call this function while GS is busy. * * 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. */ ================ MS-Windows 16bit ================ 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 used by only one program at a time. However, GSDLL16 is marked as having SINGLE SHARED data segments which allows multiple applications to load GSDLL16. (The DLL wouldn't load at all if MULTIPLE NONSHARED was used). Applications loading GSDLL16 should check the return value of gsdll_init(). If it 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 caller program should terminate. The segmented architecture of the 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; } ================== Changes 1996-09-05 ================== Ghostscript locks the device just before closing it, to prevent closing the device while the caller is drawing from it. Added gsdll_get_bitmap_row() to MS-Windows. ================== Changes 1996-07-02 ================== gsdll_init() error code changed. ================== Changes 1996-05-30 ================== gsdll_init() error code changed. ================== Changes 1996-05-16 ================== Added HWND as a parameter to gsdll_init(). THIS IS A NON BACKWARD COMPATIBLE CHANGE. Documented that gsdll_execute_end() must not be called if gsdll_execute_cont() returns with error. ================== Changes 1996-03-16 ================== New call back message GSDLL_POLL. Removed OS/2 gsdll_lock_bitmap(). Replaced by gsdll_lock_device(). Add gsdll_lock_device to OS/2 and MS-Windows. For non multi-threaded environments (Win16, Win32s) this will return a lock count but return immediately. For multi-threaded environments this will maintain a lock using a mutex (which may block). Changed gsdll_init() to take argc and argv. Now supports the full range of Ghostscript command line options. *** OS/2 has't been tested yet. ================== Changes 1995-07-26 ================== gsdll_revision needs to be checked before using other APIs, to protect against finding a later version of the GS DLL with changed APIs. gsdll_execute removed. gsdll_execute_begin/cont/end added. Keywords can now be split across calls to gsdll_execute_cont. DLL input can now be done without using the stdin callback Now use GSDLLAPI instead of WINAPI Under Windows, the calling convention is CALLBACK, not pascal. For Win32 this is _stdcall, for Win16 this is _far _pascal. Original version 1994-07-03 ==================== /* end of dll.txt */ ====================