WMI unter Windows 2000
-
Hallöchen zusammen,
mein Problem besteh darin, dass die Funktion ExecQuery unter Windows 2000 aber nicht unter XP oder Vista fehlschlägt. Bei der Funktion CoInitializeSecurity steht ja sogar dabei, dass man unter Win 2000 eine SOLE_AUTHENTICATION_LIST als Paramter mitgeben muss. Mein Problem besteht darin, das in allen Beispielen die ich gefunden habe für die SEC_WINNT_AUTH_IDENTITY_W Members wirklich das Passwort, Dömaine und Benutzer angegeben werden muss was ich nicht kann und will. Wie muss ich denn den Code umbauen, dass er auch unter Win 2000 läuft?
HRESULT hres; // Step 1: -------------------------------------------------- // Initialize COM. ------------------------------------------ hres = CoInitializeEx (0, COINIT_MULTITHREADED); if ((hres != S_OK) && (hres != S_FALSE) && (hres != RPC_E_CHANGED_MODE)) { err.NewError (EXCEPTION, L"CoInitializeEx, WMIQuery"); return; } // Step 2: -------------------------------------------------- // Set general COM security levels -------------------------- // Note: If you are using Windows 2000, you need to specify - // the default authentication credentials for a user by using // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- // parameter of CoInitializeSecurity ------------------------ SEC_WINNT_AUTH_IDENTITY_W AuthIdentity; ZeroMemory(&AuthIdentity, sizeof(AuthIdentity)); AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; SOLE_AUTHENTICATION_LIST sal = { 0 }; SOLE_AUTHENTICATION_INFO sai = { 0 }; sal.cAuthInfo = 1; sal.aAuthInfo = &sai; sai.dwAuthnSvc = RPC_C_AUTHN_WINNT; sai.dwAuthzSvc = RPC_C_AUTHZ_NONE; sai.pAuthInfo = &AuthIdentity; hres = CoInitializeSecurity ( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation &sal, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); if ((hres != S_OK) && (hres != RPC_E_TOO_LATE)) { err.NewError (EXCEPTION, L"CoInitializeSecurity failed, WMIQuery"); CoUninitialize (); return; } // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- IWbemLocator *pLoc = NULL; hres = CoCreateInstance (CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*) &pLoc); if (FAILED (hres)) { err.NewError (EXCEPTION, L"CoCreateInstance failed, WMIQuery"); CoUninitialize (); return; } // Step 4: ----------------------------------------------------- // Connect to WMI through the IWbemLocator::ConnectServer method IWbemServices *pSvc = NULL; // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. hres = pLoc->ConnectServer ( L"ROOT\\CIMV2", // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (e.g. Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED (hres)) { err.NewError (EXCEPTION, L"ConnectServer failed, WMIQuery"); pLoc->Release (); CoUninitialize (); return; } // Step 5: -------------------------------------------------- // Set security levels on the proxy ------------------------- hres = CoSetProxyBlanket ( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED (hres)) { err.NewError (EXCEPTION, L"CoSetProxyBlanket failed, WMIQuery"); pSvc->Release (); pLoc->Release (); CoUninitialize (); return; } // Step 6: -------------------------------------------------- // Use the IWbemServices pointer to make requests of WMI ---- IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery ( L"WQL", L"SELECT CSName, Caption, ServicePackMajorVersion, Version, LastBootUpTime " \ L"FROM Win32_OperatingSystem", WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator ); if (FAILED (hres)) { err.NewError (EXCEPTION, L"ExecQuery failed, WMIQuery"); pSvc->Release (); pLoc->Release (); CoUninitialize (); return; }
-
Also warum es nicht funktioniert habe ich mittlerweile herausgefunden. Es liegt gar nicht an CoInitializeSecurity, dort muss keine SOLE_AUTHENTICATION_LIST als Parameter übergeben werden. Auch wird CoSetProxyBlanket zumindest in meinem Fall überhaupt nicht benötigt. Das Problem lag an dem CLSID_WbemLocator Parameter bei CoCreateInstance. Ändert man dies zu CLSID_WbemAdministrativeLocator funktioniert es unter Windows 2000 mit Administratorrechten und unter Windows XP und Vista auch mit eingeschränkten Rechten. Falls mir jemand sagen könnte wie ich es unter Windows 2000 mit eingeschränkten Rechten (Normaler Benutzer, nicht Admin) zum laufen bekomme, wäre ich überglücklich...