annotate dynload/dynload_windows.c @ 315:3840e0188520

- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
author Tassilo Philipp
date Wed, 06 Nov 2019 14:13:49 +0100
parents 2e8be88dad11
children 655cafde0859
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
281
f5577f6bf97a - file header cleanups for release
Tassilo Philipp
parents: 242
diff changeset
9 Copyright (c) 2007-2018 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 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
48 /* convert from UTF-8 to wide chars, so count required size... */
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
56 /* ... reserve temp space, ... */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
57 ws = (wchar_t*)dlAllocMem(r * sizeof(wchar_t));
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
61 /* ... convert (and use r as success flag), ... */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
62 r = (MultiByteToWideChar(CP_UTF8, 0, libPath, -1, ws, r) == r);
310
2e8be88dad11 - comment cleanup
Tassilo Philipp
parents: 308
diff changeset
63 pLib = (DLLib*)(r ? LoadLibraryW(ws) : NULL);
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
64
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
65 /* ... free temp space and return handle */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
66 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
67 return pLib;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
68 }
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
71
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
72 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
74 return (void*)GetProcAddress((HINSTANCE)pLib, pSymbolName);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
77
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
78 void dlFreeLibrary(DLLib* pLib)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
80 FreeLibrary((HINSTANCE)pLib);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
83
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
84 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
85 {
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
86 /* 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
87 /* 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
88
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
89 wchar_t* ws;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
90 int r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
91
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
92 /* 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
93 /* 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
94 /* (including \0), which is the extended path ("\\?\...") maximum */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
95 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
96 int nc = 1<<6; /* guess start buffer size, */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
97
310
2e8be88dad11 - comment cleanup
Tassilo Philipp
parents: 308
diff changeset
98 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
99 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
100 ws = (wchar_t*)dlAllocMem(nc * sizeof(wchar_t));
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
101 if(!ws)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
102 break;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
103
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
104 r = GetModuleFileNameW((HMODULE)pLib, ws, nc);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
105
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
106 /* r == nc if string was truncated, double temp buffer size */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
107 if(r == nc) {
310
2e8be88dad11 - comment cleanup
Tassilo Philipp
parents: 308
diff changeset
108 nc <<= 1;
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
109 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
110 continue;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
111 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
112 /* error if r is 0 */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
113 else if(!r) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
114 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
115 break;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
116 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
117
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
118 /* check if output buffer is big enough */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
119 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
120 if(r <= bufSize)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
121 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
122
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
123 /* 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
124 dlFreeMem(ws);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
125 return r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
126 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
127
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
128 return 0;
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
129 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 229
diff changeset
130