Russian Qt Forum
Май 19, 2024, 13:02 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: 1 [2]   Вниз
  Печать  
Автор Тема: Как программно выключить компьютер  (Прочитано 16143 раз)
Пантер
Administrator
Джедай : наставник для всех
*****
Offline Offline

Сообщений: 5876


Жаждущий знаний


Просмотр профиля WWW
« Ответ #15 : Ноябрь 10, 2010, 11:16 »

Команда mmc, там добавляешь оснастку (не помню какую) и настраиваешь. Но это тебе уже не другой форум.
Записан

1. Qt - Qt Development Frameworks; QT - QuickTime
2. Не используйте в исходниках символы кириллицы!!!
3. Пользуйтесь тегом code при оформлении сообщений.
stump
Гость
« Ответ #16 : Ноябрь 10, 2010, 14:07 »

Код:

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;
}

Записан
iukash
Гость
« Ответ #17 : Ноябрь 10, 2010, 14:42 »

stump спасибо за код но проект не компилится, вот только часть ошибок
Цитировать
mainwindow.cpp: In member function 'bool MainWindow::Reboot()':

mainwindow.cpp:73: error: 'DWORD' was not declared in this scope

mainwindow.cpp:73: error: expected ';' before 'Res'

mainwindow.cpp:75: error: 'IWbemLocator' was not declared in this scope

mainwindow.cpp:75: error: 'locator' was not declared in this scope

mainwindow.cpp:76: error: 'IWbemServices' was not declared in this scope

mainwindow.cpp:76: error: 'pSvc' was not declared in this scope

mainwindow.cpp:77: error: 'IWbemClassObject' was not declared in this scope

mainwindow.cpp:77: error: 'object' was not declared in this scope

mainwindow.cpp:79: error: 'COAUTHIDENTITY' was not declared in this scope

mainwindow.cpp:79: error: 'userAcct' was not declared in this scope

mainwindow.cpp:81: error: 'Res' was not declared in this scope

mainwindow.cpp:81: error: 'COINIT_MULTITHREADED' was not declared in this scope

mainwindow.cpp:81: error: 'CoInitializeEx' was not declared in this scope

mainwindow.cpp:83: error: 'NO_ERROR' was not declared in this scope

mainwindow.cpp:92: error: 'RPC_C_AUTHN_LEVEL_DEFAULT' was not declared in this scope
Мне наверное какой то ашник подключить надо или что?
Записан
Пантер
Administrator
Джедай : наставник для всех
*****
Offline Offline

Сообщений: 5876


Жаждущий знаний


Просмотр профиля WWW
« Ответ #18 : Ноябрь 10, 2010, 14:47 »

#include <qt_windows.h>
Записан

1. Qt - Qt Development Frameworks; QT - QuickTime
2. Не используйте в исходниках символы кириллицы!!!
3. Пользуйтесь тегом code при оформлении сообщений.
iukash
Гость
« Ответ #19 : Ноябрь 10, 2010, 14:49 »

Добавил qt_windows.h ушел целый ряд проблем остались эти
Цитировать
mainwindow.cpp: In member function 'bool MainWindow::Reboot()':

mainwindow.cpp:76: error: 'IWbemLocator' was not declared in this scope

mainwindow.cpp:76: error: 'locator' was not declared in this scope

mainwindow.cpp:77: error: 'IWbemServices' was not declared in this scope

mainwindow.cpp:77: error: 'pSvc' was not declared in this scope

mainwindow.cpp:78: error: 'IWbemClassObject' was not declared in this scope

mainwindow.cpp:78: error: 'object' was not declared in this scope

mainwindow.cpp:80: error: 'COAUTHIDENTITY' was not declared in this scope

mainwindow.cpp:80: error: 'userAcct' was not declared in this scope

mainwindow.cpp:105: error: 'CLSID_WbemLocator' was not declared in this scope

mainwindow.cpp:108: error: 'IID_IWbemLocator' was not declared in this scope

mainwindow.cpp:120: error: '_bstr_t' was not declared in this scope

mainwindow.cpp:139: error: expected type-specifier before 'COAUTHIDENTITY'

mainwindow.cpp:139: error: expected ';' before 'COAUTHIDENTITY'

mainwindow.cpp:177: error: 'IEnumWbemClassObject' was not declared in this scope

mainwindow.cpp:177: error: 'pEnumerator' was not declared in this scope

mainwindow.cpp:180: error: 'WBEM_FLAG_RETURN_IMMEDIATELY' was not declared in this scope

mainwindow.cpp:180: error: 'WBEM_FLAG_FORWARD_ONLY' was not declared in this scope

mainwindow.cpp:215: error: 'System' was not declared in this scope

mainwindow.cpp:218: error: 'WBEM_INFINITE' was not declared in this scope

mainwindow.cpp:249: error: '_com_util' has not been declared

mainwindow.cpp:254: error: '_com_util' has not been declared
Записан
stump
Гость
« Ответ #20 : Ноябрь 10, 2010, 19:37 »

Виноват...забыл)))
Код:

#include <windows.h>
#include <comutil.h>

#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")


и wbemuuid.lib к проекту подключи...
Записан
iukash
Гость
« Ответ #21 : Ноябрь 11, 2010, 15:44 »

Блин, не работает! У меня Qt + MinGW думаю в этом проблема!
Записан
Страниц: 1 [2]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.047 секунд. Запросов: 19.