A problem about shutdown the window programming by wmi

J

Jet

Hi all,
I want to shutdown or reboot or logoff my window by my programme.And when I
connect to my machine, and then run ManagementObjectSearcher.Get() method it
throw me an exception mean "Cannot use the user certify to connect local
connection".But actually my user name and password for login the window are
correct.How can I solve this problem?
How can I shutdown,reboot or logoff my system by programming?
And I had use the window API ExitWindowsEx() to do the same things,But
unluckly,this method not work in my system.And my system is Windows2003.And
I use the api method like this:

// use API ExitWindowsEx()
[DllImport("user32")] public static extern int ExitWindowsEx(int uFlags, int
dwReserved) ;


public static void DoAPI(int uFlag)
{
ExitWindowsEx(uFlag,_dwReserved);

}
What's wrong with my code?

// use WMI
public class WMIHandle
{
public static void WMI_Do(Win32ShutdownFlag wndFlag,String
_MachineName,String _AdminUser,String _AdminPassword)
{
ConnectionOptions _options=new ConnectionOptions();
_options.Impersonation = ImpersonationLevel.Impersonate ;
_options.EnablePrivileges = true;
_options.Username = _AdminUser;
_options.Password = _AdminPassword ;
ManagementScope mScope=new
ManagementScope("\\"+_MachineName+@"\root\cimv2",_options);
SelectQuery oQuery=new SelectQuery("SELECT * FROM
win32_operatingsystem");
ManagementObjectSearcher oResults=new
ManagementObjectSearcher(mScope,oQuery);

// PropertyData oProperty;
object[] myParams=new object[]{wndFlag,0};
foreach(ManagementObject oItem in oResults.Get())
{
oItem.InvokeMethod("Win32Shutdown",myParams);

}

}
}
public enum Win32ShutdownFlag
{
Logoff = 0,
Shutdown = 1,
Reboot = 2,
ForceShutdown=5,
ForceLogOff = 4 ,
ForceReboot = 6 ,
PowerDown = 8 ,
ForcePowerDown = 12
}
 
W

Willy Denoyette [MVP]

_
There is no need to use ExitWindowsEx.

Willy.

Jet said:
Hi all,
I want to shutdown or reboot or logoff my window by my programme.And when
I connect to my machine, and then run ManagementObjectSearcher.Get()
method it throw me an exception mean "Cannot use the user certify to
connect local connection".But actually my user name and password for login
the window are correct.How can I solve this problem?
How can I shutdown,reboot or logoff my system by programming?
And I had use the window API ExitWindowsEx() to do the same things,But
unluckly,this method not work in my system.And my system is
Windows2003.And I use the api method like this:

// use API ExitWindowsEx()
[DllImport("user32")] public static extern int ExitWindowsEx(int uFlags,
int dwReserved) ;


public static void DoAPI(int uFlag)
{
ExitWindowsEx(uFlag,_dwReserved);

}
What's wrong with my code?

// use WMI
public class WMIHandle
{
public static void WMI_Do(Win32ShutdownFlag wndFlag,String
_MachineName,String _AdminUser,String _AdminPassword)
{
ConnectionOptions _options=new ConnectionOptions();
_options.Impersonation = ImpersonationLevel.Impersonate ;
_options.EnablePrivileges = true;
_options.Username = _AdminUser;
_options.Password = _AdminPassword ;
ManagementScope mScope=new
ManagementScope("\\"+_MachineName+@"\root\cimv2",_options);
SelectQuery oQuery=new SelectQuery("SELECT * FROM
win32_operatingsystem");
ManagementObjectSearcher oResults=new
ManagementObjectSearcher(mScope,oQuery);

// PropertyData oProperty;
object[] myParams=new object[]{wndFlag,0};
foreach(ManagementObject oItem in oResults.Get())
{
oItem.InvokeMethod("Win32Shutdown",myParams);

}

}
}
public enum Win32ShutdownFlag
{
Logoff = 0,
Shutdown = 1,
Reboot = 2,
ForceShutdown=5,
ForceLogOff = 4 ,
ForceReboot = 6 ,
PowerDown = 8 ,
ForcePowerDown = 12
}


Remove the ConnectionOptions stuff, you can't authenticate when connecting
to the local machine.
All you need is...

ManagementScope(@"\root\cimv2");
....

Willy.
 
Top