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
2564 Views

Similar Articles

[PageSpeed] 17

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 (15974)
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 (15974)
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 (15974)
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 (15974)
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 (1131)
12/24/2008 4:54:35 AM
Reply:

Similar Artilces:

% Return Line graph?
Hi- Can someone show me how to customize a graph in Money 2005 to show my stock portfolio's % change over time? I just want a simple line chart showing my percentage change since the beginning - as a way to just figure out relative performance but I can't seem to find the right graph to customize. I can't just chart the value of the portfolio as I have been adding money to it over time which has drastically changed the % returns. I am not sure if what I want is possible or if this is clear but I also have the plugin for MS Excel that I can import money data so if there is...

Use first day of month formula in a drop down list
I'm using the formula =DATE(YEAR(B4),MONTH(B4),1) where cell B4 = Today() and this works to give me the 1st day of the current month. My question is: How do I use this in a drop down list on a separate worksheet in the same workbook? I have tried to name the cell containing the 'date' formula, but it looks like Excel does not allow naming a cell with a formula. Each time I name the cell (select the cell, click on the 'name box', type name, enter) when I re-select the cell it hasn't changed the cell name. It defaults back to the row/column name. Than...

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...

two problems using Outlook and Word
Does anyone know why it is that when I open an email in Outlook which comes in as an HTML doc, and try to respond to it, the format of the response converts it to plain text format in a hideous courier font? Which cannot be converted into HTML because the option is grayed out? Is this in some way related to the reason why I cannot now respond to emails without getting a warning that something is trying to access Microsoft Word and should I allow it and if so for how long? Whatever option I try, the result is the same, either Word opens as it should, as Word seems to open as this TXT version ...

Using CDialog as base class
Hi, Im kinda new to vc++ programming and i have a problem with CDialog, im making my own class to create skinnable dialogs with CDialog as base class, class CSkinDialog : public CDialog { public: CSkin(); virtual ~CSkin(); <i will add my skin functions here> }; In my dialog class i replaced "public CDialog" with "public CSkinDialog" but when i try to compile it i get this error: error C2614: 'CTestDlg' : illegal member initialization: 'CDialog' is not a base or member Any help is welcome :) Thanks in advance, Thomas "Thomas" <Da_Q...

Values in List Box
Hello, I have a multi select list box control on a form and I need to return the values of highlighted multi select items. Code With Me.lstExtractRouteMulti For Each varItem In .ItemsSelected If Not IsNull(varItem) Then Debug.Print End If Next End With In this, I need to return the values that are selected in the list box. Using Me.lstExtractRouteMulti.Value returns Null Thanks. Using Me.lstExtractRouteMulti.Value returns Null Yes it does because it is a multi select list box. In the code you posted, yo...

Index
Hi, I created a primary key when I created my table. I now want to create an index on multiple fields. How do I do that and how do I use this new index in a quary Thank you. Avaya Assuming Access 2003 or earlier, to create a multi-field index, either click on the Index icon on the button bar (it's a bunch of horizontal lines with a lightening bolt to the right), or select Indexes from the View menu. Pick a name for the index (it doesn't matter what you call it) and put that in the first column of the first empty row. In the next column, put the name of the first field you...

using outlook for email
I have windows xp and also office 200 premium.however, when i try sending an email while using explorer, i receive a message that reads "either there is no default mail client or current client cannot fulfill messaging request.Please run outlook and set it as the default mail client. I use AOL and have no problem sending email when i use it, however, when i use explorer i can't. is there a way i can set it up to be able to send an email? do i always have to open aol to send one? also, i have no idea how to configure it.also, when i try to set outlokk up, it asks for a postof...

Writing Formula For conditional Formatting Using Excel 2000
Hi Everyone: Does anyone know how to write three seperate formulas for these questions in the conditional formatting box Using Excel 2000? 1?: Red. If the value that is calculated in cell B42 is = to or less then the value in Cell B43 turn Cell B42 Red. Actual value in cell B42 and B43 are whole numbers 2?: Light Green. If the value that is calculated in cell B42 is between 1% and 29.99% greater than the value in Cell B43 turn Cell B42 light green. 3?: Dark Green. If the value that is calculated in cell B42 is greater by 30% or more then the then the value in Cell B43 turn...

