bool Reboot(){DWORD Res=0;IWbemLocator *locator=NULL;IWbemServices *pSvc=NULL;IWbemClassObject *object=NULL;COAUTHIDENTITY *userAcct=NULL;Res=CoInitializeEx(0,COINIT_MULTITHREADED); if(Res!=NO_ERROR) { qDebug()<<"Error CoInitialize"; return 0; } Res=CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL ); if(Res!=NO_ERROR) { qDebug()<<"Error CoInitializeSecurity"; return 0; } Res=CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&locator ); if(Res!=NO_ERROR) { qDebug()<<"Error CoCreateInstance"; CoUninitialize(); return 0; } Res=locator->ConnectServer(_bstr_t(L"\\\\server\\root\\cimv2"), //вместо server - имя удаленного компа или его IP _bstr_t(L"Admin"), //логин _bstr_t(L"1"), //пароль NULL, NULL, NULL, NULL, &pSvc ); if(Res!=NO_ERROR) { qDebug()<<"Error ConnectServer"; locator->Release(); CoUninitialize(); return 0; } userAcct=new COAUTHIDENTITY; ZeroMemory(userAcct,sizeof(COAUTHIDENTITY)); QString *login = new QString("Admin"); //входные параметры QString *password = new QString("1"); std::wstring log = login->toStdWString(); std::wstring pas = password->toStdWString(); userAcct->User = (USHORT*)log.c_str(); userAcct->UserLength = login->length(); userAcct->Password = (USHORT*)pas.c_str();; userAcct->PasswordLength = password->length(); userAcct->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; Res = CoSetProxyBlanket( pSvc, RPC_C_AUTHN_DEFAULT , RPC_C_AUTHZ_DEFAULT, NULL, RPC_C_AUTHN_LEVEL_CALL , RPC_C_IMP_LEVEL_IMPERSONATE, userAcct, EOAC_NONE ); if(Res!=NO_ERROR) { qDebug()<<"Error CoSetProxyBlanket"; pSvc->Release(); locator->Release(); CoUninitialize(); return 0; }IEnumWbemClassObject *pEnumerator = NULL;Res = pSvc->ExecQuery(_bstr_t(L"WQL"), _bstr_t(L"SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator); if(Res!=NO_ERROR) { qDebug()<<"Error ExecQuery"; pSvc->Release(); locator->Release(); CoUninitialize(); return 0; } Res = CoSetProxyBlanket( pEnumerator, RPC_C_AUTHN_DEFAULT , RPC_C_AUTHZ_DEFAULT, NULL, RPC_C_AUTHN_LEVEL_CALL , RPC_C_IMP_LEVEL_IMPERSONATE, userAcct, EOAC_NONE ); if(FAILED(Res)) { pEnumerator->Release(); pSvc->Release(); locator->Release(); CoUninitialize(); return 0; }IWbemClassObject *System = NULL;ULONG Returned = 0;Res = pEnumerator->Next(WBEM_INFINITE, 1, &System, &Returned);if(Res != NO_ERROR){ pEnumerator->Release(); pSvc->Release(); locator->Release(); CoUninitialize(); return 0;}VARIANT var;ZeroMemory(&var,sizeof(VARIANT));Res = System->Get(L"Name",0,&var,NULL,NULL);if(Res != NO_ERROR && var.vt != VT_BSTR){ System->Release(); pEnumerator->Release(); pSvc->Release(); locator->Release(); CoUninitialize(); return 0;}QString name(_com_util::ConvertBSTRToString(var.bstrVal)); QString objPath("Win32_OperatingSystem.Name=\""+ name +"\"");objPath.replace(QString("\\"),QString("\\\\"));Res = pSvc->ExecMethod(_com_util::ConvertStringToBSTR(objPath.toStdString().c_str()),_bstr_t(L"Shutdown"),0,NULL,NULL,NULL,NULL);if(Res != NO_ERROR){ SysFreeString(var.bstrVal); System->Release(); pEnumerator->Release(); pSvc->Release(); locator->Release(); CoUninitialize(); return 0; }SysFreeString(var.bstrVal);System->Release();pEnumerator->Release();pSvc->Release();locator->Release();CoUninitialize();return 1;}
#include <windows.h>#include <comutil.h>#define _WIN32_DCOM#include <comdef.h>#include <Wbemidl.h>#pragma comment(lib, "wbemuuid.lib")