VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testdriver/winbase.py@ 57181

Last change on this file since 57181 was 56295, checked in by vboxsync, 10 years ago

ValidationKit: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: winbase.py 56295 2015-06-09 14:29:55Z vboxsync $
3
4"""
5This module is here to externalize some Windows specifics that gives pychecker
6a hard time when running on non-Windows systems.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2015 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 56295 $"
31
32
33# Standard Python imports.
34import os
35
36# Windows specific imports.
37import win32api; # pylint: disable=F0401
38import win32con; # pylint: disable=F0401
39import win32console; # pylint: disable=F0401
40import win32event; # pylint: disable=F0401
41import win32process; # pylint: disable=F0401
42
43# Validation Kit imports.
44from testdriver import reporter;
45
46
47
48#
49# Windows specific implementation of base functions.
50#
51
52def processInterrupt(uPid):
53 """
54 The Windows version of base.processInterrupt
55
56 Note! This doesn't work terribly well with a lot of processes.
57 """
58 try:
59 win32console.GenerateConsoleCtrlEvent(win32con.CTRL_BREAK_EVENT, uPid);
60 #GenerateConsoleCtrlEvent = ctypes.windll.kernel32.GenerateConsoleCtrlEvent
61 #rc = GenerateConsoleCtrlEvent(1, uPid);
62 #reporter.log('GenerateConsoleCtrlEvent -> %s' % (rc,));
63 fRc = True;
64 except:
65 reporter.logXcpt('uPid=%s' % (uPid,));
66 fRc = False;
67 return fRc;
68
69def postThreadMesssageClose(uTid):
70 """ Posts a WM_CLOSE message to the specified thread."""
71 fRc = False;
72 try:
73 win32api.PostThreadMessage(uTid, win32con.WM_CLOSE, 0, 0);
74 fRc = True;
75 except:
76 reporter.logXcpt('uTid=%s' % (uTid,));
77 return fRc;
78
79def postThreadMesssageQuit(uTid):
80 """ Posts a WM_QUIT message to the specified thread."""
81 fRc = False;
82 try:
83 win32api.PostThreadMessage(uTid, win32con.WM_QUIT, 0x40010004, 0); # DBG_TERMINATE_PROCESS
84 fRc = True;
85 except:
86 reporter.logXcpt('uTid=%s' % (uTid,));
87 return fRc;
88
89def processTerminate(uPid):
90 """ The Windows version of base.processTerminate """
91 fRc = False;
92 try:
93 hProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, uPid);
94 except:
95 reporter.logXcpt('uPid=%s' % (uPid,));
96 else:
97 try:
98 win32process.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS
99 fRc = True;
100 except:
101 reporter.logXcpt('uPid=%s' % (uPid,));
102 win32api.CloseHandle(hProcess)
103 return fRc;
104
105def processKill(uPid):
106 """ The Windows version of base.processKill """
107 return processTerminate(uPid);
108
109def processExists(uPid):
110 """ The Windows version of base.processExists """
111 fRc = False;
112 try:
113 hProcess = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, uPid);
114 except:
115 reporter.logXcpt('uPid=%s' % (uPid,));
116 else:
117 win32api.CloseHandle(hProcess)
118 fRc = True;
119 return fRc;
120
121def processCheckPidAndName(uPid, sName):
122 """ The Windows version of base.processCheckPidAndName """
123 fRc = processExists(uPid);
124 if fRc is True:
125 try:
126 from win32com.client import GetObject; # pylint: disable=F0401
127 oWmi = GetObject('winmgmts:');
128 aoProcesses = oWmi.InstancesOf('Win32_Process');
129 for oProcess in aoProcesses:
130 if long(oProcess.Properties_("ProcessId").Value) == uPid:
131 sCurName = oProcess.Properties_("Name").Value;
132 reporter.log2('uPid=%s sName=%s sCurName=%s' % (uPid, sName, sCurName));
133 sName = sName.lower();
134 sCurName = sCurName.lower();
135 if os.path.basename(sName) == sName:
136 sCurName = os.path.basename(sCurName);
137
138 if sCurName == sName \
139 or sCurName + '.exe' == sName \
140 or sCurName == sName + '.exe':
141 fRc = True;
142 break;
143 except:
144 reporter.logXcpt('uPid=%s sName=%s' % (uPid, sName));
145 return fRc;
146
147#
148# Some helper functions.
149#
150def processCreate(sName, asArgs):
151 """
152 Returns a (pid, handle, tid) tuple on success. (-1, None) on failure (logged).
153 """
154
155 # Construct a command line.
156 sCmdLine = '';
157 for sArg in asArgs:
158 if sCmdLine == '':
159 sCmdLine += '"';
160 else:
161 sCmdLine += ' "';
162 sCmdLine += sArg;
163 sCmdLine += '"';
164
165 # Try start the process.
166 dwCreationFlags = win32con.CREATE_NEW_PROCESS_GROUP;
167 oStartupInfo = win32process.STARTUPINFO();
168 try:
169 (hProcess, hThread, uPid, uTid) = win32process.CreateProcess(sName,
170 sCmdLine, # CommandLine
171 None, # ProcessAttributes
172 None, # ThreadAttibutes
173 1, # fInheritHandles
174 dwCreationFlags,
175 None, # Environment
176 None, # CurrentDirectory.
177 oStartupInfo);
178 except:
179 reporter.logXcpt('sName="%s" sCmdLine="%s"' % (sName, sCmdLine));
180 return (-1, None, -1);
181
182 # Dispense with the thread handle.
183 try:
184 win32api.CloseHandle(hThread);
185 except:
186 reporter.logXcpt();
187
188 # Try get full access to the process.
189 try:
190 hProcessFullAccess = win32api.DuplicateHandle( \
191 win32api.GetCurrentProcess(), \
192 hProcess, \
193 win32api.GetCurrentProcess(), \
194 win32con.PROCESS_TERMINATE \
195 | win32con.PROCESS_QUERY_INFORMATION \
196 | win32con.SYNCHRONIZE \
197 | win32con.DELETE, \
198 False, \
199 0);
200 win32api.CloseHandle(hProcess);
201 hProcess = hProcessFullAccess;
202 except:
203 reporter.logXcpt();
204 reporter.log2('processCreate -> %#x, hProcess=%#x' % (uPid, hProcess,));
205 return (uPid, hProcess, uTid);
206
207def processPollByHandle(hProcess):
208 """
209 Polls the process handle to see if it has finished (True) or not (False).
210 """
211 try:
212 dwWait = win32event.WaitForSingleObject(hProcess, 0);
213 except:
214 reporter.logXcpt('hProcess=%s %#x' % (hProcess, hProcess,));
215 return True;
216 return dwWait != win32con.WAIT_TIMEOUT; #0x102; #
217
218
219def processTerminateByHandle(hProcess):
220 """
221 Terminates the process.
222 """
223 try:
224 win32api.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS
225 except:
226 reporter.logXcpt('hProcess=%s %#x' % (hProcess, hProcess,));
227 return False;
228 return True;
229
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette