我有一个C ++应用程序,我正在使用Win32_Volume类的格式方法。我使用以下内容作为参考:
Getting WMI Data from the Local Computer
我的问题是,当应用程序未以管理员身份运行时,Format方法返回代码3(拒绝访问)。当应用程序被提升时,返回码为0(ok)。用户必须能够在没有提升的情况下运行我的应用程序。但是,用户确实有权通过Windows格式化实用程序格式化驱动器,因此我想知道我的COM安全设置是否正确。
//Initialize COM component
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
//Set general COM security levels
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reserved
IWbemLocator *locator = 0;
//Create connection to WMI namespace
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&locator));
IWbemServices *service = 0;
//Connect to the root\cimv2 namespace (location of WIN32_Volume) with the current user
hr = locator->ConnectServer(BSTR(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &service);
//Set security level on the WMI conection
//Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(
service,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE);
无论海拔高度是多少,我都可以检索类属性(卷标,文件系统......)。它只是方法调用本身返回访问被拒绝。这都是在本地机器上完成的。如果需要更多代码,请告诉我。谢谢!