7

我需要在非托管进程中托管 .NET 运行时。我有可以通过 COM 加载运行时的代码,我可以将程序集加载到 AppDomain 中并很好地执行代码。

但是,我遇到了托管在网络共享上的应用程序的问题,并且必须更改应用程序策略才能让它们执行,这不是一个选项。所以我想做的是将运行时的主 AppDomain 的权限级别设置为不受限制。

有人可以提供有关如何设置 AppDomain 策略级别的示例吗?我不太清楚如何从非托管代码中实例化所需的类以创建 PolicyLevel 和相关对象并设置策略。基本上我不知道我需要什么包含/命名空间引用才能从我使用的 C++ 代码中工作。

这是我此时的代码:

/// Starts up the CLR and creates a Default AppDomain
DWORD WINAPI ClrLoad(char *ErrorMessage, DWORD *dwErrorSize)
{
    if (spDefAppDomain)
        return 1;


    //Retrieve a pointer to the ICorRuntimeHost interface
    HRESULT hr = CorBindToRuntimeEx(
                    ClrVersion, //Retrieve latest version by default
                    L"wks", //Request a WorkStation build of the CLR
                    STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN | STARTUP_CONCURRENT_GC, 
                    CLSID_CorRuntimeHost,
                    IID_ICorRuntimeHost,
                    (void**)&spRuntimeHost
                    );

    if (FAILED(hr)) 
    {
        *dwErrorSize = SetError(hr,ErrorMessage);   
        return hr;
    }

    //Start the CLR
    hr = spRuntimeHost->Start();

    if (FAILED(hr))
        return hr;

    CComPtr<IUnknown> pUnk;

    //Retrieve the IUnknown default AppDomain
    //hr = spRuntimeHost->GetDefaultDomain(&pUnk);
    //if (FAILED(hr)) 
    //  return hr;


    WCHAR domainId[50];
    swprintf(domainId,L"%s_%i",L"wwDotNetBridge",GetTickCount());
    hr = spRuntimeHost->CreateDomain(domainId,NULL,&pUnk);  

    hr = pUnk->QueryInterface(&spDefAppDomain.p);
    if (FAILED(hr)) 
        return hr;

      // // Create a new AppDomain PolicyLevel.
   //PolicyLevel polLevel = PolicyLevel:: CreateAppDomainLevel();

   //// Create a new, empty permission set.
   // PermissionSet permSet = gcnew PermissionSet( PermissionState::Unrestricted);

   //// Add permission to execute code to the permission set.
   //permSet->AddPermission( gcnew SecurityPermission( SecurityPermissionFlag::Execution ) );

   ////// Give the policy level's root code group a new policy statement based
   ////// on the new permission set.
   ////polLevel->RootCodeGroup->PolicyStatement = gcnew PolicyStatement( permSet );

   //// Give the new policy level to the application domain.
   //spDefAppdomain->SetAppDomainPolicy( polLevel );



    return 1;
}

我选择了一些示例代码(已注释),这些代码似乎可以满足我的需要,但我无法弄清楚我需要哪些 lib/include 引用才能使 PermissionSet 和 PolicyLevel 的类型引用工作。

任何想法都非常感谢...

4

1 回答 1

2

我认为你需要使用“非平凡”的方法来创建一个AppDomain以获得任何好处:

  • CreateDomainSetup(IUnknown** pAppDomainSetup),这会让你回到一个IAppDomainSetup实例。
  • 适当地填写(我认为所有的政策资料都在那里)
  • 使用CreateDomainEx, 传入初始化的设置实例作为第二个参数
  • 利润?

参考:

于 2012-11-28T20:01:11.433 回答