Click here to Skip to main content
16,004,974 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralPowerPoint and OLE Server and MFC problem Pin
gicio20-Aug-03 0:24
gicio20-Aug-03 0:24 
Generaldefine "draw" area in CDC Pin
AnTri20-Aug-03 0:04
AnTri20-Aug-03 0:04 
GeneralRe: define "draw" area in CDC Pin
MCING20-Aug-03 0:20
MCING20-Aug-03 0:20 
GeneralRe: define "draw" area in CDC Pin
Steve S20-Aug-03 0:44
Steve S20-Aug-03 0:44 
Generalconvert Integer to String Pin
wow999919-Aug-03 23:46
wow999919-Aug-03 23:46 
GeneralRe: convert Integer to String Pin
MCING19-Aug-03 23:51
MCING19-Aug-03 23:51 
GeneralRe: convert Integer to String Pin
Ted Ferenc19-Aug-03 23:57
Ted Ferenc19-Aug-03 23:57 
GeneralWhere is the glitch in this code (minimizing and restoring another app) Pin
Kayembi19-Aug-03 23:37
Kayembi19-Aug-03 23:37 
Hi,

I am writing a small program that launches a third party application (a game). It extracts a few temporary, sensitive files to the hard disk, and makes sure that the user can't access them. This is the method it uses, and after trying most other avenues, I am pretty happy with it:

1) My program starts and extracts the files needed for the game to a temporary folder (and tells the system to delete these files if the system is rebooted)
2) My program then launches the game using ShellExecuteEx()
3) My program then finds the HWND handle to the game using FindWindow()
4) My program then monitors the game, checking to make sure it is the foreground window.
5) If the game (or one of its children - eg. an error box) is no longer the foreground window, my program deletes any sensitive files.
6) When the game is restored to the foreground window, my program re-extracts the necessary files. To ensure that the game doesn't try to access them before they are back on the disk, it takes these steps:
a) When the game is restored by the user, my program minimizes it again immediately.
b) My program will then show a dialog box saying "Restoring data, please wait..." with a progress bar.
c) Once the data is reloaded, my program restores the game window to the main window.

(If this sounds like an odd process, believe me that it is the best solution for what I am doing. Not necessarily the most ideal, but the best given the time I have and the little skill I have. Smile | :) )

So far, so good. However, because I am a relative novice, my code is far from perfect, and I am hoping someone here can show me a better way of writing the code that I am posting below, that deals with checking whether the game is in the foreground and with minimizing/restoring it accordingly. At the moment I am using a variable, int not_foreground, with GetForeGroundWindow() to check whether the game is the active window or not. However, although this works mostly, I have noticed that it doesn't work 100% of the time, which must be due to something seriously wobbly in my code (and is obviously not acceptable). Very occasionally (about 1 in 10) the game window isn't minimized by my program when the user tries to return to it, though I can't understand why. If someone could give me some pointers on how to improve this code, I would be really grateful.

Also, as I don't have access to my WinXP system for a few days and can only test this on WinMe, is there any reason the method I use for minimizing and restoring the game window wouldn't work on XP/2000 systems?

The code:

[code]
//ThirdPartyApp_hWnd = the application (game) that my program launched with ShellExecuteEx:
HWND ThirdPartyApp_hWnd = FindWindow(NULL,"Game window");

...

int not_foreground = 0;

//in main message loop:

//while the game window is active, test that it remains so:
if(not_foreground == 0)
{

if(ThirdPartyApp_hWnd != GetForegroundWindow())
{
//if the game window loses focus, make sure it's not just to a child window:
HWND checkwnd = GetForegroundWindow();
if(GetParent(checkwnd)!=ThirdPartyApp_hWnd)
{
/* My file deletion code would go here */

not_foreground = 1; //no longer foreground window
}
}
}

//while the game window is not active, check for it being restored:
if(not_foreground == 1) //not foreground
{
if(ThirdPartyApp_hWnd == GetForegroundWindow()) //game is back in foreground
{
//Minimize the program again:
SendMessage(ThirdPartyApp_hWnd,WM_SYSCOMMAND,SC_MINIMIZE,0);

/* My file restore code would go here */
Sleep(5000); //For testing purposes, just wait 5 secs

//And now restore the game as the active window:
SetWindowPos(ThirdPartyApp_hWnd,HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
ShowWindow(ThirdPartyApp_hWnd,SW_RESTORE);

not_foreground = 0; //we are foreground again, so don't do this again
}
}

//message processing:
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
//If user destroys the launcher, close the game too:
SendMessage(ThirdPartyApp_hWnd,WM_CLOSE,0,0);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
[/code]

As you can see, I'm using the straight WinAPI SDK with _no_ MFC. Any suggestions for improvements or tips as to why my code might be failing on occasion would be greatly appreciated.

May thanks,
KB
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Dangleberry20-Aug-03 2:19
sussDangleberry20-Aug-03 2:19 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Kayembi20-Aug-03 3:33
Kayembi20-Aug-03 3:33 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Ryan Binns20-Aug-03 5:25
Ryan Binns20-Aug-03 5:25 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Dangleberry20-Aug-03 6:00
sussDangleberry20-Aug-03 6:00 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Ryan Binns20-Aug-03 14:55
Ryan Binns20-Aug-03 14:55 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Dangleberry21-Aug-03 0:47
sussDangleberry21-Aug-03 0:47 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Kayembi20-Aug-03 7:13
Kayembi20-Aug-03 7:13 
GeneralRe: Where is the glitch in this code (minimizing and restoring another app) Pin
Ryan Binns20-Aug-03 14:59
Ryan Binns20-Aug-03 14:59 
Generalgoing through each enumeration of an enum Pin
Jerome Conus19-Aug-03 23:29
Jerome Conus19-Aug-03 23:29 
GeneralRe: going through each enumeration of an enum Pin
Neville Franks19-Aug-03 23:55
Neville Franks19-Aug-03 23:55 
GeneralRe: going through each enumeration of an enum Pin
David Crow20-Aug-03 3:05
David Crow20-Aug-03 3:05 
GeneralFastest bitmap display Pin
MCING19-Aug-03 23:25
MCING19-Aug-03 23:25 
GeneralMMX optimization Pin
sharlila19-Aug-03 23:14
sharlila19-Aug-03 23:14 
GeneralSaving a dropped file into a specified folder Pin
YolandaSteyn19-Aug-03 22:53
YolandaSteyn19-Aug-03 22:53 
QuestionHow to convert TCHAR[] to CString? Pin
Exceter19-Aug-03 22:30
Exceter19-Aug-03 22:30 
AnswerRe: How to convert TCHAR[] to CString? Pin
Dominik Reichl19-Aug-03 22:34
Dominik Reichl19-Aug-03 22:34 
GeneralRe: How to convert TCHAR[] to CString? Pin
Exceter19-Aug-03 22:47
Exceter19-Aug-03 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.