dotnet 获取指定进程的输入命令行
本文告诉大家如何在 dotnet 获取指定的进程的命令行参数
很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数?
我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 程序,另一个都是C#代码,但是只支持 x64 程序
本文提供一个由 StackOverflow 大神开发的库拿到进程的命令行
在使用下面的代码需要引用两个 C++ 的库,可以从 csdn 下载
使用下面的代码就可以拿到传入进程的参数,在使用之前,需要在输出的文件夹里面包含 ProcCmdLine32.dll 和 ProcCmdLine64.dll 可以从csdn 下载
public static string GetCommandLineOfProcess(Process process)
{
// max size of a command line is USHORT/sizeof(WCHAR), so we are going
// just allocate max USHORT for sanity sake.
var stringBuilder = new StringBuilder(0xFFFF);
if (Environment.Is64BitProcess)
{
GetProcCmdLine64((uint) process.Id, stringBuilder, (uint) stringBuilder.Capacity);
}
else
{
GetProcCmdLine32((uint) process.Id, stringBuilder, (uint) stringBuilder.Capacity);
}
return stringBuilder.ToString();
}
[DllImport("ProcCmdLine32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
private static extern bool GetProcCmdLine32(uint nProcId, StringBuilder stringBuilder, uint dwSizeBuf);
[DllImport("ProcCmdLine64.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
private static extern bool GetProcCmdLine64(uint nProcId, StringBuilder stringBuilder, uint dwSizeBuf);
获取所有的进程的命令行可以使用这个代码
foreach (var process in Process.GetProcesses())
{
Console.WriteLine($"{process.ProcessName} {GetCommandLineOfProcess(process)}");
}
代码请看 https://github.com/lindexi/lindexi_gd/tree/cf4054b0f479986bd295a8e5b69c31ad8fd7fe10/GetProcessCommandLine
上面的代码需要引用一个 C++ 的库,看起来不清真,下面通过全部 C# 的代码
public static string GetCommandLineOfProcess(int processId)
{
var pid = processId;
var pbi = new NativeMethods.PROCESS_BASIC_INFORMATION();
IntPtr proc = NativeMethods.OpenProcess
(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid
);
if (proc == IntPtr.Zero)
{
return "";
}
if (NativeMethods.NtQueryInformationProcess(proc, 0, ref pbi, pbi.Size, IntPtr.Zero) == 0)
{
var buff = new byte[IntPtr.Size];
if (NativeMethods.ReadProcessMemory
(
proc,
(IntPtr) (pbi.PebBaseAddress.ToInt32() + 0x10),
buff,
IntPtr.Size, out _
))
{
var buffPtr = BitConverter.ToInt32(buff, 0);
var commandLine = new byte[Marshal.SizeOf(typeof(NativeMethods.UNICODE_STRING))];
if
(
NativeMethods.ReadProcessMemory
(
proc, (IntPtr) (buffPtr + 0x40),
commandLine,
Marshal.SizeOf(typeof(NativeMethods.UNICODE_STRING)), out _
)
)
{
var ucsData = ByteArrayToStructure<NativeMethods.UNICODE_STRING>(commandLine);
var parms = new byte[ucsData.Length];
if
(
NativeMethods.ReadProcessMemory
(
proc, ucsData.buffer, parms,
ucsData.Length, out _
)
)
{
return Encoding.Unicode.GetString(parms);
}
}
}
}
NativeMethods.CloseHandle(proc);
return "";
}
private const uint PROCESS_QUERY_INFORMATION = 0x400;
private const uint PROCESS_VM_READ = 0x010;
private static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
var stuff = (T) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();
return stuff;
}
private static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr OpenProcess
(
uint dwDesiredAccess,
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
int dwProcessId
);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReadProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
int nSize,
out IntPtr lpNumberOfBytesRead
);
[DllImport("ntdll.dll")]
internal static extern int NtQueryInformationProcess
(
IntPtr ProcessHandle,
uint ProcessInformationClass,
ref PROCESS_BASIC_INFORMATION ProcessInformation,
uint ProcessInformationLength,
IntPtr ReturnLength
);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct PROCESS_BASIC_INFORMATION
{
internal int ExitProcess;
internal IntPtr PebBaseAddress;
internal IntPtr AffinityMask;
internal int BasePriority;
internal IntPtr UniqueProcessId;
internal IntPtr InheritedFromUniqueProcessId;
internal uint Size => (uint) Marshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION));
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct UNICODE_STRING
{
internal ushort Length;
internal ushort MaximumLength;
internal IntPtr buffer;
}
}
获取所有进程的参数
[STAThread]
private static void Main(string[] args)
{
if (Environment.Is64BitProcess)
{
throw new InvalidOperationException("暂时只支持x86程序");
}
foreach (var process in Process.GetProcesses())
{
Console.WriteLine($"{process.ProcessName} {GetCommandLineOfProcess(process.Id)}");
}
}
我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新
如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
dotnet 获取指定进程的输入命令行的更多相关文章
- dotnet 通过 WMI 获取指定进程的输入命令行
本文告诉大家如何使用 WMI 通过 Process 获取这个进程传入的命令行 使用下面代码,使用 Win32_Process 拿到所有的进程,通过 WHERE 判断当前的进程,然后拿到进程传入的命令 ...
- 2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行
原文:2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行 title author date CreateTime categories dotnet 通过 WMI 获取指定进 ...
- 2019-11-29-dotnet-获取指定进程的输入命令行
title author date CreateTime categories dotnet 获取指定进程的输入命令行 lindexi 2019-11-29 08:35:11 +0800 2019-0 ...
- 2019-8-31-dotnet-获取指定进程的输入命令行
title author date CreateTime categories dotnet 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:58 +0800 2019-0 ...
- 2019-8-31-dotnet-通过-WMI-获取指定进程的输入命令行
title author date CreateTime categories dotnet 通过 WMI 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:59 +0800 ...
- Delphi 获取进程路径及命令行参数
Delphi 获取进程路径及命令行参数, 但有的进程获取时会报错,不知为啥 type PVOID64 = UINT64; _UNICODE_STRING = packed record Length ...
- ruby中输入命令行编译sass(ruby小白)
Ruby(或cmd中)输入命令行编译sass步骤如下: (1)举例而言:首先在F盘下建立一个总文件夹,比如test文件夹:其次在该文件夹下建立html,images,js,sass等文件夹. (2)在 ...
- Ruby(或cmd中)输入命令行编译sass
Ruby(或cmd中)输入命令行编译sass步骤如下: 举例: 1.在F盘中新建一个总文件夹,比如test文件夹,其中在该文件夹下面建立html.images.js.sass等文件夹. 2.在sass ...
- C# 调试程序时如何输入命令行参数
调试程序时如何输入命令行参数http://www.a769.com/archives/320.html 开发命令行程序时,我们会疑惑,从那里输入参数呢?请看下面的教程,让你摆脱困扰. 1.点击菜单栏: ...
随机推荐
- Linux进程管理(二、 进程创建)
通常使用fork创建进程, 也可以用vfork()和clone().fork.vfork和clone三个用户态函数均由libc库提供,它们分别会调用Linux内核提供的同名系统调用fork,vfork ...
- Linux配置redis开机启动(CentOS 7)
https://blog.csdn.net/qq_31803503/article/details/79246205 本次配置linux版本是CentOS 7 首先将 redis-3.2.3/uti ...
- Servlet接口
ServletRequest接口 ServletRequest的对象用于向Servlet提供客户端请求信息,如内容类型,内容长度,参数名称和值,标题信息,属性等. RequestDispatcher接 ...
- sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决
sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决 sqlplus 连接数据库报错SP2-0642: ...
- Java面向对象----方法重载
方法重载(over load):方法名相同,参数列表不同,返回类型无关 package com.tanlei.newer; public class OverLoadDemo { public sta ...
- [HLSL]HLSL 入门参考 (dx11龙书附录B译文)
原文:[HLSL]HLSL 入门参考 (dx11龙书附录B译文) HLSL 高级着色语言 参考文档 龙书DirectX12现已推出中文版,其附录B的高级着色器语言参考的翻译质量比本文更高,有条件的读者 ...
- python 自动识别黄图
from watchdog.observers import Observerfrom watchdog.events import *import timeimport sysimport osim ...
- C运行时库函数
C运行时库函数是指C语言本省支持的一些基本函数,通常是汇编直接实现的. API函数是操作系统提供给用户方便设计应用程序的函数,实现一些特定的功能,API函数也是C语言的函数实现的. 他们之间区别是: ...
- H3C 常用信息查看命令
- NodeMCU快速上云集锦
摘要: 上至智慧园区项目,下至 `Hello world`,基于开源 MQTT 协议,阿里云 IoT 用户使用 NodeMCU 完成了不少 IoT 项目,以下为项目和教程集锦,欢迎大家一起上手试试. ...