2012-10-05 02:57:32 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
2012-10-05 07:19:05 +04:00
|
|
|
#include <winpr/path.h>
|
|
|
|
#include <winpr/tchar.h>
|
2012-10-05 02:57:32 +04:00
|
|
|
#include <winpr/windows.h>
|
2012-10-05 22:45:54 +04:00
|
|
|
#include <winpr/library.h>
|
2012-10-05 02:57:32 +04:00
|
|
|
|
2012-10-19 00:58:27 +04:00
|
|
|
typedef int (*TEST_AB_FN)(int a, int b);
|
|
|
|
|
2012-10-05 02:57:32 +04:00
|
|
|
int TestLibraryGetProcAddress(int argc, char* argv[])
|
|
|
|
{
|
2012-10-19 00:58:27 +04:00
|
|
|
int a, b, c;
|
2012-10-18 23:37:00 +04:00
|
|
|
HINSTANCE library;
|
2012-10-19 00:58:27 +04:00
|
|
|
TEST_AB_FN pFunctionA;
|
|
|
|
TEST_AB_FN pFunctionB;
|
2016-06-10 14:10:39 +03:00
|
|
|
LPCSTR SharedLibraryExtension;
|
|
|
|
CHAR LibraryPath[PATHCCH_MAX_CCH];
|
|
|
|
PCHAR p;
|
2012-10-05 07:19:05 +04:00
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH))
|
2015-04-03 17:21:01 +03:00
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: GetModuleFilenameA failed: 0x%08"PRIX32"\n", __FUNCTION__, GetLastError());
|
2015-04-03 17:21:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-10 14:10:39 +03:00
|
|
|
|
|
|
|
/* PathCchRemoveFileSpec is not implemented in WinPR */
|
|
|
|
|
|
|
|
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: Error identifying module directory path\n", __FUNCTION__);
|
2015-06-17 23:08:02 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-10 14:10:39 +03:00
|
|
|
*p = 0;
|
2016-05-26 13:19:36 +03:00
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
|
|
|
|
SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
|
|
|
|
NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
|
2016-05-26 13:19:36 +03:00
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: Loading Library: '%s'\n", __FUNCTION__, LibraryPath);
|
2012-10-05 07:19:05 +04:00
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
if (!(library = LoadLibraryA(LibraryPath)))
|
2012-10-05 07:19:05 +04:00
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: LoadLibraryA failure: 0x%08"PRIX32"\n", __FUNCTION__, GetLastError());
|
2012-10-05 07:19:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
if (!(pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA")))
|
2012-10-19 00:58:27 +04:00
|
|
|
{
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: GetProcAddress failure (FunctionA)\n", __FUNCTION__);
|
2012-10-19 00:58:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
if (!(pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB")))
|
2012-10-19 00:58:27 +04:00
|
|
|
{
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: GetProcAddress failure (FunctionB)\n", __FUNCTION__);
|
2012-10-19 00:58:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 2;
|
|
|
|
b = 3;
|
|
|
|
|
|
|
|
c = pFunctionA(a, b); /* LibraryA / FunctionA multiplies a and b */
|
|
|
|
|
|
|
|
if (c != (a * b))
|
|
|
|
{
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: pFunctionA call failed\n", __FUNCTION__);
|
2012-10-19 00:58:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 10;
|
|
|
|
b = 5;
|
|
|
|
|
|
|
|
c = pFunctionB(a, b); /* LibraryA / FunctionB divides a by b */
|
|
|
|
|
|
|
|
if (c != (a / b))
|
|
|
|
{
|
2016-06-10 14:10:39 +03:00
|
|
|
printf("%s: pFunctionB call failed\n", __FUNCTION__);
|
2012-10-19 00:58:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-05 07:19:05 +04:00
|
|
|
if (!FreeLibrary(library))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: FreeLibrary failure: 0x%08"PRIX32"\n", __FUNCTION__, GetLastError());
|
2012-10-05 07:19:05 +04:00
|
|
|
return -1;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
2012-10-05 07:19:05 +04:00
|
|
|
|
2012-10-05 02:57:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|