使用 ShellExecuteEx 打开 Office 文档时出现 ERROR_DDE_FAIL

发布于 2024-11-26 06:59:51 字数 1773 浏览 1 评论 0原文

我在打开 Office 文件时遇到从 ShellExecuteEx 返回的错误。这种情况仅发生在某些电脑上,即使它们具有相同的操作系统/Office 版本/等。

我收到的错误是 ERROR_DDE_FAIL,办公室给出一条消息,其中包含文本:“将命令发送到应用程序时发生错误。”

这是我正在使用的代码:

// Create SHELLEXECUTEINFO structure for passing as parameter to the ShellExecuteEx
// function. Suppress errors by enabling SEE_MASK_FLAG_NO_UI on fMask member.
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize           = sizeof( SHELLEXECUTEINFO );
ShExecInfo.fMask            = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI ;
ShExecInfo.hwnd             = NULL;
ShExecInfo.lpFile           = lpFile;
ShExecInfo.lpVerb           = "open";
ShExecInfo.lpDirectory      = NULL;
ShExecInfo.nShow            = SW_SHOWNORMAL;
ShExecInfo.hInstApp         = NULL;

//HINSTANCE nResult = ShellExecute(NULL, "open", lpFile, NULL, NULL, SW_SHOWNORMAL);
HRESULT hr = ::ShellExecuteEx( &ShExecInfo );

if (hr == TRUE)
{
    ::WaitForInputIdle( ShExecInfo.hProcess, INFINITE );

    DWORD dwProcessId =  ::GetProcessId( ShExecInfo.hProcess );

    BOOL bHadLock = FALSE;

    // Wait while file lock has been released.
    while ( FileInUse( lpFile ) ) {
        bHadLock = TRUE;
        Sleep( 100 );
    }

    // Wait while process has stopped running in case of notepad or other
    // editors who don't lock file.
    if ( !bHadLock ) {
        DWORD lpExitCode;
        ::GetExitCodeProcess( ShExecInfo.hProcess, &lpExitCode );

        while ( lpExitCode == STATUS_PENDING ) {

            Sleep( 100 );
            ::GetExitCodeProcess( ShExecInfo.hProcess, &lpExitCode );
        }
    }
}
else
{
    DWORD dwError = ::GetLastError( );
    if (dwError == ERROR_DDE_FAIL) {
        // Why do I get this error and how to prevent this?
    }
}

I encountered an error returned from ShellExecuteEx when opening an office file. This only happens on some pc's even while they have same OS/Office version/ etc.

The error I am getting is an ERROR_DDE_FAIL, with an message given from office with the text: "An error occurred in sending the command to the application."

This is the code I am using:

// Create SHELLEXECUTEINFO structure for passing as parameter to the ShellExecuteEx
// function. Suppress errors by enabling SEE_MASK_FLAG_NO_UI on fMask member.
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize           = sizeof( SHELLEXECUTEINFO );
ShExecInfo.fMask            = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI ;
ShExecInfo.hwnd             = NULL;
ShExecInfo.lpFile           = lpFile;
ShExecInfo.lpVerb           = "open";
ShExecInfo.lpDirectory      = NULL;
ShExecInfo.nShow            = SW_SHOWNORMAL;
ShExecInfo.hInstApp         = NULL;

//HINSTANCE nResult = ShellExecute(NULL, "open", lpFile, NULL, NULL, SW_SHOWNORMAL);
HRESULT hr = ::ShellExecuteEx( &ShExecInfo );

if (hr == TRUE)
{
    ::WaitForInputIdle( ShExecInfo.hProcess, INFINITE );

    DWORD dwProcessId =  ::GetProcessId( ShExecInfo.hProcess );

    BOOL bHadLock = FALSE;

    // Wait while file lock has been released.
    while ( FileInUse( lpFile ) ) {
        bHadLock = TRUE;
        Sleep( 100 );
    }

    // Wait while process has stopped running in case of notepad or other
    // editors who don't lock file.
    if ( !bHadLock ) {
        DWORD lpExitCode;
        ::GetExitCodeProcess( ShExecInfo.hProcess, &lpExitCode );

        while ( lpExitCode == STATUS_PENDING ) {

            Sleep( 100 );
            ::GetExitCodeProcess( ShExecInfo.hProcess, &lpExitCode );
        }
    }
}
else
{
    DWORD dwError = ::GetLastError( );
    if (dwError == ERROR_DDE_FAIL) {
        // Why do I get this error and how to prevent this?
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文