Link to home
Start Free TrialLog in
Avatar of hpchong7
hpchong7

asked on

On WinExec

Dear all,

  I've a minor question want to ask about winexec:

Inside my mfc application, I will use winexec to open a few wordpad, depending on what file the user click on my UI.After the user close my mfc application, I want the wordpad which opened by my mfc will also be closed.How can I do it.I know I can use the following:

CWnd *pWord = CWnd::FindWindow(NULL, _T("WordPad title"));
CString szTitle;
pWord->GetWindowText(szTitle);
if(pWord != NULL)
{          
   pWord->SendMessage(WM_CLOSE);
}
     
The problem is that using FindWindow I must give the exact title of all those wordpad which is extremely difficult in my situation.Is there any method to get around that I can still close all the wordpads?Thanks!
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi hpchong7,

if you would use CreateProcess instead of winexec you'd recieve the
other applications process and thread handles and IDs.

You could then either find out if other application is still running via
GetExitCodeProcess()
and send a WM_QUIT message via
PostThreadMessage()

hope that helps,

ZOPPO
Avatar of hpchong7
hpchong7

ASKER

Dear Zoppo,
  Thanks!!!!BTW, as I did not use createprocess before, may you tell me how can I open wordpad using createprocess and how to get the corresponding handle?Thanks!

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use EnumWindows, which returns the HWND of all top-level windows.  With each window, you can use GetWindowText to get the caption, then you can use

   CString::Find("WordPad");

or other technique to examine the window titles to see if it is one that you you want to close.

-- Dan
I will try it tomorrow when back to work.Thank in advance!
Netminder, many of the questions were deleted, I copied you on a Community Support link regarding this.

Moondancer
Community Support Moderator @ Experts Exchange
Dear Zoppo,
   It's so strange that it does not work until I changed a little bit:

char pszExePath[512];
char pszOpenFilePath[512];

strcpy(pszExePath,"D:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
strcpy(pszOpenFilePath," C:\\temp\\test.doc");
strcat(pszExePath,pszOpenFilePath);
BOOL bRet = CreateProcess(NULL,pszExePath,.....)

Then it worked.Did you know why?Thanks!

BTW, once the wordpad popped up, I can't kill it by the following:

CloseHandle(pi.dwProcessId)

May you help me to solve?Thanks!
>Then it worked.Did you know why?
Maybe this problem comes from 'space' in long directory name '...\Program Files\...',
could be that ir would work as expected when using quoted file path:
char* pszExePath = "\"D:\\Program Files\\Windows NT\\Accessories\\wordpad.exe\"";

To terminate the process you'll need to call
TerminateProcess( pi.hProcess, 0 );

previously you can check if the process is still running with
DWORD dwExitCode;
GetExitCodeProcess( pi.hProcess, &dwExitCode );
if ( dwExitCode == STILL_ACTIVE )
 TerminateProcess( pi.hProcess, 0 );

ZOPPO
Dear Zoppo,
   thanks.However, according to documention we should not use terminateprocess. I am using Enumwindow to do it and sent WM_CLOSE.But it still do not work.I asked this question in another thread.If possible, hope you can take a look.Thanks!
>we should not use terminateprocess
That's true, but in some circumstances there's no other way.

I.e. if you want to close WordPad without the user being asked if he wants to save
changes I think there's no other way.

With SendMessage WM_CLOSE or WM_QUIT you cannot make sure that
the other app is really closed. I.e. if WordPad asks the user if he wants to
save changes the user can press 'Cancel' and nothing happens.

ZOPPO