Milestone 5: deliver embedded RDP sessions and lifecycle hardening

This commit is contained in:
Keith Smith
2026-03-03 18:59:26 -07:00
parent 230a401386
commit 36006bd4aa
2941 changed files with 724359 additions and 77 deletions

View File

@@ -0,0 +1,22 @@
# WinPR: Windows Portable Runtime
# libwinpr-library cmake build script
#
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
winpr_module_add(library.c)
if(BUILD_TESTING_INTERNAL OR BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@@ -0,0 +1,9 @@
set(MINWIN_LAYER "1")
set(MINWIN_GROUP "core")
set(MINWIN_MAJOR_VERSION "1")
set(MINWIN_MINOR_VERSION "1")
set(MINWIN_SHORT_NAME "libraryloader")
set(MINWIN_LONG_NAME "Dynamic-Link Library Functions")
set(MODULE_LIBRARY_NAME
"api-ms-win-${MINWIN_GROUP}-${MINWIN_SHORT_NAME}-l${MINWIN_LAYER}-${MINWIN_MAJOR_VERSION}-${MINWIN_MINOR_VERSION}"
)

View File

@@ -0,0 +1,423 @@
/**
* WinPR: Windows Portable Runtime
* Library Loader
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/crt.h>
#include <winpr/platform.h>
#include <winpr/library.h>
#include "../log.h"
#define TAG WINPR_TAG("library")
/**
* api-ms-win-core-libraryloader-l1-1-1.dll:
*
* AddDllDirectory
* RemoveDllDirectory
* SetDefaultDllDirectories
* DisableThreadLibraryCalls
* EnumResourceLanguagesExA
* EnumResourceLanguagesExW
* EnumResourceNamesExA
* EnumResourceNamesExW
* EnumResourceTypesExA
* EnumResourceTypesExW
* FindResourceExW
* FindStringOrdinal
* FreeLibrary
* FreeLibraryAndExitThread
* FreeResource
* GetModuleFileNameA
* GetModuleFileNameW
* GetModuleHandleA
* GetModuleHandleExA
* GetModuleHandleExW
* GetModuleHandleW
* GetProcAddress
* LoadLibraryExA
* LoadLibraryExW
* LoadResource
* LoadStringA
* LoadStringW
* LockResource
* QueryOptionalDelayLoadedAPI
* SizeofResource
*/
#if !defined(_WIN32) || defined(_UWP)
#ifndef _WIN32
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef __MACOSX__
#include <mach-o/dyld.h>
#endif
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
#endif
DLL_DIRECTORY_COOKIE AddDllDirectory(WINPR_ATTR_UNUSED PCWSTR NewDirectory)
{
/* TODO: Implement */
WLog_ERR(TAG, "not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return nullptr;
}
BOOL RemoveDllDirectory(WINPR_ATTR_UNUSED DLL_DIRECTORY_COOKIE Cookie)
{
/* TODO: Implement */
WLog_ERR(TAG, "not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL SetDefaultDllDirectories(WINPR_ATTR_UNUSED DWORD DirectoryFlags)
{
/* TODO: Implement */
WLog_ERR(TAG, "not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
HMODULE LoadLibraryA(LPCSTR lpLibFileName)
{
if (!lpLibFileName)
return nullptr;
#if defined(_UWP)
int status;
HMODULE hModule = nullptr;
WCHAR* filenameW = nullptr;
filenameW = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
if (filenameW)
return nullptr;
hModule = LoadLibraryW(filenameW);
free(filenameW);
return hModule;
#else
HMODULE library = dlopen(lpLibFileName, RTLD_LOCAL | RTLD_LAZY);
if (!library)
{
// NOLINTNEXTLINE(concurrency-mt-unsafe)
const char* err = dlerror();
WLog_ERR(TAG, "failed with %s", err);
return nullptr;
}
return library;
#endif
}
HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
{
#if defined(_UWP)
return LoadPackagedLibrary(lpLibFileName, 0);
#else
char* name = nullptr;
if (lpLibFileName)
name = ConvertWCharToUtf8Alloc(lpLibFileName, nullptr);
HMODULE module = LoadLibraryA(name);
free(name);
return module;
#endif
}
HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
if (dwFlags != 0)
WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
if (hFile)
WLog_WARN(TAG, "does not support hFile != nullptr");
return LoadLibraryA(lpLibFileName);
}
HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
if (dwFlags != 0)
WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
if (hFile)
WLog_WARN(TAG, "does not support hFile != nullptr");
return LoadLibraryW(lpLibFileName);
}
#endif
#if !defined(_WIN32) && !defined(__CYGWIN__)
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
{
FARPROC proc = nullptr;
proc = dlsym(hModule, lpProcName);
if (proc == nullptr)
{
// NOLINTNEXTLINE(concurrency-mt-unsafe)
WLog_ERR(TAG, "GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
return (FARPROC) nullptr;
}
return proc;
}
BOOL FreeLibrary(HMODULE hLibModule)
{
int status = 0;
status = dlclose(hLibModule);
return (status == 0);
}
HMODULE GetModuleHandleA(LPCSTR lpModuleName)
{
return dlopen(lpModuleName, RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY);
}
HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
{
char* name = nullptr;
if (lpModuleName)
name = ConvertWCharToUtf8Alloc(lpModuleName, nullptr);
HANDLE hdl = GetModuleHandleA(name);
free(name);
return hdl;
}
/**
* GetModuleFileName:
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
*
* Finding current executable's path without /proc/self/exe:
* http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
*/
DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
{
DWORD status = 0;
if (!lpFilename)
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
char* name = calloc(nSize, sizeof(char));
if (!name)
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
status = GetModuleFileNameA(hModule, name, nSize);
if ((status > INT_MAX) || (nSize > INT_MAX))
{
SetLastError(ERROR_INTERNAL_ERROR);
status = 0;
}
if (status > 0)
{
if (ConvertUtf8NToWChar(name, status, lpFilename, nSize) < 0)
{
free(name);
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
}
free(name);
return status;
}
#if defined(__linux__) || defined(__NetBSD__) || defined(__DragonFly__)
static DWORD module_from_proc(const char* proc, LPSTR lpFilename, DWORD nSize)
{
char buffer[8192] = WINPR_C_ARRAY_INIT;
ssize_t status = readlink(proc, buffer, ARRAYSIZE(buffer) - 1);
if ((status < 0) || ((size_t)status >= ARRAYSIZE(buffer)))
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
const size_t length = strnlen(buffer, ARRAYSIZE(buffer));
if (length < nSize)
{
CopyMemory(lpFilename, buffer, length);
lpFilename[length] = '\0';
return (DWORD)length;
}
CopyMemory(lpFilename, buffer, nSize - 1);
lpFilename[nSize - 1] = '\0';
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return nSize;
}
#endif
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
{
if (hModule)
{
WLog_ERR(TAG, "is not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
}
#if defined(__linux__)
return module_from_proc("/proc/self/exe", lpFilename, nSize);
#elif defined(__FreeBSD__)
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t cb = nSize;
{
const int rc = sysctl(mib, ARRAYSIZE(mib), nullptr, &cb, nullptr, 0);
if (rc != 0)
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
}
char* fullname = calloc(cb + 1, sizeof(char));
if (!fullname)
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
{
size_t cb2 = cb;
const int rc = sysctl(mib, ARRAYSIZE(mib), fullname, &cb2, nullptr, 0);
if ((rc != 0) || (cb2 != cb))
{
SetLastError(ERROR_INTERNAL_ERROR);
free(fullname);
return 0;
}
}
if (nSize > 0)
{
strncpy(lpFilename, fullname, nSize - 1);
lpFilename[nSize - 1] = '\0';
}
free(fullname);
if (nSize < cb)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (DWORD)MIN(nSize, cb);
#elif defined(__NetBSD__)
return module_from_proc("/proc/curproc/exe", lpFilename, nSize);
#elif defined(__DragonFly__)
return module_from_proc("/proc/curproc/file", lpFilename, nSize);
#elif defined(__MACOSX__)
char path[4096] = WINPR_C_ARRAY_INIT;
char buffer[4096] = WINPR_C_ARRAY_INIT;
uint32_t size = sizeof(path);
const int status = _NSGetExecutablePath(path, &size);
if (status != 0)
{
/* path too small */
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
/*
* _NSGetExecutablePath may not return the canonical path,
* so use realpath to find the absolute, canonical path.
*/
realpath(path, buffer);
const size_t length = strnlen(buffer, sizeof(buffer));
if (length < nSize)
{
CopyMemory(lpFilename, buffer, length);
lpFilename[length] = '\0';
return (DWORD)length;
}
CopyMemory(lpFilename, buffer, nSize - 1);
lpFilename[nSize - 1] = '\0';
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return nSize;
#else
WLog_ERR(TAG, "is not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
#endif
}
#endif
HMODULE LoadLibraryX(LPCSTR lpLibFileName)
{
#if defined(_WIN32)
HMODULE hm = nullptr;
WCHAR* wstr = nullptr;
if (lpLibFileName)
wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
hm = LoadLibraryW(wstr);
free(wstr);
return hm;
#else
return LoadLibraryA(lpLibFileName);
#endif
}
HMODULE LoadLibraryExX(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
if (!lpLibFileName)
return nullptr;
#if defined(_WIN32)
HMODULE hm = nullptr;
WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
if (wstr)
hm = LoadLibraryExW(wstr, hFile, dwFlags);
free(wstr);
return hm;
#else
return LoadLibraryExA(lpLibFileName, hFile, dwFlags);
#endif
}

View File

@@ -0,0 +1,30 @@
add_subdirectory(TestLibraryA)
add_subdirectory(TestLibraryB)
set(MODULE_NAME "TestLibrary")
set(MODULE_PREFIX "TEST_LIBRARY")
disable_warnings_for_directory(${CMAKE_CURRENT_BINARY_DIR})
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS TestLibraryLoadLibrary.c TestLibraryGetProcAddress.c TestLibraryGetModuleFileName.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_DRIVER} ${${MODULE_PREFIX}_TESTS})
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
add_dependencies(${MODULE_NAME} TestLibraryA TestLibraryB)
target_link_libraries(${MODULE_NAME} winpr)
set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
set(TEST_AREA "${MODULE_NAME}Area")
foreach(test ${${MODULE_PREFIX}_TESTS})
get_filename_component(TestName ${test} NAME_WE)
add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
endforeach()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")

View File

@@ -0,0 +1,30 @@
# WinPR: Windows Portable Runtime
# libwinpr-library cmake build script
#
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(MODULE_NAME "TestLibraryA")
set(MODULE_PREFIX "TEST_LIBRARY_A")
set(SRCS TestLibraryA.c)
add_library(${MODULE_NAME} SHARED ${SRCS})
set_target_properties(${MODULE_NAME} PROPERTIES PREFIX "")
set_target_properties(
${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}" RUNTIME_OUTPUT_DIRECTORY
"${TESTING_OUTPUT_DIRECTORY}"
)
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra")

View File

@@ -0,0 +1,14 @@
#include <winpr/spec.h>
DECLSPEC_EXPORT int FunctionA(int a, int b);
DECLSPEC_EXPORT int FunctionB(int a, int b);
int FunctionA(int a, int b)
{
return (a * b); /* multiply */
}
int FunctionB(int a, int b)
{
return (a / b); /* divide */
}

View File

@@ -0,0 +1,31 @@
# WinPR: Windows Portable Runtime
# libwinpr-library cmake build script
#
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(MODULE_NAME "TestLibraryB")
set(MODULE_PREFIX "TEST_LIBRARY_B")
set(SRCS TestLibraryB.c)
add_library(${MODULE_NAME} SHARED ${SRCS})
set_target_properties(${MODULE_NAME} PROPERTIES PREFIX "")
set_target_properties(
${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}" RUNTIME_OUTPUT_DIRECTORY
"${TESTING_OUTPUT_DIRECTORY}"
)
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra")

View File

@@ -0,0 +1,14 @@
#include <winpr/spec.h>
DECLSPEC_EXPORT int FunctionA(int a, int b);
DECLSPEC_EXPORT int FunctionB(int a, int b);
int FunctionA(int a, int b)
{
return (a + b); /* add */
}
int FunctionB(int a, int b)
{
return (a - b); /* subtract */
}

View File

@@ -0,0 +1,56 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/windows.h>
#include <winpr/library.h>
int TestLibraryGetModuleFileName(int argc, char* argv[])
{
char ModuleFileName[4096];
DWORD len = 0;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
/* Test insufficient buffer size behaviour */
SetLastError(ERROR_SUCCESS);
len = GetModuleFileNameA(nullptr, ModuleFileName, 2);
if (len != 2)
{
printf("%s: GetModuleFileNameA unexpectedly returned %" PRIu32 " instead of 2\n", __func__,
len);
return -1;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("%s: Invalid last error value: 0x%08" PRIX32
". Expected 0x%08X (ERROR_INSUFFICIENT_BUFFER)\n",
__func__, GetLastError(), ERROR_INSUFFICIENT_BUFFER);
return -1;
}
/* Test with real/sufficient buffer size */
SetLastError(ERROR_SUCCESS);
len = GetModuleFileNameA(nullptr, ModuleFileName, sizeof(ModuleFileName));
if (len == 0)
{
printf("%s: GetModuleFileNameA failed with error 0x%08" PRIX32 "\n", __func__,
GetLastError());
return -1;
}
if (len == sizeof(ModuleFileName))
{
printf("%s: GetModuleFileNameA unexpectedly returned nSize\n", __func__);
return -1;
}
if (GetLastError() != ERROR_SUCCESS)
{
printf("%s: Invalid last error value: 0x%08" PRIX32 ". Expected 0x%08X (ERROR_SUCCESS)\n",
__func__, GetLastError(), ERROR_SUCCESS);
return -1;
}
printf("GetModuleFileNameA: %s\n", ModuleFileName);
return 0;
}

View File

@@ -0,0 +1,105 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/windows.h>
#include <winpr/library.h>
#include <winpr/nt.h>
typedef int (*TEST_AB_FN)(int a, int b);
int TestLibraryGetProcAddress(int argc, char* argv[])
{
int a = 0;
int b = 0;
int c = 0;
HINSTANCE library = nullptr;
TEST_AB_FN pFunctionA = nullptr;
TEST_AB_FN pFunctionB = nullptr;
LPCSTR SharedLibraryExtension = nullptr;
CHAR LibraryPath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
PCHAR p = nullptr;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!GetModuleFileNameA(nullptr, LibraryPath, PATHCCH_MAX_CCH))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: GetModuleFilenameA failed: %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
return -1;
}
/* PathCchRemoveFileSpec is not implemented in WinPR */
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
{
printf("%s: Error identifying module directory path\n", __func__);
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", __func__, LibraryPath);
if (!(library = LoadLibraryA(LibraryPath)))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: LoadLibraryA failure: %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
return -1;
}
if (!(pFunctionA = GetProcAddressAs(library, "FunctionA", TEST_AB_FN)))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: GetProcAddress failure (FunctionA) %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
return -1;
}
if (!(pFunctionB = GetProcAddressAs(library, "FunctionB", TEST_AB_FN)))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: GetProcAddress failure (FunctionB) %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
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", __func__);
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", __func__);
return -1;
}
if (!FreeLibrary(library))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: FreeLibrary failure: %s - %s [0x%08" PRIX32 "]\n", __func__, NtStatus2Tag(herr),
Win32ErrorCode2Tag(err), err);
return -1;
}
return 0;
}

View File

@@ -0,0 +1,61 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/windows.h>
#include <winpr/library.h>
#include <winpr/nt.h>
int TestLibraryLoadLibrary(int argc, char* argv[])
{
HINSTANCE library = nullptr;
LPCSTR SharedLibraryExtension = nullptr;
CHAR LibraryPath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
PCHAR p = nullptr;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!GetModuleFileNameA(nullptr, LibraryPath, PATHCCH_MAX_CCH))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: GetModuleFilenameA failed: %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
return -1;
}
/* PathCchRemoveFileSpec is not implemented in WinPR */
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
{
printf("%s: Error identifying module directory path\n", __func__);
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", __func__, LibraryPath);
if (!(library = LoadLibraryA(LibraryPath)))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: LoadLibraryA failure: %s - %s [0x%08" PRIX32 "]\n", __func__,
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
return -1;
}
if (!FreeLibrary(library))
{
const UINT32 err = GetLastError();
const HRESULT herr = HRESULT_FROM_WIN32(err);
printf("%s: FreeLibrary failure: %s - %s [0x%08" PRIX32 "]\n", __func__, NtStatus2Tag(herr),
Win32ErrorCode2Tag(err), err);
return -1;
}
return 0;
}