I'm trying to get certain window handle. I was searching for solution for many hours and I understand that my question sounds similar to this one: FindWindow() doesn't find my window [C++] But that discussion didn't help.
I was trying to use both FindWindow() and FindWindowEx() like these two:
IntPtr SysPropWndHandler = FindWindow("#32770", "Параметри продуктивності");
IntPtr SysPropWndHandler = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Параметри продуктивності");
Weird part is that when I run the program, it starts new process for certain system settings program from system32 folder and it can't find it's handle during same launch time (if that's correct to say so). I tried to pause it to give it time to create window and assign handle, but that doesn't help. But! If that system program is launched first and then I run my program - it finds it's handle right away. Two ways for that "external launch":
- I run system program manually before launching my program
- I run my program, which launches that system program, then I close my program, system program doesn't close then. After that I run my program again.
But what I'm actually trying to make my program do is this:
- launch system program (some productivity settings)
- hide window
- change some settings via WinApi (kind of checkbox clicking emulation)
- click ok
- close it
Since my code works, at least in some conditions, looks like it has nothing to do with encoding, which was mansioned in that similar question. Otherwise it wouldn't work at all.
I was trying to launch it hidden, but it didn't work. I tried the same code for notepad for debugging it - it works.
string prog_path = @"C:\Windows\System32\SystemPropertiesPerformance.exe";
Process process = new Process();
process.StartInfo.FileName = prog_path;
process.StartInfo.CreateNoWindow = true; // no need for that, but I tried with it and without it just in case it works
process.StartInfo.UseShellExecute = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
According to Microsoft documentation you need to set UseShellExecute
to true
in order to use StartInfo.WindowStyle = ProcessWindowStyle.Hidden
(which I did), but program still can choose to ignore that. Looks like that's exactly what's happening there.
But I tried to get exect window handle via Spy++ and try to hide it - it works, so I can manipulate it from there and do my thing. The only problem is to find it's handle...
How do I find that handle in this case?
P.S.
- Windows 10 x64 Pro Ukrainian (for other languages that window title in the code won't work)
- .NET Framework 4.7.2
- Code is inside .NET Framework Class Library, which is launched from C# Console Application.