Using REPLACE function in Excel
Is it possible to Replace multiple lines of text in an Excel Cell as a single action - including the line returns? Manually? And did you use alt-enter to force the new line within the cell? Edit|replace what: (yourstringhere) with: (yournewstringhere) replace all When you're typing in the strings, you can use ctrl-j to represent the newline in the cell. Don't look at the screen--'cause it may not look like you're typing anything after the ctrl-j, but you are. GarryG wrote: > > Is it possible to Replace multiple lines of text in an Excel Cell as a single > act...

redrawindows don't work in embedded, what can i use?
Hi, i would like to know the alternative of RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW) for Embedded VC++. I try Updatewindows but that's not work. If someone can write me the code equivalence the that line please. There the part of the code : void CDrawingView:nMouseMove(UINT nFlags, CPoint point) { if(m_Pan && GetDC()->PtVisible(point)) { if(m_pDrawing->isOpen()) { double OnePixel; VIEW drwview; m_pDrawing->GetViewProperties(&drwview); OnePixel = 1/(drwview.PPU*drwview.ZoomLevel); drwview.ViewLeft = drwview.ViewLeft ...

Is there a VOIP Control that I can use in my form
Has anyone ever heard of a VOIP Control or even an application that I can interact with. I am happy to pay for it. ie let the user make calls but my software can record the number/time/user etc. -- Regards Tom There is a dialer for a computer modem that uses TAPI which would work for VoIP phones like Vonage. There's even a wizard, just turn on the toolbox wizards and add a command button. The navigate to Miscellaneous on the left listbox, and AutoDialer on the right listbox. -- Arvin Meyer, MCP, MVP http://www.datastrat.com http://www.mvps.org/access http://www.accessmvp.com &q...

Excel changes the values of my numbers
I have coded a simple user form in Excel that calculates a balance an assigns the proper values to cells in a worksheet. What I don' understand is that when I enter a number into the user form, the valu of that number changes once it gets assigned to the cell in th worksheet. For example, if I enter the number 542.73 ,the user for assigns the number to the proper cell in the worksheet but it shows u as 542.72998046875. It should show up as 542.73 because that's what entered. I am calculating a running balance so my calculations aren' coming out right. Sometimes they are off ...

The messaging interface has returned an unknown error
I just upgraded to Office XP and it give me this message any time I try to add a calendar item: The messaging interface has returned an unknown error. I can also not send or receive email. I have downloaded the service packages, and installed them. Now, I can't even get Outlook to open completely. I see my folders, but it just stalls on opening and doesn't allow me to click on anything. Can anyone help? ...

Burning DVD's using VC++
I am writing an application to burn DVDs using Visual C++. Is there any way to burn a DVD using Visual C++ 6.0 EnterPrise Edition? I have a DVD-R media to burn. What I need to do is to first format them so that they can fit any data and then burn them with data from my harddisk. How can this be done? Are there any functions to do that? I know of mciSendCommand and DeviceIoControl. Can DVD burning be accomplished by using these 2 functions? If so can you just briefly go over the procedure involved(i.e.., what functions do I need to use for the important steps involved) to burn a DVD g...

Activate macro based on cell value
I want to have a macro that constantly plays in the background lookin for a change in a cell value. When the cell value changes a secon macro activates. So far i have this but its not really working can anyone help? Sub Auto_Open() Do If Range("C1") > ("0") Then Application.Run "'Workbook.xls'!Macro" Else Exit Sub End If Loop End Su -- Message posted from http://www.ExcelForum.com Hi LB79 You can use the change event for this Read Chip Pearson's site about Events http://www.cpearson.com/excel/events.htm -- Regards Ron de Bruin http:...

Adding data to a table using a combo box in a form
I just bought ACCESS 2007 and am having a few problems. Here's the one I'm trying to figure out now. I have two tables. PRODUCERS & CLIENTINFO. I would like to use the form, CLIENT INFO TRACKING to add new data to the PRODUCERS table. I've set up the form so PRODUCERS would be a combo box. I would like to use the combo box to enter the name of the producers and have it save those names in the PRODUCERS table. That way I can continue to enter new producers in the combo box or click on the combo box and have the previously entered producer's names pop up. I...

