home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dwdll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.8 KB  |  357 lines

  1. /* Copyright (C) 1996, 1998, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19.  
  20. // $Id: dwdll.cpp,v 1.2 2000/09/19 19:00:09 lpd Exp $
  21.  
  22. // gsdll class  for MS-Windows
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. extern "C" {
  30. #include "stdpre.h"
  31. #undef public
  32. #include "gpgetenv.h"
  33. #include "gsdll.h"
  34. #include "gsdllwin.h"
  35. #include "gscdefs.h"
  36. }
  37.  
  38. #include "dwdll.h"   // gsdll_class
  39.  
  40. static char not_loaded[] = "DLL is not loaded";
  41. static char func_null[]  = "A function pointer to the DLL is NULL";
  42. static char not_init[] = "Not initialized";
  43.  
  44. int
  45. gsdll_class::load(const HINSTANCE in_hinstance, 
  46.     const char *name, const long need_version)
  47. {
  48. char fullname[1024];
  49. const char *shortname;
  50. char *p;
  51. long version;
  52. int len;
  53.  
  54.     // Don't load if already loaded
  55.     if (hmodule)
  56.     return 0;
  57.     hinstance = in_hinstance;
  58.     initialized = FALSE;
  59.   
  60.  
  61.     // First try to load DLL with name in registry or environment variable
  62.     hmodule = (HINSTANCE)NULL;
  63.     len = sizeof(fullname);
  64.     if (gp_getenv("GS_DLL", fullname, &len) == 0)
  65.         hmodule = LoadLibrary(fullname);
  66.  
  67.     // Next try to load DLL first with given path
  68.     if (hmodule < (HINSTANCE)HINSTANCE_ERROR)
  69.         hmodule = LoadLibrary(name);
  70.     if (hmodule < (HINSTANCE)HINSTANCE_ERROR) {
  71.     // failed
  72.     // try again, with path of EXE
  73.     if ((shortname = strrchr((char *)name, '\\')) == (const char *)NULL)
  74.         shortname = name;
  75.     GetModuleFileName(hinstance, fullname, sizeof(fullname));
  76.     if ((p = strrchr(fullname,'\\')) != (char *)NULL)
  77.         p++;
  78.     else
  79.         p = fullname;
  80.     *p = '\0';
  81.     strcat(fullname, shortname);
  82.         hmodule = LoadLibrary(fullname);
  83.         if (hmodule < (HINSTANCE)HINSTANCE_ERROR) {
  84.         // failed again
  85.         // try once more, this time on system search path
  86.             hmodule = LoadLibrary(shortname);
  87.             if (hmodule < (HINSTANCE)HINSTANCE_ERROR) {
  88.         sprintf(last_error, "Couldn't load DLL, LoadLibrary error code %d", (int)hmodule);
  89.         hmodule = (HINSTANCE)0;
  90.         return 1;
  91.         }
  92.     }
  93.     }
  94.  
  95.     // DLL is now loaded
  96.     // Get pointers to functions
  97.     c_revision = (PFN_gsdll_revision) GetProcAddress(hmodule, "gsdll_revision");
  98.     if (c_revision == NULL) {
  99.     sprintf(last_error, "Can't find gsdll_revision\n");
  100.     unload();
  101.     return 1;
  102.     }
  103.     // check DLL version
  104.     c_revision(NULL, NULL, &version, NULL);
  105.     if (version != need_version) {
  106.     sprintf(last_error, "Wrong version of DLL found.\n  Found version %ld\n  Need version  %ld\n", version, need_version);
  107.     unload();
  108.     return 1;
  109.     }
  110.  
  111.     // continue loading other functions */
  112.     c_init = (PFN_gsdll_init) GetProcAddress(hmodule, "gsdll_init");
  113.     if (c_init == NULL) {
  114.     sprintf(last_error, "Can't find gsdll_init\n");
  115.     unload();
  116.     return 1;
  117.     }
  118.     c_execute_begin = (PFN_gsdll_execute_begin) GetProcAddress(hmodule, "gsdll_execute_begin");
  119.     if (c_execute_begin == NULL) {
  120.     sprintf(last_error, "Can't find gsdll_execute_begin\n");
  121.     unload();
  122.     return 1;
  123.     }
  124.     c_execute_cont = (PFN_gsdll_execute_cont) GetProcAddress(hmodule, "gsdll_execute_cont");
  125.     if (c_execute_cont == NULL) {
  126.     sprintf(last_error, "Can't find gsdll_execute_cont\n");
  127.     unload();
  128.     return 1;
  129.     }
  130.     c_execute_end = (PFN_gsdll_execute_end) GetProcAddress(hmodule, "gsdll_execute_end");
  131.     if (c_execute_end == NULL) {
  132.     sprintf(last_error, "Can't find gsdll_execute_end\n");
  133.     unload();
  134.     return 1;
  135.     }
  136.     c_exit = (PFN_gsdll_exit) GetProcAddress(hmodule, "gsdll_exit");
  137.     if (c_exit == NULL) {
  138.     sprintf(last_error, "Can't find gsdll_exit\n");
  139.     unload();
  140.     return 1;
  141.     }
  142.     c_lock_device = (PFN_gsdll_lock_device) GetProcAddress(hmodule, "gsdll_lock_device");
  143.     if (c_lock_device == NULL) {
  144.     sprintf(last_error, "Can't find gsdll_lock_device\n");
  145.     unload();
  146.     return 1;
  147.     }
  148.     c_copy_dib = (PFN_gsdll_copy_dib) GetProcAddress(hmodule, "gsdll_copy_dib");
  149.     if (c_copy_dib == NULL) {
  150.     sprintf(last_error, "Can't find gsdll_copy_dib\n");
  151.     unload();
  152.     return 1;
  153.     }
  154.     c_copy_palette = (PFN_gsdll_copy_palette) GetProcAddress(hmodule, "gsdll_copy_palette");
  155.     if (c_copy_palette == NULL) {
  156.     sprintf(last_error, "Can't find gsdll_copy_palette\n");
  157.     unload();
  158.     return 1;
  159.     }
  160.     c_draw = (PFN_gsdll_draw) GetProcAddress(hmodule, "gsdll_draw");
  161.     if (c_draw == NULL) {
  162.     sprintf(last_error, "Can't find gsdll_draw\n");
  163.     unload();
  164.     return 1;
  165.     }
  166.     return 0;
  167. }
  168.  
  169. int
  170. gsdll_class::unload(void)
  171. {
  172.     // exit Ghostscript interpreter
  173.     if (initialized) {
  174.     if (!execute_code)
  175.         c_execute_end();
  176.     c_exit();
  177. #ifndef __WIN32__
  178.     FreeProcInstance((FARPROC)callback);
  179. #endif
  180.         callback = NULL;
  181.     }
  182.  
  183.     // Set functions to NULL to prevent use
  184.     c_revision = NULL;
  185.     c_init = NULL;
  186.     c_execute_begin = NULL;
  187.     c_execute_cont = NULL;
  188.     c_execute_end = NULL;
  189.     c_exit = NULL;
  190.     c_lock_device = NULL;
  191.     c_copy_dib = NULL;
  192.     c_copy_palette = NULL;
  193.     c_draw = NULL;
  194.  
  195.     // need to do more than this
  196.     device = NULL;
  197.  
  198.     // Don't do anything else if already unloaded
  199.     if (hmodule == (HINSTANCE)NULL)
  200.     return 0;
  201.  
  202.     FreeLibrary(hmodule);
  203.     return 0;
  204. }
  205.  
  206. int 
  207. gsdll_class::revision(char FAR * FAR *product, char FAR * FAR *copyright, 
  208.     long FAR *revision, long FAR *revisiondate)
  209. {
  210.     if (!hmodule) {
  211.     strcpy(last_error, not_loaded);
  212.     return 1;
  213.     }
  214.     if (!c_revision)
  215.     return c_revision(product, copyright, revision, revisiondate);
  216.     strcpy(last_error, func_null);
  217.     return 1;
  218. }
  219.  
  220. int 
  221. gsdll_class::init(GSDLL_CALLBACK in_callback, HWND hwnd, int argc, char FAR * FAR *argv)
  222. {
  223. int rc;
  224.     execute_code = 0;
  225.     if (!hmodule) {
  226.     strcpy(last_error, not_loaded);
  227.     return 1;
  228.     }
  229.     if (initialized) {
  230.     strcpy(last_error, "Already initialized");
  231.     return 1;
  232.     }
  233.     if (in_callback == (GSDLL_CALLBACK)NULL) {
  234.     strcpy(last_error, "Callback not provided");
  235.     return 1;
  236.     }
  237.  
  238. #ifdef __WIN32__
  239.     callback = in_callback;
  240. #else
  241.     callback = (GSDLL_CALLBACK)MakeProcInstance((FARPROC)in_callback, hinstance);
  242. #endif
  243.  
  244.     if (c_init && c_execute_begin) {
  245.     rc = c_init(callback, hwnd, argc, argv);
  246.     if (rc) {
  247.         if (rc == GSDLL_INIT_IN_USE)
  248.             sprintf(last_error, "gsdll_init returns %d\nDLL already in use", rc);
  249.         else
  250.             sprintf(last_error, "gsdll_init returns %d\n", rc);
  251.         return rc;
  252.     }
  253.     else {
  254.         if ((rc = c_execute_begin()) != 0)
  255.             sprintf(last_error, "gsdll_execute_begin returns %d", rc);
  256.         else
  257.             initialized = TRUE;
  258.         return rc;
  259.     }
  260.     }
  261.     strcpy(last_error, func_null);
  262.     return 1;
  263. }
  264.  
  265. int
  266. gsdll_class::restart(int argc, char FAR * FAR *argv)
  267. {
  268.     if (!hmodule) {
  269.     strcpy(last_error, not_loaded);
  270.     return 1;
  271.     }
  272.     if (!initialized) {
  273.     strcpy(last_error, not_init);
  274.     return 1;
  275.     }
  276.     if (c_execute_end && c_exit) {
  277.     if (!execute_code)
  278.         c_execute_end();
  279.     c_exit();
  280. #ifndef __WIN32__
  281.     FreeProcInstance((FARPROC)callback);
  282. #endif
  283.     // ignore return codes since they we may be aborting from an error
  284.     initialized = FALSE;
  285.     return init(callback, hwnd, argc, argv);
  286.     }
  287.  
  288.     strcpy(last_error, func_null);
  289.     return 1;
  290. }
  291.  
  292. int 
  293. gsdll_class::get_last_error(char *str, int len)
  294. {
  295.     strncpy(str, last_error,  
  296.     (len < sizeof(last_error) ? len : sizeof(last_error)));
  297.     return 0;
  298. }
  299.  
  300.  
  301. int 
  302. gsdll_class::execute(const char FAR *str, int len)
  303. {
  304.     if (!hmodule) {
  305.     strcpy(last_error, not_loaded);
  306.     return 1;
  307.     }
  308.     if (!initialized) {
  309.     strcpy(last_error, not_init);
  310.     return 1;
  311.     }
  312.     if (c_execute_cont) {
  313.     execute_code = c_execute_cont(str, len);
  314.     return execute_code;
  315.     }
  316.  
  317.     strcpy(last_error, func_null);
  318.     return 1;
  319. }
  320.  
  321. int 
  322. gsdll_class::draw(const char FAR *device, HDC hdc, int dx, int dy, int wx, int wy, int sx, int sy)
  323. {
  324. RECT source, dest;
  325.     source.left = sx;
  326.     source.right = sx+wx;
  327.     source.top = sy;
  328.     source.bottom = sy+wy;
  329.  
  330.     dest.left = dx;
  331.     dest.right = dx+wx;
  332.     dest.top = dy;
  333.     dest.bottom = dy+wy;
  334.     c_draw((unsigned char FAR *)device, hdc, &dest, &source);
  335.     return 0;
  336. }
  337.  
  338. HPALETTE 
  339. gsdll_class::copy_palette(const char FAR *device)
  340. {
  341.     return c_copy_palette((unsigned char FAR *)device);
  342. }
  343.  
  344.  
  345. HGLOBAL
  346. gsdll_class::copy_dib(const char FAR *device)
  347. {
  348.     return c_copy_dib((unsigned char FAR *)device);
  349. }
  350.  
  351.  
  352. int 
  353. gsdll_class::lock_device(const char FAR *device, int lock)
  354. {
  355.     return c_lock_device((unsigned char FAR *)device, lock);
  356. }
  357.