comparison dynload/dynload_windows.c @ 350:655cafde0859

- dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix - cosmetics, cr update
author Tassilo Philipp
date Fri, 10 Jan 2020 13:30:57 +0100
parents 2e8be88dad11
children
comparison
equal deleted inserted replaced
349:fb70995ac923 350:655cafde0859
4 Library: dynload 4 Library: dynload
5 File: dynload/dynload_windows.c 5 File: dynload/dynload_windows.c
6 Description: 6 Description:
7 License: 7 License:
8 8
9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, 9 Copyright (c) 2007-2020 Daniel Adler <dadler@uni-goettingen.de>,
10 Tassilo Philipp <tphilipp@potion-studios.com> 10 Tassilo Philipp <tphilipp@potion-studios.com>
11 11
12 Permission to use, copy, modify, and distribute this software for any 12 Permission to use, copy, modify, and distribute this software for any
13 purpose with or without fee is hereby granted, provided that the above 13 purpose with or without fee is hereby granted, provided that the above
14 copyright notice and this permission notice appear in all copies. 14 copyright notice and this permission notice appear in all copies.
43 DLLib* dlLoadLibrary(const char* libPath) 43 DLLib* dlLoadLibrary(const char* libPath)
44 { 44 {
45 if(libPath == NULL) 45 if(libPath == NULL)
46 return (DLLib*)GetModuleHandle(NULL); 46 return (DLLib*)GetModuleHandle(NULL);
47 else { 47 else {
48 /* convert from UTF-8 to wide chars, so count required size... */ 48 /* convert from UTF-8 to wide chars, so count required size */
49 DLLib* pLib; 49 DLLib* pLib;
50 wchar_t* ws; 50 wchar_t* ws;
51 int r = MultiByteToWideChar(CP_UTF8, 0, libPath, -1, NULL, 0); 51 int r = MultiByteToWideChar(CP_UTF8, 0, libPath, -1, NULL, 0);
52 if(!r) { 52 if(!r) {
53 return NULL; 53 return NULL;
54 } 54 }
55 55
56 /* ... reserve temp space, ... */ 56 /* Reserve temp space with room for extra '.' suffix (see below) */
57 ws = (wchar_t*)dlAllocMem(r * sizeof(wchar_t)); 57 ws = (wchar_t*)dlAllocMem((r+1) * sizeof(wchar_t));
58 if(!ws) 58 if(!ws)
59 return NULL; 59 return NULL;
60 60
61 /* ... convert (and use r as success flag), ... */ 61 /* Convert path and add a '.' suffix, needed to tell windows not to add
62 r = (MultiByteToWideChar(CP_UTF8, 0, libPath, -1, ws, r) == r); 62 .dll to any path that doesn't have it (see MS doc for LoadLibraryW).
63 pLib = (DLLib*)(r ? LoadLibraryW(ws) : NULL); 63 This is to get same behaviour as on other platforms which don't do any
64 magic like this. Library search path behaviour stays unaffected, though */
65 pLib = NULL;
66 if(MultiByteToWideChar(CP_UTF8, 0, libPath, -1, ws, r) == r) {
67 ws[r-1] = '.';
68 ws[r] = 0;
69 pLib = (DLLib*)LoadLibraryW(ws);
70 }
64 71
65 /* ... free temp space and return handle */ 72 /* ... free temp space and return handle */
66 dlFreeMem(ws); 73 dlFreeMem(ws);
67 return pLib; 74 return pLib;
68 } 75 }