我需要在非托管进程中托管 .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 的类型引用正常工作。
非常感谢任何想法...
最佳答案
我认为您需要使用创建 AppDomain
的“非平凡”方法才能获得任何好处:
CreateDomainSetup(IUnknown** pAppDomainSetup)
,这将使您返回一个IAppDomainSetup
实例。- 适当填写(我认为所有政策内容都在那里)
- 使用
CreateDomainEx
,将您初始化的设置实例作为第二个参数传递 - 利润?
引用资料:
关于c# - 创建具有完全信任权限(包括网络权限)的托管 CLR AppDomain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877075/