• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Problems with WaitForSingleObjectEx()

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
dsch
Asked:
dsch
  • 6
  • 3
1 Solution
 
NickRepinCommented:
You have to use ShellExecuteEx and wait on a process handle.
0
 
NickRepinCommented:
Specify SEE_MASK_NOCLOSEPROCESS for SHELLEXECUTEINFO.fMask.

Call WaitFor...(SHELLEXECUTEINFO.hProcess).

Don't forget to CloseHandle(SHELLEXECUTEINFO.hProcess).
0
 
NickRepinCommented:
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
NickRepinCommented:
For this particular case (just run an exe) I'd use the CreateProcess instead of ShellExecute.
But probably that doesn't matter.
0
 
dschAuthor Commented:
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
 
NickRepinCommented:
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
 
dschAuthor Commented:
Yes
0
 
NickRepinCommented:
That's strange.
Sorry, cannot help you more.
0
 
dschAuthor Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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