Skip to content

Commit 9843855

Browse files
Fixing shared library not run properly on Windows
1 parent 3079ad2 commit 9843855

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Include/runcpp2/PlatformUtil.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace runcpp2
2424
int& outReturnCode,
2525
std::string runDirectory = "");
2626

27+
#if defined(_WIN32)
28+
std::string GetWindowsError();
29+
#endif
2730

2831
template <typename T>
2932
inline bool HasValueFromPlatformMap(const std::unordered_map<PlatformName, T>& map)

Src/runcpp2/PlatformUtil.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ bool runcpp2::RunCommandAndGetOutput( const std::string& command,
127127

128128
return true;
129129
}
130+
131+
132+
#if defined(_WIN32)
133+
//Credit: https://stackoverflow.com/a/17387176
134+
std::string runcpp2::GetWindowsError()
135+
{
136+
//Get the error message ID, if any.
137+
DWORD errorMessageID = ::GetLastError();
138+
if(errorMessageID == 0) {
139+
return std::string(); //No error message has been recorded
140+
}
141+
142+
LPSTR messageBuffer = nullptr;
143+
144+
//Ask Win32 to give us the string version of that message ID.
145+
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
146+
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
147+
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
148+
149+
//Copy the error message into a std::string.
150+
std::string message(messageBuffer, size);
151+
152+
//Free the Win32's string's buffer.
153+
LocalFree(messageBuffer);
154+
155+
return message;
156+
}
157+
#endif

Src/runcpp2/runcpp2.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
extern "C" const uint8_t DefaultScriptInfo[];
1919
extern "C" const size_t DefaultScriptInfo_size;
2020

21+
//Use for SetDllDirectory
22+
#if defined(_WIN32)
23+
#ifndef WIN32_LEAN_AND_MEAN
24+
#define WIN32_LEAN_AND_MEAN
25+
#endif
26+
#ifndef NOMINMAX
27+
#define NOMINMAX
28+
#endif
29+
#include <windows.h>
30+
#endif
31+
2132
namespace
2233
{
2334
bool CreateLocalBuildDirectory( const std::string& scriptPath,
@@ -131,6 +142,17 @@ namespace
131142

132143
try
133144
{
145+
//TODO: We might want to use unicode instead for the path
146+
#if defined(_WIN32)
147+
std::string sharedLibDir = compiledSharedLibPath.parent_path().string();
148+
if(SetDllDirectoryA(sharedLibDir.c_str()) == FALSE)
149+
{
150+
std::string lastError = runcpp2::GetWindowsError();
151+
ssLOG_ERROR("Failed to set DLL directory: " << lastError);
152+
return false;
153+
}
154+
#endif
155+
134156
sharedLib = std::unique_ptr<dylib>(new dylib( compiledSharedLibPath.string(),
135157
dylib::no_filename_decorations));
136158
}

0 commit comments

Comments
 (0)