ELIJAH E.

Hello,

how can I shutdown windows safely using visual c++ 2005 express edition I'm considering stopping some processes. What helping namespaces and also raising events available



Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

nobugz

Use the ExitWindowsEx() API function if you want to shutdown Windows. Use Process.CloseMainWindow() to safely stop a running process. Or Process.Kill() if it isn't cooperating.





Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

EZAGA

Good.

However, I seem 2 be stuck here. How do I get the SE_SHUTDOWN_NAME privilege





Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

nobugz

Try this:

Control Panel + Administrative Tools + Local Security Settings. Select Local Policies + User Rights Assignment, "Shut down the system". Double-click, Add user.





Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

EZAGA

Hello,

Well, no choice but 2 show d code. Written in MS Visual C++ 2005 Edition. It starts and stops
Windows Calculator ( by clicking b_startp button). Intended 2 shutdown my pc also, but fails 2 do so.
It does not report any exception on clicking b_shutd(button)
(like the security-privilege exception and invalid parameter exception) at
this stage though it did b4 but only had 2 change some code.

Talking about d SE_SHUTDOWN_NAME or SeShutdownPrivilege issue, just figured out dat .Net now
like implements privileges as Permissions. I started .NET 2.0 configuration
(CONTROL PANEL + ADMINISTRATIVE TOOLS) and checked the permission my assembly. It showed Unrestricted but my code doesn't execute.

/*

All variables declared b4 use appropriately

*/


//start and stop Windows Calculator program
private: System::Void b_startp_Click(System::Object^ sender, System::EventArgs^ e) {

/* To start any program using d Create method, pass a
array parameter of dimension 4, with the value of the fisrt element (commandline parameter)
to be a string representation of the program. Give full path if necessary.
*/

array <System::Object^>^ myparam=gcnew array <System::Object^> (4);
myparam[0]="calc.exe";

try{
/*just only use the System::Management::ManagementClass instance and it works well */


mc->InvokeMethod(gcnew System::Management::ManagementOperationObserver(),"Create",myparam);
}


catch (Exception^ e) { t_error->Text = t_error->Text->Concat("MESSAGE: ",e->Message,"\r\n",e->StackTrace); }

}

//shutdown my pc

private: System::Void b_shutd_Click(System::Object^ sender, System::EventArgs^ e) {
//all have worked. this is d last arrow.

//define my parameters. the win32Shutdown method has 2 parameters, only one needed
//value of 1 meaning safe shutdown
array <System::Object^>^ myparam= gcnew array <System::Object^>(2);
myparam[0]=1;

//get an instance of Win32_OperatingSystem class into object collection
obs->Query= gcnew System::Management::ObjectQuery("select * from Win32_OperatingSystem");
System::Management::ManagementObjectCollection^ moc=obs->Get();


//just 2 be sure, I have the instance 4rm above, I get qualifiers for Win32Shutdown method 4 test
System::Management::MethodDataCollection^ mdc1=mc_s->Methods;
System::Management::QualifierDataCollection^ qdc1=mdc1["Win32Shutdown"]->Qualifiers;

try{

for each (System::Management::ManagementObject^ mor in moc) {

for each(System::Management::QualifierData^ qd in qdc1){
if (qd->Name == "Privileges") {
//set MappingStrings properties
mc_s->Methods["Win32Shutdown"]->Qualifiers["MappingStrings"]->Value="ExitWindowsEx";

//chose 2 use ManagementClass instead of ManagementObjectinstance bcos
//it worked 2 start and stop calc.exe
mc_s->InvokeMethod(gcnew System::Management::ManagementOperationObserver(),"Win32Shutdown",myparam);
/* end if */ } }


/* end loop mor */ }

/* end try */ }

catch(Exception^ e)
{ t_error->Text = t_error->Text->Concat("MESSAGE: ",e->Message,"\r\n","SOURCE: ",e->StackTrace); }

}





Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

nobugz

I don't know about WMI, its got its own idea about permissions. Why don't you just call ExitWindowEx() You'll have to #include <windows.h>





Re: Visual C++ Express Edition SHUTTING DOWN WINDOWS

EZAGA


I never knew I was so close to d answer. I made a few changes to code as follows and then I could
Shutdown and reboot my system. Here it is (written in MS Visual C++ 2005 Express Edition)

1. enabled privileges;
2. used instance of System::Management::ManagementObject class instead of
System::Management::ManagementClass
3. Redefined parameters

4. When I ran d code on my system, I had to click twice b4 shutdown began. I think this might happen 2 anyone however just put d code in a loop to run twice and everything works.

Here is d code altogether

/* define my parameters for Win32Shutdown method.
* {1,nullptr} means shutdown
* {2, nullptr} means reboot
* etc
*/
array <System::Object^>^ myparam= {1,nullptr};

for (int count=0; count < 2; count++){

//object searcher should return collection of objects of d specified query
obs->Query= gcnew System::Management::ObjectQuery("select * from Win32_OperatingSystem");
System::Management::ManagementObjectCollection^ moc=obs->Get();


try{

for each (System::Management::ManagementObject^ mor in moc) {

//enable privileges 4 my assembly
mc_s->Scope->Options->EnablePrivileges=true;

//invoke Win32Shutdown method as usual
mor->InvokeMethod(gcnew System::Management::ManagementOperationObserver(),"Win32Shutdown",myparam );

/* end loop (mor in moc) */ }

/* end try */ }

catch(Exception^ e)
{ t_error->Text = t_error->Text->Concat("MESSAGE: ",e->Message,"\r\n","SOURCE: ",e->StackTrace); }

/* end of count loop */}


I give a some advice.

1. if you've .NET framework 2.0, check d permissions available 4 ur assembly(i.e. your program
or executable).How
Control panel + Administrative Tools + .NeT Framework configuration 2.0
If it reports 'Unrestricted', ur safe 2 go on else u have 2 enable it for d process.

2. I saw a few codes over d internet dat I learnt 4rm particularly one in VB.net. d programmer
seemed 2 enable and then disable privileges. This I think will not work cos I tried it and it didn't
.He/she would have 2 enable privileges once and leave it like dat.

mc_s->Scope->Options->EnablePrivileges=true;

3. Thirdly, of all d InvokeMethods available, I found only 2 useful and co-operating, i.e.
the one of four parameters and the other of three parameters (d one used above).