on the excel's startup i want to set a cell value to zero
on the excel's startup i want to set a cell value to zero. Sub initialization() Range("Input_Output_Sheet!D9").Value = 0 End Sub is there a way to run this macro at the excels startup automatically Michael, Presuming you mean when a certain workbook is opened, not when Excel starts up, you want to initialize that cell, put this in the ThisWorkbook module of the workbook: Private Sub Workbook_Open() Range("Input_Output_Sheet!D9").Value = 0 End Sub If you really want to do this to a certain workbook when Excel is started up, I think you'll have to pu...

Create view using data autofilter
I use Excel quite a lot, and would like to expand my ability. I use the AutoFilter feature to look at particular data. Usually it involves filtering a few different columns. I would like to set-up some "Views" that define the conditions and values for several columns. I could then click on one thing and get the pre-defined sets of data. -- Arleigh Birchler The simplest way I think is to record a macro that creates the view you want. Bart Snel "Arleigh Birchler" <ArleighBirchler@discussions.microsoft.com> schreef in bericht news:48B6926E-8BF3-471B-A9E7-9D...

How to convert computed values to the coreresponding constants?
In an Excel table I use a formula =if(a1=a2, 1, 0) to mark first of the two consecutive lines that have identical content in cell a by putting 1 or 0 in column b. Then I want to sort the table by its b values. To do this I have to change the computed 1's and 0's to unconditional constants. ijl wrote: > In an Excel table I use a formula =if(a1=a2, 1, 0) to mark first of the two > consecutive lines that have identical content in cell a by putting 1 or 0 in > column b. > Then I want to sort the table by its b values. To do this I have to change > the computed 1&#...

How to find a record using more than one field?
I have created a table with several fields such as ID, name, date, etc. I also created a switchboard with a find command to locate a record in forms view . However the find command only searches under one field. Can I create a find command that matches 2 fields? I want my find command to find a record in form view that matches both the ID and date fields. Yes, it is possible. There are several methods that should work. Can you post what method you used to for your find command? Then if you used VBA code we can suggest a modification to the code. John Spencer Access MVP...

Using a macro on shared workbook excel 2003
I have 2 computers sharing a workbook. There is a macro in it that sorts column A by date. There are about 300 rows of data. About one third of the rows have a few different colours. The colours are changed a few times a day. The colours identify the current assembly status of a product. Only one computer changes the colours, but both computers use the macro to organize by date in column A. My problem is that the coloured cells don't seem to always move with the rows when organized by date. Most of the time they do, but about once per day, either the colour is missing, or it is in a differ...

Filling Source Data Array with Decimal Values
I'm trying to write a macro that will fill a data series based on a single value in a spreadsheet. In this example, Series 1 is the actual observation data. Series 2 would be the average for all points in Series 1. My code works fine as long as the series average is rounded to zero decimal places. If not, I receive the error "Subscript out of Range". I've tried declaring "i" as various data types to no avail. Help would be appreciated. Sub AddAverageLine() 'Populate GrandMean values Dim ar As Variant ReDim ar(1 To p) p = ActiveChart.SeriesCollection(...

The screen should not move to focus on column using when typing
When typing down a column Excell seems to move the screen so that this column is the first one on the page. This is very very very annoying as you are then unable to see the row headings as you work. Previous versions did not do this and the user is normally perfectly capable of moving the screen if she or he so wishes ---------------- 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" button in the message pane. If you do not see the button, follow this link to open the sugg...

Deserializing an Object from XML Using Schema
Hi, I need to deserialize an existing object (not generate its code) from a serialized XML file. The only information I have about how the deserialization method is an input XML schema (which can vary) associated with the XML file containing the serialized object data. Therefore pre-defining it in code using xml attributes on the serialized class narrows it to only one serialization method and won't do. How can I use the schema to deserialize the object? Thanks, Sigalit. "Veilchen" <Veilchen@discussions.microsoft.com> wrote in message news:A71F15AB-7D2C-4E50-97DB-...