int TestLibraryLoadLibrary(int argc, char* argv[]) { HINSTANCE library; LPCSTR SharedLibraryExtension; CHAR LibraryPath[PATHCCH_MAX_CCH]; PCHAR p; if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH)) { 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; } *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); if (!(library = LoadLibraryA(LibraryPath))) { printf("%s: LoadLibraryA failure: 0x%08"PRIX32"\n", __FUNCTION__, GetLastError()); return -1; } if (!FreeLibrary(library)) { printf("%s: FreeLibrary failure: 0x%08"PRIX32"\n", __FUNCTION__, GetLastError()); return -1; } return 0; }
void* freerdp_load_dynamic_addin(LPCSTR pszFileName, LPCSTR pszPath, LPCSTR pszEntryName) { void* entry; BOOL bHasExt; PCSTR pszExt; size_t cchExt; HINSTANCE library; size_t cchFileName; LPSTR pszFilePath; size_t cchFilePath; LPSTR pszAddinFile; size_t cchAddinFile; LPSTR pszAddinInstallPath; size_t cchAddinInstallPath; entry = NULL; cchExt = 0; bHasExt = TRUE; cchFileName = strlen(pszFileName); if (PathCchFindExtensionA(pszFileName, cchFileName + 1, &pszExt) != S_OK) { pszExt = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT); cchExt = strlen(pszExt); bHasExt = FALSE; } pszAddinInstallPath = freerdp_get_dynamic_addin_install_path(); cchAddinInstallPath = strlen(pszAddinInstallPath); cchFilePath = cchAddinInstallPath + cchFileName + 32; pszFilePath = (LPSTR) malloc(cchFilePath + 1); if (bHasExt) { pszAddinFile = _strdup(pszFileName); cchAddinFile = strlen(pszAddinFile); } else { cchAddinFile = cchFileName + cchExt + 2; pszAddinFile = (LPSTR) malloc(cchAddinFile + 1); sprintf_s(pszAddinFile, cchAddinFile, "%s%s", pszFileName, pszExt); cchAddinFile = strlen(pszAddinFile); } CopyMemory(pszFilePath, pszAddinInstallPath, cchAddinInstallPath); pszFilePath[cchAddinInstallPath] = '\0'; NativePathCchAppendA((LPSTR) pszFilePath, cchFilePath + 1, pszAddinFile); library = LoadLibraryA(pszFilePath); free(pszAddinInstallPath); free(pszAddinFile); free(pszFilePath); if (!library) return NULL; entry = GetProcAddress(library, pszEntryName); if (entry) return entry; FreeLibrary(library); return entry; }
void* freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName, LPSTR pszSubsystem, LPSTR pszType, DWORD dwFlags) { void* entry; LPSTR pszFileName; size_t cchFileName; LPCSTR pszExtension; pszExtension = PathGetSharedLibraryExtensionA(0); if (pszName && pszSubsystem && pszType) { cchFileName = strlen(pszName) + strlen(pszSubsystem) + strlen(pszType) + strlen(pszExtension) + 32; pszFileName = (LPSTR) malloc(cchFileName); sprintf_s(pszFileName, cchFileName, "%s-client-%s-%s.%s", pszName, pszSubsystem, pszType, pszExtension); cchFileName = strlen(pszFileName); } else if (pszName && pszSubsystem) { cchFileName = strlen(pszName) + strlen(pszSubsystem) + strlen(pszExtension) + 32; pszFileName = (LPSTR) malloc(cchFileName); sprintf_s(pszFileName, cchFileName, "%s-client-%s.%s", pszName, pszSubsystem, pszExtension); cchFileName = strlen(pszFileName); } else if (pszName) { cchFileName = strlen(pszName) + strlen(pszExtension) + 32; pszFileName = (LPSTR) malloc(cchFileName); sprintf_s(pszFileName, cchFileName, "%s-client.%s", pszName, pszExtension); cchFileName = strlen(pszFileName); } else { return NULL; } if (pszSubsystem) { LPSTR pszEntryName; size_t cchEntryName; /* subsystem add-in */ cchEntryName = 64 + strlen(pszName); pszEntryName = (LPSTR) malloc(cchEntryName + 1); sprintf_s(pszEntryName, cchEntryName + 1, "freerdp_%s_client_subsystem_entry", pszName); entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszEntryName); free(pszEntryName); free(pszFileName); if (entry) return entry; } else { /* channel add-in */ if (dwFlags & FREERDP_ADDIN_CHANNEL_STATIC) entry = freerdp_load_dynamic_addin(pszFileName, NULL, "VirtualChannelEntry"); else if (dwFlags & FREERDP_ADDIN_CHANNEL_DYNAMIC) entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DVCPluginEntry"); else if (dwFlags & FREERDP_ADDIN_CHANNEL_DEVICE) entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DeviceServiceEntry"); else entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszType); free(pszFileName); if (entry) return entry; } return NULL; }
PVIRTUALCHANNELENTRY freerdp_load_dynamic_addin(LPCSTR pszFileName, LPCSTR pszPath, LPCSTR pszEntryName) { LPSTR pszAddinInstallPath = freerdp_get_dynamic_addin_install_path(); PVIRTUALCHANNELENTRY entry = NULL; BOOL bHasExt = TRUE; PCSTR pszExt; size_t cchExt = 0; HINSTANCE library = NULL; size_t cchFileName; size_t cchFilePath; LPSTR pszAddinFile = NULL; LPSTR pszFilePath = NULL; LPSTR pszRelativeFilePath = NULL; size_t cchAddinFile; size_t cchAddinInstallPath; if (!pszFileName || !pszEntryName) goto fail; cchFileName = strlen(pszFileName); /* Get file name with prefix and extension */ if (FAILED(PathCchFindExtensionA(pszFileName, cchFileName + 1, &pszExt))) { pszExt = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT); cchExt = strlen(pszExt); bHasExt = FALSE; } if (bHasExt) { pszAddinFile = _strdup(pszFileName); if (!pszAddinFile) goto fail; } else { cchAddinFile = cchFileName + cchExt + 2 + sizeof(FREERDP_SHARED_LIBRARY_PREFIX); pszAddinFile = (LPSTR) malloc(cchAddinFile + 1); if (!pszAddinFile) goto fail; sprintf_s(pszAddinFile, cchAddinFile, FREERDP_SHARED_LIBRARY_PREFIX"%s%s", pszFileName, pszExt); } cchAddinFile = strlen(pszAddinFile); /* If a path is provided prefix the library name with it. */ if (pszPath) { size_t relPathLen = strlen(pszPath) + cchAddinFile + 1; pszRelativeFilePath = calloc(relPathLen, sizeof(CHAR)); if (!pszRelativeFilePath) goto fail; sprintf_s(pszRelativeFilePath, relPathLen, "%s", pszRelativeFilePath); NativePathCchAppendA(pszRelativeFilePath, relPathLen, pszAddinFile); } else pszRelativeFilePath = _strdup(pszAddinFile); if (!pszRelativeFilePath) goto fail; /* If a system prefix path is provided try these locations too. */ if (pszAddinInstallPath) { cchAddinInstallPath = strlen(pszAddinInstallPath); cchFilePath = cchAddinInstallPath + cchFileName + 32; pszFilePath = (LPSTR) malloc(cchFilePath + 1); if (!pszFilePath) goto fail; CopyMemory(pszFilePath, pszAddinInstallPath, cchAddinInstallPath); pszFilePath[cchAddinInstallPath] = '\0'; NativePathCchAppendA((LPSTR) pszFilePath, cchFilePath + 1, pszRelativeFilePath); } else pszFilePath = _strdup(pszRelativeFilePath); library = LoadLibraryA(pszFilePath); if (!library) goto fail; entry = (PVIRTUALCHANNELENTRY)GetProcAddress(library, pszEntryName); fail: free(pszRelativeFilePath); free(pszAddinFile); free(pszFilePath); free(pszAddinInstallPath); if (!entry && library) FreeLibrary(library); return entry; }