首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ShellExecute,我如何告诉我的项目应用程序已经关闭了?

ShellExecute,我如何告诉我的项目应用程序已经关闭了?
EN

Stack Overflow用户
提问于 2016-04-26 15:50:57
回答 1查看 1.6K关注 0票数 0

如果我从德尔菲应用程序中调用ShellExecute,

我如何确定我调用的程序是否已经完成,这样我就可以返回我的应用程序,在其他程序完成之后再做一些其他的事情。

例如,,打开记事本,完成并关闭后,在我的应用程序中显示消息“完成!”

EN

回答 1

Stack Overflow用户

发布于 2016-04-26 16:21:26

您需要将ShellExecuteExSEE_MASK_NOCLOSEPROCESS结合使用。看看这个,并看到我的评论内联:

代码语言:javascript
运行
复制
var
    sei: TShellExecuteInfo;
    exitCode: Cardinal;
begin
    ZeroMemory(@sei, SizeOf(sei));
    with sei do
    begin
        cbSize := SizeOf(sei);
        fMask := SEE_MASK_NOCLOSEPROCESS; // Tell ShellExecuteEx to keep the process handle open
        Wnd := WindowHandleIfNeeded; // Can be omitted
        lpVerb := 'open';
        lpFile := PChar(PathOfExeToRun);
        lpParameters := PChar(ParametersToUse);
        lpDirectory := PChar(WorkingDirectoryToUse); // Can be omitted
        nShow := SW_NORMAL; // Can be omitted
    end;

    if ShellExecuteEx(@sei) then
    begin
        // I have encapsulated the different ways in begin/end and commented.

        // *** EITHER: Wait for the child process to close, without processing messages (if you do it in a background thread)
        begin
            WaitForSingleObject(sei.hProcess, INFINITE);
        end;

        // *** OR: Wait for the child process to close, while processing messages (if you do it in the UI thread)
        begin
            while MsgWaitForMultipleObjects(1, sei.hProcess, FALSE, INFINITE, QS_ALLINPUT) = (WAIT_OBJECT_0 + 1) do begin
                Application.ProcessMessages
            end;
        end;

        // *** OR: Do something else, and in the middle you can check whether the child is still running using this:
        begin
            GetExitCodeProcess(sei.hProcess, exitCode);
            if exitCode == STILL_ACTIVE then begin
                // It's still running!
            end else begin
                // It has finished!
            end;
        end;

        // At the end, close the handle
        CloseHandle(sei.hProcess);
    end; 
end;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36869898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档