| 1 |
#include "ffi_platypus.h" |
| 2 |
|
| 3 |
#ifdef PERL_OS_WINDOWS |
| 4 |
|
| 5 |
#ifdef HAVE_WINDOWS_H |
| 6 |
#include <windows.h> |
| 7 |
#endif |
| 8 |
#ifdef HAVE_SYS_CYGWIN_H |
| 9 |
#include <sys/cygwin.h> |
| 10 |
#endif |
| 11 |
#ifdef HAVE_STRING_H |
| 12 |
#include <string.h> |
| 13 |
#endif |
| 14 |
/* |
| 15 |
* TODO: c::ac is not detecting psapi.h for some reason ... |
| 16 |
* but it should always be there in any platform that |
| 17 |
* we support |
| 18 |
*/ |
| 19 |
#include <psapi.h> |
| 20 |
|
| 21 |
typedef struct _library_handle { |
| 22 |
int is_null; |
| 23 |
int flags; |
| 24 |
HMODULE os_handle; |
| 25 |
} library_handle; |
| 26 |
|
| 27 |
static const char *error = NULL; |
| 28 |
|
| 29 |
/* |
| 30 |
* dlopen() |
| 31 |
*/ |
| 32 |
|
| 33 |
void * |
| 34 |
windlopen(const char *filename, int flags) |
| 35 |
{ |
| 36 |
char *win_path_filename; |
| 37 |
library_handle *handle; |
| 38 |
|
| 39 |
win_path_filename = NULL; |
| 40 |
|
| 41 |
#ifdef PERL_OS_CYGWIN |
| 42 |
if(filename != NULL) |
| 43 |
{ |
| 44 |
ssize_t size; |
| 45 |
size = cygwin_conv_path(CCP_POSIX_TO_WIN_A | CCP_RELATIVE, filename, NULL, 0); |
| 46 |
if(size < 0) |
| 47 |
{ |
| 48 |
error = "unable to determine length of string for cygwin_conv_path"; |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
win_path_filename = malloc(size); |
| 52 |
if(win_path_filename == NULL) |
| 53 |
{ |
| 54 |
error = "unable to allocate enough memory for cygwin_conv_path"; |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
if(cygwin_conv_path(CCP_POSIX_TO_WIN_A | CCP_RELATIVE, filename, win_path_filename, size)) |
| 58 |
{ |
| 59 |
error = "error in conversion for cygwin_conv_path"; |
| 60 |
free(win_path_filename); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
filename = win_path_filename; |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
handle = malloc(sizeof(library_handle)); |
| 68 |
|
| 69 |
if(handle == NULL) |
| 70 |
{ |
| 71 |
if(win_path_filename != NULL) |
| 72 |
free(win_path_filename); |
| 73 |
error = "unable to allocate memory for handle"; |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
if(filename == NULL) |
| 78 |
{ |
| 79 |
handle->is_null = 1; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
handle->is_null = 0; |
| 84 |
handle->os_handle = LoadLibrary(filename); |
| 85 |
if(handle->os_handle == NULL) |
| 86 |
{ |
| 87 |
free(handle); |
| 88 |
error = "Error loading file"; |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
handle->flags = flags; |
| 94 |
|
| 95 |
if(win_path_filename != NULL) |
| 96 |
free(win_path_filename); |
| 97 |
error = NULL; |
| 98 |
return (void*) handle; |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* dlsym() |
| 103 |
*/ |
| 104 |
|
| 105 |
void * |
| 106 |
windlsym(void *void_handle, const char *symbol_name) |
| 107 |
{ |
| 108 |
library_handle *handle = (library_handle*) void_handle; |
| 109 |
static const char *not_found = "symbol not found"; |
| 110 |
void *symbol; |
| 111 |
|
| 112 |
if(!handle->is_null) |
| 113 |
{ |
| 114 |
symbol = GetProcAddress(handle->os_handle, symbol_name); |
| 115 |
if(symbol == NULL) |
| 116 |
error = not_found; |
| 117 |
else |
| 118 |
error = NULL; |
| 119 |
return symbol; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
int n; |
| 124 |
DWORD needed; |
| 125 |
HANDLE process; |
| 126 |
HMODULE mods[1024]; |
| 127 |
TCHAR mod_name[MAX_PATH]; |
| 128 |
|
| 129 |
process = OpenProcess( |
| 130 |
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, |
| 131 |
FALSE, GetCurrentProcessId() |
| 132 |
); |
| 133 |
|
| 134 |
if(process == NULL) |
| 135 |
{ |
| 136 |
error = "Process for self not found"; |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
|
| 140 |
if(EnumProcessModules(process, mods, sizeof(mods), &needed)) |
| 141 |
{ |
| 142 |
for(n=0; n < (needed/sizeof(HMODULE)); n++) |
| 143 |
{ |
| 144 |
if(GetModuleFileNameEx(process, mods[n], mod_name, sizeof(mod_name) / sizeof(TCHAR))) |
| 145 |
{ |
| 146 |
HMODULE handle = LoadLibrary(mod_name); |
| 147 |
if(handle == NULL) |
| 148 |
continue; |
| 149 |
symbol = GetProcAddress(handle, symbol_name); |
| 150 |
|
| 151 |
if(symbol != NULL) |
| 152 |
{ |
| 153 |
error = NULL; |
| 154 |
FreeLibrary(handle); |
| 155 |
return symbol; |
| 156 |
} |
| 157 |
|
| 158 |
FreeLibrary(handle); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
error = not_found; |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* dlerror() |
| 169 |
*/ |
| 170 |
|
| 171 |
const char * |
| 172 |
windlerror(void) |
| 173 |
{ |
| 174 |
return error; |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* dlclose() |
| 179 |
*/ |
| 180 |
|
| 181 |
int |
| 182 |
windlclose(void *void_handle) |
| 183 |
{ |
| 184 |
library_handle *handle = (library_handle*) void_handle; |
| 185 |
if(!handle->is_null) |
| 186 |
{ |
| 187 |
FreeLibrary(handle->os_handle); |
| 188 |
} |
| 189 |
free(handle); |
| 190 |
error = NULL; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
#endif |