FreeRDP/winpr/libwinpr/library/test/TestLibraryGetProcAddress.c

87 lines
2.0 KiB
C
Raw Normal View History

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