~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxCore/NProcess.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-12-17 13:59:57 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101217135957-5gvg6fkjxaa252i0
Tags: upstream-0.9.12
ImportĀ upstreamĀ versionĀ 0.9.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2010 Inalogic Inc.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU Lesser General Public License version 3, as
6
 
 * published by the  Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
12
 
 * License for more details.
13
 
 *
14
 
 * You should have received a copy of both the GNU Lesser General Public
15
 
 * License version 3 along with this program.  If not, see
16
 
 * <http://www.gnu.org/licenses/>
17
 
 *
18
 
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
19
 
 *
20
 
 */
21
 
 
22
 
 
23
 
#include "NuxCore.h"
24
 
 
25
 
namespace nux
26
 
{
27
 
 
28
 
#ifdef _WIN32
29
 
//
30
 
// Launch a uniform resource locator (i.e. http://www.yahoo.com/finance).
31
 
// This is expected to return immediately as the URL is launched by another
32
 
// task.
33
 
//
34
 
  void inlLaunchURL ( const TCHAR *URL, const TCHAR *Parms, NString *Error )
35
 
  {
36
 
    nuxDebugMsg ( TEXT ("LaunchURL %s %s"), URL, Parms ? Parms : TEXT ("") );
37
 
    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) );
38
 
 
39
 
    if (Error)
40
 
      *Error = (int) Code <= 32 ? TEXT ("UrlFailed") : TEXT ("");
41
 
  }
42
 
 
43
 
//
44
 
// Creates a new process and its primary thread. The new process runs the
45
 
// specified executable file in the security context of the calling process.
46
 
//
47
 
  void *inlCreateProc ( const TCHAR *URL, const TCHAR *Parms )
48
 
  {
49
 
    nuxDebugMsg (  TEXT ("CreateProc %s %s"), URL, Parms );
50
 
 
51
 
    TCHAR CommandLine[1024];
52
 
    Snprintf ( CommandLine, 1024, 1024 - 1, TEXT ("%s %s"), URL, Parms );
53
 
 
54
 
    PROCESS_INFORMATION ProcInfo;
55
 
    SECURITY_ATTRIBUTES Attr;
56
 
    Attr.nLength = sizeof (SECURITY_ATTRIBUTES);
57
 
    Attr.lpSecurityDescriptor = NULL;
58
 
    Attr.bInheritHandle = TRUE;
59
 
 
60
 
    STARTUPINFO StartupInfo = { sizeof (STARTUPINFO), NULL, NULL, NULL,
61
 
                                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
62
 
                                NULL, NULL, NULL, NULL, SW_HIDE, NULL, NULL,
63
 
                                NULL, NULL, NULL
64
 
                              };
65
 
 
66
 
    if ( !CreateProcess ( NULL, CommandLine, &Attr, &Attr, TRUE, DETACHED_PROCESS | REALTIME_PRIORITY_CLASS,
67
 
                          NULL, NULL, &StartupInfo, &ProcInfo ) )
68
 
      return NULL;
69
 
 
70
 
    return (void *) ProcInfo.hProcess;
71
 
  }
72
 
 
73
 
//
74
 
// Retrieves the termination status of the specified process.
75
 
//
76
 
  BOOL inlGetProcReturnCode ( void *ProcHandle, INT *ReturnCode )
77
 
  {
78
 
    return GetExitCodeProcess ( (HANDLE) ProcHandle, (DWORD *) ReturnCode ) && * ( (DWORD *) ReturnCode) != STILL_ACTIVE;
79
 
  }
80
 
 
81
 
 
82
 
  NUX_IMPLEMENT_GLOBAL_OBJECT (NProcess);
83
 
 
84
 
  void NProcess::Constructor()
85
 
  {
86
 
    m_ProcessID = GetCurrentProcessId();
87
 
    m_ProcessHandle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, m_ProcessID);
88
 
 
89
 
    m_MainThreadID = GetCurrentThreadId();
90
 
    m_MainThreadHandle = OpenThread (THREAD_ALL_ACCESS, FALSE, m_MainThreadID);
91
 
  }
92
 
 
93
 
  void NProcess::Destructor()
94
 
  {
95
 
    CloseHandle (m_MainThreadHandle);
96
 
    CloseHandle (m_ProcessHandle);
97
 
  }
98
 
 
99
 
  HANDLE NProcess::GetProcessHandle()
100
 
  {
101
 
    return m_ProcessHandle;
102
 
  }
103
 
 
104
 
  DWORD NProcess::GetProcessID()
105
 
  {
106
 
    return m_ProcessID;
107
 
  }
108
 
 
109
 
  HANDLE NProcess::GetMainThreadHandle()
110
 
  {
111
 
    return m_MainThreadHandle;
112
 
  }
113
 
 
114
 
  DWORD NProcess::GetMainThreadID()
115
 
  {
116
 
    return m_MainThreadID;
117
 
  }
118
 
 
119
 
  HANDLE NProcess::GetCurrentThreadHandle()
120
 
  {
121
 
    DWORD ThreadID = GetCurrentThreadId();
122
 
    return OpenThread (THREAD_ALL_ACCESS, FALSE, ThreadID);
123
 
  }
124
 
 
125
 
  DWORD NProcess::GetCurrentThreadID()
126
 
  {
127
 
    return GetCurrentThreadId();
128
 
  }
129
 
 
130
 
#endif
131
 
 
132
 
}
133