473,241 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Why does this WMI Invoke not work?

Hi,

can someone please tell me why the **** this does not
work as expected:
First at all, thats what a WMI parameter looks like:

public class WMIParameter {

private string pName;
private Type pType;
private object pValue;

public WMIParameter(string Name, Type Type, object Value) {
this.pName = Name;
this.pValue = Value;
this.pType = Type;
}
public Type Type {
get {
return this.pType;
}
}
public string Name
{
get
{
return this.pName;
}
}
public object Value
{
get
{
return this.pValue;
}
}
}
private static ManagementBaseObject
ExecuteWMIMethodOnRemoteMachine_ThrowsException(st ring TargetMachine,
string ManagementPathString,
string MethodName,
WMIParameter[] inParams, string UserName, string Password)
{
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
if (UserName != string.Empty && Password != string.Empty)
{
connOptions.Username = UserName;
connOptions.SecurePassword = CreateSecureString(Password);
}
ManagementScope manScope = new
ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", TargetMachine),
connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath(ManagementPathString);
ManagementClass processClass = new ManagementClass(manScope, managementPath,
objectGetOptions);
ManagementBaseObject Params = processClass.GetMethodParameters(MethodName);
foreach (WMIParameter wmip in inParams)
{
Params[wmip.Name] = Convert.ChangeType(wmip.Value,wmip.Type);
}
ManagementBaseObject outParams;
if (inParams == null)
{
outParams = processClass.InvokeMethod(MethodName, null, null);
}
else
{
outParams = processClass.InvokeMethod(MethodName, Params, null);
}
return outParams;
}
catch (Exception e)
{
throw e;
}
}

I always get a error saying "The Methods Parameter are invalid".
Thats how i call this:

WMIParameter[] wmip = new WMIParameter[2];
wmip[0] = new WMIParameter("Reserved", typeof(Int32), 0);
wmip[1] = new WMIParameter("Flags", typeof(Int32), (Int32)ew);

ExecuteWMIMethodOnRemoteMachine_ThrowsException(Ta rgetMachineName,
"Win32_OperatingSystem",
"Win32Shutdown",
wmip,
TargetMachineUserName,
TargetMachineUserPassword);

I want to hold the call to the WMI methods as transparent as possible, so
hats why i wrote this function. Please can you show me, or if possible
correct this function,...
TIA,...

Regards

Kerem
--
-----------------------
Beste Grsse / Best regards / Votre bien devoue
Kerem Gmrkc
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Jun 27 '08 #1
1 4411
"Kerem Gümrükcü" wrote:
Hi,

can someone please tell me why the **** this does not
work as expected:

private static ManagementBaseObject
ExecuteWMIMethodOnRemoteMachine_ThrowsException(st ring TargetMachine,
string ManagementPathString,
string MethodName,
WMIParameter[] inParams, string UserName, string Password)
{
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
if (UserName != string.Empty && Password != string.Empty)
{
connOptions.Username = UserName;
connOptions.SecurePassword = CreateSecureString(Password);
}
ManagementScope manScope = new
ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", TargetMachine),
connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath(ManagementPathString);
ManagementClass processClass = new ManagementClass(manScope, managementPath,
objectGetOptions);
ManagementBaseObject Params = processClass.GetMethodParameters(MethodName);
foreach (WMIParameter wmip in inParams)
{
Params[wmip.Name] = Convert.ChangeType(wmip.Value,wmip.Type);
}
ManagementBaseObject outParams;
if (inParams == null)
{
outParams = processClass.InvokeMethod(MethodName, null, null);
}

Win32_OperatingSystem.Win32Shutdown() is an instance method and you are
invoking it on a class (processClass). Here is a sample:
ManagementClass OSClass =
new ManagementClass("Win32_OperatingSystem");

ManagementBaseObject inParams =
OSClass.Methods["Win32Shutdown"].InParameters;

inParams["Flags"] = 0;

// This line will throw invalid method parameters exception:
//
//OS.InvokeMethod("Win32Shutdown", inParams, null);
//

foreach (ManagementObject OSInstance in OSClass.GetInstances())
{
OSInstance.InvokeMethod("Win32Shutdown", inParams, null);
}
This code works for me locally to log off the current user. There is no need
to set the Reserved parameter as it defaults to 0 and is ignored. I don't
know if there are other issues with your code.

Hope this helps.

--
urkec
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Prabhat | last post by:
Hi All, The "Session_OnStart" event doesnot fire if the website has .htm file as the startup document. If I change the extension of the default document from ..htm to .asp then I can see the...
11
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
1
by: Tommy Svensson \(InfoGrafix\) | last post by:
Hi, Can I use P/Invoke to obtain a C++ class type contained in a dll? I know how to get structs and how to pass classes as parameters to native code but how do I get C++ unmanaged classes from a...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i dont want to filter the return value of the method, i have to pass a new instance of the...
1
by: Steve | last post by:
I need to update my UI from a Process or worker thread. I did some readinf and basically ended up adapting an MS example to fot my needs. It all made sense until I tried it :) My process...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
2
by: Adam Benson | last post by:
Hi, If you're not on the UI thread - responding to a network event, say - and you want to read the SelectedIndex of a combo box, do you have to do an Invoke? Or is it just a simple data read...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.