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); } }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。