Question : How to set the position and size for firefox using CreateProcess and MoveWindow (Visual Studio 2008, MFC)

Hello,

I want to start firefox and set the size of the window to a specific value. Say 800x600 for tesing a homepage.

I wrote a small sample that works find with notepad.exe but not firefox.exe.

I used FindWindow the compare processID and it does not match.

Does anyone have an idea?

Stefan
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
HWND m_Window;
 
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
	DWORD		processID;
	::GetWindowThreadProcessId(hwnd, &processID);
 
	if (processID == lParam)
	{
		m_Window = hwnd;
		return FALSE;
	}
 
	return TRUE;
}
 
void CRunResDlg::OnBnClickedButtonfirefox()
{
	PROCESS_INFORMATION sProcessInfo;
	STARTUPINFO                  sStartupInfo;
 
	ZeroMemory(&sStartupInfo,sizeof(STARTUPINFO));
	ZeroMemory(&sProcessInfo,sizeof(PROCESS_INFORMATION));
 
	sStartupInfo.cb = sizeof( STARTUPINFO);
 
//	char szCommandLine[260] = {"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"};
	char szCommandLine[260] = {"C:\\windows\\notepad.exe"};
 
	CreateProcess(NULL, szCommandLine, 0, 0, 0, CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS, 0, 0, &sStartupInfo, &sProcessInfo);
 
	DWORD dw = WaitForInputIdle((HANDLE)sProcessInfo.hProcess, 10000);
 
	Sleep(2000);
 
	m_Window = 0;
	EnumWindows(EnumWindowsProc, sProcessInfo.dwProcessId);
 
	if (m_Window != 0)
	{
		::MoveWindow(m_Window, 100, 100, 800, 600, TRUE);
	}
}

Answer : How to set the position and size for firefox using CreateProcess and MoveWindow (Visual Studio 2008, MFC)

Found the same thing. Many windows, but the only process. :-)
I even port it to Delphi to test.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
program SizeFFWindow1;
{$APPTYPE Console}
 
uses
	SysUtils,
	Windows;
 
var m_Window: HWND;
 
function EnumWindowsProc(Wnd:HWND; lParam:LPARAM): boolean;
var processID: DWORD;
begin
	Result := true;
	processID := 1;
	GetWindowThreadProcessId(Wnd, @processID);
 
	writeln('PID = '+Inttohex(processID,8)+'  Wnd = '+Inttohex(Wnd,8)+
	'   Look for '+Inttohex(lParam,8));
 
	if processId = lParam then
		begin
			m_Window := Wnd;
			Result := false;
			writeln('------ Matched -----');
		end;
end;
 
var
				sProcessInfo: PROCESS_INFORMATION;
				sStartupInfo: STARTUPINFO;
				szCommandLine: PChar;
				dw: DWORD;
begin
			//start ff
				ZeroMemory(@sStartupInfo,sizeof(STARTUPINFO));
				ZeroMemory(@sProcessInfo,sizeof(PROCESS_INFORMATION));
 
				sStartupInfo.cb := sizeof( STARTUPINFO);
 
				szCommandLine := PChar('"H:\Program Files\Mozilla Firefox\firefox.exe"');
				//char szCommandLine[260] = {"C:\\windows\\notepad.exe"};
 
				CreateProcess(nil, szCommandLine, nil, nil, false, {CREATE_NO_WINDOW or}
					NORMAL_PRIORITY_CLASS, 0, 0, sStartupInfo, sProcessInfo);
 
				dw := WaitForInputIdle(sProcessInfo.hProcess, 10000);
 
				Sleep(2000);
 
				m_Window := 0;
				EnumWindows(@EnumWindowsProc, sProcessInfo.dwProcessId);
 
				if (m_Window <> 0) then
					MoveWindow(m_Window, 100, 100, 800, 600, TRUE);
 
				writeln('Press Enter');
				Readln;
 
end.
Random Solutions  
 
programming4us programming4us