#pragma region CPL License /* Nuclex Engine Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion #include #include
#include #include #include #ifdef NUCLEX_WIN32 #define WIN32_LEAN_AND_MEAN #define VC_EXTRALEAN #include #endif NUCLEX_WIN32 // --------------------------------------------------------------------------------------------- // namespace Nuclex { /// Default implementation of the evironmental information provider class Win32Environment : public Environment { /// Initializes an instance of Win32Environment /// Path the application was launched in public: Win32Environment(const string &sLaunchPath) : m_sLaunchPath(sLaunchPath), m_sCommandLine(::GetCommandLineA()) { MEMORYSTATUS MemoryStatus; ::GlobalMemoryStatus(&MemoryStatus); m_MemorySize = MemoryStatus.dwTotalPhys / 1048576; { char pszPath[MAX_PATH]; char *pszFilenameInPath = NULL; // Locate the application's directory ::GetModuleFileNameA(NULL, pszPath, sizeof(pszPath)); ::GetFullPathNameA(pszPath, sizeof(pszPath), pszPath, &pszFilenameInPath); m_sApplicationPath = string(pszPath, pszFilenameInPath - pszPath); } } /// Releases all memory used by the Win32Environment instance public: virtual ~Win32Environment() {} // // Win32Environment implementation // /// Display a message box public: void showMessageBox(const string &sCaption, const string &sText) { MessageBoxA(NULL, sText.c_str(), sCaption.c_str(), MB_ICONINFORMATION | MB_OK); } /// Display an error message with ok / cancel buttons public: bool showErrorBox(const string &sCaption, const string &sText) { return MessageBoxA( NULL, sText.c_str(), sCaption.c_str(), MB_ICONERROR | MB_OKCANCEL ) == IDOK; } /// Get command line string public: const string &getCommandLine() const { return m_sCommandLine; } /// Get launch path public: const string &getLaunchPath() const { return m_sLaunchPath; } /// Get application path public: const string &getApplicationPath() const { return m_sApplicationPath; } /// Get operating system public: OperatingSystem getOperatingSystem() const { return OS_WIN32; } /// Get amount of available memory public: size_t getMemorySize() const { return m_MemorySize; } /// Command line string private: string m_sCommandLine; /// Launch path private: string m_sLaunchPath; /// Application path private: string m_sApplicationPath; /// Amount of installed memory private: size_t m_MemorySize; }; } // namespace Nuclex // --------------------------------------------------------------------------------------------- // /// Entry point for console applications /// Number of arguments /// Program path and arguments /// Zero on success int main(int nArgC, char *ppszArgV[]) { using namespace Nuclex; // Obtain the current working directory, aka path from which the application // has been launched std::string sLaunchPath; { char pszPath[MAX_PATH]; char *pszFilenameInPath = NULL; ::GetCurrentDirectoryA(sizeof(pszPath), pszPath); sLaunchPath = pszPath; } #ifdef NUCLEX_WIN32 Win32Environment TheEnvironment(sLaunchPath); ::SetCurrentDirectoryA(TheEnvironment.getApplicationPath().c_str()); #else #error Not implemented yet #endif try { Kernel::logMessage(Kernel::MT_INFO, "Entering main application."); NuclexMain(TheEnvironment); } catch(const Exception &Exception) { if(!TheEnvironment.showErrorBox( "Nuclex error report", string("An unhandled exception has caused the application to quit:\n") + typeid(Exception).name() + "\n" + "\n" + "Source of the error:\n" + Exception.getSource() + "\n" + "\n" + "Error description:\n" + Exception.what() + "\n" + "\n" + "Use cancel to let the exception pass to the debugger\n" )) throw; } Kernel::logMessage(Kernel::MT_INFO, "Clean exit."); return 0; } // --------------------------------------------------------------------------------------------- // #ifdef NUCLEX_WIN32 /// Program entry point for windows applications /// Instance handle of the new process /// Always NULL /// Arguments provided on the command line /// Initial window mode /// Zero on success int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdParam, int nCmdShow ) { return main(0, NULL); } #endif // NUCLEX_WIN32 // --------------------------------------------------------------------------------------------- //