Skip to content

Commit

Permalink
fix: report load library failures for the VST3 sdk
Browse files Browse the repository at this point in the history
This fixes an issue where when the package loading fails, the error message is ignored and is not reported to the user.
  • Loading branch information
aminya committed Mar 30, 2024
1 parent a8ae6ed commit f27287b
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,25 @@ class Win32Module : public Module
}
}

static void getLastError(const std::string& inPath, std::string& errorDescription) {
auto lastError = GetLastError ();
LPVOID lpMessageBuffer {nullptr};
if (FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr, lastError, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMessageBuffer, 0, nullptr) > 0)
{
errorDescription = "LoadLibray failed for path " + inPath + ". " + std::string ((char*)lpMessageBuffer);
LocalFree (lpMessageBuffer);
}
else
{
errorDescription =
"LoadLibrary failed with error number " + std::to_string (lastError) + " for path " + inPath;
}
}

//--- -----------------------------------------------------------------------
HINSTANCE loadAsPackage (const std::string& inPath, const char* archString = architectureString)
HINSTANCE loadAsPackage (const std::string& inPath, std::string& errorDescription, const char* archString = architectureString)
{
filesystem::path p (inPath);
auto filename = p.filename ();
Expand All @@ -179,10 +196,18 @@ class Win32Module : public Module
HINSTANCE instance = LoadLibraryW (reinterpret_cast<LPCWSTR> (wideStr.data ()));
#if SMTG_CPU_ARM_64EC
if (instance == nullptr)
instance = loadAsPackage (inPath, architectureArm64XString);
instance = loadAsPackage (inPath, errorDescription, architectureArm64XString);
if (instance == nullptr)
instance = loadAsPackage (inPath, architectureX64String);
instance = loadAsPackage (inPath, errorDescription, architectureX64String);
if (instance == nullptr) {
Win32Module::getLastError(inPath, errorDescription);
}
#else
if (instance == nullptr) {
Win32Module::getLastError(p.string(), errorDescription);
}
#endif

return instance;
}

Expand All @@ -193,20 +218,7 @@ class Win32Module : public Module
HINSTANCE instance = LoadLibraryW (reinterpret_cast<LPCWSTR> (wideStr.data ()));
if (instance == nullptr)
{
auto lastError = GetLastError ();
LPVOID lpMessageBuffer {nullptr};
if (FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr, lastError, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMessageBuffer, 0, nullptr) > 0)
{
errorDescription = "LoadLibray failed: " + std::string ((char*)lpMessageBuffer);
LocalFree (lpMessageBuffer);
}
else
{
errorDescription =
"LoadLibrary failed with error number : " + std::to_string (lastError);
}
Win32Module::getLastError(inPath, errorDescription);
}
else
{
Expand All @@ -227,7 +239,7 @@ class Win32Module : public Module
if (filesystem::is_directory (tmp))
{
// try as package (bundle)
mModule = loadAsPackage (inPath);
mModule = loadAsPackage (inPath, errorDescription);
}
else
{
Expand Down Expand Up @@ -546,7 +558,7 @@ Module::PathList Module::getModulePaths ()
#endif
findModules (path, list);
}

// find plug-ins located in VST3 (application folder)
WCHAR modulePath[kIPPathNameMax];
GetModuleFileNameW (nullptr, modulePath, kIPPathNameMax);
Expand Down

0 comments on commit f27287b

Please sign in to comment.