B
Brian Worth
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0
was able to shut down or restart the (windows XP) machine using a series of
API calls. (Getlasterror, GetCurrentProcess, OpenProcessToken,
LookupPrivilegeValue, AdjustTokenPrivilegese, ExitWindowsEx.
I am trying to avoid using any API calls if possible and to use managed code
instead. I couldn't find any easy way of doing this but searching the
Internet with Google I found the following code for C:
using System;
using System.Management;
using System.Runtime.InteropServices;
// Shutdown a system using WMI (management classes)
public class Wmis {
public static void Main() {
ManagementObject o;
ManagementPath path = new ManagementPath( );
path.Server = "xxxxxx"; // your server name here
path.NamespacePath = @"root\CIMV2";
// check your boot.ini file for the correct operating system entry & boot
partition
path.RelativePath = @"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINNT|\\Device\\Harddisk0\\Partition1""";
o = new ManagementObject(path);
ManagementBaseObject inParams = null;
bool EnablePrivileges = o.Scope.Options.EnablePrivileges; // set required
privileges
o.Scope.Options.EnablePrivileges = true;
// Invoke method (shutdown or reboot)
ManagementBaseObject outParams = o.InvokeMethod("Shutdown", inParams, null);
<====================
o.Scope.Options.EnablePrivileges = EnablePrivileges; // restore privs
(optional)
I don't know C at all but I tried nevertheless to convert it to VB .NET 2002
as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim o As New ManagementObject()
Dim BWManagementPath As New ManagementPath()
Dim inParams As System.Management.ManagementBaseObject()
Dim OptParams As InvokeMethodOptions
Dim OutParams As ManagementBaseObject()
Dim enableprivileges As Boolean
BWManagementPath.Server = "DESKTOP"
BWManagementPath.NamespacePath = "root\CIMV2"
BWManagementPath.RelativePath =
"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Partition1"""
o = New ManagementObject(BWManagementPath)
EnablePrivileges = o.Scope.Options.EnablePrivileges
o.Scope.Options.EnablePrivileges = True
OutParams = o.InvokeMethod("Shutdown", inParams)
<==========================
o.Scope.Options.EnablePrivileges = enableprivileges ' restore privs
(optional)
End Sub
When I tried to convert the outParams=o.InvokeMethod line, I couldn't find a
way of specifying "Null" without the compiler objecting with the following
message:
"\Shutdown Test\Form1.vb(73): Overload resolution failed because no
accessible 'InvokeMethod' can be called with these arguments:
'Public Overloads Function InvokeMethod(methodName As String,
inParameters As System.Management.ManagementBaseObject, options As
System.Management.InvokeMethodOptions) As
System.Management.ManagementBaseObject': Value of type '1-dimensional array
of System.Management.ManagementBaseObject' cannot be converted to
'System.Management.ManagementBaseObject'.
'Public Overloads Function InvokeMethod(methodName As String,
inParameters As System.Management.ManagementBaseObject, options As
System.Management.InvokeMethodOptions) As
System.Management.ManagementBaseObject': Value of type 'String' cannot be
converted to 'System.Management.InvokeMethodOptions'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type 'String' cannot be converted to
'System.Management.ManagementOperationObserver'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type '1-dimensional array of
System.Management.ManagementBaseObject' cannot be converted to 'String'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type 'String' cannot be converted to '1-dimensional
array of System.Object'."
So I left the "null" bit off and ran the program.
When I do this, the outparams=o.InvokeMethod line fails with
"Privilege Not Held"
Any help would be greatefully received.
was able to shut down or restart the (windows XP) machine using a series of
API calls. (Getlasterror, GetCurrentProcess, OpenProcessToken,
LookupPrivilegeValue, AdjustTokenPrivilegese, ExitWindowsEx.
I am trying to avoid using any API calls if possible and to use managed code
instead. I couldn't find any easy way of doing this but searching the
Internet with Google I found the following code for C:
using System;
using System.Management;
using System.Runtime.InteropServices;
// Shutdown a system using WMI (management classes)
public class Wmis {
public static void Main() {
ManagementObject o;
ManagementPath path = new ManagementPath( );
path.Server = "xxxxxx"; // your server name here
path.NamespacePath = @"root\CIMV2";
// check your boot.ini file for the correct operating system entry & boot
partition
path.RelativePath = @"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINNT|\\Device\\Harddisk0\\Partition1""";
o = new ManagementObject(path);
ManagementBaseObject inParams = null;
bool EnablePrivileges = o.Scope.Options.EnablePrivileges; // set required
privileges
o.Scope.Options.EnablePrivileges = true;
// Invoke method (shutdown or reboot)
ManagementBaseObject outParams = o.InvokeMethod("Shutdown", inParams, null);
<====================
o.Scope.Options.EnablePrivileges = EnablePrivileges; // restore privs
(optional)
I don't know C at all but I tried nevertheless to convert it to VB .NET 2002
as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim o As New ManagementObject()
Dim BWManagementPath As New ManagementPath()
Dim inParams As System.Management.ManagementBaseObject()
Dim OptParams As InvokeMethodOptions
Dim OutParams As ManagementBaseObject()
Dim enableprivileges As Boolean
BWManagementPath.Server = "DESKTOP"
BWManagementPath.NamespacePath = "root\CIMV2"
BWManagementPath.RelativePath =
"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Partition1"""
o = New ManagementObject(BWManagementPath)
EnablePrivileges = o.Scope.Options.EnablePrivileges
o.Scope.Options.EnablePrivileges = True
OutParams = o.InvokeMethod("Shutdown", inParams)
<==========================
o.Scope.Options.EnablePrivileges = enableprivileges ' restore privs
(optional)
End Sub
When I tried to convert the outParams=o.InvokeMethod line, I couldn't find a
way of specifying "Null" without the compiler objecting with the following
message:
"\Shutdown Test\Form1.vb(73): Overload resolution failed because no
accessible 'InvokeMethod' can be called with these arguments:
'Public Overloads Function InvokeMethod(methodName As String,
inParameters As System.Management.ManagementBaseObject, options As
System.Management.InvokeMethodOptions) As
System.Management.ManagementBaseObject': Value of type '1-dimensional array
of System.Management.ManagementBaseObject' cannot be converted to
'System.Management.ManagementBaseObject'.
'Public Overloads Function InvokeMethod(methodName As String,
inParameters As System.Management.ManagementBaseObject, options As
System.Management.InvokeMethodOptions) As
System.Management.ManagementBaseObject': Value of type 'String' cannot be
converted to 'System.Management.InvokeMethodOptions'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type 'String' cannot be converted to
'System.Management.ManagementOperationObserver'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type '1-dimensional array of
System.Management.ManagementBaseObject' cannot be converted to 'String'.
'Public Overloads Sub InvokeMethod(watcher As
System.Management.ManagementOperationObserver, methodName As String, args()
As Object)': Value of type 'String' cannot be converted to '1-dimensional
array of System.Object'."
So I left the "null" bit off and ran the program.
When I do this, the outparams=o.InvokeMethod line fails with
"Privilege Not Held"
Any help would be greatefully received.