/**************************************************************************** For each file on the command line, launch it with its registered application. For Windows computers only. Compiled and tested with the 3.2.3 version of the MinGW g++ compiler: g++ -o app.exe app.cpp ---------------------------------------------------------------------- Copyright (C) 2006 Don Peterson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ****************************************************************************/ #include #include #include #include using std::cerr; using std::string; using std::endl; const string edit = "edit"; const string explore = "explore"; const string find = "find"; const string open = "open"; const string print = "print"; string program_name = ""; // ---------------------------------------------------------------------- void Usage(void) { cerr << "Usage: " << program_name << " [-a action] file1 [file2...]" << endl << "Launches each file with its registered application." << endl << " Actions are:" << endl << " edit" << endl << " explore" << endl << " find" << endl << " open (this is the default)" << endl << " print" << endl; exit(1); } // ---------------------------------------------------------------------- void ErrorMessage(const char * file_to_launch, const string message) { cerr << file_to_launch << ": " << message << endl; } // ---------------------------------------------------------------------- void LaunchFile(const char * file_to_launch, const string action) { const char * command_parameters = ""; const char * working_directory = ""; const int open_in_normal_window = 1; HINSTANCE ret = ShellExecute( 0, action.c_str(), file_to_launch, command_parameters, working_directory, open_in_normal_window ); int return_value = int(ret); const int failed_status = 32; if (return_value > failed_status) return; // Successfully executed // The call did not succeed; inform the user of the problem. These // ShellExecute return values are from the MSDN help shipped with the // Visual C++ 2003 package. switch (return_value) { case 0: // Fall through intentional case SE_ERR_OOM: ErrorMessage(file_to_launch, "Out of memory"); break; case ERROR_FILE_NOT_FOUND: ErrorMessage(file_to_launch, "File not found"); break; case ERROR_PATH_NOT_FOUND: ErrorMessage(file_to_launch, "Path not found"); break; case ERROR_BAD_FORMAT: ErrorMessage(file_to_launch, "EXE file is invalid"); break; case SE_ERR_ACCESSDENIED: ErrorMessage(file_to_launch, "Access denied"); break; case SE_ERR_ASSOCINCOMPLETE: ErrorMessage(file_to_launch, "File name association incomplete or invalid"); break; case SE_ERR_DDEBUSY: ErrorMessage(file_to_launch, "DDE busy"); break; case SE_ERR_DDEFAIL: ErrorMessage(file_to_launch, "DDE failed"); break; case SE_ERR_DDETIMEOUT: ErrorMessage(file_to_launch, "DDE timed out"); break; case SE_ERR_DLLNOTFOUND: ErrorMessage(file_to_launch, "DLL not found"); break; case SE_ERR_NOASSOC: ErrorMessage(file_to_launch, "No application associated with file"); break; case SE_ERR_SHARE: ErrorMessage(file_to_launch, "Sharing violation"); break; default: break; } } int main(int argc, char** argv) { program_name = argv[0]; string action = open; // Default action is to open the file if (argc < 2) Usage(); // Check for -a option argv++; if (argv[0][0] == '-' and argv[0][1] == 'a') { argv++; if (not *argv) Usage(); // Make sure the action is allowed string action = *argv; if (action != edit and action != explore and action != find and action != open and action != print) { Usage(); } argv++; if (not *argv) // Need at least one file Usage(); } while (*argv) { LaunchFile(*argv, action); argv++; } return 0; }