Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

command.h

Go to the documentation of this file.
00001 
00002 // Name:        command.h
00003 // Author:      Joe Yates
00004 // Purpose:     An alternative to wxExecute
00005 // Created:     2004-11-25
00006 // Copyright:   (c) Joe Yates
00007 // License:     BSD license (see the file 'LICENSE.txt')
00009 
00010 #ifndef __wx_msw_command_h__
00011 #define __wx_msw_command_h__
00012 
00013 #include <wx/wxprec.h>
00014 #ifndef WX_PRECOMP
00015   #include "wx/wx.h"
00016 #endif
00017 
00018 #include <wx/exception.h>
00019 
00020 namespace wx
00021 {
00022 
00028 class Command
00029   {
00030   public:
00031     Command
00032       (
00033       const wxString& sCommand,
00034       unsigned long   ulTimeOut = INFINITE
00035       ) :
00036       m_sCommand(sCommand),
00037       m_dwTimeOut(ulTimeOut)
00038       {}
00039   public:
00040     long Run()
00041       {
00042       STARTUPINFO si;
00043       wxZeroMemory(si);
00044       si.cb = sizeof(si);
00045       si.dwFlags     = STARTF_USESTDHANDLES;
00046       si.hStdInput   = GetStdHandle(STD_INPUT_HANDLE);
00047       si.hStdOutput  = GetStdHandle(STD_OUTPUT_HANDLE);
00048       si.hStdError   = GetStdHandle(STD_ERROR_HANDLE);
00049 
00050       PROCESS_INFORMATION pi;
00051       BOOL nOk = ::CreateProcess
00052         (
00053         NULL,
00054         (wxChar *) m_sCommand.c_str(),
00055         NULL,
00056         NULL,
00057         0,
00058         CREATE_NEW_CONSOLE, // CREATE_NO_WINDOW
00059         NULL,
00060         NULL,
00061         &si,
00062         &pi
00063         );
00064 
00065       if(nOk == 0)
00066         {
00067         DWORD dwLastError = GetLastError();
00068         LPTSTR szBuffer = NULL;
00069         FormatMessage
00070           (
00071           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00072           NULL,
00073           dwLastError,
00074           0,
00075           szBuffer,
00076           20,
00077           NULL
00078           );
00079         wxString sError = szBuffer;
00080         LocalFree(szBuffer);
00081         throw wx::Exception(sError);
00082         }
00083 
00084       WaitForSingleObject(pi.hProcess, m_dwTimeOut);
00085 
00086       DWORD dwExitCode = 0;
00087       GetExitCodeProcess(pi.hProcess, &dwExitCode);
00088 
00089       return (long) dwExitCode;
00090       }
00091 
00092   private:
00093     wxString  m_sCommand;
00094     DWORD     m_dwTimeOut;
00095   };
00096 
00097 } // namespace wx
00098 
00099 #endif // ndef __wx_msw_command_h__

Generated on Wed Jan 25 08:13:08 2006 for Sherpa wxWidgets Classes by doxygen 1.3.6