DLL.htm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>How to use the Ghostscript Dynamic Link Library (DLL)</title>
  5. <!-- $Id: DLL.htm,v 1.43 2005/10/20 19:46:23 ray Exp $ -->
  6. <!-- Originally: dll.txt -->
  7. <link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
  8. </head>
  9. <body>
  10. <!-- [1.0 begin visible header] ============================================ -->
  11. <!-- [1.1 begin headline] ================================================== -->
  12. <h1>How to use the Ghostscript Dynamic Link Library (DLL)</h1>
  13. <!-- [1.1 end headline] ==================================================== -->
  14. <!-- [1.2 begin table of contents] ========================================= -->
  15. <h2>Table of contents</h2>
  16. <ul>
  17. <li><a href="#DLL">What is the Ghostscript DLL?</a>
  18. <li><a href="#Common_functions">Platform-independent DLL functions</a>
  19. <ul>
  20. <li><a href="#revision"><b><tt>gsdll_revision()</tt></b></a>
  21. <li><a href="#init"><b><tt>gsdll_init()</tt></b></a>
  22. <li><a href="#execute_begin"><b><tt>gsdll_execute_begin()</tt></b></a>
  23. <li><a href="#execute_cont"><b><tt>gsdll_execute_cont()</tt></b></a>
  24. <li><a href="#execute_end"><b><tt>gsdll_execute_end()</tt></b></a>
  25. <li><a href="#exit"><b><tt>gsdll_exit()</tt></b></a>
  26. <li><a href="#lock_device"><b><tt>gsdll_lock_device()</tt></b></a>
  27. </ul>
  28. <li><a href="#Callback">Callback function</a>
  29. <li><a href="#OS2_device">Ghostscript DLL device for OS/2</a>
  30. <ul>
  31. <li><a href="#OS2_bmp"><b><tt>gsdll_get_bitmap()</tt></b></a>
  32. <li><a href="#OS2_example">Example DLL usage for OS/2</a>
  33. </ul>
  34. <li><a href="#Win_device">Ghostscript DLL device for MS Windows</a>
  35. <ul>
  36. <li><a href="#Win_copydib"><b><tt>gsdll_copy_dib()</tt></b></a>
  37. <li><a href="#Win_copypalette"><b><tt>gsdll_copy_palette()</tt></b></a>
  38. <li><a href="#Win_draw"><b><tt>gsdll_draw()</tt></b></a>
  39. <li><a href="#Win_get_row"><b><tt>gsdll_get_bitmap_row()</tt></b></a>
  40. </ul>
  41. <li><a href="#Win16">Ghostscript DLL Device for 16-bit MS Windows</a>
  42. </ul>
  43. <!-- [1.2 end table of contents] =========================================== -->
  44. <!-- [1.3 begin hint] ====================================================== -->
  45. <p>For other information, see the <a href="Readme.htm">Ghostscript
  46. overview</a>.
  47. <p>
  48. <b>WARNING: The API described in this document is obsolete and will
  49. be removed in the future.</b>
  50. The current Ghostscript Interpreter API
  51. is described in <a href="API.htm">API.htm</a>.
  52. <!-- [1.3 end hint] ======================================================== -->
  53. <hr>
  54. <!-- [1.0 end visible header] ============================================== -->
  55. <!-- [2.0 begin contents] ================================================== -->
  56. <h2><a name="DLL"></a>What is the Ghostscript DLL?</h2>
  57. <p>
  58. For the OS/2, Win16 and Win32 platforms, Ghostscript is built as a dynamic
  59. link library (DLL), and to provide the interface described in the <a
  60. href="Use.htm">usage documentation</a>, a smaller independent executable
  61. (<b><tt>.EXE</tt></b>) loads this DLL, which provides all the interaction
  62. with the windowing system, including image windows and, if necessary, a
  63. text window. This document describes the DLL interface, which consists of
  64. eight main functions, <a href="#functions">seven platform-independent
  65. ones</a> provided by the DLL and one, <a href="#Callback">the callback
  66. function</a>, provided by the caller. The DLL provides some other
  67. platform-specific functions for display devices.
  68. <p>
  69. The DLL's name and characteristics differ among the three platforms:
  70. <ul>
  71. <li>The <a href="#OS2_device">OS/2 DLL <b><tt>GSDLL2.DLL</tt></b></a> has
  72. MULTIPLE NONSHARED data segments and can be called by multiple programs
  73. simultaneously.
  74. <li>The <a href="#Win_device">Win32 DLL <b><tt>GSDLL32.DLL</tt></b></a> has
  75. MULTIPLE NONSHARED data segments. Under Win32s it can be used by only one
  76. program at a time, but under Windows 95/98 or Windows NT it can be called by
  77. multiple programs simultaneously.
  78. <li>The <a href="#Win16">Win16 DLL <b><tt>GSDLL16.DLL</tt></b></a> is a
  79. large-memory model DLL with far static data. Due to the limitations of
  80. 16-bit MS Windows -- Windows 3.n -- the DLL can be used by only one program
  81. at a time.
  82. </ul>
  83. <p>
  84. The source for the executable is in <b><tt>dp</tt></b>*.* (OS/2) and
  85. <b><tt>dw</tt></b>*.* (Windows). See these source files for examples of
  86. how to use the DLL.
  87. <hr>
  88. <h2><a name="Common_functions"></a>Platform-independent DLL functions</h2>
  89. <p>
  90. The seven functions provided by the DLL are
  91. <ul>
  92. <li><b><tt>int GSDLLAPI <a href="#revision">gsdll_revision</a>(char **product, char **copyright, long *gs_revision, long *gs_revisiondate)</tt></b>
  93. <li><b><tt>int GSDLLAPI <a href="#init">gsdll_init</a>(GSDLL_CALLBACK callback, HWND hwnd, int argc, char *argv[]);</tt></b>
  94. <li><b><tt>int GSDLLAPI <a href="#execute_begin">gsdll_execute_begin</a>(void);</tt></b>
  95. <li><b><tt>int GSDLLAPI <a href="#execute_cont">gsdll_execute_cont</a>(const char *str, int len);</tt></b>
  96. <li><b><tt>int GSDLLAPI <a href="#execute_end">gsdll_execute_end</a>(void);</tt></b>
  97. <li><b><tt>int GSDLLAPI <a href="#exit">gsdll_exit</a>(void);</tt></b>
  98. <li><b><tt>int GSDLLAPI <a href="#lock_device">gsdll_lock_device</a>(unsigned char *device, int flag);</tt></b>
  99. <p>
  100. where <b><tt>GSDLLAPI</tt></b> is defined under OS/2 as
  101. <blockquote><b><tt>
  102. #define GSDLLAPI
  103. </tt></b></blockquote>
  104. <p>
  105. and under MS Windows as
  106. <blockquote><b><tt>
  107. #define GSDLLAPI CALLBACK _export
  108. </tt></b></blockquote>
  109. </ul>
  110. <h3><a name="revision"></a><b><tt>gsdll_revision()</tt></b></h3>
  111. <blockquote>
  112. This function returns the revision numbers and strings of the Ghostscript
  113. DLL; you should call it before <b><tt>gsdll_init()</tt></b> to make sure
  114. that the correct version of the Ghostscript DLL has been loaded. For
  115. example
  116. <blockquote>
  117. <pre>char *product;
  118. char *copyright;
  119. long revision;
  120. long revisiondate;
  121. gsdll_revision(&amp;product, &amp;copyright, &amp;revision, &amp;revisiondate);
  122. </pre></blockquote>
  123. <p>
  124. You may use <b><tt>NULL</tt></b> pointers if you do not want a particular
  125. value.
  126. </blockquote>
  127. <h3><a name="init"></a><b><tt>gsdll_init()</tt></b></h3>
  128. <blockquote>
  129. <b><tt>gsdll_init()</tt></b> must be called after loading the DLL and
  130. before executing any Ghostscript commands. The arguments are the address
  131. of the callback function, a parent window handle, the count of arguments
  132. and an array of pointers to the arguments. For example
  133. <blockquote>
  134. <pre>char *argv[5];
  135. argv[0] = "gswin.exe";
  136. argv[1] = "-Ic:\\gs;c:\gs\\fonts";
  137. argv[2] = "-dNOPAUSE",
  138. argv[3] = "-sDEVICE=djet500",
  139. argv[4] = NULL;
  140. argc = 4;
  141. code = gsdll_init(gsdll_callback, hwnd, argc, argv);
  142. </pre></blockquote>
  143. <p>
  144. <b><tt>hwnd</tt></b> is used as the parent window handle for any windows
  145. created by Ghostscript. <b><tt>hwnd</tt></b> may be <b><tt>NULL</tt></b>
  146. if the caller has no windows, but if it is <b><tt>NULL</tt></b>, you should
  147. avoid using devices which may open windows.
  148. <p>
  149. A return code of 0 (zero) indicates no errors, and the code may now call
  150. <b><tt>gsdll_execute_begin()</tt></b> or <b><tt>gsdll_exit()</tt></b>. If
  151. the return value is non-zero then <b><tt>gsdll_exit()</tt></b> must not be
  152. called.
  153. <p>
  154. A return value of <b><tt>GSDLL_INIT_QUIT</tt></b> indicates that one of the
  155. command line files or arguments called "<b><tt>quit</tt></b>", or that
  156. Ghostscript was reading stdin and reached end-of-file. This is not an
  157. error. <b><tt>gsdll_exit()</tt></b> must not be called.
  158. <p>
  159. A return value of <b><tt>GSDLL_INIT_IN_USE</tt></b> indicates that the DLL
  160. is in use by another application (Windows 3.1 only). The DLL should be
  161. immediately unloaded (or the caller terminated).
  162. <b><tt>gsdll_exit()</tt></b> must not be called.
  163. </blockquote>
  164. <h3><a name="execute_begin"></a><b><tt>gsdll_execute_begin()</tt></b></h3>
  165. <blockquote>
  166. This must be called after <b><tt>gsdll_init()</tt></b> and before
  167. <b><tt>gsdll_execute_cont()</tt></b>.
  168. </blockquote>
  169. <h3><a name="execute_cont"></a><b><tt>gsdll_execute_cont()</tt></b></h3>
  170. <blockquote>
  171. After successfully calling <b><tt>gsdll_init()</tt></b> and
  172. <b><tt>gsdll_execute_begin()</tt></b>, commands may be given to Ghostscript
  173. with <b><tt>gsdll_execute_cont()</tt></b>. Examples are:
  174. <blockquote>
  175. <pre>char *command = "1 2 add == flush\n";
  176. code = gsdll_execute_cont(command, strlen(command));
  177. command = "qu"
  178. code = gsdll_execute_cont(command, strlen(command));
  179. command = "it\n"
  180. code = gsdll_execute_cont(command, strlen(command));
  181. </pre>
  182. <a name="cont_returns"></a>
  183. <table cellpadding=0 cellspacing=0>
  184. <tr><th colspan=3 bgcolor="#CCCC00"><hr><font size="+1">Return codes from <b><tt>gsdll_execute_cont()</tt></b></font><hr>
  185. <tr valign=bottom>
  186. <th align=left>Code
  187. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  188. <th align=left>Status
  189. <tr> <td colspan=3><hr>
  190. <tr valign=top> <td align=left>0
  191. <td>&nbsp;
  192. <td>No errors
  193. <tr valign=top> <td align=left>&lt; 0
  194. <td>&nbsp;
  195. <td>Error
  196. <tr valign=top> <td align=left>&lt;= -100
  197. <td>&nbsp;
  198. <td>"<b><tt>quit</tt></b>" has been executed, or fatal error.
  199. <b><tt>gsdll_exit()</tt></b> must then be called: do not call
  200. <b><tt>gsdll_execute_end()</tt></b>.
  201. </table>
  202. </blockquote>
  203. <b><tt>gsdll_execute_cont()</tt></b>
  204. does not flush stdio, so if you want to see output from Ghostscript you
  205. must do this explicitly as shown in the example above.
  206. <p>
  207. When executing a string with <b><tt>gsdll_execute_cont()</tt></b>,
  208. <b><tt>currentfile</tt></b> is the input from
  209. <b><tt>gsdll_execute_cont()</tt></b>. Reading from <b><tt>%stdin</tt></b>
  210. uses the callback.
  211. <p>
  212. Please note that there is a 64 KB length limit on the passed string.
  213. If you have more than 65535 bytes of input to process at a given time,
  214. it must be split into separate smaller buffers, each passed on a
  215. separate call.
  216. </blockquote>
  217. <h3><a name="execute_end"></a><b><tt>gsdll_execute_end()</tt></b></h3>
  218. <blockquote>
  219. If <b><tt>gsdll_execute_cont()</tt></b> did not return an error, then
  220. <b><tt>gsdll_execute_end()</tt></b> must be called after
  221. <b><tt>gsdll_execute_cont()</tt></b> and before
  222. <b><tt>gsdll_exit()</tt></b>.
  223. </blockquote>
  224. <h3><a name="exit"></a><b><tt>gsdll_exit()</tt></b></h3>
  225. <blockquote>
  226. Call gsdll_exit() to terminate the Ghostscript DLL. It must be called if a
  227. fatal error has occurred; see the <a href="#cont_returns">return value
  228. of <b><tt>gsdll_execute_cont()</tt></b></a>. After calling
  229. <b><tt>gsdll_exit()</tt></b>, there are two options:
  230. <ul>
  231. <li>Unload the DLL, either by terminating the application or by
  232. calling <b><tt>DosFreeModule</tt></b> (OS/2) or <b><tt>FreeLibrary</tt></b> (MS Windows).
  233. <li>Call <b><tt>gsdll_init()</tt></b> again to restart Ghostscript.
  234. </ul>
  235. </blockquote>
  236. <h3><a name="lock_device"></a><b><tt>gsdll_lock_device()</tt></b></h3>
  237. <blockquote>
  238. Since the caller may be multithreaded, a lock is needed to control access
  239. to the display device, and <b><tt>gsdll_lock_device()</tt></b> provides that
  240. locking.
  241. <blockquote>
  242. <pre>int gsdll_lock_device(unsigned char *device, int flag);
  243. /* Lock the device if flag = TRUE */
  244. /* Unlock the device if flag = FALSE */
  245. /* device is a pointer to Ghostscript os2dll or mswindll device */
  246. /* from GSDLL_DEVICE message. */
  247. /* Return value is the lock count. */
  248. </pre>
  249. <table cellpadding=0 cellspacing=0>
  250. <tr><th colspan=3 bgcolor="#CCCC00"><hr><font size="+1">Locking and unlocking devices</font><hr>
  251. <tr valign=top> <td>To lock a device
  252. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  253. <td><b><tt>gsdll_lock_device(device, 1)</tt></b>;
  254. <tr valign=top> <td>To unlock a device
  255. <td>&nbsp;
  256. <td><b><tt>gsdll_lock_device(device, 0)</tt></b>;
  257. </table>
  258. </blockquote>
  259. <p>
  260. This function is typically used to lock the device while repainting a
  261. window or copying the device bitmap to the clipboard. Ghostscript may draw
  262. into the device bitmap or update the palette entries while the device is
  263. locked by the caller, but locking the device prevents the Ghostscript DLL
  264. from closing the device or changing its size or depth.
  265. <p>
  266. Under OS/2, Windows 95/98 and Windows NT, this lock is implemented using a
  267. mutual exclusion semaphore (mutex). The return value is the lock count,
  268. which is either 0 ("unlocked") or 1 ("locked"). The function blocks until
  269. the device is locked by the caller.
  270. <p>
  271. Under Win16 or Win32s, <b><tt>gsdll_lock_device()</tt></b> always returns
  272. immediately with a lock count as its return value. A lock count of 2 or
  273. more is definite indication of an error, probably calling the function
  274. twice. Access to the device should be controlled by checking the Windows
  275. message queue only when the bitmap is not being accessed.
  276. </blockquote>
  277. <hr>
  278. <h2><a name="Callback"></a>Callback function</h2>
  279. <blockquote>
  280. As an argument to <a href="#init"><b><tt>gsdll_init()</tt></b></a> the
  281. caller must provide a callback function which the DLL invokes for stdio and
  282. to notify the caller about device events. The function provided by the
  283. caller has this prototype:
  284. <blockquote><b><tt>
  285. int gsdll_callback(int&nbsp;message, char&nbsp;*str, unsigned&nbsp;long&nbsp;count);
  286. </tt></b></blockquote>
  287. <p>
  288. The Pascal calling convention is not used. An example callback function
  289. is:
  290. <blockquote>
  291. <pre>int
  292. gsdll_callback(int message, char *str, unsigned long count)
  293. {
  294. char *p;
  295. switch (message) {
  296. case GSDLL_STDIN:
  297. p = fgets(str, count, stdin);
  298. if (p)
  299. return strlen(str);
  300. else
  301. return 0;
  302. case GSDLL_STDOUT:
  303. if (str != (char *)NULL)
  304. fwrite(str, 1, count, stdout);
  305. return count;
  306. case GSDLL_DEVICE:
  307. fprintf(stdout,"Callback: DEVICE %p %s\n", str,
  308. count ? "open" : "close");
  309. break;
  310. case GSDLL_SYNC:
  311. fprintf(stdout,"Callback: SYNC %p\n", str);
  312. break;
  313. case GSDLL_PAGE:
  314. fprintf(stdout,"Callback: PAGE %p\n", str);
  315. break;
  316. case GSDLL_SIZE:
  317. fprintf(stdout,"Callback: SIZE %p width=%d height=%d\n", str,
  318. (int)(count &amp; 0xffff), (int)((count&gt;&gt;16) &amp; 0xffff) );
  319. break;
  320. case GSDLL_POLL:
  321. return 0; /* no error */
  322. default:
  323. fprintf(stdout,"Callback: Unknown message=%d\n",message);
  324. break;
  325. }
  326. return 0;
  327. }
  328. </pre>
  329. <table cellpadding=0 cellspacing=0>
  330. <tr><th colspan=5 bgcolor="#CCCC00"><hr><font size="+1">Messages used by callback</font><hr>
  331. <tr valign=bottom>
  332. <th align=left>Symbol
  333. <td>&nbsp;&nbsp;
  334. <th align=left>
  335. <td>&nbsp;&nbsp;
  336. <th align=left>Use
  337. <tr> <td colspan=5><hr>
  338. <tr valign=top> <td><b><tt>GSDLL_STDIN</tt></b>
  339. <td>&nbsp;
  340. <td>1
  341. <td>&nbsp;
  342. <td>get <b><tt>count</tt></b> characters to <b><tt>str</tt></b> from stdin, return number of characters read
  343. <tr valign=top> <td><b><tt>GSDLL_STDOUT</tt></b>
  344. <td>&nbsp;
  345. <td>2
  346. <td>&nbsp;
  347. <td>put <b><tt>count</tt></b> characters from <b><tt>str</tt></b>
  348. to stdout, return number of characters written
  349. <tr valign=top> <td><b><tt>GSDLL_DEVICE</tt></b>
  350. <td>&nbsp;
  351. <td>3
  352. <td>&nbsp;
  353. <td>device <b><tt>str</tt></b> has been opened if
  354. <b><tt>count</tt></b>&nbsp;=&nbsp;1, closed if
  355. <b><tt>count</tt></b>&nbsp;=&nbsp;0
  356. <tr valign=top> <td><b><tt>GSDLL_SYNC</tt></b>
  357. <td>&nbsp;
  358. <td>4
  359. <td>&nbsp;
  360. <td>sync_output for device <b><tt>str</tt></b>
  361. <tr valign=top> <td><b><tt>GSDLL_PAGE</tt></b>
  362. <td>&nbsp;
  363. <td>5
  364. <td>&nbsp;
  365. <td>output_page for device <b><tt>str</tt></b>
  366. <tr valign=top> <td><b><tt>GSDLL_SIZE</tt></b>
  367. <td>&nbsp;
  368. <td>6
  369. <td>&nbsp;
  370. <td>resize for device <b><tt>str</tt></b>:
  371. LOWORD(<b><tt>count</tt></b>) is new <b><tt>xsize</tt></b>,
  372. HIWORD(<b><tt>count</tt></b>) is new <b><tt>ysize</tt></b>
  373. <tr valign=top> <td><b><tt>GSDLL_POLL</tt></b>
  374. <td>&nbsp;
  375. <td>7
  376. <td>&nbsp;
  377. <td>Called from <b><tt>gp_check_interrupt()</tt></b><br>
  378. Can be used by the caller to poll the message queue.
  379. Normally returns 0. To abort
  380. <b><tt>gsdll_execute_cont()</tt></b>, return a non-zero
  381. error code until <b><tt>gsdll_execute_cont()</tt></b>
  382. returns.
  383. </table>
  384. </blockquote>
  385. </blockquote>
  386. <hr>
  387. <h2><a name="OS2_device"></a>Ghostscript DLL device for OS/2</h2>
  388. <p>
  389. The <b><tt>os2dll</tt></b> device is provided in the Ghostscript DLL for
  390. use by the caller. No drawing facilities are provided by the DLL because
  391. the DLL may be loaded by a text-only (non-PM) application. The caller is
  392. notified via the <b><tt>gsdll_callback()</tt></b> when a new
  393. <b><tt>os2dll</tt></b> device is opened or closed
  394. (<b><tt>GSDLL_DEVICE</tt></b>), when the window should be redrawn
  395. (<b><tt>GSDLL_SYNC</tt></b> or <b><tt>GSDLL_PAGE</tt></b>) or when the
  396. bitmap size changes (<b><tt>GSDLL_SIZE</tt></b>).
  397. Note that more than one <b><tt>os2dll</tt></b> device may be opened.
  398. <h3><a name="OS2_bmp"></a><b><tt>gsdll_get_bitmap()</tt></b></h3>
  399. <blockquote>
  400. <b><tt>gsdll_get_bitmap()</tt></b> returns a pointer to a bitmap in BMP
  401. format. The <b><tt>os2dll</tt></b> device draws into this bitmap.
  402. <blockquote>
  403. <pre>unsigned long gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap);
  404. /* return in pbitmap the address of the bitmap */
  405. /* device is a pointer to Ghostscript os2dll device from GSDLL_DEVICE message */
  406. </pre></blockquote>
  407. <p>
  408. The caller can then display the bitmap however it likes, but should lock
  409. the bitmap with <b><tt>gsdll_lock_device()</tt></b> before painting from
  410. it, and unlock it afterwards. The bitmap address does not change until the
  411. <b><tt>os2dll</tt></b> device is closed; however the bitmap size and
  412. palette may change whenever the bitmap is not locked.
  413. </blockquote>
  414. <h3><a name="OS2_example"></a>Example DLL usage for OS/2</h3>
  415. <p>
  416. The example here shows a minimal usage of the Ghostscript DLL under OS/2.
  417. The sample callback function above is needed.
  418. <blockquote>
  419. <pre>#define INCL_DOS
  420. #include &lt;os2.h&gt;
  421. #include &lt;stdio.h&gt;
  422. #include "gsdll.h"
  423. PFN_gsdll_init pgsdll_init;
  424. PFN_gsdll_execute_begin pgsdll_execute_begin;
  425. PFN_gsdll_execute_cont pgsdll_execute_cont;
  426. PFN_gsdll_execute_end pgsdll_execute_end;
  427. PFN_gsdll_exit pgsdll_exit;
  428. HMODULE hmodule_gsdll;
  429. char buf[256];
  430. int
  431. main(int argc, char *argv[])
  432. {
  433. int code;
  434. APIRET rc;
  435. if (!DosLoadModule(buf, sizeof(buf), "GSDLL2", &amp;hmodule_gsdll)) {
  436. fprintf(stderr, "Loaded GSDLL2\n");
  437. DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_init", (PFN *)(&amp;pgsdll_init));
  438. DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_begin", (PFN *)(&amp;pgsdll_execute_begin));
  439. DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_cont", (PFN *)(&amp;pgsdll_execute_cont));
  440. DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_end", (PFN *)(&amp;pgsdll_execute_end));
  441. DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_exit", (PFN *)(&amp;pgsdll_exit));
  442. }
  443. else {
  444. fprintf(stderr, "Can't load GSDLL2\n");
  445. }
  446. code = (*pgsdll_init)(gsdll_callback, NULL, argc, argv);
  447. fprintf(stdout,"gsdll_init returns %d\n", code);
  448. code = (*pgsdll_execute_begin)();
  449. if (code==0) {
  450. while (fgets(buf, sizeof(buf), stdin)) {
  451. code = (*pgsdll_execute_cont)(buf, strlen(buf));
  452. fprintf(stdout,"gsdll_execute returns %d\n", code);
  453. if (code &lt; 0)
  454. break;
  455. }
  456. if (!code)
  457. code = (*pgsdll_execute_end)();
  458. code = (*pgsdll_exit)();
  459. fprintf(stdout,"gsdll_exit returns %d\n", code);
  460. }
  461. rc = DosFreeModule(hmodule_gsdll);
  462. fprintf(stdout,"DosFreeModule returns %d\n", rc);
  463. return 0;
  464. }
  465. </pre></blockquote>
  466. <hr>
  467. <h2><a name="Win_device"></a>Ghostscript DLL device for MS Windows</h2>
  468. <p>
  469. The <b><tt>mswindll</tt></b> device is provided in the Ghostscript DLL for
  470. use by the caller. The caller is notified via the
  471. <b><tt>gsdll_callback()</tt></b> when a new <b><tt>mswindll</tt></b> device
  472. is opened or closed (<b><tt>GSDLL_DEVICE</tt></b>), when the window should
  473. be redrawn (<b><tt>GSDLL_SYNC</tt></b> or <b><tt>GSDLL_PAGE</tt></b>) or
  474. when the bitmap size changes (<b><tt>GSDLL_SIZE</tt></b>). Note that more
  475. than one <b><tt>mswindll</tt></b> device may be opened.
  476. <p>
  477. Four DLL functions are available to use the <b><tt>mswindll</tt></b>
  478. device.
  479. <h3><a name="Win_copydib"></a><b><tt>gsdll_copy_dib()</tt></b></h3>
  480. <blockquote>
  481. Copy the <b><tt>mswindll</tt></b> bitmap to the clipboard.
  482. <blockquote>
  483. <pre>HGLOBAL GSDLLAPI gsdll_copy_dib(unsigned char *device);
  484. /* make a copy of the device bitmap and return shared memory handle to it */
  485. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  486. </pre></blockquote>
  487. </blockquote>
  488. <h3><a name="Win_copypalette"></a><b><tt>gsdll_copy_palette()</tt></b></h3>
  489. <blockquote>
  490. Copy the <b><tt>mswindll</tt></b> palette to the clipboard.
  491. <blockquote>
  492. <pre>HPALETTE GSDLLAPI gsdll_copy_palette(unsigned char *device);
  493. /* make a copy of the device palette and return a handle to it */
  494. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  495. </pre></blockquote>
  496. </blockquote>
  497. <h3><a name="Win_draw"></a><b><tt>gsdll_draw()</tt></b></h3>
  498. <blockquote>
  499. Display output from the <b><tt>mswindll</tt></b> device. The caller should
  500. create a window and call <b><tt>gsdll_draw()</tt></b> in response to the
  501. <b><tt>WM_PAINT</tt></b> message. The device context <b><tt>hdc</tt></b>
  502. must be for a device because <b><tt>SetDIBitsToDevice()</tt></b> is used.
  503. <blockquote>
  504. <pre>void GSDLLAPI gsdll_draw(unsigned char *device, HDC hdc,
  505. LPRECT dest, LPRECT src);
  506. /* copy the rectangle src from the device bitmap */
  507. /* to the rectangle dest on the device given by hdc */
  508. /* hdc must be a device context for a device (NOT a bitmap) */
  509. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  510. </pre></blockquote>
  511. </blockquote>
  512. <h3><a name="Win_get_row"></a><b><tt>gsdll_get_bitmap_row()</tt></b></h3>
  513. <blockquote>
  514. Get a BMP header, a palette, and a pointer to a row in the bitmap. This
  515. function exists to allow the bitmap to be copied to a file or structured
  516. storage without the overhead of having two copies of the bitmap in memory
  517. at the same time.
  518. <p>
  519. Ghostscript can change the palette while the device is locked. Do not call
  520. this function while Ghostscript is busy.
  521. <blockquote>
  522. <pre>int GSDLLAPI gsdll_get_bitmap_row(unsigned char *device, LPBITMAPINFOHEADER pbmih,
  523. LPRGBQUAD prgbquad, LPBYTE *ppbyte, unsigned int row)
  524. /* If pbmih nonzero, copy the BITMAPINFOHEADER.
  525. * If prgbquad nonzero, copy the palette.
  526. * number of entries copied is given by pbmih-&gt;biClrUsed
  527. * If ppbyte nonzero, return pointer to row.
  528. * pointer is only valid while device is locked
  529. */
  530. </pre></blockquote>
  531. </blockquote>
  532. <hr>
  533. <h2><a name="Win16"></a>Ghostscript DLL Device for 16-bit MS Windows</h2>
  534. <p>
  535. This platform has the most problems of the three. Support for it may be
  536. dropped in future.
  537. <p>
  538. The Win16 DLL <b><tt>GSDLL16.DLL</tt></b> is a large-memory model DLL with
  539. far static data. Due to the limitations of 16-bit MS Windows, the DLL can
  540. be used by only one program at a time.
  541. <p>
  542. However, <b><tt>GSDLL16</tt></b> is marked as having SINGLE SHARED data
  543. segments, allowing multiple applications to load it with no error
  544. indication. (The DLL wouldn't load at all if MULTIPLE NONSHARED was used).
  545. Nonetheless, <b>it cannot be used by more than one application at a
  546. time</b>, so applications loading <b><tt>GSDLL16</tt></b> should check the
  547. return value of <b><tt>gsdll_init()</tt></b>: if this value is non-zero,
  548. then <b><tt>GSDLL16</tt></b> is already in use by another application and
  549. should <b><em>not</em></b> be used: <b><tt>GSDLL16</tt></b> should be
  550. unloaded immediately using <b><tt>FreeLibrary()</tt></b>, or the calling
  551. program should quit without attempting to use the library..
  552. <p>
  553. The segmented architecture of the Intel 80286 causes the usual amount of
  554. grief when using <b><tt>GSDLL16</tt></b>. Because the callback is called
  555. from the DLL, which is using a different data segment, the callback must be
  556. declared as <b><tt>_far&nbsp;_export</tt></b>:
  557. <blockquote>
  558. <pre>int _far _export gsdll_callback(int message, char *str, unsigned long count);
  559. </pre></blockquote>
  560. <p>
  561. Instead of giving <b><tt>gsdll_init()</tt></b> the address of
  562. <b><tt>gsdll_callback()</tt></b>, it should instead be given the address of
  563. a thunk created by <b><tt>MakeProcInstance</tt></b>. This thunk changes
  564. the data segment back to that used by the caller:
  565. <blockquote>
  566. <pre>FARPROC lpfnCallback;
  567. lpfnCallback = (FARPROC)MakeProcInstance((FARPROC)gsdll_callback, hInstance);
  568. code = (*pgsdll_init)((GSDLL_CALLBACK)lpfnCallback, NULL, argc, argv);
  569. if (!code) {
  570. fprintf(stderr, "GSDLL16 is already in use\n");
  571. return -1;
  572. }
  573. </pre></blockquote>
  574. <!-- [2.0 end contents] ==================================================== -->
  575. <!-- [3.0 begin visible trailer] =========================================== -->
  576. <hr>
  577. <p>
  578. <small>Copyright &copy; 1996, 1997, 1998 Aladdin Enterprises.
  579. All rights reserved.</small>
  580. <p>
  581. This software is provided AS-IS with no warranty, either express or
  582. implied.
  583. This software is distributed under license and may not be copied,
  584. modified or distributed except as expressly authorized under the terms
  585. of the license contained in the file LICENSE in this distribution.
  586. For more information about licensing, please refer to
  587. http://www.ghostscript.com/licensing/. For information on
  588. commercial licensing, go to http://www.artifex.com/licensing/ or
  589. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  590. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  591. <p>
  592. <small>Ghostscript version 8.53, 20 October 2005
  593. <!-- [3.0 end visible trailer] ============================================= -->
  594. </body>
  595. </html>