Set Window To Foreground

Hi there!

This is my first time to ask over the internet. I was just reading forums regarding my queries and this time, i want to try it myself. So hopefully, you guys would help me.

I'm having a problem setting the UI to foreground. An MSI was being called by a bootstrapper but everytime i call the installer, it's running on the background. I am new to C++ so please bear with me. Here's a function that i got from the internet:

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
BOOL RunProgram(CString commandLine, BOOL wait, DWORD& exitCode)
{
    BOOL ret;
    STARTUPINFO startupInfo;
    PROCESS_INFORMATION processInfo;
    CString strHelpPath;
	
    memset(&startupInfo, 0, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);
    memset(&processInfo, 0, sizeof(processInfo));
	
   ret = ::CreateProcess(
	   NULL,
	   (LPTSTR) (LPCTSTR) commandLine,
	   NULL,
	   NULL,
	   FALSE,
	   0,
	   NULL,
	   NULL,
	   &startupInfo,
	   &processInfo);
		
   if (ret)
   {
   	if (wait)
   	{
		while (::WaitForSingleObject(processInfo.hProcess, 100) == WAIT_TIMEOUT)
			ProcessMessages();
			::GetExitCodeProcess(processInfo.hProcess, &exitCode);
	}
	::CloseHandle(processInfo.hProcess);
	::CloseHandle(processInfo.hThread);
   }

    return ret;
}


//The part where the RunProgram function is being called
RunProgram( "msiexec /i \"" + [MSI Path] + "\"", TRUE, ret);


When i set the second param to false, the installer's UI will run on the background but when i set it to true, the UI will run in foreground but the installer will run twice. What am i missing here?
Last edited on
What am i missing here?

I don't think you've posted the relevant code.

Your function RunProgram only launches the program once; so the question is, why is it being re-invoked.

As passing TRUE to RunProgram cause ProcessMessages to be called, it looks like something in your message processing code is deciding it's a good idea to call RunProgram again.

By the way, if the purpose of ProcessMessages is to process windows messages, rather than some other sort of message, you should really be using MsgWaitForMultipleObjects instead of WaitForSingleObject with a timeout.

Andy
Last edited on
Hi Andy, thanks for your reply.

Im sorry, i forgot to add the ProcessMessages function. Here it is:

1
2
3
4
5
6
7
8
void ProcessMessages()
{
	CWinApp* pApp = AfxGetApp();
	MSG msg;
	
        while ( PeekMessage ( &msg, NULL, 0, 0, PM_NOREMOVE ))
      pApp->PumpMessage();
}


I haven't mentioned that aside from installing the said installer, prior to this, i need also to run 2 driver installers silently so i need to wait for this drivers to install first before i call the main dialog.

Btw, i just made the main dialog run once in foreground just now by calling

::WaitForSingleObject(processInfo.hProcess, 100);
only and not calling ProcessMessage function. Here's the updated code that solved my problem:

1
2
3
4
5
6
7
8
9
10
11
if (notThirdMSI)
{
	while (::WaitForSingleObject(processInfo.hProcess, 100) == WAIT_TIMEOUT)
		ProcessMessages();

		::GetExitCodeProcess(processInfo.hProcess, &exitCode);
}
else
{
	::WaitForSingleObject(processInfo.hProcess, 100); 
}



Please help explain what happened. Thanks!
Last edited on
Please help explain what happened.

You need to run your launcher app under a debugger and get it to break when the installer is run for the second timer and see what's triggering it. Or add logging to provide the same info.

It looks like you app is being told to launch the app twice, which suggests it's processing a message twice, or an unexpected messages is arriving, or something.

Andy

PS Please also see the following article on how to tag your code, Thanks.

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Hi Andy,

That's my real problem. I cant make the debugger work. Im using vc++ for IDE and debug in there seems to malfunction every time i use it.

Btw, thanks for the article.
Topic archived. No new replies allowed.