|
| 1 | +import time |
1 | 2 | import windows |
2 | | -import windows.com |
3 | | -import windows.generated_def as gdef |
4 | 3 |
|
5 | | -def bstr_variant(s): |
6 | | - v = windows.com.Variant() |
7 | | - v.vt = gdef.VT_BSTR |
8 | | - v._VARIANT_NAME_3.bstrVal = s |
9 | | - return v |
10 | 4 |
|
| 5 | +wmispace = windows.system.wmi["root\\cimv2"] |
| 6 | +print("WMI namespace is <{0}>".format(wmispace)) |
| 7 | +proc_class = wmispace.get_object("Win32_process") |
| 8 | +print("Process class is {0}".format(proc_class)) |
11 | 9 |
|
12 | | -wmireq = windows.system.wmi["root\\cimv2"] |
13 | | -proc_class = wmireq.get_object("Win32_process") |
| 10 | +inparam_cls = proc_class.get_method("Create").inparam |
| 11 | +print("Method Create InParams is <{0}>".format(inparam_cls)) |
| 12 | +print("Method Create InParams properties are <{0}>".format(inparam_cls.properties)) |
| 13 | +print("Creating instance of inparam") |
14 | 14 |
|
15 | | -# # Method 1 |
16 | | -inparam = proc_class.get_method("Create").inparam.spawn_instance() |
17 | | -inparam["CommandLine"] = r"c:\windows\system32\notepad.exe trolol.exe" |
18 | | -# Create a test checking return value |
19 | | -xx = wmireq.exec_method(proc_class, "Create", inparam) |
20 | | -print(xx) |
21 | | -print(xx.as_dict()) |
| 15 | +inparam = inparam_cls() |
| 16 | +print("InParam instance is <{0}>".format(inparam)) |
| 17 | +print("Setting <CommandLine>") |
| 18 | +inparam["CommandLine"] = r"c:\windows\system32\notepad.exe" |
22 | 19 |
|
23 | | -## Method2 |
| 20 | +print("Executing method") |
| 21 | +# This API may change for something that better wraps cls/object/Parameters handling |
| 22 | +outparam = wmispace.exec_method(proc_class, "Create", inparam) |
24 | 23 |
|
25 | | -# class MyResult(gdef.IWbemCallResult): |
26 | | - # def result(self): |
27 | | - # res = type(proc_class)() |
28 | | - # self.GetResultObject(gdef.WBEM_INFINITE, res) |
29 | | - # return res |
| 24 | +print("OutParams is {0}".format(outparam)) |
| 25 | +print("Out params values are: {0}".format(outparam.properties)) |
| 26 | +target = windows.WinProcess(pid=int(outparam["ProcessId"])) |
| 27 | +print("Created process is {0}".format(target)) |
| 28 | +print("Waiting 1s") |
| 29 | +time.sleep(1) |
| 30 | +print("Killing the process") |
| 31 | +target.exit(0) |
30 | 32 |
|
31 | 33 |
|
32 | | -# proc = proc_class.spawn() |
33 | | -# cmdline = bstr_variant(r"c:\windows\system32\notepad.exe") |
34 | | -# proc.put_variant("CommandLine", cmdline) |
35 | | -# res = wmireq.put_instance(proc) |
36 | 34 |
|
0 commit comments