Solved

Problems with WaitForSingleObjectEx()

Posted on 2000-03-27
9
467 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

20 Experts available now in Live!

Get 1:1 Help Now