• 0

[C#] System.Management (Win32_Process)


Question

public void Create(string Name)
        {
            ManagementPath path = new ManagementPath("Win32_Process");
            ManagementClass processClass = new ManagementClass(_mc.Scope, path, null);

            //Gets Path and Executables name.
            string[] splitPath = Name.Split('\\');
            string exeName = splitPath[splitPath.Length - 1];
            string exePath = null;

            ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

            if (splitPath.Length > 1)
                exePath = Name.Substring(0, Name.LastIndexOf('\\'))+"\\";

            //Parameters for creation of process.
            inParams["CommandLine"] = exeName;
            inParams["CurrentDirectory"] = exePath;

            //Invoke Method.
            ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
            Object returnValue = outParams["ReturnValue"];
            
            switch(int.Parse(returnValue.ToString()))
            {
                case 0: break;
                case 2: throw new System.Security.SecurityException("Access denied");
                case 3: throw new System.Security.SecurityException("Insufficient privilege");
                case 9: throw new Exception(string.Format("Path not found \"{0}\"",exePath));
                case 21: throw new Exception("Invalid parameter");
                default : throw new Exception("Unknown failure");
            }

            //Reload processes.
            getProcesses();
        }

This code is fine for creating processes such as notepad, cmd or calc but as soon as i try and access a path such as "c:\program files\internet explorer\iexplorer.exe" it has a path not found error. Any ideas?

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Interesting code :)

As for your problem, what occured to me is that "Program Files" has a space in it, but accessing Notepad would access e.g. C:\Windows\Notepad.exe. Is it one of those paths that need to be enclosed with quotes?

Link to comment
Share on other sites

  • 0
Interesting code :)

As for your problem, what occured to me is that "Program Files" has a space in it, but accessing Notepad would access e.g. C:\Windows\Notepad.exe. Is it one of those paths that need to be enclosed with quotes?

586359842[/snapback]

Sadly not, I should have said that i've tried that. Still comes up with the same error. Thank you anyway :D

Link to comment
Share on other sites

  • 0
why don't you use the Process class?

586359880[/snapback]

I don't think it's possible to create processes remotely using the process class without having separate client/server apps. I may be wrong?

Link to comment
Share on other sites

  • 0
I don't think it's possible to create processes remotely using the process class without having separate client/server apps. I may be wrong?

586359902[/snapback]

yeah u're right :-S I didn't realise this code could do that

Link to comment
Share on other sites

  • 0

The scopes right, well I'm prety sure it is, because if I send the command to open notepad.exe through it will open fine. It just can't open anything else outside the c:\windows directory.

Link to comment
Share on other sites

  • 0

You see for your path for internet explorer, try this:

Instead of:

"c:\Program Files\Internet Explorer\IEXPLORE.EXE"

Try the following:

@"c:\Program Files\Internet Explorer\IEXPLORE.EXE"

I know you mentioned you had tried to solve the issues with the spaces in the name, but humour me on this by using the @ symbol at the beginning :)

Link to comment
Share on other sites

  • 0

Not that i think this is part of your issue (though it might just make for cleaner, easier to debug code) but can i ask why you didn't use the built-in methods for separating the filename out of a path?

As an example:

string strFullPath = @"c:\program files\internet explorer\iexplore.exe";
string strFileName = System.IO.Path.GetFileName(strFullPath); //  returns "iexplore.exe"
string strPath = System.IO.Path.GetDirectoryName(strFullPath); // returns "c:\program files\internet explorer\"

Link to comment
Share on other sites

  • 0
but can i ask why you didn't use the built-in methods for separating the filename out of a path?

LOL! because i'm abit abstract and don't think things through fully. :happy:

You see for your path for internet explorer, try this:

Instead of:

"c:\Program Files\Internet Explorer\IEXPLORE.EXE"

Try the following:

@"c:\Program Files\Internet Explorer\IEXPLORE.EXE"

I know you mentioned you had tried to solve the issues with the spaces in the name, but humour me on this by using the @ symbol at the beginning :)

586360529[/snapback]

Yeah tried this to, usualy i go with "\\" instead of @"\"

For the path i've tried.

"c:\\program files\\Internet Explorer\\"

"\"c:\\program files\\Internet Explorer\\\""

"'c:\\program files\\Internet Explorer\\'"

@"c:\Program Files\Internet Explorer\"

@"'c:\Program Files\Internet Explorer\'"

why don't you print out exe path and exe name right before you run the process so you can see what they are coming out as

Tested this to, prints out what it should.

Link to comment
Share on other sites

  • 0

Well the path to IE, if it's in a directory called "Program Files" (this depends on the language of Windows) on C:, it should definitely be in @"C:\Program Files\Internet Explorer\iexplore.exe". Case shouldn't matter. Seems to me the problem is something else than the path. :-/

Hmm, here's a guide to WMI and remote process execution in .NET that I found:

http://www.devcity.net/PrintArticle.aspx?ArticleID=144

It's for VB .NET, but consider the API is the same, you'll only have to put up with translating the syntax to C# in your head when reading. :p

Link to comment
Share on other sites

  • 0

I have the same problem:

//ConnectionOptions connOption = new ConnectionOptions();

//connOption.Username = "deldom"+@"\"+"deladmin2k";

//connOption.Password = "delhi2k";

//ManagementScope wmiScope = new ManagementScope(@"\\xpbuild",connOption);

ManagementScope wmiScope = new ManagementScope(@"\\xpbuild");

wmiScope.Connect();

ManagementClass process = new ManagementClass("Win32_Process");

process.Scope = wmiScope;

ManagementBaseObject inParams = process.GetMethodParameters("Create");

inParams["CommandLine"] = command + " " + argStr;

//inParams["CommandLine"] = @"C:\WINNT\NOTEPAD.EXE";

ManagementBaseObject outParams = process.InvokeMethod("Create", inParams, null);

err.Append("Command " + command);

err.Append("Argument " + argStr);

err.Append("returnValue = " + outParams["ReturnValue"]);

err.Append("Process ID = " + outParams["processId"]);

resultString = err.ToString();

- - - my result string always comes back with return value "3" ...

command = C:\Inetpub\wwwroot\BESAdmin\bin\BESUserAdminClient.exe

argStr = -set_password test -b XYZServer02 -u BlackBerryUser1@XYZServer02.na.usauser -n xyzserver03 -p Password123

ph8l: did you fixed this issue.

this same command i am able to use without any issue.

Edited by avasi
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.