Skip to content

Commit

Permalink
Merge pull request #107 from modelica-3rdparty/PilingYetAnotherWokara…
Browse files Browse the repository at this point in the history
…ound

Try to load ModelicaError/ModelicaWarning symbols also on Windows
  • Loading branch information
casella authored May 14, 2024
2 parents ed8d0c4 + 675bed5 commit d961b18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Projects/Sources/errorhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ void errorMessage(char *errorMsg){
#else
// Workaround code for Windows, will be removed when a proper solution to
// call ModelicaUtilities functions from DLLs is finally delivered
if(ModelicaErrorPtr == nullptr)
ModelicaErrorPtr = tryImportSymbol<void (*)(const char *)>("ModelicaError");

if(ModelicaErrorPtr)
// The pointer to ModelicaError has already been initialized by a previous
// call to setState_xx
ModelicaErrorPtr(msg.c_str());
ModelicaErrorPtr(msg.c_str());
else
{
// The pointer to ModelicaError is not yet initialized, so all we can do
Expand All @@ -58,10 +61,13 @@ void warningMessage(char *warningMsg){
#else
// Workaround code for Windows, will be removed when a proper solution to
// call ModelicaUtilities functions from DLLs is finally delivered
if(ModelicaWarningPtr == nullptr)
ModelicaWarningPtr = tryImportSymbol<void (*)(const char *)>("ModelicaWarning");

if(ModelicaWarningPtr)
// The pointer to ModelicaError has already been initialized by a previous
// call to setState_xx
ModelicaWarningPtr(msg.c_str());
ModelicaWarningPtr(msg.c_str());
else
{
// The pointer to ModelicaError is not yet initialized, so all we can do
Expand Down
12 changes: 11 additions & 1 deletion Projects/Sources/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <psapi.h>

template<typename T>
T importSymbol(const char *funcName)
T tryImportSymbol(const char *funcName)
{
/* TODO: we should do caching */

Expand Down Expand Up @@ -54,6 +54,16 @@ T importSymbol(const char *funcName)
if(pfn) return pfn;
}

/* not found */
return NULL;
}

template<typename T>
T importSymbol(const char *funcName)
{
T result = tryImportSymbol<T>(funcName);
if(result) return result;

fprintf(stderr, "Can't get handle to %s in all loaded modules.\n", funcName);
exit(1);
}
Expand Down

0 comments on commit d961b18

Please sign in to comment.