/** * @file include/ActivMiddleware/Misc/MainHelper.h * * @brief Header file to implement common main() function. */ #if (!defined ACTIV_MISC_MAIN_HELPER_H) #define ACTIV_MISC_MAIN_HELPER_H #include "ActivMiddleware/ActivBase/CommandLineParser.h" #include "ActivMiddleware/ActivBase/LogMessageException.h" #include "ActivMiddleware/ActivBase/MessageBuilder.h" #include "ActivMiddleware/ActivBase/Process.h" #include "ActivMiddleware/ActivBase/StatusCodeException.h" #include "ActivMiddleware/Misc/Fmt.h" #include ACTIV_BOOST_INCLUDE(smart_ptr/scoped_array.hpp) /** * @brief main() implementation to wrap user's Main() and catch common exceptions. * * To use this function include this file at the bottom of your main source file. * * @param argc * @param argv * * @return program return code. */ int main(int argc, char *argv[]) { using namespace Activ; int res; #if (defined _DEBUG) if (Process::WasStartedViaDebugger()) { res = Main(argc, argv); } else #endif { #if (defined ACTIV_PLATFORM_LINUX) if (Environment::Get("ACTIV_SET_DUMPABLE", true)) { // Applications with file capabilities set may have the dumpable flag cleared. // Try to set the dumpable flag. (void) ::prctl(PR_SET_DUMPABLE, true, 0, 0, 0); } #endif try { res = Main(argc, argv); } catch (const Exception &e) { LogMessage(LOG_TYPE_ERROR, Fmt::Format("Exception: {}", e)); res = EXIT_FAILURE; } catch (const Boost::program_options::error &e) { LogMessage(LOG_TYPE_ERROR, Fmt::Format("Command line error: {}", e.what())); res = EXIT_FAILURE; } catch (const std::exception &e) { LogMessage(LOG_TYPE_ERROR, Fmt::Format("Exception: {}", e.what())); res = EXIT_FAILURE; } ACTIV_CATCH_ALL() { LogMessage(LOG_TYPE_ERROR, "Unknown exception."); res = EXIT_FAILURE; } } return res; } // --------------------------------------------------------------------------------------------------------------------------------- #if (defined ACTIV_PLATFORM_WINDOWS) && (!defined _CONSOLE) /** * @brief WinMain() implementation to invoke the main() above. Only really useful for creating a window-less application. * * @param hInstance * @param hPrevInstance * @param lpCmdLine * @param nCmdShow * * @return program return code. */ int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { using namespace Activ; // Round those houses to get an argv. CxxYZ::vector argList; { int numberOfArgs; LPWSTR * const pWideArgs = CommandLineToArgvW(GetCommandLineW(), &numberOfArgs); if (NULL == pWideArgs) return EXIT_FAILURE; bool hasFailed = false; for (int i = 0; i < numberOfArgs; ++i) { MessageBuilder messageBuilder; if (STATUS_CODE_SUCCESS == messageBuilder.AppendPrintf("%ls%c", pWideArgs[i], 0)) { argList.emplace_back(CxxYZ::move(messageBuilder)); } else { hasFailed = true; break; } } ::LocalFree(pWideArgs); if (hasFailed) return EXIT_FAILURE; } const Boost::scoped_array argv(new char*[argList.size()]); for (size_t i = 0; i < argList.size(); ++i) { argv[i] = reinterpret_cast(const_cast(argList[i].GetMessage())); } return main(static_cast(argList.size()), argv.get()); } #endif // (defined ACTIV_PLATFORM_WINDOWS) && (!defined _CONSOLE) #endif // (!defined ACTIV_MIDDLEWARE_MISC_MAIN_HELPER_H)