简体   繁体   English

重点介绍从C ++ Shell扩展启动的C#UI应用程序

[英]Give focus to a C# UI Application launched from a C++ Shell Extension

I have a WPF application launched from a Windows Shell Extension (a simple context menu) that I want to display it focused. 我有一个WPF应用程序,它是从Windows Shell扩展(一个简单的上下文菜单)启动的,希望将其重点显示。

The UI Application appears on focus and active when is launched from a command line, but when I use the windows function CreateProcessAsUserW and launch it from the context menu is showed without focus. 从命令行启动时,UI应用程序出现在焦点上并且处于活动状态,但是当我使用Windows函数CreateProcessAsUserW并从上下文菜单中启动它时,它显示为没有焦点。 There is some parameter configuration that I am missing when I create the process in the following code? 在以下代码中创建流程时缺少一些参数配置?

bool launchProcessAsCurrentUser(HANDLE token, const std::wstring& command)
{
    bool result = false;
    void* environment = nullptr;

    PROCESS_INFORMATION processInfo{ 0 };

    STARTUPINFOW startupInfo { 0 };
    ZeroMemory(&startupInfo, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);

    const bool impersonateUser = ImpersonateLoggedOnUser(token);
    if (impersonateUser && CreateEnvironmentBlock(&environment, token, FALSE))
    {
        SECURITY_ATTRIBUTES secA{ 0 };
        SECURITY_ATTRIBUTES secB{ 0 };
        result = CreateProcessAsUserW(token,
                                      0,
                                      const_cast<LPWSTR>(command.c_str()),
                                      &secA,
                                      &secB,
                                      FALSE, CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
                                      environment,
                                      0,
                                      &startupInfo,
                                      &processInfo);
        DestroyEnvironmentBlock(environment);
    }
    else
    {
        //Log error
    }

    if (impersonateUser)
    {
        RevertToSelf();
    }
    return result;
}

Note: The C# windows is set on focus before it is displayed. 注意: C#窗口在显示之前已设置为焦点。

Best Regards, 最好的祝福,

You could try explicitly setting the show mode by filling in the startup info: 您可以尝试通过填写启动信息来显式设置显示模式:

// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOWNORMAL;

Also, since the process you're creating is a window app and not a console app, CREATE_NO_WINDOW seems out of place. 另外,由于您创建的过程是窗口应用程序而不是控制台应用程序,因此CREATE_NO_WINDOW似乎不合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM