dwdll.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (C) 1996-2000 Ghostgum Software Pty Ltd. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /* $Id: dwdll.c,v 1.2 2001/03/27 09:35:22 ghostgum Exp $ */
  16. /* dwdll.c */
  17. #define STRICT
  18. #include <windows.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "stdpre.h"
  22. #include "gpgetenv.h"
  23. #include "gscdefs.h"
  24. #define GSREVISION gs_revision
  25. #define GSDLLEXPORT
  26. #define GSDLLAPI CALLBACK
  27. #define GSDLLCALL
  28. #include "dwdll.h"
  29. static const char name[] = "gsdll32.dll";
  30. int load_dll(GSDLL *gsdll, char *last_error, int len)
  31. {
  32. char fullname[1024];
  33. char *p;
  34. long version;
  35. int length;
  36. gsapi_revision_t rv;
  37. /* Don't load if already loaded */
  38. if (gsdll->hmodule)
  39. return 0;
  40. /* First try to load DLL from the same directory as EXE */
  41. GetModuleFileName(GetModuleHandle(NULL), fullname, sizeof(fullname));
  42. if ((p = strrchr(fullname,'\\')) != (char *)NULL)
  43. p++;
  44. else
  45. p = fullname;
  46. *p = '\0';
  47. strcat(fullname, name);
  48. gsdll->hmodule = LoadLibrary(fullname);
  49. /* Next try to load DLL with name in registry or environment variable */
  50. if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR) {
  51. length = sizeof(fullname);
  52. if (gp_getenv("GS_DLL", fullname, &length) == 0)
  53. gsdll->hmodule = LoadLibrary(fullname);
  54. }
  55. /* Finally try the system search path */
  56. if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR)
  57. gsdll->hmodule = LoadLibrary(name);
  58. if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR) {
  59. /* Failed */
  60. DWORD err = GetLastError();
  61. sprintf(fullname, "Can't load DLL, LoadLibrary error code %ld", err);
  62. strncpy(last_error, fullname, len-1);
  63. gsdll->hmodule = (HINSTANCE)0;
  64. return 1;
  65. }
  66. /* DLL is now loaded */
  67. /* Get pointers to functions */
  68. gsdll->revision = (PFN_gsapi_revision) GetProcAddress(gsdll->hmodule,
  69. "gsapi_revision");
  70. if (gsdll->revision == NULL) {
  71. strncpy(last_error, "Can't find gsapi_revision\n", len-1);
  72. unload_dll(gsdll);
  73. return 1;
  74. }
  75. /* check DLL version */
  76. if (gsdll->revision(&rv, sizeof(rv)) != 0) {
  77. sprintf(fullname, "Unable to identify Ghostscript DLL revision - it must be newer than needed.\n");
  78. strncpy(last_error, fullname, len-1);
  79. unload_dll(gsdll);
  80. return 1;
  81. }
  82. if (rv.revision != GSREVISION) {
  83. sprintf(fullname, "Wrong version of DLL found.\n Found version %ld\n Need version %ld\n", rv.revision, GSREVISION);
  84. strncpy(last_error, fullname, len-1);
  85. unload_dll(gsdll);
  86. return 1;
  87. }
  88. /* continue loading other functions */
  89. gsdll->new_instance = (PFN_gsapi_new_instance) GetProcAddress(gsdll->hmodule,
  90. "gsapi_new_instance");
  91. if (gsdll->new_instance == NULL) {
  92. strncpy(last_error, "Can't find gsapi_new_instance\n", len-1);
  93. unload_dll(gsdll);
  94. return 1;
  95. }
  96. gsdll->delete_instance = (PFN_gsapi_delete_instance) GetProcAddress(gsdll->hmodule,
  97. "gsapi_delete_instance");
  98. if (gsdll->delete_instance == NULL) {
  99. strncpy(last_error, "Can't find gsapi_delete_instance\n", len-1);
  100. unload_dll(gsdll);
  101. return 1;
  102. }
  103. gsdll->set_stdio = (PFN_gsapi_set_stdio) GetProcAddress(gsdll->hmodule,
  104. "gsapi_set_stdio");
  105. if (gsdll->set_stdio == NULL) {
  106. strncpy(last_error, "Can't find gsapi_set_stdio\n", len-1);
  107. unload_dll(gsdll);
  108. return 1;
  109. }
  110. gsdll->set_poll = (PFN_gsapi_set_poll) GetProcAddress(gsdll->hmodule,
  111. "gsapi_set_poll");
  112. if (gsdll->set_poll == NULL) {
  113. strncpy(last_error, "Can't find gsapi_set_poll\n", len-1);
  114. unload_dll(gsdll);
  115. return 1;
  116. }
  117. gsdll->set_display_callback = (PFN_gsapi_set_display_callback)
  118. GetProcAddress(gsdll->hmodule, "gsapi_set_display_callback");
  119. if (gsdll->set_display_callback == NULL) {
  120. strncpy(last_error, "Can't find gsapi_set_display_callback\n", len-1);
  121. unload_dll(gsdll);
  122. return 1;
  123. }
  124. gsdll->init_with_args = (PFN_gsapi_init_with_args)
  125. GetProcAddress(gsdll->hmodule, "gsapi_init_with_args");
  126. if (gsdll->init_with_args == NULL) {
  127. strncpy(last_error, "Can't find gsapi_init_with_args\n", len-1);
  128. unload_dll(gsdll);
  129. return 1;
  130. }
  131. gsdll->run_string = (PFN_gsapi_run_string) GetProcAddress(gsdll->hmodule,
  132. "gsapi_run_string");
  133. if (gsdll->run_string == NULL) {
  134. strncpy(last_error, "Can't find gsapi_run_string\n", len-1);
  135. unload_dll(gsdll);
  136. return 1;
  137. }
  138. gsdll->exit = (PFN_gsapi_exit) GetProcAddress(gsdll->hmodule,
  139. "gsapi_exit");
  140. if (gsdll->exit == NULL) {
  141. strncpy(last_error, "Can't find gsapi_exit\n", len-1);
  142. unload_dll(gsdll);
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. void unload_dll(GSDLL *gsdll)
  148. {
  149. /* Set functions to NULL to prevent use */
  150. gsdll->revision = NULL;
  151. gsdll->new_instance = NULL;
  152. gsdll->delete_instance = NULL;
  153. gsdll->init_with_args = NULL;
  154. gsdll->run_string = NULL;
  155. gsdll->exit = NULL;
  156. gsdll->set_stdio = NULL;
  157. gsdll->set_poll = NULL;
  158. gsdll->set_display_callback = NULL;
  159. if (gsdll->hmodule != (HINSTANCE)NULL)
  160. FreeLibrary(gsdll->hmodule);
  161. gsdll->hmodule = NULL;
  162. }