我正在尝试编写几个用于与Microsoft Virtual Server2005 R2 SP1交互的NAnt任务,并且我已经在"Controlling Virtual Server through PowerShell"帖子中引用了"Virtual PC Guy's WebLog“中的代码。
它不工作:当调用CreateVirtualMachine时,我总是得到一个失败:
System.Runtime.InteropServices.COMException (0x80070542): Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)
at Microsoft.VirtualServer.Interop.VMVirtualServerClass.CreateVirtualMachine(String configurationName, String configurationPath)
我的代码如下:
var virtualServer = new VMVirtualServerClass();
SetSecurity(virtualServer);
var virtualMachine = virtualServer.CreateVirtualMachine("TEST",
@"D:\Virtual Server\TEST.vmc");...where SetSecurity的定义如下:
private static void SetSecurity(object dcomObject)
{
IntPtr pProxy = Marshal.GetIUnknownForObject(dcomObject);
int hr = CoSetProxyBlanket(pProxy,
RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_DEFAULT,
IntPtr.Zero,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
IntPtr.Zero,
EOAC_DYNAMIC_CLOAKING);
Marshal.ThrowExceptionForHR(hr);
}
private const uint RPC_C_AUTHN_NONE = 0;
private const uint RPC_C_AUTHN_WINNT = 10;
private const uint RPC_C_AUTHN_DEFAULT = 0xFFFFFFFF;
private const uint RPC_C_AUTHZ_NONE = 0;
private const uint RPC_C_AUTHZ_DEFAULT = 0xFFFFFFFF;
private const uint RPC_C_AUTHN_LEVEL_DEFAULT = 0;
private const uint RPC_C_AUTHN_LEVEL_PKT_PRIVACY = 6;
private const uint RPC_C_IMP_LEVEL_IDENTIFY = 2;
private const uint RPC_C_IMP_LEVEL_IMPERSONATE = 3;
private const uint EOAC_NONE = 0;
private const uint EOAC_DYNAMIC_CLOAKING = 0x40;
private const uint EOAC_DEFAULT = 0x0800;
[DllImport("Ole32.dll")]
public static extern int CoSetProxyBlanket(IntPtr pProxy,
UInt32 dwAuthnSvc,
UInt32 dwAuthzSvc,
IntPtr pServerPrincName,
UInt32 dwAuthnLevel,
UInt32 dwImpLevel,
IntPtr pAuthInfo,
UInt32 dwCapabilities);如果我编写一个独立的程序并添加一个对CoInitializeSecurity的调用,那么它就可以工作。但是,我不想要一个独立的程序--我想要一组NAnt任务(所以是一个DLL),我也不想调用CoInitializeSecurity,因为无法保证其他NAnt任务不会调用它。
有人把它修好了吗?
发布于 2008-11-30 07:49:57
在从托管代码使用CoSetProxyBlanket时,您可能会遇到一个基本问题。遗憾的是,由于CLR封送处理接口的方式,在托管代码中没有可靠的方法来与此方法进行互操作。
下面是描述这个问题的几个博客条目
发布于 2009-08-23 21:14:35
无论如何,看起来.NET 4.0将添加一个新的方法GetObjectForIUnknownWithBlanket (在System.Runtime.InteropServices.Marshal下)来帮助解决这个问题。摘自(Beta) MSDN文章:
GetObjectForIUnknownWithBlanket将IUnknown包装在一个唯一的托管对象中,并在所有请求的接口上调用CoSetProxyBlanket函数。它确保返回一个唯一的对象,而不是在缓存中查找给定的IUnknown与现有对象相匹配。
我还没有试过,但它看起来很有前途。
顺便说一句,很好的问题和被接受的答案!我刚刚遇到了同样的问题,这是我唯一找到合适解释的地方。
https://stackoverflow.com/questions/327404
复制相似问题