1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2007, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: process L "Process"
16 *
17 * Summary: Process, shell, arguments and environment functions.
18 *
19 * List: *,argc,argv,exit,getenv,processf,setenv,shell
20 *
21 -----------------------------------------------------------------------------*/
22
23 /*-----------------------------------------------------------------------------
24 * Id: processf F process
25 *
26 * Summary: Starting a process.
27 *
28 * Params: cmdline - The command line.
29 workdir - The working directory. It can be 0->str.
30 result - The pointer to uint for getting the result. If #b(0), the /
31 function will not wait until the process finishes its work.
32 *
33 * Return: #b(1) if the calling process was successful; otherwise #b(0).
34 *
35 -----------------------------------------------------------------------------*/
36
37 func uint process( str cmdline, str workdir, uint result, uint state )
38 {
39 PROCESS_INFORMATION stpi
40 STARTUPINFO start
41 uint handle
42
43 start.cb = sizeof( STARTUPINFO )
44 if state != $SW_SHOWNORMAL
45 {
46 start.wShowWindow = state
47 start.dwFlags = 1 //$STARTF_USESHOWWINDOW
48 }
49 handle = CreateProcess( 0, cmdline.ptr( ), 0, 0, 1,
50 $CREATE_DEFAULT_ERROR_MODE | $NORMAL_PRIORITY_CLASS,
51 0, ?( workdir && *workdir, workdir.ptr(), 0 ),
52 start, stpi )
53 if !handle && GetLastError( ) == 740 //ERROR_ELEVATION_REQUIRED
54 {
55 /* if ( ShellExecute( 0, "runas".ptr(), cmdline.ptr(), 0,
56 ?( workdir && *workdir, workdir.ptr(), 0 ), $SW_SHOWNORMAL ))
57 {
58 return 1
59 }*/
60 SHELLEXECUTEINFO shex
61 str filename params
62 uint off
63
64 if cmdline[0] == '"'
65 {
66 off = cmdline.findchfrom( '"', 1 )
67 filename.substr( cmdline, 1, off - 1 )
68 params.substr( cmdline, off + 1, *cmdline - off - 1 )
69 }
70 else
71 {
72 off = cmdline.findchfrom( ' ', 0 )
73 filename.substr( cmdline, 0, off )
74 params.substr( cmdline, off + 1, *cmdline - off - 1 )
75 }
76 shex.cbSize = sizeof( SHELLEXECUTEINFO )
77 // shex.fMask = 0
78 // shex.hwnd = NULL;
79 shex.lpVerb = "runas".ptr()
80 shex.lpFile = filename.ptr()
81 shex.lpParameters = ?( *params, params.ptr(), 0 )
82 shex.lpDirectory = ?( workdir && *workdir, workdir.ptr(), 0 )
83 shex.nShow = state;
84 return ShellExecuteEx( shex )
85 }
86 if handle
87 {
88 if result
89 {
90 WaitForSingleObject( stpi.hThread, $INFINITE )
91 GetExitCodeProcess( stpi.hProcess, result )
92 }
93 CloseHandle( stpi.hThread )
94 CloseHandle( stpi.hProcess )
95 return 1
96 }
97 return 0
98 }
99
100 func uint process( str cmdline, str workdir, uint result )
101 {
102 return process( cmdline, workdir, result, $SW_SHOWNORMAL )
103 }
104
105 /*-----------------------------------------------------------------------------
106 * Id: shell F
107 *
108 * Summary: Launch or open a file in the associated application.
109 *
110 * Params: name - Filename.
111 *
112 -----------------------------------------------------------------------------*/
113
114 func shell( str name )
115 {
116 ShellExecute( 0, "open".ptr(), name.ptr(), 0, 0, $SW_SHOWNORMAL )
117 }
118
119 /*-----------------------------------------------------------------------------
120 * Id: exit F
121 *
122 * Summary: Exit the current program.
123 *
124 * Params: code - A return code or the results of the work of the program.
125 *
126 -----------------------------------------------------------------------------*/
127
128 func exit( uint code )
129 {
130 ExitProcess( code )
131 }
132
133 /*-----------------------------------------------------------------------------
134 * Id: getenv F
135 *
136 * Summary: Get an environment variable.
137 *
138 * Params: varname - Environment variable name.
139 ret - String for getting the value.
140 *
141 * Return: #lng/retpar( ret )
142 *
143 -----------------------------------------------------------------------------*/
144
145 func str getenv( str varname, str ret )
146 {
147 uint ptr
148
149 ret.clear()
150 if ptr = _getenv( varname.ptr())
151 {
152 ret.copy( ptr )
153 }
154 return ret
155 }
156
157 /*-----------------------------------------------------------------------------
158 ** Id: setenv F
159 *
160 * Summary: Set a value of an environment variable. The function adds new
161 environment variable or modifies the value of the existing
162 environment variable. New values will be valid only in the
163 current process.
164 *
165 * Params: varname - Environment variable name.
166 varvalue - A new value of the environment variable.
167 *
168 * Return: #lng/retf#
169 *
170 -----------------------------------------------------------------------------*/
171
172 func uint setenv( str varname, str varvalue )
173 {
174 return ?( _setenv( "\(varname)=\(varvalue)".ptr()), 0, 1 )
175 }
176
177