title author date CreateTime categories
dotnet 获取指定进程的输入命令行
lindexi
2019-11-29 08:35:11 +0800
2019-02-22 11:25:47 +0800
dotnet

本文告诉大家如何在 dotnet 获取指定的进程的命令行参数

很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数?

我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 程序,另一个都是C#代码,但是只支持 x64 程序

本文提供一个由 StackOverflow 大神开发的库拿到进程的命令行

在使用下面的代码需要引用两个 C++ 的库,可以从 csdn 下载

使用下面的代码就可以拿到传入进程的参数,在使用之前,需要在输出的文件夹里面包含 ProcCmdLine32.dll 和 ProcCmdLine64.dll 可以从csdn 下载

  1. public static string GetCommandLineOfProcess(Process process)
  2. {
  3. // max size of a command line is USHORT/sizeof(WCHAR), so we are going
  4. // just allocate max USHORT for sanity sake.
  5. var stringBuilder = new StringBuilder(0xFFFF);
  6. if (Environment.Is64BitProcess)
  7. {
  8. GetProcCmdLine64((uint) process.Id, stringBuilder, (uint) stringBuilder.Capacity);
  9. }
  10. else
  11. {
  12. GetProcCmdLine32((uint) process.Id, stringBuilder, (uint) stringBuilder.Capacity);
  13. }
  14.  
  15. return stringBuilder.ToString();
  16. }
  17.  
  18. [DllImport("ProcCmdLine32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
  19. private static extern bool GetProcCmdLine32(uint nProcId, StringBuilder stringBuilder, uint dwSizeBuf);
  20.  
  21. [DllImport("ProcCmdLine64.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
  22. private static extern bool GetProcCmdLine64(uint nProcId, StringBuilder stringBuilder, uint dwSizeBuf);

获取所有的进程的命令行可以使用这个代码

  1. foreach (var process in Process.GetProcesses())
  2. {
  3. Console.WriteLine($"{process.ProcessName} {GetCommandLineOfProcess(process)}");
  4. }

代码请看 https://github.com/lindexi/lindexi_gd/tree/cf4054b0f479986bd295a8e5b69c31ad8fd7fe10/GetProcessCommandLine

上面的代码需要引用一个 C++ 的库,看起来不清真,下面通过全部 C# 的代码

  1. public static string GetCommandLineOfProcess(int processId)
  2. {
  3. var pid = processId;
  4.  
  5. var pbi = new NativeMethods.PROCESS_BASIC_INFORMATION();
  6.  
  7. IntPtr proc = NativeMethods.OpenProcess
  8. (
  9. PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid
  10. );
  11.  
  12. if (proc == IntPtr.Zero)
  13. {
  14. return "";
  15. }
  16.  
  17. if (NativeMethods.NtQueryInformationProcess(proc, 0, ref pbi, pbi.Size, IntPtr.Zero) == 0)
  18. {
  19. var buff = new byte[IntPtr.Size];
  20. if (NativeMethods.ReadProcessMemory
  21. (
  22. proc,
  23. (IntPtr) (pbi.PebBaseAddress.ToInt32() + 0x10),
  24. buff,
  25. IntPtr.Size, out _
  26. ))
  27. {
  28. var buffPtr = BitConverter.ToInt32(buff, 0);
  29. var commandLine = new byte[Marshal.SizeOf(typeof(NativeMethods.UNICODE_STRING))];
  30.  
  31. if
  32. (
  33. NativeMethods.ReadProcessMemory
  34. (
  35. proc, (IntPtr) (buffPtr + 0x40),
  36. commandLine,
  37. Marshal.SizeOf(typeof(NativeMethods.UNICODE_STRING)), out _
  38. )
  39. )
  40. {
  41. var ucsData = ByteArrayToStructure<NativeMethods.UNICODE_STRING>(commandLine);
  42. var parms = new byte[ucsData.Length];
  43. if
  44. (
  45. NativeMethods.ReadProcessMemory
  46. (
  47. proc, ucsData.buffer, parms,
  48. ucsData.Length, out _
  49. )
  50. )
  51. {
  52. return Encoding.Unicode.GetString(parms);
  53. }
  54. }
  55. }
  56. }
  57.  
  58. NativeMethods.CloseHandle(proc);
  59.  
  60. return "";
  61. }
  62.  
  63. private const uint PROCESS_QUERY_INFORMATION = 0x400;
  64. private const uint PROCESS_VM_READ = 0x010;
  65.  
  66. private static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
  67. {
  68. var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  69. var stuff = (T) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  70. handle.Free();
  71. return stuff;
  72. }
  73.  
  74. private static class NativeMethods
  75. {
  76. [DllImport("kernel32.dll", SetLastError = true)]
  77. [return: MarshalAs(UnmanagedType.Bool)]
  78. internal static extern bool CloseHandle(IntPtr hObject);
  79.  
  80. [DllImport("kernel32.dll", SetLastError = true)]
  81. internal static extern IntPtr OpenProcess
  82. (
  83. uint dwDesiredAccess,
  84. [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
  85. int dwProcessId
  86. );
  87.  
  88. [DllImport("kernel32.dll", SetLastError = true)]
  89. [return: MarshalAs(UnmanagedType.Bool)]
  90. internal static extern bool ReadProcessMemory
  91. (
  92. IntPtr hProcess,
  93. IntPtr lpBaseAddress,
  94. byte[] lpBuffer,
  95. int nSize,
  96. out IntPtr lpNumberOfBytesRead
  97. );
  98.  
  99. [DllImport("ntdll.dll")]
  100. internal static extern int NtQueryInformationProcess
  101. (
  102. IntPtr ProcessHandle,
  103. uint ProcessInformationClass,
  104. ref PROCESS_BASIC_INFORMATION ProcessInformation,
  105. uint ProcessInformationLength,
  106. IntPtr ReturnLength
  107. );
  108.  
  109. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  110. internal struct PROCESS_BASIC_INFORMATION
  111. {
  112. internal int ExitProcess;
  113. internal IntPtr PebBaseAddress;
  114. internal IntPtr AffinityMask;
  115. internal int BasePriority;
  116. internal IntPtr UniqueProcessId;
  117. internal IntPtr InheritedFromUniqueProcessId;
  118.  
  119. internal uint Size => (uint) Marshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION));
  120. }
  121.  
  122. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  123. internal struct UNICODE_STRING
  124. {
  125. internal ushort Length;
  126. internal ushort MaximumLength;
  127. internal IntPtr buffer;
  128. }
  129. }

获取所有进程的参数

  1. [STAThread]
  2. private static void Main(string[] args)
  3. {
  4. if (Environment.Is64BitProcess)
  5. {
  6. throw new InvalidOperationException("暂时只支持x86程序");
  7. }
  8.  
  9. foreach (var process in Process.GetProcesses())
  10. {
  11. Console.WriteLine($"{process.ProcessName} {GetCommandLineOfProcess(process.Id)}");
  12. }
  13. }

更简单是通过 WMI 获取指定进程的输入命令行

2019-11-29-dotnet-获取指定进程的输入命令行的更多相关文章

  1. dotnet 获取指定进程的输入命令行

    本文告诉大家如何在 dotnet 获取指定的进程的命令行参数 很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数? 我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 ...

  2. dotnet 通过 WMI 获取指定进程的输入命令行

    本文告诉大家如何使用 WMI 通过 Process 获取这个进程传入的命令行 使用下面代码,使用 Win32_Process 拿到所有的进程,通过 WHERE 判断当前的进程,然后拿到进程传入的命令 ...

  3. 2019-8-31-dotnet-获取指定进程的输入命令行

    title author date CreateTime categories dotnet 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:58 +0800 2019-0 ...

  4. 2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行

    原文:2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行 title author date CreateTime categories dotnet 通过 WMI 获取指定进 ...

  5. 2019-8-31-dotnet-通过-WMI-获取指定进程的输入命令行

    title author date CreateTime categories dotnet 通过 WMI 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:59 +0800 ...

  6. Delphi 获取进程路径及命令行参数

    Delphi 获取进程路径及命令行参数, 但有的进程获取时会报错,不知为啥 type PVOID64 = UINT64; _UNICODE_STRING = packed record Length ...

  7. 获取指定进程号,并kill掉

    直接上案例: 例子:获取nginx进程 方法:$ps -aux |grep nginx |grep -v grep |awk '{print $2}'  或者 $ps -ef |grep nginx ...

  8. python 中 使用sys模块 获取运行脚本时在命令行输入的参数

    在python项目的开发的过程中, 经常需要运行各种python脚本, 有时候还需要根据不同的使用情况输入不同的参数, 如果每次都去编辑一下脚本那就太麻烦,太耗费时间了, 这时就可以使用Python自 ...

  9. 在Shell脚本中获取指定进程的PID

    注意这条命令用反引号(Tab上面的那个键)括起来,作用类似于${ } processId = ` ps -ef | grep fms.jar | grep -v grep | awk '{print ...

随机推荐

  1. 【转】diamond专题(三)—— diamond架构

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. AJAX请求和普通HTTP请求区别

    两者本质区别: AJAX通xmlHttpRequest象请求服务器服务器接受请求返数据实现刷新交互 普通http请求通httpRequest象请求服务器接受请求返数据需要页面刷新 AJAX请求 普通请 ...

  3. 百度echars 插件 横坐标信息倾斜显示

    只需要 在xAxis   中加入 axisLabel:{                                  interval:0,//横轴信息全部显示                  ...

  4. 让socket端口不被子进程继承

    有两个程序A,B A绑定端口8000,然后用system函数启动程序B,然后再把A杀掉,这个时候再重启A的时候会发现绑定端口失败, 原因是端口被B绑定了,实际上是被继承了,为了避免这种情况,需要对主s ...

  5. CNN入门讲解-为什么要有最后一层全连接?

    原文地址:https://baijiahao.baidu.com/s?id=1590121601889191549&wfr=spider&for=pc 今天要说的是CNN最后一层了,C ...

  6. Random Initialization for K-Means

    K-Means的中心初始化惯用方式是随机初始化.也就是说:从training set中随机挑选出K个 作为中心,再进行下一步的K-Means算法. 这个方法很容易导致收敛到局部最优解,当簇个个数(K) ...

  7. EMQTT测试--安装与测试 (windows)

    我下载的是windows版 安装 参考http://emqtt.com/docs/install.html 将下载的压缩包解压,我解压到了D盘 命令行窗口,cd到程序目录 控制台模式启动: .\bin ...

  8. python文档的数据读取,把读取数据写入到新的表里

    目的:接口自动化过程需要从表格文件读取,然后把结果写到表格中.没有多余内容全部是精华! 读取文件1 读取文件2 代码如下图: # -*-coding:utf-8 -*-# Author:wangjun ...

  9. Metinfo5.1 /message/access.php SQL注入漏洞

  10. 四、Kubernetes_V1.10集群部署-master-创建kubeconfig

    1.生成配置文件 # 创建 TLS Bootstrapping Token # export BOOTSTRAP_TOKEN=$( /dev/urandom | od -An -t x | tr -d ...