[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

Application hangs after ShellExecute

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
mrgentry
Asked:
mrgentry
  • 5
  • 3
  • 3
  • +2
1 Solution
 
jhanceCommented:
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
 
mrgentryAuthor Commented:
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
 
jhanceCommented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
DarrinECommented:
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
 
mrgentryAuthor Commented:
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
 
EllisMillerCommented:
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
 
DarrinECommented:
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
 
mrgentryAuthor Commented:
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
 
mrgentryAuthor Commented:
Incidentally, replacing the "." in my code with NULL has no effect.
0
 
jhanceCommented:
>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
 
EllisMillerCommented:
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
 
EllisMillerCommented:
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
 
mrgentryAuthor Commented:
I can't image why I didn't try that, but it seems to work beautifully.  Thanks.
0
 
budgie_bCommented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now