int TestLibraryLoadLibrary(int argc, char* argv[]) { char* str; int length; LPTSTR BasePath; HINSTANCE library; LPCTSTR SharedLibraryExtension; TCHAR LibraryPath[PATHCCH_MAX_CCH]; str = argv[1]; #ifdef UNICODE length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0); BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR)); if (!BasePath) { _tprintf(_T("Memory allocation failed\n")); return -1; } MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR)); BasePath[length] = 0; #else BasePath = _strdup(str); if (!BasePath) { printf("Memory allocation failed"); return -1; } length = strlen(BasePath); #endif CopyMemory(LibraryPath, BasePath, length * sizeof(TCHAR)); LibraryPath[length] = 0; NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* subdirectory */ NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* file name without extension */ SharedLibraryExtension = PathGetSharedLibraryExtension(PATH_SHARED_LIB_EXT_WITH_DOT); NativePathCchAddExtension(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension); /* add shared library extension */ _tprintf(_T("Loading Library: %s\n"), LibraryPath); library = LoadLibrary(LibraryPath); if (!library) { _tprintf(_T("LoadLibrary failure\n")); return -1; } if (!FreeLibrary(library)) { _tprintf(_T("FreeLibrary failure\n")); return -1; } return 0; }
int TestFileFindFirstFile(int argc, char* argv[]) { char* str; int length; HANDLE hFind; LPTSTR BasePath; WIN32_FIND_DATA FindData; TCHAR FilePath[PATHCCH_MAX_CCH]; str = argv[1]; #ifdef UNICODE length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0); BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR)); if (!BasePath) { _tprintf(_T("Unable to allocate memory\n")); return -1; } MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR)); BasePath[length] = 0; #else BasePath = _strdup(str); if (!BasePath) { printf("Unable to allocate memory\n"); return -1; } length = strlen(BasePath); #endif CopyMemory(FilePath, BasePath, length * sizeof(TCHAR)); FilePath[length] = 0; PathCchConvertStyle(BasePath, length, PATH_STYLE_WINDOWS); NativePathCchAppend(FilePath, PATHCCH_MAX_CCH, _T("TestFile1")); _tprintf(_T("Finding file: %s\n"), FilePath); hFind = FindFirstFile(FilePath, &FindData); if (hFind == INVALID_HANDLE_VALUE) { _tprintf(_T("FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n"), FilePath); return -1; } _tprintf(_T("FindFirstFile: %s"), FindData.cFileName); if (_tcscmp(FindData.cFileName, testFile1) != 0) { _tprintf(_T("FindFirstFile failure: Expected: %s, Actual: %s\n"), testFile1, FindData.cFileName); return -1; } FindClose(hFind); return 0; }
int TestLibraryGetProcAddress(int argc, char* argv[]) { char* str; int length; int a, b, c; LPTSTR BasePath; HINSTANCE library; TEST_AB_FN pFunctionA; TEST_AB_FN pFunctionB; LPCTSTR SharedLibraryExtension; TCHAR LibraryPath[PATHCCH_MAX_CCH]; str = argv[1]; #ifdef UNICODE length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0); BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR)); if (!BasePath) { _tprintf(_T("Memory allocation failed\n")); return -1; } MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR)); BasePath[length] = 0; #else BasePath = _strdup(str); length = strlen(BasePath); #endif CopyMemory(LibraryPath, BasePath, length * sizeof(TCHAR)); LibraryPath[length] = 0; NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* subdirectory */ NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* file name without extension */ SharedLibraryExtension = PathGetSharedLibraryExtension(PATH_SHARED_LIB_EXT_WITH_DOT); NativePathCchAddExtension(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension); /* add shared library extension */ _tprintf(_T("Loading Library: %s\n"), LibraryPath); library = LoadLibrary(LibraryPath); if (!library) { _tprintf(_T("LoadLibrary failure\n")); return -1; } pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA"); if (!pFunctionA) { _tprintf(_T("GetProcAddress failure (FunctionA)\n")); return -1; } pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB"); 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; } if (!FreeLibrary(library)) { _tprintf(_T("FreeLibrary failure\n")); return -1; } return 0; }
int TestFileFindNextFile(int argc, char* argv[]) { char* str; int length; BOOL status; HANDLE hFind; LPTSTR BasePath; WIN32_FIND_DATA FindData; TCHAR FilePath[PATHCCH_MAX_CCH]; str = argv[1]; #ifdef UNICODE length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0); BasePath = (WCHAR*) calloc((length + 1), sizeof(WCHAR)); if (!BasePath) { _tprintf(_T("Unable to allocate memory")); return -1; } MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR)); BasePath[length] = 0; #else BasePath = _strdup(str); if (!BasePath) { printf("Unable to allocate memory"); return -1; } length = strlen(BasePath); #endif /* Simple filter matching all files inside current directory */ CopyMemory(FilePath, BasePath, length * sizeof(TCHAR)); FilePath[length] = 0; PathCchConvertStyle(BasePath, length, PATH_STYLE_WINDOWS); NativePathCchAppend(FilePath, PATHCCH_MAX_CCH, _T("TestDirectory2")); NativePathCchAppend(FilePath, PATHCCH_MAX_CCH, _T("TestDirectory2File*")); _tprintf(_T("Finding file: %s\n"), FilePath); hFind = FindFirstFile(FilePath, &FindData); if (hFind == INVALID_HANDLE_VALUE) { _tprintf(_T("FindFirstFile failure: %s\n"), FilePath); return -1; } _tprintf(_T("FindFirstFile: %s"), FindData.cFileName); /** * The current implementation does not enforce a particular order */ if ((_tcscmp(FindData.cFileName, testDirectory2File1) != 0) && (_tcscmp(FindData.cFileName, testDirectory2File2) != 0)) { _tprintf(_T("FindFirstFile failure: Expected: %s, Actual: %s\n"), testDirectory2File1, FindData.cFileName); return -1; } status = FindNextFile(hFind, &FindData); if (!status) { _tprintf(_T("FindNextFile failure: Expected: TRUE, Actual: %")_T(PRId32)_T("\n"), status); return -1; } if ((_tcscmp(FindData.cFileName, testDirectory2File1) != 0) && (_tcscmp(FindData.cFileName, testDirectory2File2) != 0)) { _tprintf(_T("FindNextFile failure: Expected: %s, Actual: %s\n"), testDirectory2File2, FindData.cFileName); return -1; } status = FindNextFile(hFind, &FindData); if (status) { _tprintf(_T("FindNextFile failure: Expected: FALSE, Actual: %")_T(PRId32)_T("\n"), status); return -1; } FindClose(hFind); return 0; }