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-05 07:19:05 +04:00
|
|
|
char* str;
|
|
|
|
int length;
|
2012-10-19 00:58:27 +04:00
|
|
|
int a, b, c;
|
2012-10-05 07:19:05 +04:00
|
|
|
LPTSTR BasePath;
|
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;
|
2012-10-18 23:37:00 +04:00
|
|
|
LPCTSTR SharedLibraryExtension;
|
|
|
|
TCHAR LibraryPath[PATHCCH_MAX_CCH];
|
2012-10-05 07:19:05 +04:00
|
|
|
|
|
|
|
str = argv[1];
|
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0);
|
|
|
|
BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR));
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!BasePath)
|
|
|
|
{
|
2015-04-07 15:38:09 +03:00
|
|
|
_tprintf(_T("Memory allocation failed\n"));
|
2015-04-03 17:21:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-05 07:19:05 +04:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR));
|
|
|
|
BasePath[length] = 0;
|
|
|
|
#else
|
2012-10-05 22:45:54 +04:00
|
|
|
BasePath = _strdup(str);
|
2012-10-18 23:37:00 +04:00
|
|
|
length = strlen(BasePath);
|
2012-10-05 07:19:05 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
CopyMemory(LibraryPath, BasePath, length * sizeof(TCHAR));
|
2012-10-19 05:11:12 +04:00
|
|
|
LibraryPath[length] = 0;
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* subdirectory */
|
|
|
|
NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* file name without extension */
|
2012-10-05 07:19:05 +04:00
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
SharedLibraryExtension = PathGetSharedLibraryExtension(PATH_SHARED_LIB_EXT_WITH_DOT);
|
|
|
|
NativePathCchAddExtension(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension); /* add shared library extension */
|
2012-10-05 07:19:05 +04:00
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
_tprintf(_T("Loading Library: %s\n"), LibraryPath);
|
2012-10-05 07:19:05 +04:00
|
|
|
|
|
|
|
library = LoadLibrary(LibraryPath);
|
|
|
|
|
|
|
|
if (!library)
|
|
|
|
{
|
2012-10-18 23:37:00 +04:00
|
|
|
_tprintf(_T("LoadLibrary failure\n"));
|
2012-10-05 07:19:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-19 05:11:12 +04:00
|
|
|
pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA");
|
2012-10-19 00:58:27 +04:00
|
|
|
|
|
|
|
if (!pFunctionA)
|
|
|
|
{
|
|
|
|
_tprintf(_T("GetProcAddress failure (FunctionA)\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-19 05:11:12 +04:00
|
|
|
pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB");
|
2012-10-19 00:58:27 +04:00
|
|
|
|
|
|
|
if (!pFunctionB)
|
|
|
|
{
|
|
|
|
_tprintf(_T("GetProcAddress failure (FunctionB)\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 2;
|
|
|
|
b = 3;
|
|
|
|
|
|
|
|
c = pFunctionA(a, b); /* LibraryA / FunctionA multiplies a and b */
|
|
|
|
|
|
|
|
if (c != (a * b))
|
|
|
|
{
|
|
|
|
_tprintf(_T("pFunctionA call failed\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 10;
|
|
|
|
b = 5;
|
|
|
|
|
|
|
|
c = pFunctionB(a, b); /* LibraryA / FunctionB divides a by b */
|
|
|
|
|
|
|
|
if (c != (a / b))
|
|
|
|
{
|
|
|
|
_tprintf(_T("pFunctionB call failed\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-05 07:19:05 +04:00
|
|
|
if (!FreeLibrary(library))
|
|
|
|
{
|
2012-10-18 23:37:00 +04:00
|
|
|
_tprintf(_T("FreeLibrary failure\n"));
|
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;
|
|
|
|
}
|