Back to index

nux  3.0.0
Process.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 
00025 namespace nux
00026 {
00027 
00028 #ifdef _WIN32
00029 //
00030 // Launch a uniform resource locator (i.e. http://www.yahoo.com/finance).
00031 // This is expected to return immediately as the URL is launched by another
00032 // task.
00033 //
00034   void inlLaunchURL ( const TCHAR *URL, const TCHAR *Parms, NString *Error )
00035   {
00036     nuxDebugMsg ( TEXT ("LaunchURL %s %s"), URL, Parms ? Parms : TEXT ("") );
00037     HINSTANCE Code = CALL_OS_TCHAR_FUNCTION (ShellExecuteW (NULL, TEXT ("open"), URL, Parms ? Parms : TEXT (""), TEXT (""), SW_SHOWNORMAL), ShellExecuteA (NULL, "open", TCHAR_TO_ANSI (URL), Parms ? TCHAR_TO_ANSI (Parms) : "", "", SW_SHOWNORMAL) );
00038 
00039     if (Error)
00040       *Error = (int) Code <= 32 ? TEXT ("UrlFailed") : TEXT ("");
00041   }
00042 
00043 //
00044 // Creates a new process and its primary thread. The new process runs the
00045 // specified executable file in the security context of the calling process.
00046 //
00047   void *inlCreateProc ( const TCHAR *URL, const TCHAR *Parms )
00048   {
00049     nuxDebugMsg (  TEXT ("CreateProc %s %s"), URL, Parms );
00050 
00051     TCHAR CommandLine[1024];
00052     Snprintf ( CommandLine, 1024, 1024 - 1, TEXT ("%s %s"), URL, Parms );
00053 
00054     PROCESS_INFORMATION ProcInfo;
00055     SECURITY_ATTRIBUTES Attr;
00056     Attr.nLength = sizeof (SECURITY_ATTRIBUTES);
00057     Attr.lpSecurityDescriptor = NULL;
00058     Attr.bInheritHandle = TRUE;
00059 
00060     STARTUPINFO StartupInfo = { sizeof (STARTUPINFO), NULL, NULL, NULL,
00061                                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
00062                                 NULL, NULL, NULL, NULL, SW_HIDE, NULL, NULL,
00063                                 NULL, NULL, NULL
00064                               };
00065 
00066     if ( !CreateProcess ( NULL, CommandLine, &Attr, &Attr, TRUE, DETACHED_PROCESS | REALTIME_PRIORITY_CLASS,
00067                           NULL, NULL, &StartupInfo, &ProcInfo ) )
00068       return NULL;
00069 
00070     return (void *) ProcInfo.hProcess;
00071   }
00072 
00073 //
00074 // Retrieves the termination status of the specified process.
00075 //
00076   BOOL inlGetProcReturnCode ( void *ProcHandle, INT *ReturnCode )
00077   {
00078     return GetExitCodeProcess ( (HANDLE) ProcHandle, (DWORD *) ReturnCode ) && * ( (DWORD *) ReturnCode) != STILL_ACTIVE;
00079   }
00080 
00081 
00082   NUX_IMPLEMENT_GLOBAL_OBJECT (NProcess);
00083 
00084   void NProcess::Constructor()
00085   {
00086     m_ProcessID = GetCurrentProcessId();
00087     m_ProcessHandle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, m_ProcessID);
00088 
00089     m_MainThreadID = GetCurrentThreadId();
00090     m_MainThreadHandle = OpenThread (THREAD_ALL_ACCESS, FALSE, m_MainThreadID);
00091   }
00092 
00093   void NProcess::Destructor()
00094   {
00095     CloseHandle (m_MainThreadHandle);
00096     CloseHandle (m_ProcessHandle);
00097   }
00098 
00099   HANDLE NProcess::GetProcessHandle()
00100   {
00101     return m_ProcessHandle;
00102   }
00103 
00104   DWORD NProcess::GetProcessID()
00105   {
00106     return m_ProcessID;
00107   }
00108 
00109   HANDLE NProcess::GetMainThreadHandle()
00110   {
00111     return m_MainThreadHandle;
00112   }
00113 
00114   DWORD NProcess::GetMainThreadID()
00115   {
00116     return m_MainThreadID;
00117   }
00118 
00119   HANDLE NProcess::GetCurrentThreadHandle()
00120   {
00121     DWORD ThreadID = GetCurrentThreadId();
00122     return OpenThread (THREAD_ALL_ACCESS, FALSE, ThreadID);
00123   }
00124 
00125   DWORD NProcess::GetCurrentThreadID()
00126   {
00127     return GetCurrentThreadId();
00128   }
00129 
00130 #endif
00131 
00132 }
00133