| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
int func_from_language_runtime(void); |
| 5 |
typedef int (*fptr) (void); |
| 6 |
|
| 7 |
#ifdef _WIN32 |
| 8 |
|
| 9 |
#include <windows.h> |
| 10 |
|
| 11 |
static wchar_t* |
| 12 |
win32_get_last_error (void) |
| 13 |
{ |
| 14 |
wchar_t *msg = NULL; |
| 15 |
|
| 16 |
FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 17 |
| FORMAT_MESSAGE_IGNORE_INSERTS |
| 18 |
| FORMAT_MESSAGE_FROM_SYSTEM, |
| 19 |
NULL, GetLastError (), 0, |
| 20 |
(LPWSTR) &msg, 0, NULL); |
| 21 |
return msg; |
| 22 |
} |
| 23 |
|
| 24 |
int main(int argc, char **argv) |
| 25 |
{ |
| 26 |
HINSTANCE handle; |
| 27 |
fptr importedfunc; |
| 28 |
int expected, actual; |
| 29 |
int ret = 1; |
| 30 |
if(argc==0) {}; |
| 31 |
|
| 32 |
handle = LoadLibraryA (argv[1]); |
| 33 |
if (!handle) { |
| 34 |
wchar_t *msg = win32_get_last_error (); |
| 35 |
printf ("Could not open %s: %S\n", argv[1], msg); |
| 36 |
goto nohandle; |
| 37 |
} |
| 38 |
|
| 39 |
importedfunc = (fptr) GetProcAddress (handle, "func"); |
| 40 |
if (importedfunc == NULL) { |
| 41 |
wchar_t *msg = win32_get_last_error (); |
| 42 |
printf ("Could not find 'func': %S\n", msg); |
| 43 |
goto out; |
| 44 |
} |
| 45 |
|
| 46 |
actual = importedfunc (); |
| 47 |
expected = func_from_language_runtime (); |
| 48 |
if (actual != expected) { |
| 49 |
printf ("Got %i instead of %i\n", actual, expected); |
| 50 |
goto out; |
| 51 |
} |
| 52 |
|
| 53 |
ret = 0; |
| 54 |
out: |
| 55 |
FreeLibrary (handle); |
| 56 |
nohandle: |
| 57 |
return ret; |
| 58 |
} |
| 59 |
|
| 60 |
#else |
| 61 |
|
| 62 |
#include<dlfcn.h> |
| 63 |
#include<assert.h> |
| 64 |
|
| 65 |
int main(int argc, char **argv) { |
| 66 |
void *dl; |
| 67 |
fptr importedfunc; |
| 68 |
int expected, actual; |
| 69 |
char *error; |
| 70 |
int ret = 1; |
| 71 |
if(argc==0) {}; |
| 72 |
|
| 73 |
dlerror(); |
| 74 |
dl = dlopen(argv[1], RTLD_LAZY); |
| 75 |
error = dlerror(); |
| 76 |
if(error) { |
| 77 |
printf("Could not open %s: %s\n", argv[1], error); |
| 78 |
goto nodl; |
| 79 |
} |
| 80 |
|
| 81 |
importedfunc = (fptr) dlsym(dl, "func"); |
| 82 |
if (importedfunc == NULL) { |
| 83 |
printf ("Could not find 'func'\n"); |
| 84 |
goto out; |
| 85 |
} |
| 86 |
|
| 87 |
assert(importedfunc != func_from_language_runtime); |
| 88 |
|
| 89 |
actual = (*importedfunc)(); |
| 90 |
expected = func_from_language_runtime (); |
| 91 |
if (actual != expected) { |
| 92 |
printf ("Got %i instead of %i\n", actual, expected); |
| 93 |
goto out; |
| 94 |
} |
| 95 |
|
| 96 |
ret = 0; |
| 97 |
out: |
| 98 |
dlclose(dl); |
| 99 |
nodl: |
| 100 |
return ret; |
| 101 |
} |
| 102 |
|
| 103 |
#endif |