dso_win32.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "e_os.h"
  10. #include "dso_local.h"
  11. #if defined(DSO_WIN32)
  12. # ifdef _WIN32_WCE
  13. # if _WIN32_WCE < 300
  14. static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
  15. {
  16. WCHAR lpProcNameW[64];
  17. int i;
  18. for (i = 0; lpProcName[i] && i < 64; i++)
  19. lpProcNameW[i] = (WCHAR)lpProcName[i];
  20. if (i == 64)
  21. return NULL;
  22. lpProcNameW[i] = 0;
  23. return GetProcAddressW(hModule, lpProcNameW);
  24. }
  25. # endif
  26. # undef GetProcAddress
  27. # define GetProcAddress GetProcAddressA
  28. static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
  29. {
  30. WCHAR *fnamw;
  31. size_t len_0 = strlen(lpLibFileName) + 1, i;
  32. # ifdef _MSC_VER
  33. fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
  34. # else
  35. fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  36. # endif
  37. if (fnamw == NULL) {
  38. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  39. return NULL;
  40. }
  41. # if defined(_WIN32_WCE) && _WIN32_WCE>=101
  42. if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
  43. # endif
  44. for (i = 0; i < len_0; i++)
  45. fnamw[i] = (WCHAR)lpLibFileName[i];
  46. return LoadLibraryW(fnamw);
  47. }
  48. # endif
  49. /* Part of the hack in "win32_load" ... */
  50. # define DSO_MAX_TRANSLATED_SIZE 256
  51. static int win32_load(DSO *dso);
  52. static int win32_unload(DSO *dso);
  53. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
  54. static char *win32_name_converter(DSO *dso, const char *filename);
  55. static char *win32_merger(DSO *dso, const char *filespec1,
  56. const char *filespec2);
  57. static int win32_pathbyaddr(void *addr, char *path, int sz);
  58. static void *win32_globallookup(const char *name);
  59. static const char *openssl_strnchr(const char *string, int c, size_t len);
  60. DEFINE_STACK_OF(void)
  61. static DSO_METHOD dso_meth_win32 = {
  62. "OpenSSL 'win32' shared library method",
  63. win32_load,
  64. win32_unload,
  65. win32_bind_func,
  66. NULL, /* ctrl */
  67. win32_name_converter,
  68. win32_merger,
  69. NULL, /* init */
  70. NULL, /* finish */
  71. win32_pathbyaddr, /* pathbyaddr */
  72. win32_globallookup
  73. };
  74. DSO_METHOD *DSO_METHOD_openssl(void)
  75. {
  76. return &dso_meth_win32;
  77. }
  78. /*
  79. * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
  80. * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
  81. */
  82. static int win32_load(DSO *dso)
  83. {
  84. HINSTANCE h = NULL, *p = NULL;
  85. /* See applicable comments from dso_dl.c */
  86. char *filename = DSO_convert_filename(dso, NULL);
  87. if (filename == NULL) {
  88. DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
  89. goto err;
  90. }
  91. h = LoadLibraryA(filename);
  92. if (h == NULL) {
  93. DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
  94. ERR_add_error_data(3, "filename(", filename, ")");
  95. goto err;
  96. }
  97. p = OPENSSL_malloc(sizeof(*p));
  98. if (p == NULL) {
  99. DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
  100. goto err;
  101. }
  102. *p = h;
  103. if (!sk_void_push(dso->meth_data, p)) {
  104. DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
  105. goto err;
  106. }
  107. /* Success */
  108. dso->loaded_filename = filename;
  109. return 1;
  110. err:
  111. /* Cleanup ! */
  112. OPENSSL_free(filename);
  113. OPENSSL_free(p);
  114. if (h != NULL)
  115. FreeLibrary(h);
  116. return 0;
  117. }
  118. static int win32_unload(DSO *dso)
  119. {
  120. HINSTANCE *p;
  121. if (dso == NULL) {
  122. DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  123. return 0;
  124. }
  125. if (sk_void_num(dso->meth_data) < 1)
  126. return 1;
  127. p = sk_void_pop(dso->meth_data);
  128. if (p == NULL) {
  129. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
  130. return 0;
  131. }
  132. if (!FreeLibrary(*p)) {
  133. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
  134. /*
  135. * We should push the value back onto the stack in case of a retry.
  136. */
  137. sk_void_push(dso->meth_data, p);
  138. return 0;
  139. }
  140. /* Cleanup */
  141. OPENSSL_free(p);
  142. return 1;
  143. }
  144. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
  145. {
  146. HINSTANCE *ptr;
  147. union {
  148. void *p;
  149. FARPROC f;
  150. } sym;
  151. if ((dso == NULL) || (symname == NULL)) {
  152. DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  153. return NULL;
  154. }
  155. if (sk_void_num(dso->meth_data) < 1) {
  156. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
  157. return NULL;
  158. }
  159. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  160. if (ptr == NULL) {
  161. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
  162. return NULL;
  163. }
  164. sym.f = GetProcAddress(*ptr, symname);
  165. if (sym.p == NULL) {
  166. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
  167. ERR_add_error_data(3, "symname(", symname, ")");
  168. return NULL;
  169. }
  170. return (DSO_FUNC_TYPE)sym.f;
  171. }
  172. struct file_st {
  173. const char *node;
  174. int nodelen;
  175. const char *device;
  176. int devicelen;
  177. const char *predir;
  178. int predirlen;
  179. const char *dir;
  180. int dirlen;
  181. const char *file;
  182. int filelen;
  183. };
  184. static struct file_st *win32_splitter(DSO *dso, const char *filename,
  185. int assume_last_is_dir)
  186. {
  187. struct file_st *result = NULL;
  188. enum { IN_NODE, IN_DEVICE, IN_FILE } position;
  189. const char *start = filename;
  190. char last;
  191. if (!filename) {
  192. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
  193. return NULL;
  194. }
  195. result = OPENSSL_zalloc(sizeof(*result));
  196. if (result == NULL) {
  197. DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
  198. return NULL;
  199. }
  200. position = IN_DEVICE;
  201. if ((filename[0] == '\\' && filename[1] == '\\')
  202. || (filename[0] == '/' && filename[1] == '/')) {
  203. position = IN_NODE;
  204. filename += 2;
  205. start = filename;
  206. result->node = start;
  207. }
  208. do {
  209. last = filename[0];
  210. switch (last) {
  211. case ':':
  212. if (position != IN_DEVICE) {
  213. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
  214. OPENSSL_free(result);
  215. return NULL;
  216. }
  217. result->device = start;
  218. result->devicelen = (int)(filename - start);
  219. position = IN_FILE;
  220. start = ++filename;
  221. result->dir = start;
  222. break;
  223. case '\\':
  224. case '/':
  225. if (position == IN_NODE) {
  226. result->nodelen = (int)(filename - start);
  227. position = IN_FILE;
  228. start = ++filename;
  229. result->dir = start;
  230. } else if (position == IN_DEVICE) {
  231. position = IN_FILE;
  232. filename++;
  233. result->dir = start;
  234. result->dirlen = (int)(filename - start);
  235. start = filename;
  236. } else {
  237. filename++;
  238. result->dirlen += (int)(filename - start);
  239. start = filename;
  240. }
  241. break;
  242. case '\0':
  243. if (position == IN_NODE) {
  244. result->nodelen = (int)(filename - start);
  245. } else {
  246. if (filename - start > 0) {
  247. if (assume_last_is_dir) {
  248. if (position == IN_DEVICE) {
  249. result->dir = start;
  250. result->dirlen = 0;
  251. }
  252. result->dirlen += (int)(filename - start);
  253. } else {
  254. result->file = start;
  255. result->filelen = (int)(filename - start);
  256. }
  257. }
  258. }
  259. break;
  260. default:
  261. filename++;
  262. break;
  263. }
  264. }
  265. while (last);
  266. if (!result->nodelen)
  267. result->node = NULL;
  268. if (!result->devicelen)
  269. result->device = NULL;
  270. if (!result->dirlen)
  271. result->dir = NULL;
  272. if (!result->filelen)
  273. result->file = NULL;
  274. return result;
  275. }
  276. static char *win32_joiner(DSO *dso, const struct file_st *file_split)
  277. {
  278. int len = 0, offset = 0;
  279. char *result = NULL;
  280. const char *start;
  281. if (!file_split) {
  282. DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
  283. return NULL;
  284. }
  285. if (file_split->node) {
  286. len += 2 + file_split->nodelen; /* 2 for starting \\ */
  287. if (file_split->predir || file_split->dir || file_split->file)
  288. len++; /* 1 for ending \ */
  289. } else if (file_split->device) {
  290. len += file_split->devicelen + 1; /* 1 for ending : */
  291. }
  292. len += file_split->predirlen;
  293. if (file_split->predir && (file_split->dir || file_split->file)) {
  294. len++; /* 1 for ending \ */
  295. }
  296. len += file_split->dirlen;
  297. if (file_split->dir && file_split->file) {
  298. len++; /* 1 for ending \ */
  299. }
  300. len += file_split->filelen;
  301. if (!len) {
  302. DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
  303. return NULL;
  304. }
  305. result = OPENSSL_malloc(len + 1);
  306. if (result == NULL) {
  307. DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
  308. return NULL;
  309. }
  310. if (file_split->node) {
  311. strcpy(&result[offset], "\\\\");
  312. offset += 2;
  313. strncpy(&result[offset], file_split->node, file_split->nodelen);
  314. offset += file_split->nodelen;
  315. if (file_split->predir || file_split->dir || file_split->file) {
  316. result[offset] = '\\';
  317. offset++;
  318. }
  319. } else if (file_split->device) {
  320. strncpy(&result[offset], file_split->device, file_split->devicelen);
  321. offset += file_split->devicelen;
  322. result[offset] = ':';
  323. offset++;
  324. }
  325. start = file_split->predir;
  326. while (file_split->predirlen > (start - file_split->predir)) {
  327. const char *end = openssl_strnchr(start, '/',
  328. file_split->predirlen - (start -
  329. file_split->predir));
  330. if (!end)
  331. end = start
  332. + file_split->predirlen - (start - file_split->predir);
  333. strncpy(&result[offset], start, end - start);
  334. offset += (int)(end - start);
  335. result[offset] = '\\';
  336. offset++;
  337. start = end + 1;
  338. }
  339. start = file_split->dir;
  340. while (file_split->dirlen > (start - file_split->dir)) {
  341. const char *end = openssl_strnchr(start, '/',
  342. file_split->dirlen - (start -
  343. file_split->dir));
  344. if (!end)
  345. end = start + file_split->dirlen - (start - file_split->dir);
  346. strncpy(&result[offset], start, end - start);
  347. offset += (int)(end - start);
  348. result[offset] = '\\';
  349. offset++;
  350. start = end + 1;
  351. }
  352. strncpy(&result[offset], file_split->file, file_split->filelen);
  353. offset += file_split->filelen;
  354. result[offset] = '\0';
  355. return result;
  356. }
  357. static char *win32_merger(DSO *dso, const char *filespec1,
  358. const char *filespec2)
  359. {
  360. char *merged = NULL;
  361. struct file_st *filespec1_split = NULL;
  362. struct file_st *filespec2_split = NULL;
  363. if (!filespec1 && !filespec2) {
  364. DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  365. return NULL;
  366. }
  367. if (!filespec2) {
  368. merged = OPENSSL_strdup(filespec1);
  369. if (merged == NULL) {
  370. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  371. return NULL;
  372. }
  373. } else if (!filespec1) {
  374. merged = OPENSSL_strdup(filespec2);
  375. if (merged == NULL) {
  376. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  377. return NULL;
  378. }
  379. } else {
  380. filespec1_split = win32_splitter(dso, filespec1, 0);
  381. if (!filespec1_split) {
  382. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  383. return NULL;
  384. }
  385. filespec2_split = win32_splitter(dso, filespec2, 1);
  386. if (!filespec2_split) {
  387. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  388. OPENSSL_free(filespec1_split);
  389. return NULL;
  390. }
  391. /* Fill in into filespec1_split */
  392. if (!filespec1_split->node && !filespec1_split->device) {
  393. filespec1_split->node = filespec2_split->node;
  394. filespec1_split->nodelen = filespec2_split->nodelen;
  395. filespec1_split->device = filespec2_split->device;
  396. filespec1_split->devicelen = filespec2_split->devicelen;
  397. }
  398. if (!filespec1_split->dir) {
  399. filespec1_split->dir = filespec2_split->dir;
  400. filespec1_split->dirlen = filespec2_split->dirlen;
  401. } else if (filespec1_split->dir[0] != '\\'
  402. && filespec1_split->dir[0] != '/') {
  403. filespec1_split->predir = filespec2_split->dir;
  404. filespec1_split->predirlen = filespec2_split->dirlen;
  405. }
  406. if (!filespec1_split->file) {
  407. filespec1_split->file = filespec2_split->file;
  408. filespec1_split->filelen = filespec2_split->filelen;
  409. }
  410. merged = win32_joiner(dso, filespec1_split);
  411. }
  412. OPENSSL_free(filespec1_split);
  413. OPENSSL_free(filespec2_split);
  414. return merged;
  415. }
  416. static char *win32_name_converter(DSO *dso, const char *filename)
  417. {
  418. char *translated;
  419. int len, transform;
  420. len = strlen(filename);
  421. transform = ((strstr(filename, "/") == NULL) &&
  422. (strstr(filename, "\\") == NULL) &&
  423. (strstr(filename, ":") == NULL));
  424. if (transform)
  425. /* We will convert this to "%s.dll" */
  426. translated = OPENSSL_malloc(len + 5);
  427. else
  428. /* We will simply duplicate filename */
  429. translated = OPENSSL_malloc(len + 1);
  430. if (translated == NULL) {
  431. DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  432. return NULL;
  433. }
  434. if (transform)
  435. sprintf(translated, "%s.dll", filename);
  436. else
  437. sprintf(translated, "%s", filename);
  438. return translated;
  439. }
  440. static const char *openssl_strnchr(const char *string, int c, size_t len)
  441. {
  442. size_t i;
  443. const char *p;
  444. for (i = 0, p = string; i < len && *p; i++, p++) {
  445. if (*p == c)
  446. return p;
  447. }
  448. return NULL;
  449. }
  450. # include <tlhelp32.h>
  451. # ifdef _WIN32_WCE
  452. # define DLLNAME "TOOLHELP.DLL"
  453. # else
  454. # ifdef MODULEENTRY32
  455. # undef MODULEENTRY32 /* unmask the ASCII version! */
  456. # endif
  457. # define DLLNAME "KERNEL32.DLL"
  458. # endif
  459. typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
  460. typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
  461. typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
  462. static int win32_pathbyaddr(void *addr, char *path, int sz)
  463. {
  464. HMODULE dll;
  465. HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  466. MODULEENTRY32 me32;
  467. CREATETOOLHELP32SNAPSHOT create_snap;
  468. CLOSETOOLHELP32SNAPSHOT close_snap;
  469. MODULE32 module_first, module_next;
  470. if (addr == NULL) {
  471. union {
  472. int (*f) (void *, char *, int);
  473. void *p;
  474. } t = {
  475. win32_pathbyaddr
  476. };
  477. addr = t.p;
  478. }
  479. dll = LoadLibrary(TEXT(DLLNAME));
  480. if (dll == NULL) {
  481. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  482. return -1;
  483. }
  484. create_snap = (CREATETOOLHELP32SNAPSHOT)
  485. GetProcAddress(dll, "CreateToolhelp32Snapshot");
  486. if (create_snap == NULL) {
  487. FreeLibrary(dll);
  488. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  489. return -1;
  490. }
  491. /* We take the rest for granted... */
  492. # ifdef _WIN32_WCE
  493. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  494. GetProcAddress(dll, "CloseToolhelp32Snapshot");
  495. # else
  496. close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
  497. # endif
  498. module_first = (MODULE32) GetProcAddress(dll, "Module32First");
  499. module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
  500. /*
  501. * Take a snapshot of current process which includes
  502. * list of all involved modules.
  503. */
  504. hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
  505. if (hModuleSnap == INVALID_HANDLE_VALUE) {
  506. FreeLibrary(dll);
  507. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  508. return -1;
  509. }
  510. me32.dwSize = sizeof(me32);
  511. if (!(*module_first) (hModuleSnap, &me32)) {
  512. (*close_snap) (hModuleSnap);
  513. FreeLibrary(dll);
  514. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
  515. return -1;
  516. }
  517. /* Enumerate the modules to find one which includes me. */
  518. do {
  519. if ((size_t) addr >= (size_t) me32.modBaseAddr &&
  520. (size_t) addr < (size_t) (me32.modBaseAddr + me32.modBaseSize)) {
  521. (*close_snap) (hModuleSnap);
  522. FreeLibrary(dll);
  523. # ifdef _WIN32_WCE
  524. # if _WIN32_WCE >= 101
  525. return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
  526. path, sz, NULL, NULL);
  527. # else
  528. {
  529. int i, len = (int)wcslen(me32.szExePath);
  530. if (sz <= 0)
  531. return len + 1;
  532. if (len >= sz)
  533. len = sz - 1;
  534. for (i = 0; i < len; i++)
  535. path[i] = (char)me32.szExePath[i];
  536. path[len++] = '\0';
  537. return len;
  538. }
  539. # endif
  540. # else
  541. {
  542. int len = (int)strlen(me32.szExePath);
  543. if (sz <= 0)
  544. return len + 1;
  545. if (len >= sz)
  546. len = sz - 1;
  547. memcpy(path, me32.szExePath, len);
  548. path[len++] = '\0';
  549. return len;
  550. }
  551. # endif
  552. }
  553. } while ((*module_next) (hModuleSnap, &me32));
  554. (*close_snap) (hModuleSnap);
  555. FreeLibrary(dll);
  556. return 0;
  557. }
  558. static void *win32_globallookup(const char *name)
  559. {
  560. HMODULE dll;
  561. HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  562. MODULEENTRY32 me32;
  563. CREATETOOLHELP32SNAPSHOT create_snap;
  564. CLOSETOOLHELP32SNAPSHOT close_snap;
  565. MODULE32 module_first, module_next;
  566. union {
  567. void *p;
  568. FARPROC f;
  569. } ret = { NULL };
  570. dll = LoadLibrary(TEXT(DLLNAME));
  571. if (dll == NULL) {
  572. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  573. return NULL;
  574. }
  575. create_snap = (CREATETOOLHELP32SNAPSHOT)
  576. GetProcAddress(dll, "CreateToolhelp32Snapshot");
  577. if (create_snap == NULL) {
  578. FreeLibrary(dll);
  579. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  580. return NULL;
  581. }
  582. /* We take the rest for granted... */
  583. # ifdef _WIN32_WCE
  584. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  585. GetProcAddress(dll, "CloseToolhelp32Snapshot");
  586. # else
  587. close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
  588. # endif
  589. module_first = (MODULE32) GetProcAddress(dll, "Module32First");
  590. module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
  591. hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
  592. if (hModuleSnap == INVALID_HANDLE_VALUE) {
  593. FreeLibrary(dll);
  594. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  595. return NULL;
  596. }
  597. me32.dwSize = sizeof(me32);
  598. if (!(*module_first) (hModuleSnap, &me32)) {
  599. (*close_snap) (hModuleSnap);
  600. FreeLibrary(dll);
  601. return NULL;
  602. }
  603. do {
  604. if ((ret.f = GetProcAddress(me32.hModule, name))) {
  605. (*close_snap) (hModuleSnap);
  606. FreeLibrary(dll);
  607. return ret.p;
  608. }
  609. } while ((*module_next) (hModuleSnap, &me32));
  610. (*close_snap) (hModuleSnap);
  611. FreeLibrary(dll);
  612. return NULL;
  613. }
  614. #endif /* DSO_WIN32 */