ros_bin.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <cstdlib>
00015 #include <sstream>
00016 #include <algorithm>
00017 #ifdef WIN32
00018   #include <windows.h>
00019 #endif
00020 
00021 char exe_name[MAX_PATH];
00022 std::stringstream arguments;
00023 std::string python_home, python_script, python_exe;
00024 
00025 /*****************************************************************************
00026 ** Functions
00027 *****************************************************************************/
00028 
00029 void debug() {
00030         std::cout << std::endl;
00031         std::cout << "Program Variables:" << std::endl;
00032         std::cout << "  Python exe: " << python_exe << std::endl;
00033         std::cout << "  Executable: " << exe_name << std::endl;
00034         std::cout << "  Python script: " << python_script << std::endl;
00035         std::cout << "  Arguments: " << arguments.str() << std::endl;
00036         std::cout << std::endl;
00037 }
00038 
00039 /*****************************************************************************
00040 ** Main
00041 *****************************************************************************/
00042 
00043 int main(int argc, char **argv) {
00044 
00045         exe_name[0] = '\0';
00046 #ifdef WIN32
00047         //_splitpath_s(argv[0], NULL, 0, NULL, 0, name, 256, NULL, 0);
00048 
00049     // could use GetModuleHandleW, WCHAR, GetModuleFileNameW, wcout and wstring here instead.
00050         HMODULE hModule = GetModuleHandle(NULL);
00051     GetModuleFileName(hModule, exe_name, MAX_PATH);
00052     python_script = std::string(exe_name);
00053     python_script.replace(python_script.end()-4, python_script.end(), ""); // replace trailing ".exe" with ""
00054 
00055         python_exe = python_home + std::string("python");
00056         arguments << python_exe << " " << python_script;
00057         for ( int i = 1; i < argc; ++i ) {
00058                 // need the quotes to make sure spaces dont muck things up
00059                 arguments << " \"" << argv[i] << "\"";
00060         }
00061 
00062         /* TODO: Need some validation checks here! */
00063 
00064         STARTUPINFO startup_info;
00065         PROCESS_INFORMATION process_info;
00066         memset(&startup_info, 0, sizeof(startup_info));
00067         memset(&process_info, 0, sizeof(process_info));
00068 
00069         startup_info.cb = sizeof(startup_info);
00070 
00071         int result =
00072                 CreateProcess(
00073                         NULL, 
00074                         const_cast<char*>(arguments.str().c_str()), // bloody windoze
00075                         NULL,
00076                         NULL,
00077                         FALSE,
00078                         0, // CREATE_NEW_CONSOLE,
00079                         NULL,
00080                         NULL,
00081                         &startup_info,
00082                         &process_info
00083                         );
00084         if ( result == 0 ) {
00085                 switch ( result ) {
00086                         case ( ERROR_FILE_NOT_FOUND ) : {
00087                                 std::cout << "The python executable could not be found - check it is in your PATH" << std::endl;
00088                                 debug();
00089                                 break;
00090                         }
00091                         default: {
00092                                 std::cout << "Process failed with error: " << GetLastError() << std::endl;
00093                                 debug();
00094                                 break;
00095                         }
00096                 }
00097         } else {
00098                 WaitForSingleObject( process_info.hProcess, INFINITE );
00099             CloseHandle( process_info.hProcess );
00100             CloseHandle( process_info.hThread );
00101         }
00102 #else
00103         std::cout << "This is a windows application only." << std::endl;
00104 #endif
00105         return 0;
00106 }
00107 
00108 
00109 
00110 


msvc_runtime
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:23:43