diff options
Diffstat (limited to 'test.c')
-rw-r--r-- | test.c | 87 |
1 files changed, 87 insertions, 0 deletions
@@ -2,6 +2,7 @@ | |||
2 | * dlfcn-win32 | 2 | * dlfcn-win32 |
3 | * Copyright (c) 2007-2009 Ramiro Polla | 3 | * Copyright (c) 2007-2009 Ramiro Polla |
4 | * Copyright (c) 2014 Tiancheng "Timothy" Gu | 4 | * Copyright (c) 2014 Tiancheng "Timothy" Gu |
5 | * Copyright (c) 2019 Pali Rohár <pali.rohar@gmail.com> | ||
5 | * | 6 | * |
6 | * dlfcn-win32 is free software; you can redistribute it and/or | 7 | * dlfcn-win32 is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | 8 | * modify it under the terms of the GNU Lesser General Public |
@@ -25,6 +26,7 @@ | |||
25 | #endif | 26 | #endif |
26 | #include <stdio.h> | 27 | #include <stdio.h> |
27 | #include <string.h> | 28 | #include <string.h> |
29 | #include <windows.h> | ||
28 | #include "dlfcn.h" | 30 | #include "dlfcn.h" |
29 | 31 | ||
30 | /* If these dlclose's fails, we don't care as the handles are going to be | 32 | /* If these dlclose's fails, we don't care as the handles are going to be |
@@ -71,12 +73,16 @@ | |||
71 | int main() | 73 | int main() |
72 | { | 74 | { |
73 | void *global; | 75 | void *global; |
76 | void *library2; | ||
74 | void *library; | 77 | void *library; |
75 | char *error; | 78 | char *error; |
76 | int (*function)( void ); | 79 | int (*function)( void ); |
80 | int (*function2_from_library2)( void ); | ||
77 | size_t (*fwrite_local) ( const void *, size_t, size_t, FILE * ); | 81 | size_t (*fwrite_local) ( const void *, size_t, size_t, FILE * ); |
82 | size_t (*fputs_default) ( const char *, FILE * ); | ||
78 | int (*nonexistentfunction)( void ); | 83 | int (*nonexistentfunction)( void ); |
79 | int ret; | 84 | int ret; |
85 | HMODULE library3; | ||
80 | 86 | ||
81 | #ifdef _DEBUG | 87 | #ifdef _DEBUG |
82 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); | 88 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); |
@@ -87,6 +93,16 @@ int main() | |||
87 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); | 93 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); |
88 | #endif | 94 | #endif |
89 | 95 | ||
96 | library2 = dlopen( "testdll2.dll", RTLD_GLOBAL ); | ||
97 | if( !library2 ) | ||
98 | { | ||
99 | error = dlerror( ); | ||
100 | printf( "ERROR\tCould not open library2 globally: %s\n", error ? error : "" ); | ||
101 | RETURN_ERROR; | ||
102 | } | ||
103 | else | ||
104 | printf( "SUCCESS\tOpened library2 globally: %p\n", library2 ); | ||
105 | |||
90 | library = dlopen( "testdll.dll", RTLD_GLOBAL ); | 106 | library = dlopen( "testdll.dll", RTLD_GLOBAL ); |
91 | if( !library ) | 107 | if( !library ) |
92 | { | 108 | { |
@@ -124,6 +140,22 @@ int main() | |||
124 | fwrite_local(hello_world,sizeof(char),strlen(hello_world),stderr); | 140 | fwrite_local(hello_world,sizeof(char),strlen(hello_world),stderr); |
125 | fflush(stderr); | 141 | fflush(stderr); |
126 | 142 | ||
143 | fputs_default = dlsym(RTLD_DEFAULT, "fputs"); | ||
144 | if (!fputs_default) | ||
145 | { | ||
146 | error = dlerror(); | ||
147 | printf("ERROR\tCould not get symbol from default handle: %s\n", | ||
148 | error ? error : ""); | ||
149 | CLOSE_LIB; | ||
150 | CLOSE_GLOBAL; | ||
151 | RETURN_ERROR; | ||
152 | } | ||
153 | else | ||
154 | printf("SUCCESS\tGot symbol from default handle: %p\n", fputs_default); | ||
155 | char * hello_world_fputs = "Hello world from default fputs!\n"; | ||
156 | fputs_default(hello_world_fputs, stderr); | ||
157 | fflush(stderr); | ||
158 | |||
127 | function = dlsym( library, "function" ); | 159 | function = dlsym( library, "function" ); |
128 | if( !function ) | 160 | if( !function ) |
129 | { | 161 | { |
@@ -139,6 +171,27 @@ int main() | |||
139 | 171 | ||
140 | RUNFUNC; | 172 | RUNFUNC; |
141 | 173 | ||
174 | function2_from_library2 = dlsym( library2, "function2" ); | ||
175 | if( !function2_from_library2 ) | ||
176 | { | ||
177 | error = dlerror( ); | ||
178 | printf( "ERROR\tCould not get symbol from library2 handle: %s\n", | ||
179 | error ? error : "" ); | ||
180 | CLOSE_LIB; | ||
181 | CLOSE_GLOBAL; | ||
182 | RETURN_ERROR; | ||
183 | } | ||
184 | else | ||
185 | printf( "SUCCESS\tGot symbol from library2 handle: %p\n", function2_from_library2 ); | ||
186 | |||
187 | ret = function2_from_library2 (); | ||
188 | if( ret != 2 ) | ||
189 | { | ||
190 | CLOSE_LIB; | ||
191 | CLOSE_GLOBAL; | ||
192 | RETURN_ERROR; | ||
193 | } | ||
194 | |||
142 | nonexistentfunction = dlsym( library, "nonexistentfunction" ); | 195 | nonexistentfunction = dlsym( library, "nonexistentfunction" ); |
143 | if( nonexistentfunction ) | 196 | if( nonexistentfunction ) |
144 | { | 197 | { |
@@ -194,6 +247,16 @@ int main() | |||
194 | else | 247 | else |
195 | printf( "SUCCESS\tClosed library.\n" ); | 248 | printf( "SUCCESS\tClosed library.\n" ); |
196 | 249 | ||
250 | ret = dlclose( library2 ); | ||
251 | if( ret ) | ||
252 | { | ||
253 | error = dlerror( ); | ||
254 | printf( "ERROR\tCould not close library2: %s\n", error ? error : "" ); | ||
255 | RETURN_ERROR; | ||
256 | } | ||
257 | else | ||
258 | printf( "SUCCESS\tClosed library2.\n" ); | ||
259 | |||
197 | library = dlopen( "testdll.dll", RTLD_LOCAL ); | 260 | library = dlopen( "testdll.dll", RTLD_LOCAL ); |
198 | if( !library ) | 261 | if( !library ) |
199 | { | 262 | { |
@@ -335,6 +398,15 @@ int main() | |||
335 | printf("SUCCESS\tGot symbol from global handle: %p\n", function); | 398 | printf("SUCCESS\tGot symbol from global handle: %p\n", function); |
336 | 399 | ||
337 | 400 | ||
401 | library3 = LoadLibraryA("testdll3.dll"); | ||
402 | if (!library3) | ||
403 | { | ||
404 | printf( "ERROR\tCould not open library3 via WINAPI\n" ); | ||
405 | RETURN_ERROR; | ||
406 | } | ||
407 | else | ||
408 | printf( "SUCCESS\tOpened library3 via WINAPI: %p\n", library3 ); | ||
409 | |||
338 | ret = dlclose( library ); | 410 | ret = dlclose( library ); |
339 | if( ret ) | 411 | if( ret ) |
340 | { | 412 | { |
@@ -346,6 +418,21 @@ int main() | |||
346 | else | 418 | else |
347 | printf( "SUCCESS\tClosed library.\n" ); | 419 | printf( "SUCCESS\tClosed library.\n" ); |
348 | 420 | ||
421 | function = dlsym(global, "function3"); | ||
422 | if (!function) | ||
423 | { | ||
424 | error = dlerror(); | ||
425 | printf("ERROR\tCould not get symbol from global handle: %s\n", | ||
426 | error ? error : ""); | ||
427 | CLOSE_LIB; | ||
428 | CLOSE_GLOBAL; | ||
429 | RETURN_ERROR; | ||
430 | } | ||
431 | else | ||
432 | printf("SUCCESS\tGot symbol from global handle: %p\n", function); | ||
433 | |||
434 | RUNFUNC; | ||
435 | |||
349 | ret = dlclose( global ); | 436 | ret = dlclose( global ); |
350 | if( ret ) | 437 | if( ret ) |
351 | { | 438 | { |