Skip to content

Commit 1e089ba

Browse files
committed
Fixed error code checking for Win32 systems
1 parent 0a2acba commit 1e089ba

1 file changed

Lines changed: 37 additions & 29 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()

0 commit comments

Comments
 (0)