This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathsystem-library-w32.hpp
More file actions
169 lines (136 loc) · 5.33 KB
/
system-library-w32.hpp
File metadata and controls
169 lines (136 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* -*-c++-*-
Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "system-private.h"
#include <foundation-auto.h>
#include <windows.h>
/* ================================================================
* HMODULE Handle Class
* ================================================================ */
/* The __MCSLibraryHandleWin32 class uses the standard Win32 HMODULE
* manipulation functions to provide the handle functionality.
*/
class __MCSLibraryHandleWin32
{
public:
__MCSLibraryHandleWin32(void)
: m_handle(nullptr)
{
}
~__MCSLibraryHandleWin32(void)
{
if (m_handle == nullptr)
return;
FreeLibrary(m_handle);
}
bool IsDefined(void) const
{
return m_handle != nullptr;
}
bool IsEqualTo(const __MCSLibraryHandleWin32& p_other) const
{
return m_handle == p_other.m_handle;
}
bool Hash(void) const
{
return MCHashPointer(static_cast<void *>(m_handle));
}
bool CreateWithNativePath(MCStringRef p_native_path)
{
MCAutoStringRefAsWString t_wstring_path;
if (!t_wstring_path.Lock(p_native_path))
{
return false;
}
m_handle = LoadLibraryExW(*t_wstring_path,
NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
if (m_handle == NULL)
{
/* TODO: Use GetLastError() */
return __MCSLibraryThrowCreateWithNativePathFailed(p_native_path);
}
return true;
}
bool CreateWithAddress(void *p_address)
{
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)p_address,
&m_handle))
{
/* TODO: Use GetLastError() */
m_handle = nullptr;
return __MCSLibraryThrowCreateWithAddressFailed(p_address);
}
return true;
}
bool CopyNativePath(MCStringRef & r_native_path) const
{
MCAutoArray<wchar_t> t_native_path;
for (uindex_t i = 1; i < (UINDEX_MAX-1) / MAX_PATH; ++i)
{
if (!t_native_path.Extend(i*MAX_PATH + 1))
{
return false;
}
// Make sure the last char in the buffer is NUL so that we can detect
// failure on Windows XP
t_native_path[t_native_path.Size() - 1] = '\0';
// On Windows XP, the returned path will be truncated if the buffer is
// too small and a NUL byte *will not* be added.
DWORD t_result_size = GetModuleFileNameW(m_handle,
t_native_path.Ptr(),
t_native_path.Size());
DWORD t_error_code = GetLastError();
// A too small buffer is indicated by a 0 return value and:
// on XP: the buffer being filled without terminating NUL
// others: GetLastError() returning ERROR_INSUFFICIENT_BUFFER
if (t_result_size == 0 &&
(t_error_code == ERROR_SUCCESS &&
t_native_path[t_native_path.Size() - 1] != '\0') ||
t_error_code == ERROR_INSUFFICIENT_BUFFER)
{
continue; /* Try again with a bigger buffer */
}
// If we get an error other than insufficient buffer, then we return
// it.
if (t_error_code != ERROR_SUCCESS)
{
/* TODO[2017-02-21]: Use last error */
break;
}
// Make sure the string is NUL terminated, we already know MAX_PATH-1
// is NUL, on non-Windows XP char t_native_path_size will be NUL, on
// WindowsXP t_native_path_size will be the length of the filename
// without NUL.
MCAssert(t_result_size < t_native_path.Size());
t_native_path[t_result_size] = '\0';
return MCStringCreateWithWString(t_native_path.Ptr(), r_native_path);
}
/* TODO[20170221] Oh dear, the path is too long to fit into a UINDEX_MAX!?! */
return __MCSLibraryThrowResolvePathFailed();
}
void *LookupSymbol(MCStringRef p_symbol) const
{
MCAutoStringRefAsCString t_cstring_symbol;
if (!t_cstring_symbol.Lock(p_symbol))
{
/* MASKS_OOM */
return nullptr;
}
return GetProcAddress(m_handle,
*t_cstring_symbol);
}
protected:
HMODULE m_handle;
};
typedef class __MCSLibraryHandleWin32 __MCSLibraryHandle;