Unlike Linux and OS X, when a library is loaded, all the symbols aren't loaded into a single namespace. You must fetch the symbol by iterating over all loaded modules. So, we shouldn't use /FORCE:UNRESOLVED since that is not what modules do on Windows. Instead, we now do exactly what GModule does on Windows. Also use `void` for functions that take no arguments.pull/1126/head
parent
dc1f537fb3
commit
f5a9b3b249
4 changed files with 129 additions and 19 deletions
@ -1,38 +1,102 @@ |
||||
|
||||
#include <stdio.h> |
||||
|
||||
int func_from_language_runtime(void); |
||||
typedef int (*fptr) (void); |
||||
|
||||
#ifdef _WIN32 |
||||
// FIXME: add implementation using Winapi functions for dlopen.
|
||||
|
||||
int main(int argc, char **argv) { |
||||
return 0; |
||||
#include <windows.h> |
||||
|
||||
wchar_t* |
||||
win32_get_last_error (void) |
||||
{ |
||||
wchar_t *msg = NULL; |
||||
|
||||
FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
||||
| FORMAT_MESSAGE_IGNORE_INSERTS |
||||
| FORMAT_MESSAGE_FROM_SYSTEM, |
||||
NULL, GetLastError (), 0, |
||||
(LPWSTR) &msg, 0, NULL); |
||||
return msg; |
||||
} |
||||
|
||||
int |
||||
main (int argc, char **argv) |
||||
{ |
||||
HINSTANCE handle; |
||||
fptr importedfunc; |
||||
int expected, actual; |
||||
int ret = 1; |
||||
|
||||
handle = LoadLibraryA (argv[1]); |
||||
if (!handle) { |
||||
wchar_t *msg = win32_get_last_error (); |
||||
printf ("Could not open %s: %S\n", argv[1], msg); |
||||
goto nohandle; |
||||
} |
||||
|
||||
importedfunc = (fptr) GetProcAddress (handle, "func"); |
||||
if (importedfunc == NULL) { |
||||
wchar_t *msg = win32_get_last_error (); |
||||
printf ("Could not find 'func': %S\n", msg); |
||||
goto out; |
||||
} |
||||
|
||||
actual = importedfunc (); |
||||
expected = func_from_language_runtime (); |
||||
if (actual != expected) { |
||||
printf ("Got %i instead of %i\n", actual, expected); |
||||
goto out; |
||||
} |
||||
|
||||
ret = 0; |
||||
out: |
||||
FreeLibrary (handle); |
||||
nohandle: |
||||
return ret; |
||||
} |
||||
|
||||
#else |
||||
|
||||
#include<dlfcn.h> |
||||
#include<assert.h> |
||||
#include<stdio.h> |
||||
|
||||
int func(); |
||||
int func_from_language_runtime(); |
||||
|
||||
int main(int argc, char **argv) { |
||||
void *dl; |
||||
int (*importedfunc)(); |
||||
int success; |
||||
fptr importedfunc; |
||||
int expected, actual; |
||||
char *error; |
||||
int ret = 1; |
||||
|
||||
dlerror(); |
||||
dl = dlopen(argv[1], RTLD_LAZY); |
||||
error = dlerror(); |
||||
if(error) { |
||||
printf("Could not open %s: %s\n", argv[1], error); |
||||
return 1; |
||||
goto nodl; |
||||
} |
||||
importedfunc = (int (*)()) dlsym(dl, "func"); |
||||
assert(importedfunc); |
||||
|
||||
importedfunc = (fptr) dlsym(dl, "func"); |
||||
if (importedfunc == NULL) { |
||||
printf ("Could not find 'func'\n"); |
||||
goto out; |
||||
} |
||||
|
||||
assert(importedfunc != func_from_language_runtime); |
||||
success = (*importedfunc)() == func_from_language_runtime(); |
||||
|
||||
actual = (*importedfunc)(); |
||||
expected = func_from_language_runtime (); |
||||
if (actual != expected) { |
||||
printf ("Got %i instead of %i\n", actual, expected); |
||||
goto out; |
||||
} |
||||
|
||||
ret = 0; |
||||
out: |
||||
dlclose(dl); |
||||
return !success; |
||||
nodl: |
||||
return ret; |
||||
} |
||||
|
||||
#endif |
||||
|
Loading…
Reference in new issue