dso_win32.c 19 KB

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