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(szTit le);
if(pWord != NULL)
{
pWord->SendMessage(WM_CLOS E);
}
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!
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(szTit
if(pWord != NULL)
{
pWord->SendMessage(WM_CLOS
}
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!
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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hpchong7,
Some of these questions have been open for some time. Please resolve them appropriately as soon as possible.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20147370
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167176
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20250968
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253484
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253960
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253962
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20254443
Thanks,
Netminder
Community Support Moderator
Experts Exchange
Some of these questions have been open for some time. Please resolve them appropriately as soon as possible.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20147370
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167176
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20250968
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253484
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253960
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253962
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20254443
Thanks,
Netminder
Community Support Moderator
Experts Exchange
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
CString::Find("WordPad");
or other technique to examine the window titles to see if it is one that you you want to close.
-- Dan
ASKER
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
Moondancer
Community Support Moderator @ Experts Exchange
ASKER
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:\\Pro gram Files\\Windows NT\\Accessories\\wordpad.e xe");
strcpy(pszOpenFilePath," C:\\temp\\test.doc");
strcat(pszExePath,pszOpenF ilePath);
BOOL bRet = CreateProcess(NULL,pszExeP ath,.....)
Then it worked.Did you know why?Thanks!
It's so strange that it does not work until I changed a little bit:
char pszExePath[512];
char pszOpenFilePath[512];
strcpy(pszExePath,"D:\\Pro
strcpy(pszOpenFilePath," C:\\temp\\test.doc");
strcat(pszExePath,pszOpenF
BOOL bRet = CreateProcess(NULL,pszExeP
Then it worked.Did you know why?Thanks!
ASKER
BTW, once the wordpad popped up, I can't kill it by the following:
CloseHandle(pi.dwProcessId )
May you help me to solve?Thanks!
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.e xe\"";
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
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.e
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
ASKER
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!
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
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
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