托管应用程序的 CoInitializeSecurity,创建垫片?

CoInitializeSecurity for managed application, creating a shim?

我目前正在研究为托管 winforms 进程将 com+ 安全上下文设置为:None 的问题。

在 .NET 中,无法将 CoInitializeSecurity 设置为 Main 之后代码的第一行,来不及了。据我了解,该方法已由 CLR 调用。

在下面的 link 中: http://www.pinvoke.net/default.aspx/ole32.coinitializesecurity

写着:

"The workaround is to write an unmanaged "shim" that will call CoInitializeSecurity, then activate and call into managed code. You can do this via an export from a mixed-mode C++ DLL, by registering a managed component for use by COM, or by using the CLR hosting API."

有人可以解释一下吗?应该如何用非托管代码编写(语言无关紧要)

托管应用程序不时调用 com+ 服务器,但我没有看到 我应该立即激活界面以通过的任何原因 指向托管代码的指针?

The workaround is to write an unmanaged "shim" that will call CoInitializeSecurity, then activate and call into managed code. You can do this via an export from a mixed-mode C++ DLL, by registering a managed component for use by COM, or by using the CLR hosting API.

这意味着您在 c/c++ 中创建一个非常小的本机 .exe ("Shim") 调用 CoInitializeEx(),然后调用 CoInitializeSecurity 作为为 整个 Windows 进程建立 COM 安全环境的方法。

 ----------------------------------
| Windows Process                  |
|  -----------         ----------  |
|  |   exe   |   COM   |  dll   |  |
|  | "Shim"  |  ====>  |        |  |
|  | (c/c++) |         |   c#   |  |
|  -----------         ----------  |
 -----------------------------------

代码:

// pseudo c++ code
.
.
.
int main (int argc, char* argv)
{
    CoInitializeEx(...);
    CoInitializeSecurity(...);

    IMyContractPtr pFoo (__uuidof (MyClass));
    pFoo->RunMe();

    CoUnitialize();
}

有了它,下一个技巧是从 c/c++ 调用您的 .NET 代码。这里最简单的事情是制作一个 ComVisible(True) c# class,向 COM 公开一个方法,比如 RunMe(),当调用时显示你的 WinForms 表单。

public interface IMyContractPtr 
{
    void RunMe();
}

[ComVisible(true)]  // there are cleaner ways to expose COM-visible 
public class MyClass : IMyContractPtr 
{
   public void RunMe()
   {
       var form = new MainForm();
       form.ShowDialog(); // example
   }
}

您需要将代码从 c# .exe 项目移至新的 c# assembly/library。正是这个库将公开将从您的 c/c++ 应用程序调用的 COM 可见 class。 (尽管 c/c++ 应用程序不关心它是在 COM exe 还是 dll 中,对于本练习,您不想通过尝试将另一个 .exe 加载到已经 运行 Windows 进程)

消息泵和对话框

我通过将主要 window 设为模态对话框来简化这里的内容。模态对话框有它们自己的 Windows 消息泵处理,我们通过将您的表单代码放入 dll 并丢弃原始程序的 Main() 方法和 Application.Run(new MainForm()); 的优点而删除了一些东西。 =23=]

告诉我更多