report windows error message
This commit is contained in:
parent
9798f880f9
commit
d0f3f64bf8
|
@ -2,11 +2,12 @@
|
||||||
|
|
||||||
typedef void (*func_handle)();
|
typedef void (*func_handle)();
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
struct hotwire_dll_t dll = hw_dlopen("./bin/exampleUnixLib.dll", RTLD_NOW);
|
||||||
struct hotwire_dll_t dll = hw_dlopen("./bin/exampleUnixLib.dll", RTLD_NOW);
|
|
||||||
|
|
||||||
func_handle func = (func_handle)hw_dlsym(dll, "test");
|
func_handle func = (func_handle)hw_dlsym(dll, "test");
|
||||||
|
|
||||||
func();
|
func();
|
||||||
|
|
||||||
|
hw_dlclose(dll);
|
||||||
}
|
}
|
|
@ -9,4 +9,6 @@ int main()
|
||||||
func_handle func = (func_handle)hw_dlsym(dll, "test");
|
func_handle func = (func_handle)hw_dlsym(dll, "test");
|
||||||
|
|
||||||
func();
|
func();
|
||||||
|
|
||||||
|
hw_dlclose(dll);
|
||||||
}
|
}
|
77
src/dlopen.c
77
src/dlopen.c
|
@ -9,9 +9,9 @@ struct hotwire_dll_t hw_dlopen(const char *file, int flags) {
|
||||||
#if defined(__linux__) || defined(__APPLE__)
|
#if defined(__linux__) || defined(__APPLE__)
|
||||||
dll.dll_handle = dlopen(file, flags);
|
dll.dll_handle = dlopen(file, flags);
|
||||||
|
|
||||||
if (dll.dll_handle == NULL){
|
if (dll.dll_handle == NULL) {
|
||||||
printf("could not load dll(UNIX api call returned error)\n");
|
printf("Could not load dll(UNIX api call returned error)\n");
|
||||||
printf("error message: %s\n", dlerror());
|
printf("Error message: %s\n", dlerror());
|
||||||
|
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,9 @@ struct hotwire_dll_t hw_dlopen(const char *file, int flags) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
dll.dll_handle = LoadLibrary(file);
|
dll.dll_handle = LoadLibrary(file);
|
||||||
|
|
||||||
if (!dll.dll_handle){
|
if (!dll.dll_handle) {
|
||||||
printf("could not load dll(windows api call returned error)\n");
|
printf("Could not load dll(Windows api call returned error)\n");
|
||||||
|
hw_platform_win32_get_last_error(TEXT("LoadLibrary"));
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,12 +31,61 @@ struct hotwire_dll_t hw_dlopen(const char *file, int flags) {
|
||||||
return dll;
|
return dll;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* hw_dlsym(struct hotwire_dll_t dll, const char* symbol){
|
void *hw_dlsym(struct hotwire_dll_t dll, const char *symbol) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
return GetProcAddress(dll.dll_handle, symbol);
|
return GetProcAddress(dll.dll_handle, symbol);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__linux) || defined(__APPLE__)
|
#if defined(__linux) || defined(__APPLE__)
|
||||||
return dlsym(dll.dll_handle, symbol);
|
return dlsym(dll.dll_handle, symbol);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum hotwire_status_code hw_dlclose(struct hotwire_dll_t handle) {
|
||||||
|
#if defined(__linux) || defined(__APPLE__)
|
||||||
|
if (dlclose(handle.dll_handle) != 0) {
|
||||||
|
return Failed;
|
||||||
|
} else {
|
||||||
|
return Success;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__WIN32)
|
||||||
|
if (FreeLibrary(handle.dll_handle)) {
|
||||||
|
return Success;
|
||||||
|
} else {
|
||||||
|
return Failed;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return NoPlatform;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
void hw_platform_win32_get_last_error(LPTSTR lpszFunction) {
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
LPVOID lpDisplayBuf;
|
||||||
|
DWORD dw = GetLastError();
|
||||||
|
|
||||||
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
|
(LPTSTR)&lpMsgBuf, 0, NULL);
|
||||||
|
|
||||||
|
// Display the error message and exit the process
|
||||||
|
|
||||||
|
lpDisplayBuf =
|
||||||
|
(LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) +
|
||||||
|
lstrlen((LPCTSTR)lpszFunction) + 40) *
|
||||||
|
sizeof(TCHAR));
|
||||||
|
StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
||||||
|
TEXT("%s failed with error %d: %s"), lpszFunction, dw,
|
||||||
|
lpMsgBuf);
|
||||||
|
|
||||||
|
printf("Error message: %s", (LPCTSTR)lpDisplayBuf);
|
||||||
|
|
||||||
|
LocalFree(lpMsgBuf);
|
||||||
|
LocalFree(lpDisplayBuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
30
src/dlopen.h
30
src/dlopen.h
|
@ -11,24 +11,32 @@
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <strsafe.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Platform specific code functions
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
void hw_platform_win32_get_last_error(LPTSTR lpszFunction);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum hotwire_status_code { Failed, Success, NoPlatform };
|
||||||
|
|
||||||
struct hotwire_dll_t {
|
struct hotwire_dll_t {
|
||||||
#if defined(__linux__) || defined(__APPLE__)
|
#if defined(__linux__) || defined(__APPLE__)
|
||||||
void* dll_handle;
|
void *dll_handle;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
HINSTANCE dll_handle;
|
HINSTANCE dll_handle;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function arguments are based off the dlopen() api on POSIX based operating systems
|
// Function arguments are based off the dlopen() api on POSIX based operating
|
||||||
// Loads dll from library
|
// systems Loads dll from library
|
||||||
struct hotwire_dll_t hw_dlopen(const char* file, int flags);
|
struct hotwire_dll_t hw_dlopen(const char *file, int flags);
|
||||||
|
|
||||||
// Loads symbol from DLL
|
// Loads symbol from DLL
|
||||||
void* hw_dlsym(struct hotwire_dll_t dll, const char* symbol);
|
void *hw_dlsym(struct hotwire_dll_t dll, const char *symbol);
|
||||||
|
|
||||||
int hw_dlclose(void* handle);
|
enum hotwire_status_code hw_dlclose(struct hotwire_dll_t handle);
|
||||||
|
|
Loading…
Reference in a new issue