-1

I need to be able to start Firefox from my app and then get a handle to the Firefox browser window in order to move it, resize it, etc. In the app, Firefox is started using CreateProcessAsUser().

The problem is, Firefox uses a launcher process by default:

In Windows builds of Firefox, the Launcher Process is the initial process that is started when the user runs Firefox. Its sole purpose is to create the browser process in a suspended state, configure the browser process, resume the browser process, and then hand off GUI foreground to the browser. Once the launcher process has completed this objective, it exits.

Because Firefox uses a launcher process to launch the browser window, the PID returned by CreateProcessAsUser() is not the PID of the browser window. So I can't use EnumWindows() and match PIDs. I know that the Window class type for the browser window is MozillaWindowClass, but it's possible there will be multiple windows of this type in my context.

How can I get a handle on the browser window in order to manipulate it programmatically?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • You could use a specific url to launch Firefox to discriminate all opened windows and then use UI Automation to get it. Something like this https://stackoverflow.com/a/5318791/403671 – Simon Mourier Aug 19 '22 at 07:29

1 Answers1

0

Firefox does not provide an API to get what you want.

You have the PID of the launcher from CreateProcessAsUser(). Until you close the HANDLE that CreateProcessAsUser() returns, that PID will stay valid and not be recycled. Which means, you can then enumerate running processes until you find one whose parent PID is the launcher's PID 1. And then, you can enumerate windows looking for one(s) that belong to that child PID.

1: Alternatively, when you create the launcher process, assign it to a Job Object that you can then monitor for a JOB_OBJECT_MSG_NEW_PROCESS message, indicating a new child process has been added to the job. Then you will have the PID of the new process.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Unfortunately I've tried that and there are no processes whose parent ID match the ID of the launcher process. If I use spy++ to find the PID of the MozillaWindowClass window... its PID doesn't even get enumerated. – alalalahehehehhahahaha Aug 18 '22 at 21:25
  • Ahh I see your edit about JobObjects. Thank you, I've also tried that... was hoping there was an easier way that method will be difficult to add into the existing codebase... I guess that is my only option. damn you firefox – alalalahehehehhahahaha Aug 18 '22 at 21:29
  • The real question is, why are you trying to manipulate an external Firefox window in the first place? This sounds like a possible [XY Problem](https://meta.stackexchange.com/questions/66377/). Perhaps you would be better off [using an embedded web browser inside your own UI](https://stackoverflow.com/questions/2611906/) instead. – Remy Lebeau Aug 18 '22 at 21:30