Terminating Application using ShellExecute return value

I use MFC Visual C++ 4.2.

I have a Main application that runs Child application using ShellExecute. 

When the Main application terminated, the Child application will also be 
terminated. I have tried to use the TerminateProcess but I think that's 
rather rough so I thought of if I can use the ShellExecute return value to 
terminate the process it ran. 

Is this possible? If it is not, what application terminate method that is 
safe?

Thank you very much.
0
landon (165)
12/22/2008 11:08:05 AM
vc.mfc 33608 articles. 0 followers. Follow

11 Replies
1418 Views

Similar Articles

[PageSpeed] 22

On Dec 22, 12:08=A0pm, Landon <Lan...@discussions.microsoft.com> wrote:
> I use MFC Visual C++ 4.2.
>
> I have a Main application that runs Child application using ShellExecute.
>
> When the Main application terminated, the Child application will also be
> terminated. I have tried to use the TerminateProcess but I think that's
> rather rough so I thought of if I can use the ShellExecute return value t=
o
> terminate the process it ran.
>
> Is this possible? If it is not, what application terminate method that is
> safe?
>
> Thank you very much.

You should really look into some/any IPC mechanism. There is no way to
__cleanly__ terminate a process from the outside (cleanly in  the
sense that terminating process can finish off whatever it's doing and
exit.

This article: http://www.ddj.com/cpp/207100682 is quite recent and
speaks about threads, but conceptually it's exactly the same for
processes. The only difference is how you ask a process, as opposed to
a thread, to terminate.

Off the top of my head, you can:

1. set a "terminate, please" flag through a shared resource (e.g. a
file, named event/mutex)
2. if the child is window-ed program, use some window handle of his
and SendMessage to it.
3. use some low-level system IPC (shared file, pipe, mailslot)
4. use COM

COM is the (ahem) official IPC of app-level programming on Windows, so
I would use that. I imagine that purpose of the two running together
is to __do__ something together, so it's likely that some IPC is
needed before termination. COM will do it all. (But, it's hard if you
have never done it before).
0
12/22/2008 12:17:17 PM
Use ShellExecuteEx instead of ShellExecute and get the hProcess from the 
structure that it returns, then get a known HWND from that process, and send 
it a message to close,

Ted.

"Landon" <Landon@discussions.microsoft.com> wrote in message 
news:97D73823-7E35-4885-B3F7-8DA8F4A2AEFC@microsoft.com...
>I use MFC Visual C++ 4.2.
>
> I have a Main application that runs Child application using ShellExecute.
>
> When the Main application terminated, the Child application will also be
> terminated. I have tried to use the TerminateProcess but I think that's
> rather rough so I thought of if I can use the ShellExecute return value to
> terminate the process it ran.
>
> Is this possible? If it is not, what application terminate method that is
> safe?
>
> Thank you very much. 

0
ted505 (8)
12/22/2008 1:02:57 PM
What good is the return ShellExecute return value going to do you?  It's just the
HINSTANCE of the process, and there's nothing you can really do with that to terminate the
process.  It might be useful, indirectly, but you have not stated your problem clearly
enough.

First, what is the nature of the process?  Is it a GUI-based or console-based process? You
have given an inadequate specification of the problem.  Is it one you wrote, or is it
written by someone else?

The best termination mechanism fora GUI-based process is to PostMessage(WM_CLOSE) to a
GUI-based process.  For a console process, it is problematic as to what should be done,
and depends upon the nature of the process.

When I do this with processes I wrote, the parent signals the child to terminate by using
some kind of notification that they both agree upon.  This could be PostMessage, or it
could be setting a named event (you also need to state if you can multiple instances of
these pairs running!).

Please explain the nature of the problem more carefully.
					joe

On Mon, 22 Dec 2008 03:08:05 -0800, Landon <Landon@discussions.microsoft.com> wrote:

>I use MFC Visual C++ 4.2.
>
>I have a Main application that runs Child application using ShellExecute. 
>
>When the Main application terminated, the Child application will also be 
>terminated. I have tried to use the TerminateProcess but I think that's 
>rather rough so I thought of if I can use the ShellExecute return value to 
>terminate the process it ran. 
>
>Is this possible? If it is not, what application terminate method that is 
>safe?
>
>Thank you very much.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
12/22/2008 3:20:09 PM
Correction to my earlier note: what ShellExecuteEx returns is not an HINSTANCE, but
something totally and completely useless for any purpose other than indicating if
ShellExecute succeeded or failed.  You can use ShellExecuteEx, but you need to explain
your problem more fully to decide what should be done with the information it returns.
					joe


On Mon, 22 Dec 2008 03:08:05 -0800, Landon <Landon@discussions.microsoft.com> wrote:

>I use MFC Visual C++ 4.2.
>
>I have a Main application that runs Child application using ShellExecute. 
>
>When the Main application terminated, the Child application will also be 
>terminated. I have tried to use the TerminateProcess but I think that's 
>rather rough so I thought of if I can use the ShellExecute return value to 
>terminate the process it ran. 
>
>Is this possible? If it is not, what application terminate method that is 
>safe?
>
>Thank you very much.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
12/22/2008 3:22:39 PM
Oh ok I am sorry Joe. An A application will run B application. Both GUI 
based. B only consists of standard MFC dialog buttons of OK and Cancel and a 
label. 

I will hide the B application window. It will working on some process 
without being displayed. As A application terminated, so does B.   

Just like that. What is a better way I can do to terminate the process than 
my recent code below?

Here is my recent code to end an app:
CString sWindowName = "Broadcaster";
CWnd* pWnd = FindWindow( NULL, sWindowName );
if( pWnd != NULL ){
	DWORD pid;		// Variable to hold the process ID.
	DWORD dThread;		// Variable to hold (unused) thread ID.
	dThread =  GetWindowThreadProcessId( pWnd->m_hWnd, &pid );
	hProcess = OpenProcess( SYNCHRONIZE | PROCESS_ALL_ACCESS, TRUE, pid );
	TerminateProcess( hProcess, 0 );
}


Thank you Joe.
0
Chris6982 (632)
12/23/2008 12:13:00 AM
See below...
On Mon, 22 Dec 2008 16:13:00 -0800, Landon <Chris@discussions.microsoft.com> wrote:

>Oh ok I am sorry Joe. An A application will run B application. Both GUI 
>based. B only consists of standard MFC dialog buttons of OK and Cancel and a 
>label. 
****
Since you are writing both, you could use PostMessage to initiate shutdown, using a
registered window message, or simply a WM_CLOSE message
****
>
>I will hide the B application window. It will working on some process 
>without being displayed. As A application terminated, so does B.   
****
This should actually be easy if B is a GUI app, see above
****
>
>Just like that. What is a better way I can do to terminate the process than 
>my recent code below?
>
>Here is my recent code to end an app:
>CString sWindowName = "Broadcaster";
>CWnd* pWnd = FindWindow( NULL, sWindowName );
>if( pWnd != NULL ){
>	DWORD pid;		// Variable to hold the process ID.
>	DWORD dThread;		// Variable to hold (unused) thread ID.
>	dThread =  GetWindowThreadProcessId( pWnd->m_hWnd, &pid );
>	hProcess = OpenProcess( SYNCHRONIZE | PROCESS_ALL_ACCESS, TRUE, pid );
>	TerminateProcess( hProcess, 0 );
****
This is absolutely the wrong way to do it.  TerminateProcess is a last-ditch, desperation,
nothing-else-has-worked, world-is-falling-apart API.  You are not in that situation.

CString sWindowName = _T("Broadcaster");

(Note that the strings are case-sensitive, so I presume this is the class name you
registered, case-exact.  Also, you should not be using obsolete 8-bit characters in this
context)

CWnd * pWnd = FindWindow(NULL, _T("Broadcaster"));

(I don't see why you need a separate variable to pass the name in...)

if(pWnd != NULL)
    {
     pWnd->PostMessage(WM_CLOSE);
    }

			joe
****

>}
>
>
>Thank you Joe.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
12/23/2008 2:14:49 AM
Joe, thanks. 

It has been worked fine. But now I have a question about the first time the 
application ran, I have succeeded in hiding the window but I did not use the 
LAST ShellExecute parameter I set it myself on the Broadcaster application 
OnInitDialog like this:
	WINDOWPLACEMENT wndpl = { 0 }; 
	wndpl.showCmd = SW_HIDE;
	SetWindowPlacement( &wndpl );
1. Why ShellExecute(...,...,...,SW_HIDE) did not work?
2. The most important thing : EVEN the application window has been hidden, 
it still appear on the Taskbar. How to also remove / hide it?

Thank you.
0
Chris6982 (632)
12/23/2008 7:53:01 AM
The SW_HIDE parameter is simply passed in to WinMain; it is the responsibility of code to
implement whatever this parameter is.  Since a dialog-based app has no code to handle
this, it has no effect.  So you put code in to handle it, and it worked.
					joe

On Mon, 22 Dec 2008 23:53:01 -0800, Chris <Chris@discussions.microsoft.com> wrote:

>Joe, thanks. 
>
>It has been worked fine. But now I have a question about the first time the 
>application ran, I have succeeded in hiding the window but I did not use the 
>LAST ShellExecute parameter I set it myself on the Broadcaster application 
>OnInitDialog like this:
>	WINDOWPLACEMENT wndpl = { 0 }; 
>	wndpl.showCmd = SW_HIDE;
>	SetWindowPlacement( &wndpl );
>1. Why ShellExecute(...,...,...,SW_HIDE) did not work?
>2. The most important thing : EVEN the application window has been hidden, 
>it still appear on the Taskbar. How to also remove / hide it?
>
>Thank you.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
12/23/2008 6:33:54 PM
Joe, how to remove the B application window from taskbar? So only main window 
that appears on the taskbar? I still cannot overcome it.

Thank you.
0
Chris6982 (632)
12/24/2008 12:01:01 AM
Joe, how to remove the child application window from the Taskbar? 

Thank you very much.
0
Chris6982 (632)
12/24/2008 12:07:01 AM
WS_EX_TOOLWINDOW

"Chris" <Chris@discussions.microsoft.com> wrote in message 
news:593A50EF-DBBB-4DCE-9DAD-7C7F452BC35C@microsoft.com...
> Joe, how to remove the child application window from the Taskbar?
>
> Thank you very much. 


0
alegr (1130)
12/24/2008 4:54:35 AM
Reply:

Similar Artilces:

Returning a range of serial numbers when consecutive, otherwise a range of one.
/* The following problem is an attempt to create a stored procedure that will act as a datasource for a crystal report. The scenario presented is a simplified version of the main problem. Using the example below, we have two customers which received two different items. The items are serialized and are always returned from the customer. Items sent are tracked in one table. Items received are tracked in another table. We need a list of each customer, the item they were sent, and the serial numbers we have not yet received from them. Now finding a list of customers, items, and seri...

Print check from SOP return
Is it possible to print a check that has been entered on an SOP return? When I do an SOP return and payment simultaneously, Dynamics transfers the check information to bank rec and the inventory item is put back into inventory. But there is not an option I can see anywhere to print the check? Thanks in advance, Russ Worth Worth & Associates, CPA's Hi Russ, If you have the Payables Module make sure your Refund Checks are setup. Tools>>Setup>>Sales>>Refund Checks. Then you can do a Refund Check through Transactions>>Sales>>Refund Checks If you use ...

Using For
Excel 2000 SR-1 I am trying to write a VB code using a For - Next statement to calcualate cells. The basic satement is: For X = 1 to 7 [A(x)] = {Calcualtion} ' Where A=Column and X = Row Next X Is there a correct way in VB code? This would greatly reduce the code I would have to write otherwise. One way: For X = 1 to 7 Range("A" & X) = {Calcualtion} ' Where A=Column and X = Row Next X Regards Trevor "Biomed" <Biomed@discussions.microsoft.com> wrote in message news:15009E4F-5179-4DC8-B455-546716E29D53@microsoft.co...

Displaying One Value in a Dropdown but Recording Another?
Hello... I've got a workbook containing two sheets. The first sheet is intended to be a template that a user fills out with various information pertaining to part orders. Each row corresponds to a machine part. The goal is to have the user fill out the workbook and send it back to us, and then we save it in a pre-defined tab delimited format and our software uploads that specially formatted file and inputs it into a database. The data-entry spreadsheet needs to be a special format, as I've already mentioned. I am restricting what the user can enter in certain columns using...

Great Plains should use Transaction Processing in SQL Server
This concerns Great Plains version 7.5 on MS SQL Server 2000 SP3. Day before yesterday a brief electric power outage occurred exactly while a user was posting a batch of invoices in PM. The database server and other network components were on UPS and did not fail in any way, but the workstation PC doing the posting was not on a UPS and it rebooted. This left the invoice batch in a sort of "half posted" state. In fact there was resulting damage in both the PM and GL areas of the database, because we have the system set up to post through in detail to GL. We first attempted...

Runtime to Terminate
I just bought a new desktop with Windows XP. I loaded Office 2000 and everything worked fine until Norton Antivirus booted up. Now I keep getting a message that says the "application requested the runtime to terminate it in an unusual way". After the message, outlook will not work until I close the app and then reopen. Any fixes??? ...

Lookup in two columns for same value
Hi and happy new year to uall, I have a problem, where I am trying to match a value in one of the sheets (say 1) to either of the two columns in another sheet (say 2) and then return the value to Sheet2 from the 3rd column of Sheet 1 Sheet 1: GM MG 1 JF FJ 2 DM MD 9 JS SJ 6 JM MJ 23 Sheet 2 (Should look like this) GM 1 MG 1 MJ 23 MD 9 Put this in B1 of Sheet2: =3DIF(ISNA(MATCH(A1,Sheet1!A:A,0)),IF(ISNA(MATCH(A1,Sheet1!B:B,0)),"not present",INDEX(Sheet1!C:C,MATCH(A1,Sheet1!B:B,0))),INDEX(Sheet1! C:C,MATCH(A1,Sheet1!A:A,0))) and then copy down...

Select alternate ship To address in Sales Trx Entry using a custo.
It would be nice to be able to have the option to populate the Sales Ship To Address Entry screen using a customer ID rather than having to type in the entire address, when the order is shipping to a customer other than the one being billed. Years ago in Solomon this was a standard feature. It would save users a lot of time if all they had to type was a customer ID rather than an entire name and address. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" bu...

Win98 console application shutdown question
Howdy, I have an MFC program that spawns a console program with CreateProcess. I find that on Windows 98 I don't know how to programatically shutdown this console app when the user selectes "shutdown" or "reset" for Windows. This isn't a problem on Win2K or latter. In those systems the MFC sets an event that shuts down the console when it gets the shutdown messages. I first tried using const BOOL bHandler = SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, // handler function TRUE); // add to list ASSERT(bHandler); and BOOL WINAPI CtrlHandler(DWORD f...

Using CRM web service within SSIS
Hi, I am trying to create an integration package with SSIS that will import data into MS CRM on a daily basis. I would have liked to create a .NET integration application, but the client has restricted us to the use of SSIS. From reading posts on this site I have come to the following conclusions: 1) It is possible 2) It should not be too difficult if I am an experienced programmer If I am correct, could someone please point me in the right direction with the following: 1) Which component do I need to use in SSIS to access the webservice? (Is it in the toolbox?) 2) Are there any exam...

Copy value from cell with custom format
Hi, I want to copy values from cells with a special custom number format. i.e. the cell value is displayed as 1060028 but it's literal value is 28. When I look at the number format in "format cells" it says the number format is 10600##. If I copy this cell from one to another even using "paste special values" it only returns 28. How can I copy the cell value 1060028 as a complete string? Would I use a function or would I have to use some VB script? thanks. The format should copy along with the value. Just use Paste, not PasteSpecial -- Gary''s Stud...

Count Rows When Using Auto-Filter
Using the auto-filter function I need to count the number of rows returned each time I select a new value. I can do a COUNTA (&#8211;1 to allow for the header row), on the column that counts all the rows but when I use the filter I still get the total count not a count of the reduced quantity by the filter. Can anyone help? Nick use the subtotal function with a first argument of 3 =subtotal(3,A2:A300) as an example. Make sure the column you do the counta on will have values in all rows. -- regards, Tom Ogilvy "Nick" <ner@westnet.com.au> wrote in message news:15...

Problems removing Form/Subform link using LinkMasterFields
Hi, I'm trying to remove a link between a form and a subform using the following code: Me!subform.LinkMasterFields = "" Me!subform.LinkChildFields = "" The result is the error code: You canceled the previous operation (run- time error 2001). Don't know what is wrong. I also tried: Me!subform.LinkMasterFields = Null Me!subform.LinkChildFields = Null but that gave me a different error message. FYI, I have access 2003. Thanks, Daniel ...

Print from console application
Hi I am trying to make my program send a page to a printer from my console application. I want to print the content of a string variable. I have tried the following to print from a text file: Dim myPrint as New PrintDocument myPrint.DocumentName="C:\temp\MyTextDocument.txt" myPrint.Print This works, but the printed page is blank. However - what I really need is to print a text string (several lines). Any ideas? best regards Frank On 5/30/2010 8:04 AM, Frank GTL wrote: > Hi > I am trying to make my program send a page to a printer from my cons...

How do I use a pivot table to get an average count?
I have a list of transactions with column headers of Month, Date, Weekday, Time and the transaction data. I would like to see the average number of transactions per hour per day. Can this be done in a pivot table? Yeah, Pivot table can show you averages. You might need to add a column in your data to group them by hour by day. Or you might use the group feature of pivot table. Right click on the field which has date/time. Group and Show detail -> Group ->...... Then in the data field, right click and then ->field settings, show average instead of sum or count or anything el...

test if a logical statement and use the answer within an if statem
How can I use the Find function within an array formula to check a column to locate evey occurence of a string and subsequently sum the corresponding numbers in a different column Something like this: =SUMPRODUCT(--(ISNUMBER(FIND("MyString",A2:A100))),B2:B100) or the non-case sensitive version: =SUMPRODUCT(--(ISNUMBER(SEARCH("MyString",A2:A100))),B2:B100) -- Best Regards, Luke M "BG Mark" <BGMark@discussions.microsoft.com> wrote in message news:767C1228-9339-46AA-8CDD-CD7A48489BB4@microsoft.com... > How can I use the Find functi...

How to add picture to MFC application
hi, i have problem i can't find How to add picture(jpeg) to MFC application ? thanks zack It depends how you want to show it. In a menu, in a control, in a dialog, or an owner-drawn window, etc. -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com "Zack" <zackm@infinite-memories.com> wrote in message news:4F7BAFD3-2084-48C6-8809-85F37C0FD58C@microsoft.com... > hi, > > i have problem i can't find How to add picture(jpeg) to MFC application ? > > thanks zack in a dialog box "Jonathan Wood" wrote: > It depends how y...

How do I use the timecard template if I am using a time clock and.
I need help using the time card template in Microsoft Excel We use a time clock and the employees punch the card when the come in and when they leave in the evening. I would like to set iup the template. HELP!!! Thanks , Gargi Hi Gargi what i've not sure about is how the information gets from the card to the excel spreadsheet - are you going to retype it? if so what assistance do you need in setting up the spreadsheet beyond ...............A.......................B.....................C........................D....................E 1.........Employee #............Date............S...

Detect termination of other process.
Calling ShellExecute, I execute a process. How can I detect when this program is turminated? I tried to combine CreateProcess & WaitForSingleObject functions. But filed. Hello ShellExecute() will run the program so you don't need CreateProcess() again. If you call CreateProcess() or ShellExecuteEx() you can use WaitForSingleObject() to wait for the process to terminate. -- Elias "Joon-ho Ryu" <junoryu@hotmail.com> wrote in message news:%23YQeGbddDHA.2432@TK2MSFTNGP10.phx.gbl... > Calling ShellExecute, I execute a process. > How can I detect when this program...

Changing format of XML Doc (using WriteXML?)
I am trying to get an output/file like this (below) in an XML file (MyXmlFile.xml) (which I will use for a slide show) -- <gallery timer="3" order="sequential" fadetime="2" looping="yes" xpos="0" ypos="0"> <image path="images2/Test1.jpg" /> <image path="images2/Test2.jpg" /> <image path="images2/Test3.jpg" /> <image path="images2/Test4.jpg" /> <image path="images2/Test5.jpg" /> <image path="images2/Test6.jpg" /> </gallery...

Pie Chart and Zero Value Labels
Morning All Is there a feature to remove all the zero value labours in a pie chart. I have 8 categories and 5 have a 0 value so it looks very messy. I can't find the feature but I'm sure it must exist. Thanks in advance to all who reply. Regards, H Hi, You could use autofilter to hide the rows of those with a zero value. Or formula as described here, http://www.andypope.info/charts/piezeros.htm Cheers Andy Hardip wrote: > Morning All > > Is there a feature to remove all the zero value labours in a pie chart. I > have 8 categories and 5 have a 0 value so it loo...

Re: Stored Proc Question: Use ad hoc Where Clause
Ive got a question on how to implement the following: I Have an sp_ that accepts two parameters. Here's essentially what the sp looks like: /****************************************************************************************************** Create Procedure sp_SearchResults @Status_ID Int, @Location_ID Int As Declare @SqlString varchar(100); /** Check values passed in to sp and based off these construct @sqlString to use in Where clause **/ If @Status_ID <> 0 Begin Select @SqlString = 'Where Mytable....

Terminating Application
Hi, Does anyone know how can I terminate an SDI application? To explain my situation, I'm trying to make an error handling mechanism and for some critical situations I have to terminate the whole application since it can't carry on anymore. I want to do this somehow that my classes destructors are called too. I have tried sending WM_CLOSE to the project's main window but it can't work out since I don't want my code execution flow to carry on after I sent this message (it makes unhandled exceptions). I would like something like exit(0) but it won't let the classes to do ...

application.run cant find open file
This is quite a strange one... I have created a macro that runs on book1 and half way through executes a procedure on book2, when that has finished it returns to book1, does a loop and starts the process again to go back to book2 then to sheet1 etc till the proccess has finished. Book1 is used to control the process and there are several different versions of Book2 which have identical code but different data. when designing the macro everything worked and it has been working fine for over a week. all of a sudden one of the versions of book2 stoped working but the others are fine. The follo...

i can't run my web application in windows7.
hi! i am MCPD.EA student i am using VS 2005 ,i have installed windows7 ,i can't run my web application; if i click on debug, it goes to web page and gives me an error that" internet explorer cannot display web page" please give me some suggestion??? On Nov 12, 1:34=A0pm, shavirak <shavi...@discussions.microsoft.com> wrote: > hi! i am MCPD.EA student > i am using VS 2005 ,i have installed windows7 ,i can't run my web applica= tion; > if i click on debug, > it goes to web page and gives me an error that" internet explorer cannot > dis...