running external program, waiting for completion

I'm trying to start an external utility from a C++ program, but I'm having
some problems

I've tried various approaches, but all seem to suffer from the same problem:
I can start the utility but if I try to wait for it to exit, then I'll wait
forever (& the utility never seems to start running). One example:

 SHELLEXECUTEINFO ShExecInfo = {0};
 ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
 ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
 ShExecInfo.hwnd = NULL;
 ShExecInfo.lpVerb = _T("open");
 ShExecInfo.lpFile = "c:\\xxx\\yyy\\zzz.exe";
 ShExecInfo.lpParameters = "REPORTS C:\\xxx\\SK.TXT 0 0/EXTENSION=XY 
C:\\XXX\\ABC";
 ShExecInfo.lpDirectory = NULL;
 ShExecInfo.nShow = SW_SHOW;
 ShExecInfo.hInstApp = NULL;
 ShellExecuteEx(&ShExecInfo);
 WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

If I single step while debugging, it works OK. If I remove the
WaitForSingleObject call (& show a messagebox asking the user to indicate
when to continue, it works OK.

I've also tried CreateProcess + WaitForSingleObject, but with the same 
results.

I *think* the utility is a 16bit program, and perhaps this might be an 
issue.

Any ideas / example code?

Many thanks
Steve Kearon



0
anonymous (74722)
11/10/2004 3:59:19 PM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
901 Views

Similar Articles

[PageSpeed] 43

That only works for 32bit apps

See this for doing the same thing for 16-bit apps

http://support.microsoft.com/default.aspx?scid=kb;en-us;125689

AliR.

"Steve Kearon" <anonymous@discussions.microsoft.com> wrote in message
news:eArMB5zxEHA.1452@TK2MSFTNGP11.phx.gbl...
> I'm trying to start an external utility from a C++ program, but I'm having
> some problems
>
> I've tried various approaches, but all seem to suffer from the same
problem:
> I can start the utility but if I try to wait for it to exit, then I'll
wait
> forever (& the utility never seems to start running). One example:
>
>  SHELLEXECUTEINFO ShExecInfo = {0};
>  ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
>  ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
>  ShExecInfo.hwnd = NULL;
>  ShExecInfo.lpVerb = _T("open");
>  ShExecInfo.lpFile = "c:\\xxx\\yyy\\zzz.exe";
>  ShExecInfo.lpParameters = "REPORTS C:\\xxx\\SK.TXT 0 0/EXTENSION=XY
> C:\\XXX\\ABC";
>  ShExecInfo.lpDirectory = NULL;
>  ShExecInfo.nShow = SW_SHOW;
>  ShExecInfo.hInstApp = NULL;
>  ShellExecuteEx(&ShExecInfo);
>  WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
>
> If I single step while debugging, it works OK. If I remove the
> WaitForSingleObject call (& show a messagebox asking the user to indicate
> when to continue, it works OK.
>
> I've also tried CreateProcess + WaitForSingleObject, but with the same
> results.
>
> I *think* the utility is a 16bit program, and perhaps this might be an
> issue.
>
> Any ideas / example code?
>
> Many thanks
> Steve Kearon
>
>
>


0
AliR (179)
11/10/2004 5:28:32 PM
>I've tried various approaches, but all seem to suffer from the same problem:
>I can start the utility but if I try to wait for it to exit, then I'll wait
>forever (& the utility never seems to start running).

>If I single step while debugging, it works OK. If I remove the
>WaitForSingleObject call (& show a messagebox asking the user to indicate
>when to continue, it works OK.

Steve,

Try using MsgWaitForMultipleObjects instead of WaitForSingleObject -
it sounds like the problem is that you need to process messages.

Dave
0
davidl7375 (2060)
11/10/2004 5:30:29 PM
Have you tried _spawnlp(_P_WAIT,...);

"Steve Kearon" wrote:

> I'm trying to start an external utility from a C++ program, but I'm having
> some problems
> 
> I've tried various approaches, but all seem to suffer from the same problem:
> I can start the utility but if I try to wait for it to exit, then I'll wait
> forever (& the utility never seems to start running). One example:
> 
>  SHELLEXECUTEINFO ShExecInfo = {0};
>  ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
>  ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
>  ShExecInfo.hwnd = NULL;
>  ShExecInfo.lpVerb = _T("open");
>  ShExecInfo.lpFile = "c:\\xxx\\yyy\\zzz.exe";
>  ShExecInfo.lpParameters = "REPORTS C:\\xxx\\SK.TXT 0 0/EXTENSION=XY 
> C:\\XXX\\ABC";
>  ShExecInfo.lpDirectory = NULL;
>  ShExecInfo.nShow = SW_SHOW;
>  ShExecInfo.hInstApp = NULL;
>  ShellExecuteEx(&ShExecInfo);
>  WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> 
> If I single step while debugging, it works OK. If I remove the
> WaitForSingleObject call (& show a messagebox asking the user to indicate
> when to continue, it works OK.
> 
> I've also tried CreateProcess + WaitForSingleObject, but with the same 
> results.
> 
> I *think* the utility is a 16bit program, and perhaps this might be an 
> issue.
> 
> Any ideas / example code?
> 
> Many thanks
> Steve Kearon
> 
> 
> 
> 
0
Phil1 (164)
11/10/2004 5:35:05 PM
I have done it, this is the sample code.
It work fine because the application process the message on queue every 1000 
milliseconds.
Let me know if it's ok.
Bye
Pretolesi Riccardo

void CWPARDlg::StartWP()
{

 STARTUPINFO si;
 PROCESS_INFORMATION pi;
 // Lancio il file del programma
 ::ZeroMemory(&si, sizeof(STARTUPINFO));
 si.cb = sizeof(STARTUPINFO);
 if(::CreateProcess(_T("\\MyApp.exe"), NULL, NULL, NULL, FALSE, 0, NULL, 
NULL, &si, &pi))
 {
  ::CloseHandle(pi.hThread);

  while(::WaitForSingleObject(pi.hProcess, 1000) != WAIT_OBJECT_0)
  {
   // Elaboro i messaggi
   PeekAndPump();
  }

  ::CloseHandle(pi.hProcess);
 }
}

BOOL CWPARDlg::PeekAndPump()
{
 MSG msg;
 if(::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
 {
  if( AfxGetApp()->PumpMessage() )
  {
   return TRUE;
  }
  else
  {
   ::PostQuitMessage (0);
   return FALSE;
  }
 }

 LONG lIdle = 0;
 while(AfxGetApp()->OnIdle(lIdle++));
 return TRUE;
}

"Steve Kearon" <anonymous@discussions.microsoft.com> ha scritto nel 
messaggio news:eArMB5zxEHA.1452@TK2MSFTNGP11.phx.gbl...
> I'm trying to start an external utility from a C++ program, but I'm having
> some problems
>
> I've tried various approaches, but all seem to suffer from the same 
> problem:
> I can start the utility but if I try to wait for it to exit, then I'll 
> wait
> forever (& the utility never seems to start running). One example:
>
> SHELLEXECUTEINFO ShExecInfo = {0};
> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> ShExecInfo.hwnd = NULL;
> ShExecInfo.lpVerb = _T("open");
> ShExecInfo.lpFile = "c:\\xxx\\yyy\\zzz.exe";
> ShExecInfo.lpParameters = "REPORTS C:\\xxx\\SK.TXT 0 0/EXTENSION=XY 
> C:\\XXX\\ABC";
> ShExecInfo.lpDirectory = NULL;
> ShExecInfo.nShow = SW_SHOW;
> ShExecInfo.hInstApp = NULL;
> ShellExecuteEx(&ShExecInfo);
> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
>
> If I single step while debugging, it works OK. If I remove the
> WaitForSingleObject call (& show a messagebox asking the user to indicate
> when to continue, it works OK.
>
> I've also tried CreateProcess + WaitForSingleObject, but with the same 
> results.
>
> I *think* the utility is a 16bit program, and perhaps this might be an 
> issue.
>
> Any ideas / example code?
>
> Many thanks
> Steve Kearon
>
>
> 


0
11/11/2004 2:29:19 PM
On Wed, 10 Nov 2004 15:59:19 -0000, "Steve Kearon"
<anonymous@discussions.microsoft.com> wrote:

>I'm trying to start an external utility from a C++ program, but I'm having
>some problems 
>I've tried various approaches, but all seem to suffer from the same problem:
>I can start the utility but if I try to wait for it to exit, then I'll wait
>forever (& the utility never seems to start running). One example:

snipped from my website:

**********
The problem arises because your application has a window but isn't
pumping messages. If the spawned application invokes SendMessage with
one of the broadcast targets (HWND_BROADCAST or HWND_TOPMOST), then
the SendMessage won't return to the new application until all
applications have handled the message - but your app can't handle the
message because it isn't pumping messages.... so the new app locks up,
so your wait never succeeds.... DEADLOCK.

If you have absolute control over the spawned application, then there
are measures you can take, such as using SendMessageTimeout rather
than SendMessage (e.g. for DDE initiations, if anybody is still using
that). But there are situations which cause implicit SendMessage
broadcasts over which you have no control, such as using the
SetSysColors API for instance.
**********

Your options are :

1. Split the wait into another thread
2. timeout on the wait and use PeekMessage in the wait loop (ugh)
3. Use the MsgWaitForMultipleObjects API


Bob Moore
http://bobmoore.mvps.org/
0
bobm (116)
11/11/2004 4:53:44 PM
This is perhaps more complicated than first appears.

Basically, ALL suggested solutions (& my original version) work great, 
except where I'm using a fancy option of that 16bit utility.

The utility can be run in two modes: in one, it shows a dialog, asks for 
options, processes data when told to (showing a little progress message) and 
exits when the user tells it to.

In the other mode, I supply all the params it could collect in the dialog, 
and the utility just does its job and exits. It shows only the progress 
message, not the dialog.

I can synchronise with the first case (the dialog) fine, but I can't 
synchronise with the 2nd case. I'm discussing this with the utility 
suppliers now.

Many thanks for the ideas, anyway

Steve



"Bob Moore" <bobm@mvps.org> wrote in message 
news:uv57p0ddd8dbmbhjpigksadk8g2qnd17f6@4ax.com...
> On Wed, 10 Nov 2004 15:59:19 -0000, "Steve Kearon"
> <anonymous@discussions.microsoft.com> wrote:
>
>>I'm trying to start an external utility from a C++ program, but I'm having
>>some problems
>>I've tried various approaches, but all seem to suffer from the same 
>>problem:
>>I can start the utility but if I try to wait for it to exit, then I'll 
>>wait
>>forever (& the utility never seems to start running). One example:
>
> snipped from my website:
>
> **********
> The problem arises because your application has a window but isn't
> pumping messages. If the spawned application invokes SendMessage with
> one of the broadcast targets (HWND_BROADCAST or HWND_TOPMOST), then
> the SendMessage won't return to the new application until all
> applications have handled the message - but your app can't handle the
> message because it isn't pumping messages.... so the new app locks up,
> so your wait never succeeds.... DEADLOCK.
>
> If you have absolute control over the spawned application, then there
> are measures you can take, such as using SendMessageTimeout rather
> than SendMessage (e.g. for DDE initiations, if anybody is still using
> that). But there are situations which cause implicit SendMessage
> broadcasts over which you have no control, such as using the
> SetSysColors API for instance.
> **********
>
> Your options are :
>
> 1. Split the wait into another thread
> 2. timeout on the wait and use PeekMessage in the wait loop (ugh)
> 3. Use the MsgWaitForMultipleObjects API
>
>
> Bob Moore
> http://bobmoore.mvps.org/ 


0
anonymous (74722)
11/11/2004 5:07:48 PM
Reply:

Similar Artilces:

Can I read outlook mail without outlook program
Hello I use outlook for my mail. I tried to upgrade my old computer and destroyed the MB. I built a new computer (someone else put it together) and it's windows 7. Our home has both windows 7 and windows xp computers. The hard drive from the fried computer has some mail on it that I would like to look at. I tried installing the hard drive on my new computer but the hardware on the new computer was so much different that xp wouldn't even load. I tried looking for the mail files on the hard drive but I couldn't find them. I have outlook installed on thes...

Is it possible to control a running appliaction via web ?
hi, Is it possible to control a running application via web pages through internet ? thanks, behzad ...

Money is already running
After I use Microsoft Money 2006 and close it, the next time I try to open the program I get an alert box stating that "money is already running" the only thing I have been able to do is go to task manager and close the msmoney.exe and then reopen the program. Is there a place to correct this abnormality? Thanks TR This normally happens if you try and open Money v quickly after closing it and/or there is a background on-line transaction that hasn't completed. -- Regards Bob Peel, Microsoft MVP - Money For UK tips & fixes see http://support.microsoft.com/default....

Running queries via forms
is there a good tutorial out there on how to make forms that run queries and reports? I've gotten all turned around on it all. On Thu, 22 Apr 2010 13:53:01 -0700, Dragon <Dragon@discussions.microsoft.com> wrote: >is there a good tutorial out there on how to make forms that run queries and >reports? I've gotten all turned around on it all. Well, forms don't "run" anything. You can use a reference to a form control on a Query, and base a Report on that query. Crystal's tutorial might be a good place to look, this is one of the techniques she d...

Report error Run Time error 2427
I have combo box where I have list of months and years. When I choose January 2009. It gives me a 2427 run time error. When I debug it takes me to: Private Sub Detail_Format(Cancel As Integr, FormatCount As Integer) If Me.somefield>0 Then Me.Detail.BackColor = 14408667 Else Me.Detail.BackColor = 16777215 If me.[anotherfield] = "Followup" Then Me.Label22.Visible = False End Sub However, when I choose any other month I get my report in view form. Can someone please explain and help me out?? Thank you. I would try: Private Sub Detail_Format(Cancel As Integr, F...

Import external data from Access query with Variable
Hello - I have been able to record a macro where I can change the "inventory.Area" from US006 to US007. I would like to be able to make the area a variable and pick up the area code from within a spreadsheet. For example, I am doing reporting for 10 areas and would like to have one template that pulls in the data from Access for one area, saves the file, goes to the next area, pulls in that data from Access and so on. But I cannot figure out how to make the area in the VBcode below a variable. Any help would be appreciated. Sub Macro3() With Selection.QueryT...

Excel/VBA programming
There is no doubt about the adavantages of programming macros and basic code with an interface made in Excel. Advantages such as portability since pretty much any computer in world has available Office programs. However, for more sophisticated research (graduate school) the speed for my programs made on VBA is much slower than other codes (Fortran, C++ and others). Is it possible to plan a better memory use and compiling options for future versions of Excel and its VBA programming capabilities?. Thank you very much, Carlos Avila ---------------- This post is a suggestion for Microsoft...

office 2010 beta programs very slow to startup
Office 2010 beta Word and Excel spend ages saying they are downloading before they will start up. Office 2003 loads in about a quarter the time. The Office 2007 apps load almost instantly for me. The Splash screen should indicate what is slowing the process. As they start, do you see something such as "Loading <some file name> add-in" in the Splash screen? If so, that's what's causing the delay. Also, for future Office 2010 questions you may want to post in the applicable newsgroup found here: http://social.technet.microsoft.com/Forums/en-US/category/of...

MAC OS Excel 2008 Visual Basic Programming
Hi All I have designed a application in MS Excel 2008 on Windows XP using Visual Basic Application (VBA). Each and every thing is going fine on my side. My client is using MAC OS and MS Office for MAC. When i deployed my application to Client end one serious problem comes arise. The problem is that when my client is opening this project he is getting it into the Design Mode. He is unable to select from drop downs, enter values to text boxes........ Please help me guys. I put my all the efforts. I think now its the time to put your efforts. ...

Publisher Cannot Complete the Operation
I've been struggling with this msg for some time now. When clicking on File > Send E-Mail > Send an message, the following error occurs. "Publisher cannot complete the operation". I am using Office 2K7 and Win 2K7 (x64). The first time this was tried was after having Office 2K3 installed and then upgraded to 2K7. Uninstalled and reinstalled multiple time and deleting the installed files aswell. Just purchased Full version on 2K7 and the same error happens. Registry has been cleaned aswell. Outlook is set as the default program. Any thoughts, I don't ...

running a macro on a protected sheet
I've got a scenario where I've got several option buttons in groups to make choices. I've got the sheet protected so the buttons and/or groups can't be accidentally moved by the user. However, I've created a "clear all" button so the user can reset the form to redo their option button choices, if they need to. But, when protected, the macro attached to the "clear all" button won't work (due to the protection). How do I get around this? Thanks! Kriss, here is one way Sub test1() ActiveSheet.Unprotect password:="123" 'you cle...

Running an application only in particular system
How to run an executable or an application in a particular computer. Now i m checking with hardware profile id but seems not a unique id. Is there any unique id to put in my code so that application runs in a computer that matches the ID? ur catch is processor id i guess but many manufacturers have disabled this interupt ..... you can also live with the hardisk manufacturer id ...... "Raj Gopal" <indupuriraj@gmail.com> wrote in message news:1171551170.284594.300910@a34g2000cwb.googlegroups.com... > How to run an executable or an application in a particular computer. >...

Auto updating pivot tables using external XML data
Hi all, I'm new to Excel and have been playing with importing XML data and pivot tables recently. My goal is this: I have bunch of XML files in a directory. I'd like to generate pivot tables and "visualize" the data in them. Once my excel sheet looks good, I'd like to publish it as an HTML file so everyone else can view it using Internet Explorer. In fact, I already did all this. Here is the tough part though: These XML files get updated from time to time. And when I open the Internet Explorer, I want to see those latest data in XMLs. In my case, Excel publishes the ...

Stopping a running workflow rule.
Hello: I am using CRM 3.0. I would like to use workflow manager to monitor several bit type attributes. Basically when a check box is checked in a specific entity I want an email notification and/or other workflow events to happen. I am successfully performing this with a simple "wait for" condition workflow rule assigned to each of the entities...when the checkbox is checked, I get an email. Not all of the checkboxes will be used during the "active" period of the entity. The problem that I am having is stopping the workflow rule when a particular entity is no ...

MFC Program and ActiveX
I have written a MFC Program, how can i rewrite it into ActiveX Document so that I can open the program in the Internet Explorer (Like Java Applet)? Thanks, Marquis http://www.codersource.net/published/view/307/creating_custom_activex_controls_in_visual.aspx Tom "Marquis" <marquix@gmail.com> wrote in message news:1142525445.730104.171790@z34g2000cwc.googlegroups.com... >I have written a MFC Program, > how can i rewrite it into ActiveX Document > so that I can open the program in the Internet Explorer (Like Java > Applet)? > > Thanks, > Marquis > ...

Form running very slow after upgrade
the Office has been upgraded from Office xp to Office 2007. the Access 2007 performance is very slow after upgraded from Access XP. the database is then upgraded from share file to SQL for faster performance. i still find the Access 2007 with SQL is still slower than Access xp running on access file. The database is just less than 10m size. the performance is much more slower after adding back some codes, like the following code sometimes, i can see the input to the database 1 character by 1 character. could anyone advise me whether my config or code is bad? does Access 2007 need hu...

How do I add periods to complete a word in a cell?
For example: brian......... angela...... bob.......... Assume your names are in the range A1:A1000, enter this formula in B1 (array formula so enter it with Ctrl+Shift+Enter) and drag down: =CONCATENATE(A1,REPT(".",MAX(LEN($A$1:$A$1000))-LEN(A1))) -- Regards, Dave "Renee R." wrote: > For example: > > brian......... > angela...... > bob.......... Renee, I think you're asking how to get Excel to complete a cell with known values (like Brian's full name when you've only started to type it). But I'm not at all sure. If so, post back...

Addition of Buttons in Toolbar at run time
hi All, I am a newbie for Tool bars. I want to provide options to the user to add new Buttons(every thing, including Bitmap etc) in the tool bar while working on it. is there any way to do this?? "Ashish" <aashish83in@gmail.com> ha scritto nel messaggio news:1192617554.299345.145370@e9g2000prf.googlegroups.com... > I am a newbie for Tool bars. I want to provide options to the > user to add new Buttons(every thing, including Bitmap etc) in the tool > bar while working on it. is there any way to do this?? You may find the following CodeProject arti...

Multiple OL2000 processes running, none visible
I've seen this question asked in Usenet archives, but I haven't found a KB article or any definitive explanation on this. Here's the problem I have with a couple of users running Outlook 2000 on Windows 2000 Professional. Both are up-to-date on patches. They both launch Outlook from an icon in the Quick Launch folder; while troubleshooting this problem, I changed that icon to be a "regular" shortcut -- in other words, OL is launched with no command line parameters. Sometimes, they will click on the icon and "nothing happens." There is no "task" di...

Migration of Activities with status Completed
Hi, using the DMF, all records in cdf_fax en cdf_letter are not migrated when they have statecode 1 (completed). If I just change this to 0 (Open) they migrate without a problem. The error in the log says : Failed to migrate object: Server was unable to process request. Any help would be greatly appreciated ! ...

Help with completing a applescript for Excel 2008
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel This is going to be part of a larger script. I'm trying to figure out how to make the default save location to where the file opened from and the default file name to the current filename in the dialog box. <br><br>Here is what I go so far... <br><br>tell application &quot;Microsoft Excel&quot; <br> tell active workbook <br> set myFile to choose file name with prompt &quot;Save file as:&quot; <br> end tell <br> end tell <br><br>I...

Can't get Money to run after spyware infiltration
I had a problem with spyware getting onto my machine and causing havoc. I managed to clean it up with Spybot and a few other tools, but now when I go to run MS Money 2004 I get an error message saying the Money is not installed correctly. I tried to uninstall and reinstall Money but it did not help. The uninstall process worked and the reinstall process worked (no error messages) but I still get a "Money is not installed correctly" pop up when I try to run it. Does anyone have any insight about how to fix this? Thanks! S, Which platform are you running M04 on and whic...

External Error
When I try to run CRM under domain administrator account i get this error: "External Error - No Such Object on Server - The selected object could not be found." But administrator account exists both in Active Directory and mscrm database - what could cause this problem? Everything worked fine, but after I had to restart the CRM server, it started to give me this error instead of CRM interface. mac And could you log in using another account ? Perhaps all the CRM Services aren't configurated to an automatic launch. "mac" wrote: > When I try to run CRM under d...

jet database error when importing external txt data
Hello With MS Access 2002 (10.4302.4219) SP-2 and an opened Access2002 Format file, I can't import a fixed width text data file into a DB. Every time I try, I receive the error message "the microsoft jet database engine could not find the object ". Make sure the object exists and that you spell its name and the path name correctly" and then another window saying "An error occured trying to import file '......filename.txt' The file was not imported." Any idea ? Best regards. ...

Track and write the number of times that sub is run into a cell
I'm looking for a way to keep track of the number of times that a sub is run (via the press of a forms button) and to write that number into a cell on the sheet, say into G1. If the button is pressed 10x, then G1 will read: 10. Thanks Sub MyMacro() If Application.Caller = "Button 1" Then Sheet1.Range("G1") = Sheet1.Range("G1") + 1 End If 'YOUR CODE End Sub -- Regards Dave Hawley www.ozgrid.com "Max" <demechanik@yahoo.com> wrote in message news:56E00F52-ED7A-49A5-9EC9-1ACDCE89B504@microsoft.com... > I'...