C++ CreateProcess SW_HIDE firefox, chrome, safari
I'm writing a web testing framework for windows that uses multiple browsers to test web apps. I want to launch the browsers hidden but it's not working.
I'm using CreateProcess to start the browsers with the SW_HIDE flag
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
CreateProcess(NULL, const_cast<char*> (cmd.c_str()) , NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);开发者_Go百科
It works fine for internet explorer and other apps, but fails with firefox and other browsers. I'm aware that a process is able to choose whether or not to follow the passed flags. Is there any way i can force it to start hidden?
I can probably wait until the window shows up and then hide it by getting a handle to the open window. But I'm looking for cleaner solution.
The problem is that the value of wShowWindow
is used only when the function ShowWindow
is called with the command SW_SHOWDEFAULT
, additionally as FastAl said the browser create some additional process to do additional tasks.
Based on you idea and the answer of FastAl, you can do this to hide the windows created by the process that you created:
1.- Create a List of Process with: CreateToolhelp32Snapshot
, Process32First
, Process32Next
and CloseHandle
; taking only the process that has been created by the process that you created. Here is a demostration of how to use this functions, it works with any windows version except Windows NT.
2.- Use EnumWindows
to Enumerate the windows on the screen and with GetWindowThreadProcessId
check if the windows was created by any of the process recovered in the previous step.
3.- If the windows was created by one of the process defined in the step 1; hide the windows using the ShowWindow function.
You can monitor the process created by you and check if new process/windows are created.
You could launch the processes from a service (running as the NETWORK SERVICE user for instance). This will prevent them from showing any UI. It will however prevent you from being able to look at the ui at all, even if you want to at a later time.
Another option would be to do something with Window Stations and Desktops, take a look at the section about Window Stations and Desktops on MSDN. An idea would be to use CreateDesktop and then specifying this desktop as the lpDesktop parameter in the STARTUPINFO structure given to CreateProcess. If you at a later time want to display the UI, use SwitchDesktop.
try si.wShowWindow = 0; funny this worked for me, not SW_HIDE which should carry the same value though.
You can launch the application and then send a WM_SIZE message to its main window.
http://msdn.microsoft.com/en-us/library/ms632646(v=vs.85).aspx
According to MSDN, looks like you need to pass this flag in dwFlags.
STARTF_USESHOWWINDOW 0x00000001 - The wShowWindow member contains additional information.
精彩评论