You can't. Simple as that. If you want to automate an application, use [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32).
– IInspectableJul 13 '20 at 16:57
This is another case of ignoring the difference between *"correct code"* and *"code, that has not yet failed"*. Your code is in the latter category. [Here's why](https://devblogs.microsoft.com/oldnewthing/20050530-11/?p=35513).
– IInspectableJul 16 '20 at 10:34