WMI 取外部進程命令列 / GetCommandLine,wmigetcommandline

來源:互聯網
上載者:User

WMI 取外部進程命令列 / GetCommandLine,wmigetcommandline

如何擷取外部進程的命令列 似乎有很多人有該問題 有人是通過PSAPI

但並不太適合C# && VBS && VB.NET那有沒有更好的辦法 有利用WMI

 服務擷取到Win32_Process class在擷取CommandLine

下面含三種不同語言的代碼:

VBScript

Function GetCommandLine(dwProcessId)    Set objWMIService = GetObject("WinMgmts:\\.\root\cimv2")    Set objWin32PCollect = objWMIService.ExecQuery("select CommandLine from Win32_Process where Handle = " & dwProcessId)    For Each objWin32Process in objWin32PCollect        GetCommandLine = GetCommandLine & objWin32Process.CommandLine & Chr(10) & Chr(13)    NextEnd FunctionMsgBox GetCommandLine(4) 

VB.NET

Imports System.Runtime.InteropServicesModule MainModule    Declare Function GetCurrentProcessId Lib "kernel32" () As UInteger    Sub Main()        Dim dwProcessId = GetCurrentProcessId()        Console.WriteLine(GetCommandLine(dwProcessId))        Console.ReadKey(False)    End Sub    Function GetCommandLine(ByVal dwProcessId As UInteger) As String        Dim objWMIService As Object = Nothing        Dim objWin32PCollect As Object = Nothing        Try            Dim strCommandLine As String = Nothing            objWMIService = GetObject("WinMgmts:\\.\root\cimv2")            objWin32PCollect = objWMIService.ExecQuery("select CommandLine from Win32_Process where Handle = " & dwProcessId)            For Each objWin32Process In objWin32PCollect                strCommandLine = objWin32Process.CommandLine            Next            Return strCommandLine        Finally            Marshal.ReleaseComObject(objWMIService)            Marshal.ReleaseComObject(objWin32PCollect)        End Try    End FunctionEnd Module

C#.NET

        public static string GetCommandLine(int dwProcessId)        {            object objVBScript = Activator.CreateInstance(Type.                GetTypeFromProgID("MSScriptControl.ScriptControl", true)            );            try            {                // Language engine to use.                objVBScript.SetProperty("Language", "VBScript");                // Add code to the global module.                objVBScript.Invoke("AddCode",                    "Function GetCommandLine(dwProcessId)\r\n" +                        "Set objWMIService = GetObject(\"WinMgmts:\\\\.\\root\\cimv2\")\r\n" +                        "Set objWin32PCollect = objWMIService.ExecQuery(\"select CommandLine from Win32_Process where Handle = \" & dwProcessId)\r\n" +                        "For Each objWin32Process in objWin32PCollect\r\n" +                            "objRetValue = objRetValue & objWin32Process.CommandLine\r\n" +                        "Next\r\n" +                        "GetCommandLine = objRetValue\r\n" +                    "End Function"                );                // Call a procedure defined in the global module.                object objCommandLine = objVBScript.Invoke("Run", "GetCommandLine", dwProcessId);                return objCommandLine as string;            }            finally            {                Marshal.ReleaseComObject(objVBScript);            }        }
        public static string GetCommandLine(IntPtr hWnd)        {            int nNoOfPID = -1;            if (!NativeMethod.GetWindowThreadProcessId(hWnd, ref nNoOfPID))                throw new ArgumentException("Unable to get window associated the process id.");            return GetCommandLine(nNoOfPID);        }
        public abstract class NativeMethod        {            [DllImport("user32.dll", SetLastError = true)]            [return: MarshalAs(UnmanagedType.Bool)]            public static extern bool GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);        }
    using System;    using System.Reflection;    using System.Runtime.InteropServices;    public static class CComObject    {        public static object Invoke(this object obj, string name, params object[] args)        {            return CComObject.Invoke(obj, name, BindingFlags.InvokeMethod, args);        }        public static object GetProperty(this object obj, string name, params object[] args)        {            return CComObject.Invoke(obj, name, BindingFlags.GetProperty, args);        }        public static object SetProperty(this object obj, string name, params object[] args)        {            return CComObject.Invoke(obj, name, BindingFlags.SetProperty, args);        }        public static object GetField(this object obj, string name)        {            return CComObject.Invoke(obj, name, BindingFlags.GetField, null);        }        public static object SetField(this object obj, string name)        {            return CComObject.Invoke(obj, name, BindingFlags.SetField, null);        }        private static object Invoke(object obj, string name, BindingFlags attr, object[] args)        {            if (obj == null || Marshal.IsComObject(obj) != true)                throw new ArgumentException("obj param can not be null and must be valid COM object.");            return (obj.GetType()).InvokeMember(name, attr, null, obj, args);        }    }


 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.