dso_win32.c 20 KB

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