16
16
import sys
17
17
from types import ModuleType
18
18
from typing import (
19
- Any ,
20
19
Dict ,
21
20
List ,
22
21
Optional ,
23
22
Tuple ,
24
23
)
25
24
25
+ import winBindings .kernel32
26
26
import winVersion
27
27
import importlib
28
28
import importlib .util
32
32
import baseObject
33
33
from logHandler import log
34
34
import NVDAHelper
35
- import NVDAState
36
35
import winKernel
37
36
import config
38
37
import NVDAObjects # Catches errors before loading default appModule
50
49
)
51
50
from comInterfaces import UIAutomationClient as UIA
52
51
import winBindings .rpcrt4
52
+ from utils import _deprecate
53
+
54
+
55
+ __getattr__ = _deprecate .handleDeprecations (
56
+ _deprecate .MovedSymbol (
57
+ "processEntry32W" ,
58
+ "winBindings.kernel32" ,
59
+ ),
60
+ _deprecate .MovedSymbol (
61
+ "_PROCESS_MACHINE_INFORMATION" ,
62
+ "winBindings.kernel32" ,
63
+ ),
64
+ _deprecate .MovedSymbol (
65
+ "NVDAProcessID" ,
66
+ "globalVars" ,
67
+ "appPid" ,
68
+ ),
69
+ )
53
70
54
71
55
72
# Dictionary of processID:appModule pairs used to hold the currently running modules
69
86
"""
70
87
71
88
72
- class processEntry32W (ctypes .Structure ):
73
- """See https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32w"""
74
-
75
- _fields_ = [
76
- ("dwSize" , ctypes .wintypes .DWORD ),
77
- ("cntUsage" , ctypes .wintypes .DWORD ),
78
- ("th32ProcessID" , ctypes .wintypes .DWORD ),
79
- ("th32DefaultHeapID" , ctypes .wintypes .PULONG ),
80
- ("th32ModuleID" , ctypes .wintypes .DWORD ),
81
- ("cntThreads" , ctypes .wintypes .DWORD ),
82
- ("th32ParentProcessID" , ctypes .wintypes .DWORD ),
83
- ("pcPriClassBase" , ctypes .c_long ),
84
- ("dwFlags" , ctypes .wintypes .DWORD ),
85
- ("szExeFile" , ctypes .c_wchar * 260 ),
86
- ]
87
-
88
-
89
- class _PROCESS_MACHINE_INFORMATION (ctypes .Structure ):
90
- _fields_ = [
91
- ("ProcessMachine" , ctypes .wintypes .USHORT ),
92
- ("Res0" , ctypes .wintypes .USHORT ),
93
- ("MachineAttributes" , ctypes .wintypes .DWORD ),
94
- ]
95
-
96
-
97
- def __getattr__ (attrName : str ) -> Any :
98
- """Module level `__getattr__` used to preserve backward compatibility.
99
- The module level variable `NVDAProcessID` is deprecated
100
- and usages should be replaced with `globalVars.appPid`.
101
- We cannot simply assign the value from `globalVars` to the old attribute
102
- since add-ons are initialized before `appModuleHandler`
103
- and when `appModuleHandler` was not yet initialized the variable was set to `None`.
104
- """
105
- if attrName == "NVDAProcessID" and NVDAState ._allowDeprecatedAPI ():
106
- log .warning ("appModuleHandler.NVDAProcessID is deprecated, use globalVars.appPid instead." )
107
- if initialize ._alreadyInitialized :
108
- return globalVars .appPid
109
- return None
110
- raise AttributeError (f"module { repr (__name__ )} has no attribute { repr (attrName )} " )
111
-
112
-
113
89
def registerExecutableWithAppModule (executableName : str , appModName : str ) -> None :
114
90
"""Registers appModule to be used for a given executable."""
115
91
_executableNamesToAppModsAddons [executableName ] = appModName
@@ -181,17 +157,17 @@ def getAppNameFromProcessID(processID: int, includeExt: bool = False) -> str:
181
157
"""
182
158
if processID == globalVars .appPid :
183
159
return "nvda.exe" if includeExt else "nvda"
184
- FSnapshotHandle = winKernel .kernel32 .CreateToolhelp32Snapshot (2 , 0 )
185
- FProcessEntry32 = processEntry32W ()
186
- FProcessEntry32 .dwSize = ctypes .sizeof (processEntry32W )
187
- ContinueLoop = winKernel .kernel32 .Process32FirstW (FSnapshotHandle , ctypes .byref (FProcessEntry32 ))
160
+ FSnapshotHandle = winBindings .kernel32 .CreateToolhelp32Snapshot (2 , 0 )
161
+ FProcessEntry32 = winBindings . kernel32 . PROCESSENTRY32W ()
162
+ FProcessEntry32 .dwSize = ctypes .sizeof (FProcessEntry32 )
163
+ ContinueLoop = winBindings .kernel32 .Process32First (FSnapshotHandle , ctypes .byref (FProcessEntry32 ))
188
164
appName = str ()
189
165
while ContinueLoop :
190
166
if FProcessEntry32 .th32ProcessID == processID :
191
167
appName = FProcessEntry32 .szExeFile
192
168
break
193
- ContinueLoop = winKernel .kernel32 .Process32NextW (FSnapshotHandle , ctypes .byref (FProcessEntry32 ))
194
- winKernel .kernel32 .CloseHandle (FSnapshotHandle )
169
+ ContinueLoop = winBindings .kernel32 .Process32Next (FSnapshotHandle , ctypes .byref (FProcessEntry32 ))
170
+ winBindings .kernel32 .CloseHandle (FSnapshotHandle )
195
171
if not includeExt :
196
172
appName = os .path .splitext (appName )[0 ].lower ()
197
173
if not appName :
@@ -548,7 +524,7 @@ def _get_processExecutablePath(self) -> str:
548
524
# Create the buffer to get the executable name
549
525
exeFileName = ctypes .create_unicode_buffer (ctypes .wintypes .MAX_PATH )
550
526
length = ctypes .wintypes .DWORD (ctypes .wintypes .MAX_PATH )
551
- if not ctypes . windll . Kernel32 . QueryFullProcessImageNameW (
527
+ if not winBindings . kernel32 . QueryFullProcessImageName (
552
528
self .processHandle ,
553
529
0 ,
554
530
exeFileName ,
@@ -571,10 +547,10 @@ def _getImmersivePackageInfo(self):
571
547
# Some apps such as File Explorer says it is an immersive process but error 15700 is shown.
572
548
# Others such as Store version of Office are not truly hosted apps but are distributed via Store.
573
549
length = ctypes .c_uint ()
574
- ctypes . windll .kernel32 .GetPackageFullName (self .processHandle , ctypes .byref (length ), None )
550
+ winBindings .kernel32 .GetPackageFullName (self .processHandle , ctypes .byref (length ), None )
575
551
packageFullName = ctypes .create_unicode_buffer (length .value )
576
552
if (
577
- ctypes . windll .kernel32 .GetPackageFullName (
553
+ winBindings .kernel32 .GetPackageFullName (
578
554
self .processHandle ,
579
555
ctypes .byref (length ),
580
556
packageFullName ,
@@ -675,7 +651,7 @@ def _get_appPath(self):
675
651
"""
676
652
size = ctypes .wintypes .DWORD (ctypes .wintypes .MAX_PATH )
677
653
path = ctypes .create_unicode_buffer (size .value )
678
- winKernel .kernel32 .QueryFullProcessImageNameW (self .processHandle , 0 , path , ctypes .byref (size ))
654
+ winBindings .kernel32 .QueryFullProcessImageName (self .processHandle , 0 , path , ctypes .byref (size ))
679
655
self .appPath = path .value if path else None
680
656
return self .appPath
681
657
@@ -691,7 +667,7 @@ def _get_is64BitProcess(self) -> bool:
691
667
# We need IsWow64Process2 to detect WOW64 on ARM64.
692
668
processMachine = ctypes .wintypes .USHORT ()
693
669
if (
694
- ctypes . windll .kernel32 .IsWow64Process2 (
670
+ winBindings .kernel32 .IsWow64Process2 (
695
671
self .processHandle ,
696
672
ctypes .byref (processMachine ),
697
673
None ,
@@ -706,7 +682,7 @@ def _get_is64BitProcess(self) -> bool:
706
682
# IsWow64Process2 is only supported on Windows 10 version 1511 and later.
707
683
# Fall back to IsWow64Process.
708
684
res = ctypes .wintypes .BOOL ()
709
- if ctypes . windll .kernel32 .IsWow64Process (self .processHandle , ctypes .byref (res )) == 0 :
685
+ if winBindings .kernel32 .IsWow64Process (self .processHandle , ctypes .byref (res )) == 0 :
710
686
self .is64BitProcess = False
711
687
return False
712
688
self .is64BitProcess = not res
@@ -764,15 +740,15 @@ def _get_appArchitecture(self) -> str:
764
740
}
765
741
# #14403: GetProcessInformation can be called from Windows 11 and later to obtain process machine.
766
742
if winVersion .getWinVer () >= winVersion .WIN11 :
767
- processMachineInfo = _PROCESS_MACHINE_INFORMATION ()
743
+ processMachineInfo = winBindings . kernel32 . _PROCESS_MACHINE_INFORMATION ()
768
744
# Constant comes from PROCESS_INFORMATION_CLASS enumeration.
769
745
ProcessMachineTypeInfo = 9
770
746
# Sometimes getProcessInformation may fail, so say "unknown".
771
- if not ctypes . windll .kernel32 .GetProcessInformation (
747
+ if not winBindings .kernel32 .GetProcessInformation (
772
748
self .processHandle ,
773
749
ProcessMachineTypeInfo ,
774
750
ctypes .byref (processMachineInfo ),
775
- ctypes .sizeof (_PROCESS_MACHINE_INFORMATION ),
751
+ ctypes .sizeof (processMachineInfo ),
776
752
):
777
753
self .appArchitecture = "unknown"
778
754
else :
@@ -784,7 +760,7 @@ def _get_appArchitecture(self) -> str:
784
760
try :
785
761
# If a native app is running (such as x64 app on x64 machines), app architecture value is not set.
786
762
processMachine = ctypes .wintypes .USHORT ()
787
- ctypes . windll .kernel32 .IsWow64Process2 (self .processHandle , ctypes .byref (processMachine ), None )
763
+ winBindings .kernel32 .IsWow64Process2 (self .processHandle , ctypes .byref (processMachine ), None )
788
764
if not processMachine .value :
789
765
self .appArchitecture = winVersion .getWinVer ().processorArchitecture
790
766
else :
0 commit comments