Solved

Application hangs after ShellExecute

Posted on 2000-02-23
14
781 Views
Last Modified: 2013-12-04
I am using ShellExecute to allow a user of my program to view a file using it's associated application.  I am having an odd problem with MS Word however.  The problem is that when the user subsequently tries to close Word, Word hangs horribly.  The only way to get Word to finally close is to close my application (the one calling ShellExecute).  Any suggestions?
0
Comment
Question by:mrgentry
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2551970
What are you passing in as a value to hwnd (parameter #1) in ShellExecute?  If it's the HWND to your application's window, this can be a problem.  Pass NULL instead and Word will not be trying to send messages back to your application.
0
 
LVL 1

Author Comment

by:mrgentry
ID: 2552142
Using NULL for the HWND parameter doesn't change anything.  Word still hangs while trying to close.  It may be worth noting that I've discovered that there is subtly different behaviour on NT versus 95/98.  On NT, Word closes immediately as I described originally, but in 95/98 Word must be shot dead.  Don't know if that helps any.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2552225
Is there something wrong with your copy of Word perhaps?

I put together a quick test program and put the following code to be executed when a button is clicked:

void CShellExAgainDlg::OnOK()
{
      // TODO: Add extra validation here
      ShellExecute(NULL, "open", "C:\\TEST.DOC", NULL, NULL, SW_SHOWNORMAL);

}

Works perfectly on Win98 with Word '97.  I opened and closed the document about a dozen times just fine...
0
 
LVL 2

Expert Comment

by:DarrinE
ID: 2562255
Are you relying on the DOC extension or are you using the entire commandline ? Sounds like your using the entire command line - limit the lpfile parameter to the document path/name

HINSTANCE ShellExecute(
    HWND hwnd,
    LPCTSTR lpVerb,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd
);

If you're still having problems post your code


DarrinE
0
 
LVL 1

Author Comment

by:mrgentry
ID: 2569631
Here is some sample code that demonstrates the problem:

#include <windows.h>

int main(int argc, char *argv[])
{
   if (argc == 1)
      return 1;

   ShellExecute(NULL, "open", argv[1], NULL, ".", SW_SHOWNORMAL);

   return 0;
}

The problem can be duplicated by supplying a Word .doc file as the argument.

This works fine on WinNT.  If I run it on Win98, Word hangs when you close Word.  Eventually Word will present a dialog saying that 'the operation is taking longer than expected' or something like that and offers you the option of aborting and closing Word, but it takes a LONG time for this to happen.
0
 

Expert Comment

by:EllisMiller
ID: 2575528
Does this happen for all Word documents, or only for specific ones?  Try a new document with a generic name like "C:\test.doc".  Are there certain filenames that fail, when others work?  Or certain files that fail, even if you rename them?
0
 
LVL 2

Expert Comment

by:DarrinE
ID: 2575944
This might sound a little silly but try

ShellExecute(NULL, "open", argv[0], NULL, ".", SW_SHOWNORMAL);

Why are you using arguments ? Why not a user selected file path. I've tried on my WIN98 machine and there is no problem like you've described that I can replicate

DarrinE

   
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:mrgentry
ID: 2577520
Here is some additional info about the problem:
Really quickly I made a Dialog based MFC app that just had one button that would call ShellExecute to open a user selected file.  This worked perfectly fine on Win95/98/NT and Word was able to close successfully after having opened the file.  So it apparently has something to do with the app not having a window.  It is beyond annoying though, that the behaviour without a window is inconsistant between 95/98 (where Word hangs) and NT (where the Word can close without difficulty).

EllisMiller:
No specific filename, the behaviour is the same for all word documents I have tried (including c:\test.doc).

DarrinE:
I've tried the code I posted on 4 different boxes running Win98 and all three exhibit the hanging Word problem when the file opened is a Word document.
Also, why would I use argv[0] as the file to open since that would be the name of my executable, not the filename the user supplies?  Without having tried it, it's probably safe to say that it wouldn't do anything other than execute my program over and over again endlessly.
0
 
LVL 1

Author Comment

by:mrgentry
ID: 2577550
Incidentally, replacing the "." in my code with NULL has no effect.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2578036
>So it apparently has something to do
>with the app not having a window

So there is MORE to this that you've let on.  Tell me more about your mystery app.
0
 

Expert Comment

by:EllisMiller
ID: 2578325
I was able to duplicate your problem on my Win98 machine with Word 2000.  The problem does appear to be the fact that your app does not have a window.  I'd suggest trying to create a hidden window with your app, to avoid the freezeup.  This is ugly, but will work.  Let me know if you need a source example.
0
 

Accepted Solution

by:
EllisMiller earned 100 total points
ID: 2579016
I duplicated your problem.  It IS an ugly lockup.  I had to reboot my computer both times that I tried it.  I had also tried passing in the return value of GetDesktopWindow, but that also locks up.  I'm assuming that's what Windows uses if you pass in NULL.  I fixed this as I suggested:  by creating a temporary hidden window to do the launch.  This works fine even though the window is destroyed and the launching program is closed before you close Word.  A very fascinating bug, to say the least.  Here is the code I used:

void main(int argc, char *argv[])
{
   // must create a hidden window to pass into ShellExecute so that Word does not lock up when closing
   HWND hHiddenWnd = CreateWindow("EDIT", "", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
   ShellExecute(hHiddenWnd, "open", argv[1], NULL, ".", SW_SHOWNORMAL);
   DestroyWindow(hHiddenWnd);
}
0
 
LVL 1

Author Comment

by:mrgentry
ID: 2580405
I can't image why I didn't try that, but it seems to work beautifully.  Thanks.
0
 

Expert Comment

by:budgie_b
ID: 2785434
EllisMiller & mrgentry

I am experiencing exactly the same problem.  Have you any idea why this would occur?  I did try to get some answers from MS

Thanks

Budgie
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now