annotate dynload/dynload_windows.c @ 357:d982a00c2177

- PPC64 asm syntax fix, specifying explicitly comparison mode for cmpi (newer toolchains complain, older ones took optional field of instruction which happened to be same value)
author Tassilo Philipp
date Tue, 25 Feb 2020 18:16:13 +0100
parents 655cafde0859
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 Package: dyncall
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Library: dynload
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 File: dynload/dynload_windows.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 Description:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 License:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
350
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
9 Copyright (c) 2007-2020 Daniel Adler <dadler@uni-goettingen.de>,
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 dynload_windows.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 dynload module for .dll files
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 #include "dynload.h"
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
38 #include "dynload_alloc.h"
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 #include <windows.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
229
0ce6beba55df - dynload_windows.c cosmetics
Tassilo Philipp
parents: 0
diff changeset
42
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 DLLib* dlLoadLibrary(const char* libPath)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 {
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
45 if(libPath == NULL)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
46 return (DLLib*)GetModuleHandle(NULL);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
47 else {
350
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
48 /* convert from UTF-8 to wide chars, so count required size */
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
49 DLLib* pLib;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
50 wchar_t* ws;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
51 int r = MultiByteToWideChar(CP_UTF8, 0, libPath, -1, NULL, 0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
52 if(!r) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
53 return NULL;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
54 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
55
350
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
56 /* Reserve temp space with room for extra '.' suffix (see below) */
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
57 ws = (wchar_t*)dlAllocMem((r+1) * sizeof(wchar_t));
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
58 if(!ws)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
59 return NULL;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
60
350
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
61 /* Convert path and add a '.' suffix, needed to tell windows not to add
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
62 .dll to any path that doesn't have it (see MS doc for LoadLibraryW).
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
63 This is to get same behaviour as on other platforms which don't do any
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
64 magic like this. Library search path behaviour stays unaffected, though */
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
65 pLib = NULL;
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
66 if(MultiByteToWideChar(CP_UTF8, 0, libPath, -1, ws, r) == r) {
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
67 ws[r-1] = '.';
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
68 ws[r] = 0;
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
69 pLib = (DLLib*)LoadLibraryW(ws);
655cafde0859 - dynload/windows: dlLoadLibrary behaviour now more in line with other platforms by not trying to load provided path with .dll suffix
Tassilo Philipp
parents: 310
diff changeset
70 }
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
71
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
72 /* ... free temp space and return handle */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
73 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
74 return pLib;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
75 }
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
78
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
79 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
81 return (void*)GetProcAddress((HINSTANCE)pLib, pSymbolName);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
84
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
85 void dlFreeLibrary(DLLib* pLib)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
87 FreeLibrary((HINSTANCE)pLib);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
90
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
91 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
92 {
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
93 /* get the path name as wide chars, then convert to UTF-8; we need */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
94 /* some trial and error to figure out needed wide char string length */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
95
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
96 wchar_t* ws;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
97 int r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
98
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
99 /* num chars to alloc temp space for, and upper limit, must be both power */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
100 /* of 2s for loop to be precise and to test allow testing up to 32768 chars */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
101 /* (including \0), which is the extended path ("\\?\...") maximum */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
102 static const int MAX_EXT_PATH = 1<<15; /* max extended path length (32768) */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
103 int nc = 1<<6; /* guess start buffer size, */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
104
310
2e8be88dad11 - comment cleanup
Tassilo Philipp
parents: 308
diff changeset
105 while(nc <= MAX_EXT_PATH)/*@@@ add testcode for super long paths*/
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
106 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
107 ws = (wchar_t*)dlAllocMem(nc * sizeof(wchar_t));
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
108 if(!ws)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
109 break;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
110
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
111 r = GetModuleFileNameW((HMODULE)pLib, ws, nc);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
112
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
113 /* r == nc if string was truncated, double temp buffer size */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
114 if(r == nc) {
310
2e8be88dad11 - comment cleanup
Tassilo Philipp
parents: 308
diff changeset
115 nc <<= 1;
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
116 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
117 continue;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
118 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
119 /* error if r is 0 */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
120 else if(!r) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
121 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
122 break;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
123 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
124
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
125 /* check if output buffer is big enough */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
126 r = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
127 if(r <= bufSize)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
128 r = WideCharToMultiByte(CP_UTF8, 0, ws, -1, sOut, bufSize, NULL, NULL);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
129
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
130 /* cleanup and return either size of copied bytes or needed buffer size */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
131 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
132 return r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
133 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
134
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
135 return 0;
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
136 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
137