none
WMI: InvokeMethod: Terminate RRS feed

  • Question

  • Hi,

    I want to kill a remote process. I have the following code and it is bascially working. But I experience differences between my code and Windows Taskmanager or pskill. Taskmanager and pskill work as expected: The process is killed and a parent process (the user GUI) takes control. Then I use my code with "InvokeMethod" and Terminate, it reacts different. The process is also killed, but the control is not given to the user GUI, but it is given back to the parent process. It is like this:
    myprocess -> called by parent process -> called by user GUI
    (the user GUI is of course also a parent process)
    Here is the code:
    ConnectionOptions connectoptions = new ConnectionOptions();
    connectoptions.Username = userName;
    connectoptions.Password = userPassword;
    connectoptions.EnablePrivileges = true;
    ManagementScope scope = new ManagementScope(@"\\" + serverName + @"\root\cimv2", connectoptions);
    scope.Connect();
    SelectQuery query = new SelectQuery("SELECT * FROM Win32_Process where name = 'APPLICATIONNAME.EXE'");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
    {
        if (searcher != null)
        {
            collection = searcher.Get();

            foreach (ManagementObject process in searcher.Get())
            {
                ...
            }
        }
    }

    ----
    And the code for InvokeMethod:
    foreach (ManagementObject mo in collection)
    {
        int processId = Convert.ToInt16(mo["ProcessId"]);

        if (processId == Convert.ToInt16(myProcessId) && mo["Name"].ToString().ToLower() == "application.exe")
        {
            mo.InvokeMethod("Terminate", null);
            break;
        }
    }

    How can I get the "Terminate" to work same a Windows Taskmanager or pskill. I guess it might be in the Parameters of the InvokeMethod, that is "null" in my code right now.
    Any help would be really appreciated. Thanks a lot!!


    Viele Grüße / Best regards CSN22

    Tuesday, December 30, 2014 10:24 AM

All replies

  • Hi,

    I want to kill a remote process. I have the following code and it is bascially working. But I experience differences between my code and Windows Taskmanager or pskill. Taskmanager and pskill work as expected: The process is killed and a parent process (the user GUI) takes control. Then I use my code with "InvokeMethod" and Terminate, it reacts different. The process is also killed, but the control is not given to the user GUI, but it is given back to the parent process. It is like this:
    myprocess -> called by parent process -> called by user GUI
    (the user GUI is of course also a parent process)
    Here is the code:
    ConnectionOptions connectoptions = new ConnectionOptions();
    connectoptions.Username = userName;
    connectoptions.Password = userPassword;
    connectoptions.EnablePrivileges = true;
    ManagementScope scope = new ManagementScope(@"\\" + serverName + @"\root\cimv2", connectoptions);
    scope.Connect();
    SelectQuery query = new SelectQuery("SELECT * FROM Win32_Process where name = 'APPLICATIONNAME.EXE'");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
    {
        if (searcher != null)
        {
            collection = searcher.Get();

            foreach (ManagementObject process in searcher.Get())
            {
                ...
            }
        }
    }

    ----
    And the code for InvokeMethod:
    foreach (ManagementObject mo in collection)
    {
        int processId = Convert.ToInt16(mo["ProcessId"]);

        if (processId == Convert.ToInt16(myProcessId) && mo["Name"].ToString().ToLower() == "application.exe")
        {
            mo.InvokeMethod("Terminate", null);
            break;
        }
    }

    How can I get the "Terminate" to work same a Windows Taskmanager or pskill. I guess it might be in the Parameters of the InvokeMethod, that is "null" in my code right now.
    Any help would be really appreciated. Thanks a lot!!


    Viele Grüße / Best regards CSN22

    • Merged by Kristin Xie Wednesday, December 31, 2014 6:16 AM the same issue
    Tuesday, December 30, 2014 10:30 AM
  • Hello,

    >>The process is also killed, but the control is not given to the user GUI, but it is given back to the parent process.

    After testing your provided code, as you mentions, it worked as expected, however, I am a bit confused what you mean about “the control”, please provide us a detail clarification so that we could help you better.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 31, 2014 6:55 AM
    Moderator
  • Sure, I will try to give more information.

    The workflow is as following:
    - all processes are executed synchronously; the current process always wait until the called process is finished
    - the user clicks a button in the Client-GUI to start a job on the server (Client GUI is level 1)
    - this job starts a new process on the server (this process is level 2)
    - this job consists of several steps
    - some of these steps are commands in this process, but some steps are new processes (those processes are level 3)

    If I use my code to kill/terminate a level 3 process, the control goes back to the next step in the caller (the level 2 process); and then it continues there. So for example the next step might be another level 3 process.
    If I user Windows Taskmanager or pskill the control goes back to the Client-GUI (level 1 process).

    I hope you can understand this; I am not a native speaker. Thanks!


    Viele Grüße / Best regards CSN22

    Friday, January 2, 2015 7:45 AM
  • Hello,

    Thanks for your clarification for this issue, I am trying to invoke someone experienced to help look into this issue, it may take some time and as soon as we get any result, we will post out result back to this forum.

    Thanks for your understand and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 5, 2015 6:00 AM
    Moderator
  • Hi,

    that's fine. I am looking Forward to your answer.


    Viele Grüße / Best regards CSN22

    Monday, January 5, 2015 8:05 AM
  • Unfortunately this is going to require more debugging. If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone
    Monday, February 2, 2015 7:17 AM
  • We are MS Gold Partner (Data PlatForm). Are there different Support Options / Prices for us?

    Viele Grüße / Best regards CSN22

    Monday, February 2, 2015 8:33 AM