Reputation: 4110
In InnoSetup, there is a function "FindWindowByClassName" to know if a program is allready in use. In my case, I want to know if my wpf application is in use before runing the InnoSetup update, but I don't know how to know my wpf application class name. When I use Spy++ with my application, the class name is:
HwndWrapper[MuProgram.exe;;1120a18a-2e29-4e19-8653-939c99b505c2]
As you can see, there is a GUID in my class name, and this GUID changes at every launch. But in any case, when I run InnoSetup (in debug mode), the FindWindowByClassName function returns 0 when I use this class name.
FindWindowByClassName('HwndWrapper[MyProgram.exe;;1120a18a-2e29-4e19-8653-939c99b505c2]'); // returns 0
I've tried to use only "MyProgram.exe" but the function allways returns 0 (the application "MyProgram.exe" is launched during the tests). Have you any idea to fixe this problem? May I set the class name in my wpf code?
Upvotes: 3
Views: 1977
Reputation: 19032
In the Inno Script source code, you can see that FindWindowByClassName calls the Windows FindWindow function. As you already noticed, the WPF form changes the class name on each invocation. According to this StackOverflow answer, the random GUID makes it impossible to use FindWindow on WPF classes.
Unfortunately there is no RegEx or wildcard capability when searching for windows. I guess you could write your own by searching through all windows using something like GetWindowEx and looping through all children of the desktop window. This StackOverflow answer suggests using UI Automation rather than old API calls with WPF.
The mutex alternative suggested by @TLama is great if you're writing the app that you will search for. If you're trying to find and close another vendor's app, it's not so easy. Also CheckForMutexes does not return a window handle, so it's not straightforward how to close the window.
Upvotes: 1