?
Solved

Application hangs after ShellExecute

Posted on 2000-02-23
14
Medium Priority
?
917 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

777 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