Skip to content

Commit 6192cc3

Browse files
authored
Merge pull request #5 from nitro44x/FreeLibrary_retcode
Fixed error code checking for Win32 systems
2 parents 8690ae6 + 1e089ba commit 6192cc3

3 files changed

Lines changed: 43 additions & 35 deletions

File tree

src/SharedLibrary.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,63 @@ bool SharedLibrary::open(const char *filename)
5555
close();
5656
#if defined(_WIN32)
5757
mPriv->implementation = (void*)LoadLibrary(filename);
58-
LPTSTR msg = nullptr;
59-
FormatMessage(
60-
FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
61-
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
62-
(LPTSTR)&msg, 0, nullptr);
58+
if (!mPriv->implementation) {
59+
LPTSTR msg = nullptr;
60+
FormatMessage(
61+
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
62+
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
63+
(LPTSTR)&msg, 0, nullptr);
6364

64-
if(msg != nullptr) {
65-
mPriv->err_message = std::string(msg);
66-
// release memory allocated by FormatMessage()
67-
LocalFree(msg); msg = nullptr;
65+
if (msg != nullptr) {
66+
mPriv->err_message = std::string(msg);
67+
// release memory allocated by FormatMessage()
68+
LocalFree(msg); msg = nullptr;
69+
}
6870
}
6971
return (mPriv->implementation != nullptr);
7072
#else
7173
mPriv->implementation = dlopen(filename, RTLD_LAZY);
72-
char* msg = dlerror();
73-
if(msg)
74-
mPriv->err_message = msg;
74+
if (!mPriv->implementation) {
75+
char* msg = dlerror();
76+
if (msg)
77+
mPriv->err_message = msg;
78+
}
7579
return mPriv->implementation != nullptr;
7680
#endif
7781
}
7882

7983
bool SharedLibrary::close() {
80-
int result = 0;
84+
bool error = false;
8185
if (mPriv->implementation != nullptr) {
8286
#if defined(WIN32)
83-
result = FreeLibrary((HINSTANCE)mPriv->implementation);
84-
LPTSTR msg = nullptr;
85-
FormatMessage(
86-
FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
87-
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
88-
(LPTSTR)&msg, 0, nullptr);
89-
90-
if(msg != nullptr) {
91-
mPriv->err_message = std::string(msg);
92-
// release memory allocated by FormatMessage()
93-
LocalFree(msg); msg = nullptr;
87+
auto result = FreeLibrary((HINSTANCE)mPriv->implementation);
88+
if (!result) {
89+
error = true;
90+
LPTSTR msg = nullptr;
91+
FormatMessage(
92+
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
93+
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
94+
(LPTSTR)&msg, 0, nullptr);
95+
96+
if (msg != nullptr) {
97+
mPriv->err_message = std::string(msg);
98+
// release memory allocated by FormatMessage()
99+
LocalFree(msg); msg = nullptr;
100+
}
94101
}
95102
#else
96-
result = dlclose(mPriv->implementation);
103+
auto result = dlclose(mPriv->implementation);
97104
if (result != 0) {
98-
char* msg = dlerror();
99-
if(msg)
100-
mPriv->err_message = msg;
105+
error = true;
106+
char* msg = dlerror();
107+
if(msg)
108+
mPriv->err_message = msg;
101109
}
102110
#endif
103111
mPriv->implementation = nullptr;
104112

105113
}
106-
return (result == 0);
114+
return error;
107115
}
108116

109117
std::string SharedLibrary::error()

src/shlibpp/SharedLibraryClassApi.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern "C" {
4040

4141
using createFn_t = void*(*)();
4242
using destroyFn_t = void(*)(void*);
43-
using getFn_t = int32_t(*)(char*, size_t);
43+
using getFn_t = size_t(*)(char*, size_t);
4444

4545
createFn_t create; // Instantiate a plugin object.
4646
destroyFn_t destroy; // Destroy a plugin object.
@@ -114,24 +114,24 @@ constexpr int32_t SHLIBPP_DEFAULT_SYSTEM_VERSION = 5;
114114
} \
115115
} \
116116
\
117-
SHLIBPP_SHARED_CLASS_FN int32_t factoryname ## _getVersion (char* ver, size_t len) \
117+
SHLIBPP_SHARED_CLASS_FN size_t factoryname ## _getVersion (char* ver, size_t len) \
118118
{ \
119119
return 0; \
120120
} \
121121
\
122-
SHLIBPP_SHARED_CLASS_FN int32_t factoryname ## _getAbi (char* abi, size_t len) \
122+
SHLIBPP_SHARED_CLASS_FN size_t factoryname ## _getAbi (char* abi, size_t len) \
123123
{ \
124124
return 0; \
125125
} \
126126
\
127-
SHLIBPP_SHARED_CLASS_FN int32_t factoryname ## _getClassName (char* name, size_t len) \
127+
SHLIBPP_SHARED_CLASS_FN size_t factoryname ## _getClassName (char* name, size_t len) \
128128
{ \
129129
char cname[] = # classname; \
130130
strncpy(name, cname, len); \
131131
return strlen(cname) + 1; \
132132
} \
133133
\
134-
SHLIBPP_SHARED_CLASS_FN int32_t factoryname ## _getBaseClassName (char* name, size_t len) \
134+
SHLIBPP_SHARED_CLASS_FN size_t factoryname ## _getBaseClassName (char* name, size_t len) \
135135
{ \
136136
char cname[] = # basename; \
137137
strncpy(name, cname, len); \

src/shlibpp/SharedLibraryFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace shlibpp {
1616

17-
class SharedLibraryClassApi;
17+
struct SharedLibraryClassApi;
1818

1919
/**
2020
* A wrapper for a named factory method in a named shared library.

0 commit comments

Comments
 (0)