bool OB_ShowLastError(const char *context) { LPVOID lpMsgBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); ////////////////////////////////////////////////////////////////////////// // Strip Newlines char *pMessage = (char*)lpMsgBuf; int i = (int)strlen(pMessage)-1; while(pMessage[i] == '\n' || pMessage[i] == '\r') pMessage[i--] = 0; ////////////////////////////////////////////////////////////////////////// Omnibot_Load_PrintErr(OB_VA("%s Failed with Error: %s", context, pMessage)); LocalFree(lpMsgBuf); return true; }
void OB_ShowLastError(const char *context) { #ifdef WIN32 char *pMessage = 0; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pMessage, 0, 0); if (pMessage) { // Strip Newlines size_t i = strlen(pMessage) - 1; while (pMessage[i] == '\n' || pMessage[i] == '\r') pMessage[i--] = 0; } #else const char *pMessage = dlerror(); #endif if (!pMessage) { pMessage = "<unknown error>"; } Omnibot_Load_PrintErr(OB_VA("%s Failed with Error: %s", context, pMessage)); #ifdef WIN32 LocalFree(pMessage); #endif }
void *Omnibot_LL(const char *file) { g_OmnibotLibPath = file; void *pLib = dlopen(g_OmnibotLibPath.c_str(), RTLD_NOW); if(!pLib) OB_ShowLastError("LoadLibrary", dlerror()); Omnibot_Load_PrintMsg(OB_VA("Looking for %s, ", g_OmnibotLibPath.c_str(), pLib ? "found." : "not found")); return pLib; }
HINSTANCE Omnibot_LL(const char *file) { ////////////////////////////////////////////////////////////////////////// // Parse Variables // $(ProgramFiles) // $(OMNIBOT) ////////////////////////////////////////////////////////////////////////// g_OmnibotLibPath = file; HINSTANCE hndl = LoadLibrary(g_OmnibotLibPath.c_str()); if(!hndl) OB_ShowLastError("LoadLibrary"); Omnibot_Load_PrintMsg(OB_VA("Looking for %s, %s", g_OmnibotLibPath.c_str(), hndl ? "found." : "not found")); return hndl; }
eomnibot_error Omnibot_LoadLibrary(int version, const char *lib, const char *path) { eomnibot_error r = BOT_ERROR_NONE; g_BotLibrary = Omnibot_LL(OB_VA("%s/%s.so", path ? path : ".", lib)); if(!g_BotLibrary) { g_BotLibrary = Omnibot_LL(OB_VA("./%s.so", lib)); } if(!g_BotLibrary) { char *homeDir = getenv("HOME"); if(homeDir) g_BotLibrary = Omnibot_LL(OB_VA("%s/omni-bot/%s.so", homeDir, lib)); } if(!g_BotLibrary) { char *homeDir = getenv("HOME"); if(homeDir) g_BotLibrary = Omnibot_LL(OB_VA("%s.so", lib)); } if(!g_BotLibrary) { g_OmnibotLibPath.clear(); r = BOT_ERROR_CANTLOADDLL; } else { Omnibot_Load_PrintMsg(OB_VA("Found Omni-bot: %s, Attempting to Initialize", g_OmnibotLibPath.c_str())); pfnGetFunctionsFromDLL pfnGetBotFuncs = 0; memset(&g_BotFunctions, 0, sizeof(g_BotFunctions)); pfnGetBotFuncs = (pfnGetFunctionsFromDLL)GetProcAddress(g_BotLibrary, "ExportBotFunctionsFromDLL"); if(!pfnGetBotFuncs) { r = BOT_ERROR_CANTGETBOTFUNCTIONS; Omnibot_Load_PrintErr(OB_VA("Omni-bot Failed with Error: %s", Omnibot_ErrorString(r))); OB_ShowLastError("GetProcAddress", dlerror()); } else { r = pfnGetBotFuncs(&g_BotFunctions, sizeof(g_BotFunctions)); if(r == BOT_ERROR_NONE) { Omnibot_Load_PrintMsg("Omni-bot Loaded Successfully"); r = g_BotFunctions.pfnInitialize(g_InterfaceFunctions, version); g_IsOmnibotLoaded = (r == BOT_ERROR_NONE); } } } return r; }
/** * @brief Omnibot_LL * @param[in] file * @return */ HINSTANCE Omnibot_LL(const char *file) { ////////////////////////////////////////////////////////////////////////// // Parse Variables // $(ProgramFiles) // $(OMNIBOT) ////////////////////////////////////////////////////////////////////////// g_OmnibotLibPath = file; #ifdef WIN32 HINSTANCE hndl = LoadLibrary(g_OmnibotLibPath.c_str()); #else void *hndl = dlopen(g_OmnibotLibPath.c_str(), RTLD_NOW); #endif if (!hndl) { OB_ShowLastError("LoadLibrary"); } Omnibot_Load_PrintMsg(OB_VA("Looking for %s, %s", g_OmnibotLibPath.c_str(), hndl ? "found." : "not found")); return hndl; }
eomnibot_error Omnibot_LoadLibrary(int version, const char *lib, const char *path) { eomnibot_error r = BOT_ERROR_NONE; g_BotLibrary = Omnibot_LL( OB_VA("%s\\%s.dll", path ? path : ".", lib) ); if(g_BotLibrary == 0) g_BotLibrary = Omnibot_LL( OB_VA(".\\omni-bot\\%s.dll", lib) ); if(g_BotLibrary == 0) g_BotLibrary = Omnibot_LL( OB_VA("%s.dll", lib) ); if(g_BotLibrary == 0) { g_OmnibotLibPath.clear(); r = BOT_ERROR_CANTLOADDLL; } else { Omnibot_Load_PrintMsg(OB_VA("Found Omni-bot: %s, Attempting to Initialize", g_OmnibotLibPath.c_str())); pfnGetFunctionsFromDLL pfnGetBotFuncs = 0; memset(&g_BotFunctions, 0, sizeof(g_BotFunctions)); pfnGetBotFuncs = (pfnGetFunctionsFromDLL)GetProcAddress(g_BotLibrary, "ExportBotFunctionsFromDLL"); if(pfnGetBotFuncs == 0) { r = BOT_ERROR_CANTGETBOTFUNCTIONS; Omnibot_Load_PrintErr(OB_VA("Omni-bot Failed with Error: %s", Omnibot_ErrorString(r))); } else { r = pfnGetBotFuncs(&g_BotFunctions, sizeof(g_BotFunctions)); if(r == BOT_ERROR_NONE) { Omnibot_Load_PrintMsg("Omni-bot Loaded Successfully"); r = g_BotFunctions.pfnInitialize(g_InterfaceFunctions, version); g_IsOmnibotLoaded = (r == BOT_ERROR_NONE); } // cs: removed else so interface errors can be printed if (r != BOT_ERROR_NONE) { Omnibot_Load_PrintErr(OB_VA("Omni-bot Failed with Error: %s", Omnibot_ErrorString(r))); Omnibot_FreeLibrary(); } } } return r; }
bool OB_ShowLastError(const char *context, const char *errormsg) { Omnibot_Load_PrintErr(OB_VA("%s Failed with Error: %s", context, errormsg?errormsg:"<unknown error>")); return true; }
/** * @brief Omnibot_LoadLibrary * @param[in] version * @param[in] lib * @param[in] path * @return */ eomnibot_error Omnibot_LoadLibrary(int version, const char *lib, const char *path) { eomnibot_error r = BOT_ERROR_NONE; #ifdef WIN32 #ifdef _WIN64 #define SUFFIX "_x64" #else #define SUFFIX #endif g_BotLibrary = Omnibot_LL(OB_VA("%s\\%s" SUFFIX ".dll", path ? path : ".", lib)); if (g_BotLibrary == 0) { g_BotLibrary = Omnibot_LL(OB_VA(".\\omni-bot\\%s" SUFFIX ".dll", lib)); } if (g_BotLibrary == 0) { g_BotLibrary = Omnibot_LL(OB_VA("%s" SUFFIX ".dll", lib)); } #else #ifdef __x86_64__ #define SUFFIX ".x86_64" #else #define SUFFIX #endif g_BotLibrary = Omnibot_LL(OB_VA("%s/%s" SUFFIX ".so", path ? path : ".", lib)); if (!g_BotLibrary) { g_BotLibrary = Omnibot_LL(OB_VA("./%s" SUFFIX ".so", lib)); } if (!g_BotLibrary) { char *homeDir = getenv("HOME"); if (homeDir) { g_BotLibrary = Omnibot_LL(OB_VA("%s/omni-bot/%s" SUFFIX ".so", homeDir, lib)); } } if (!g_BotLibrary) { g_BotLibrary = Omnibot_LL(OB_VA("%s" SUFFIX ".so", lib)); } #endif if (g_BotLibrary == 0) { g_OmnibotLibPath.clear(); r = BOT_ERROR_CANTLOADDLL; } else { Omnibot_Load_PrintMsg(OB_VA("Found Omni-bot: %s, Attempting to Initialize", g_OmnibotLibPath.c_str())); pfnGetFunctionsFromDLL pfnGetBotFuncs = 0; memset(&g_BotFunctions, 0, sizeof(g_BotFunctions)); pfnGetBotFuncs = (pfnGetFunctionsFromDLL)GetProcAddress(g_BotLibrary, "ExportBotFunctionsFromDLL"); if (pfnGetBotFuncs == 0) { r = BOT_ERROR_CANTGETBOTFUNCTIONS; OB_ShowLastError("GetProcAddress"); } else { r = pfnGetBotFuncs(&g_BotFunctions, sizeof(g_BotFunctions)); if (r == BOT_ERROR_NONE) { r = g_BotFunctions.pfnInitialize(g_InterfaceFunctions, version); } } g_IsOmnibotLoaded = (r == BOT_ERROR_NONE); if (g_IsOmnibotLoaded) { Omnibot_Load_PrintMsg("Omni-bot Loaded Successfully"); } else { Omnibot_Load_PrintErr(OB_VA("Omni-bot Failed with Error: %s", Omnibot_ErrorString(r))); Omnibot_FreeLibrary(); } } return r; }