Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assume WIN32 HINSTANCE is a void * #230

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions include/rcutils/shared_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ extern "C"

#include <string.h>

#ifndef _WIN32
#include <dlfcn.h>
typedef void * rcutils_shared_library_handle_t;
#else
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
typedef HINSTANCE rcutils_shared_library_handle_t;
#endif // _WIN32

#include "rcutils/allocator.h"
#include "rcutils/types/rcutils_ret.h"
#include "rcutils/macros.h"
Expand All @@ -41,8 +30,8 @@ typedef HINSTANCE rcutils_shared_library_handle_t;
/// Handle to a loaded shared library.
typedef struct RCUTILS_PUBLIC_TYPE rcutils_shared_library_t
{
/// The pointer to the shared library
rcutils_shared_library_handle_t lib_pointer;
/// The platform-specific pointer to the shared library
void * lib_pointer;
/// The path of the shared_library
char * library_path;
/// allocator
Expand Down
15 changes: 11 additions & 4 deletions src/shared_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ extern "C"
#include <stdio.h>
#include <stdlib.h>

#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
C_ASSERT(sizeof(void *) == sizeof(HINSTANCE));
#endif // _WIN32

#include "rcutils/error_handling.h"
#include "rcutils/shared_library.h"
#include "rcutils/strdup.h"
Expand Down Expand Up @@ -60,7 +67,7 @@ rcutils_load_shared_library(
if (!lib->lib_pointer) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("LoadLibrary error: %s", dlerror());
#else
lib->lib_pointer = LoadLibrary(lib->library_path);
lib->lib_pointer = (void *)(LoadLibrary(lib->library_path));
if (!lib->lib_pointer) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("LoadLibrary error: %lu", GetLastError());
#endif // _WIN32
Expand Down Expand Up @@ -89,7 +96,7 @@ rcutils_get_symbol(const rcutils_shared_library_t * lib, const char * symbol_nam
return NULL;
}
#else
void * lib_symbol = GetProcAddress(lib->lib_pointer, symbol_name);
void * lib_symbol = GetProcAddress((HINSTANCE)(lib->lib_pointer), symbol_name);
if (lib_symbol == NULL) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Error getting the symbol '%s'. Error '%d'",
Expand Down Expand Up @@ -121,7 +128,7 @@ rcutils_has_symbol(const rcutils_shared_library_t * lib, const char * symbol_nam
void * lib_symbol = dlsym(lib->lib_pointer, symbol_name);
return dlerror() == NULL && lib_symbol != 0;
#else
void * lib_symbol = GetProcAddress(lib->lib_pointer, symbol_name);
void * lib_symbol = GetProcAddress((HINSTANCE)(lib->lib_pointer), symbol_name);
return GetLastError() == 0 && lib_symbol != 0;
#endif // _WIN32
}
Expand All @@ -142,7 +149,7 @@ rcutils_unload_shared_library(rcutils_shared_library_t * lib)
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("dlclose error: %s", dlerror());
#else
// If the function succeeds, the return value is nonzero.
int error_code = FreeLibrary(lib->lib_pointer);
int error_code = FreeLibrary((HINSTANCE)(lib->lib_pointer));
if (!error_code) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("FreeLibrary error: %lu", GetLastError());
#endif // _WIN32
Expand Down