Languages :: General :: Shutting down Win XP |
|||
| By: omuyelijah |
Date: 03/01/2007 13:57:47 |
Points: 20 | Status: Answered Quality : Excellent |
|
Hello VGR, HAPPY NEW YEAR 2007. Have 2 questions. 1. Recently, wrote a program in visual c++ 2005 express edition to access all working processes on my system (and could even start and stop processes like Task Manager). I want 2 shut down my WIN XP O.S. How do I do it? I thought about stopping the winlogon.exe process through my program. 2. I want to be able to detect events like having a flash drive plugged on my system. I searched d docs but the best close namespaces and classes I could get are Microsoft::Win32::SystemEvents, System::Diagnostics, System::Management and d SerialPort class. AUREVOIR MONSIEUR AND HAVE A BLISSFUL YEAR. |
|||
| By: VGR | Date: 03/01/2007 18:41:21 | Type : Comment |
|
| for 1: I think you could work on the "windows workstation" process, but could also as well send a WM_SHUTDOWN message to the Windows message queue... for 2: no solution, but basically USB inserts are readable via the evenlog (eventvwr.exe) so the trace calls could be intercepted (some Int derivation in the classical man-in-the-middle way), and also kind-of "serial" so perhaps a classical API on Serial bus(es) would provide you the functionality. Alternatively, you could just poll periodically the drive letters... to see if a new drive wasn't inserted. have a nice new year too ;-) |
|||
| By: omuyelijah | Date: 15/01/2007 17:08:09 | Type : Comment |
|
| Hello VGR, Posted some replies but shocking dat I can't find any here after logging in. Don't know why sha. Well, I can now detect events effectively using WMI programming and the System::Management namespace of .NET. I could also start and stop the windows calculator. However, couldn't still shutdown my system with it. The code doesn't shout any exceptions when run(it did b4 but I edited code and it stopped). I also took care of Privileges i.e. SeShutdownPrivilege. Below is d code snippet all variables declared appropriately. System::Management::ManagementObjectSearcher^ obs; System::Management::ManagementClass^ mc_s; // mc_s has managementQuery="select * from Win32_OperatingSystem" // shutd is d name of the button to begin shutdown . . . private: System::Void Shutd(System::Object^ Sender, System::EventArgs^ e){ array<System::Object^>^ myparam=nullptr; System::Management::ManagementObjectCollection^ moc=obs.Get(); try{ /* dis code is similar to the one dat worked 4 starting and stopping calc.exe */ for each (System::Management::ManagementObject^ mo in moc){ mc_s->Invoke(gcnew System::Management::ManagementOperationsObserver(),"Shutdown",myparam); } } catch(Exception^ e) { t_error->Text = e->Message; } } I've done everything possible. Changed somethings and it validated that I'm on d right track but I guess somethings missing. Can You pls help me out? Maranatha. |
|||
| By: omuyelijah | Date: 16/01/2007 13:57:20 | Type : Comment |
|
| Hello, 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 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}; //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. Don't set dis 2 false anywhere within this handler else it wont work */ mc_s->Scope->Options->EnablePrivileges=true; /* invoke Win32Shutdown method asynchronously */ 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); } I give 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). 4. Lastly, like I also saw in another forum, I really won't agree dat there is a bug in .NET 4 permissions. |
|||
| By: omuyelijah | Date: 16/01/2007 13:57:21 | Type : Comment |
|
| Hello, 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 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}; //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. Don't set dis 2 false anywhere within this handler else it wont work */ mc_s->Scope->Options->EnablePrivileges=true; /* invoke Win32Shutdown method asynchronously */ 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); } I give 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). 4. Lastly, like I also saw in another forum, I really won't agree dat there is a bug in .NET 4 permissions. |
|||
| By: omuyelijah | Date: 16/01/2007 13:57:22 | Type : Answer |
|
| Hello, 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 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}; //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. Don't set dis 2 false anywhere within this handler else it wont work */ mc_s->Scope->Options->EnablePrivileges=true; /* invoke Win32Shutdown method asynchronously */ 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); } I give 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). 4. Lastly, like I also saw in another forum, I really won't agree dat there is a bug in .NET 4 permissions. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








