@@ -82,8 +82,18 @@ public WindowMonitor()
82
82
Log . Information ( "Foreground = SoundSwitch, don't save." ) ;
83
83
return ;
84
84
}
85
+
85
86
var process = Process . GetProcessById ( ( int ) processId ) ;
86
- var processName = process . MainModule ? . FileName ?? "N/A" ;
87
+ var processName = "N/A" ;
88
+ try
89
+ {
90
+ processName = process . MainModule ? . FileName ?? "N/A" ;
91
+ }
92
+ catch ( Exception )
93
+ {
94
+ // ignored
95
+ }
96
+
87
97
ForegroundChanged ? . Invoke ( this , new Event ( processId , processName , windowText , windowClass , hwnd ) ) ;
88
98
}
89
99
catch ( Exception )
@@ -118,7 +128,7 @@ public static (uint ProcessId, string WindowText, string WindowClass) ProcessWin
118
128
var wndClass = "" ;
119
129
try
120
130
{
121
- wndText = User32 . GetWindowText ( hwnd ) ;
131
+ User32 . NativeMethods . GetWindowThreadProcessId ( hwnd , out processId ) ;
122
132
}
123
133
catch ( Exception )
124
134
{
@@ -127,7 +137,7 @@ public static (uint ProcessId, string WindowText, string WindowClass) ProcessWin
127
137
128
138
try
129
139
{
130
- wndClass = User32 . GetWindowClass ( hwnd ) ;
140
+ wndText = User32 . GetWindowText ( hwnd ) ;
131
141
}
132
142
catch ( Exception )
133
143
{
@@ -136,14 +146,13 @@ public static (uint ProcessId, string WindowText, string WindowClass) ProcessWin
136
146
137
147
try
138
148
{
139
- User32 . NativeMethods . GetWindowThreadProcessId ( hwnd , out processId ) ;
149
+ wndClass = User32 . GetWindowClass ( hwnd ) ;
140
150
}
141
151
catch ( Exception )
142
152
{
143
153
// ignored
144
154
}
145
155
146
-
147
156
return ( processId , wndText , wndClass ) ;
148
157
}
149
158
}
0 commit comments