*SOLVED* ShellExecute WITH WaitForInputIdle

Hello again (still learning :) )

I have my program set; when button click open external program:
[code]ShellExecute(Handle, Operation, FileName, Params, Folder, ShowCmd)
[/code]

When I click the button it opens the program I have chosen :)
What I need now is help with getting the program to wait til the new program has actually finished loading before it goes to the next line of code.

I have found and read about WaitForInputIdle but do not understand how to fill in the paramaters (all of the sites refrence waiting til exe has closed, I dont want to wait til close, I want to wait til idle (waiting for user input)

The new program is a game, it will open the gui of the game and after loading will wait for user name and password (user name will come from INI file and password (typed into my program at start, will either be saved in memory or internal temp file)

Anyways, I want the button to open the program and wait for it to be idle then go to next line...

any help is greatly appreciated. Thank you

Comments

  • : Hello again (still learning :) )
    :
    : I have my program set; when button click open external program:
    : [code]: ShellExecute(Handle, Operation, FileName, Params, Folder, ShowCmd)
    : [/code]:
    :
    : When I click the button it opens the program I have chosen :)
    : What I need now is help with getting the program to wait til the new
    : program has actually finished loading before it goes to the next
    : line of code.
    :
    : I have found and read about WaitForInputIdle but do not understand
    : how to fill in the paramaters (all of the sites refrence waiting til
    : exe has closed, I dont want to wait til close, I want to wait til
    : idle (waiting for user input)
    :
    : The new program is a game, it will open the gui of the game and
    : after loading will wait for user name and password (user name will
    : come from INI file and password (typed into my program at start,
    : will either be saved in memory or internal temp file)
    :
    : Anyways, I want the button to open the program and wait for it to be
    : idle then go to next line...
    :
    : any help is greatly appreciated. Thank you
    :
    You need to use OpenProcess() instead of ShellExecute() for that. Then you can use the handle to verify the progress of the child process.
  • : You need to use OpenProcess() instead of ShellExecute() for that.
    : Then you can use the handle to verify the progress of the child
    : process.

    is the "openprocess" parameters the same? meaning if I create process it will open the exe file from the folder??
    ie:
    openprocess(Handle, Operation, FileName, Params, Folder, ShowCmd)

    or is the code line going to be completly different wher as I need to search more info on this new command?

  • : : You need to use OpenProcess() instead of ShellExecute() for that.
    : : Then you can use the handle to verify the progress of the child
    : : process.
    :
    : is the "openprocess" parameters the same? meaning if I create
    : process it will open the exe file from the folder??
    : ie:
    : openprocess(Handle, Operation, FileName, Params, Folder, ShowCmd)
    :
    : or is the code line going to be completly different wher as I need
    : to search more info on this new command?
    :
    :
    Here's the declaration for OpenProcess(): http://msdn2.microsoft.com/en-us/library/ms684320.aspx
    You can also find it in the help files. Just place the text cursor on the name and press F1.
  • : Here's the declaration for OpenProcess():
    : http://msdn2.microsoft.com/en-us/library/ms684320.aspx
    : You can also find it in the help files. Just place the text cursor
    : on the name and press F1.

    Unfortunatly F1 never helps a beginner, there are no examples, and the way it is written is just over my head:
    [code]OpenProcess

    The OpenProcess function opens an existing process object.


    HANDLE OpenProcess(
    DWORD dwDesiredAccess,
    BOOL bInheritHandle,
    DWORD dwProcessId
    );

    Parameters
    dwDesiredAccess
    [in] Access to the process object. This access right is checked against any security descriptor for the process. This parameter can be one or more of the process access rights.
    bInheritHandle
    [in] If this parameter is TRUE, the handle is inheritable. If the parameter is FALSE, the handle cannot be inherited.
    dwProcessId
    [in] Identifier of the process to open.
    Return Values
    If the function succeeds, the return value is an open handle to the specified process.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

    Remarks
    The handle returned by the OpenProcess function can be used in any function that requires a handle to a process, such as the wait functions, provided the appropriate access rights were requested.

    When you are finished with the handle, be sure to close it using the CloseHandle function.

    Example Code
    For an example, see Taking a Snapshot and Viewing Processes.

    Requirements
    Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
    Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.

    [/code] this dont really help....and the "example" they show...:
    [code]Taking a Snapshot and Viewing Processes

    The following example obtains a list of running processes. First, the GetProcessList function takes a snapshot of the currently executing processes in the system using the CreateToolhelp32Snapshot function, then it walks through the list recorded in the snapshot, using the Process32First and Process32Next functions. For each process, the function calls GetProcessModule, which is defined in Traversing the Module List.


    #include
    #include
    #include

    BOOL GetProcessList ()
    {
    HANDLE hProcessSnap = NULL;
    BOOL bRet = FALSE;
    PROCESSENTRY32 pe32 = {0};

    // Take a snapshot of all processes in the system.

    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if (hProcessSnap == INVALID_HANDLE_VALUE)
    return (FALSE);

    // Fill in the size of the structure before using it.

    pe32.dwSize = sizeof(PROCESSENTRY32);

    // Walk the snapshot of the processes, and for each process,
    // display information.

    if (Process32First(hProcessSnap, &pe32))
    {
    DWORD dwPriorityClass;
    BOOL bGotModule = FALSE;
    MODULEENTRY32 me32 = {0};

    do
    {
    bGotModule = GetProcessModule(pe32.th32ProcessID,
    pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));

    if (bGotModule)
    {
    HANDLE hProcess;

    // Get the actual priority class.
    hProcess = OpenProcess (PROCESS_ALL_ACCESS,
    FALSE, pe32.th32ProcessID);
    dwPriorityClass = GetPriorityClass (hProcess);
    CloseHandle (hProcess);

    // Print the process's information.
    printf( "
    Priority Class Base %d
    ",
    pe32.pcPriClassBase);
    printf( "PID %d
    ", pe32.th32ProcessID);
    printf( "Thread Count %d
    ", pe32.cntThreads);
    printf( "Module Name %s
    ", me32.szModule);
    printf( "Full Path %s

    ", me32.szExePath);
    }
    }
    while (Process32Next(hProcessSnap, &pe32));
    bRet = TRUE;
    }
    else
    bRet = FALSE; // could not walk the list of processes

    // Do not forget to clean up the snapshot object.

    CloseHandle (hProcessSnap);
    return (bRet);
    }
    [/code]
    which only confuses me even more.
    I have shellexecute for now and it works (currently using a sleep but that wont stay as some pc's are faster than others.
    so for now, the external program opens AND is ontop and the current active window.
    your telling me there is no code to add to shellexecute to wait for ...say: color change?
    (the screen is black until fully loaded)
    I ask as I have tried:
    openprocess, didnt work
    loadprocess, didnt work
    shellexecuteEX, didnt work
    the shellexecute is the only one of the options I have tried that works, so changing the code from 1 line to 50 lines just to open the external program is not an option (because the open IS working)
    now I just want a line of code to wait for color change, or idle, or something.
    thank you
  • I've looked deeper, and found a better API function: CreateProcess(). Here's an example:
    [code]
    FillChar(SuInfo, SizeOf(_STARTUPINFOA), 0);
    with SuInfo do
    begin
    cb := SizeOf(_STARTUPINFOA);
    dwX := CW_USEDEFAULT;
    dwY := CW_USEDEFAULT;
    wShowWindow := SW_SHOWDEFAULT;
    end;
    if CreateProcess('Your Child Process Name.exe',
    nil, nil, nil, false, 0, nil, nil, SuInfo, ProcessInfo) then
    begin
    WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
    ShowMessage('Stopped waiting for child process');
    end;
    [/code]
  • Thanko much for your time and help on this.
    However I have realized I will be using autoit with my code and it turns out that I can use the autoit code to wait for color change on the active window.

    So now I am learning to mix the 2 languages together (not an easy task)

    in another post you will find my current delima
  • Did you get as solution on this using AutoIT?
    can you pls paste the same to me.
    I also need it.
    Thanks.

    : Thanko much for your time and help on this.
    : However I have realized I will be using autoit with my code and it
    : turns out that I can use the autoit code to wait for color change on
    : the active window.
    :
    : So now I am learning to mix the 2 languages together (not an easy
    : task)
    :
    : in another post you will find my current delima
    :

  • : Did you get as solution on this using AutoIT?
    : can you pls paste the same to me.
    : I also need it.
    : Thanks.

    Yes, using Autoit with Delphi you need a wrapper, and the autoitx dll.
    with those you can combine the 2 programs together.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories