Solved

Problems with WaitForSingleObjectEx()

Posted on 2000-03-27
9
469 Views
Last Modified: 2013-12-03
HI
I am writing a quick app to launch in the auto-run on a CD.
First of all it tests to see if Acrobat Reader is installed by checking if a registry key exists. If true, then it will just launch the "Contents.pdf" file.
If not a message box appears & if OK is selected will launch the reader's setup program.
What I want to do is wait for the setup to finish, THEN launch the "Contents.pdf" file.
I have tried using the WaitForSingleObjectEx() function but it returns immeidiatly.
Any ideas please. Here is my code,
It is written on BCB4 using the VCL for the TRegistry Object:
        TRegistry *Reg = new TRegistry;
        char Instructions[]="\"Adobe Acrobat Reader\" does not appear to be installed on your computer.\nClick 'OK' to install the latest version of the reader.\nIf you click 'Cancel' you will not be able to view the contents of this CDROM.\n";
        PROCESS_INFORMATION pInfo;

        Reg->RootKey = HKEY_LOCAL_MACHINE;
        if(!Reg->KeyExists("Software\\Adobe\\Acrobat"))
        {
        if(IDOK      == MessageBox(Application->Handle,Instructions,"Renault Technical Manual CDROM",MB_OKCANCEL))
        {

        ar40engHandle = ShellExecute(Application->Handle, "open", "Ar40eng.exe", NULL, NULL, SW_NORMAL);
        WaitForSingleObjectEx(
    ar40engHandle,      // handle of object to wait for
    INFINITE,      // time-out interval in milliseconds
    false       // return to execute I/O completion routine if TRUE
   );
   }
   else
        {
        return 0;
        }
   }

        ShellExecute (Application->Handle, "open", "Contents.pdf", NULL, NULL, SW_NORMAL);
        return 0;
0
Comment
Question by:dsch
  • 6
  • 3
9 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 150 total points
ID: 2660422
You have to use ShellExecuteEx and wait on a process handle.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2660425
Specify SEE_MASK_NOCLOSEPROCESS for SHELLEXECUTEINFO.fMask.

Call WaitFor...(SHELLEXECUTEINFO.hProcess).

Don't forget to CloseHandle(SHELLEXECUTEINFO.hProcess).
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2660432
Something like this:

SHELLEXECUTEINFO s;
s.cbSize=sizeof(SHELLEXECUTEINFO);
s.fMask=SEE_MASK_NOCLOSEPROCESS;
s.hwnd=Application->Handle;
s.lpVerb=0;
s.lpFile="Ar40eng.exe";
s.lpParameters=0;
s.lpDirectory=0;
s.nShow=0;

ShellExecuteEx(&s);

WaitForSingleObject(s.hProcess,INFINITE)
CloseHandle(s.hProcess);
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 15

Expert Comment

by:NickRepin
ID: 2660476
For this particular case (just run an exe) I'd use the CreateProcess instead of ShellExecute.
But probably that doesn't matter.
0
 
LVL 1

Author Comment

by:dsch
ID: 2676674
Thanks Nick,
That works fine.
I do have a problem however, but I think it is a problem with the acrobat setup program.
After it has unpacked the files it has to launch the instalshield program.
For some reason, it cannot start this when my app is still running.  I have made sure that the handle can be inherited (even by trying CreateProcess) but still no joy.  I think my only other option is to close the handle after it is created, start another Thread with a timer and then to check to see it the program is still running. (I havn't tried it though).
Any-way thanks for your help & c u around.
Kind Regards
David
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2676684
It all is not clear for me.
Acrobat setup does some job and then launch the installshield. But install shield does not appear while you are waiting on the acrobat handle. Is that right?
And ishield appears once you closed the handle?
0
 
LVL 1

Author Comment

by:dsch
ID: 2679297
Yes
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2679732
That's strange.
Sorry, cannot help you more.
0
 
LVL 1

Author Comment

by:dsch
ID: 2682941
Cheers anyway,
What I ahve had to do, is close my program after giving a message that 'after installation, eject then replace the CD' (autorun)
Thanks for all your help, it has taught me a bit more as well.
Bye for now
David
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 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

15 Experts available now in Live!

Get 1:1 Help Now