2019-11-29-dotnet-获取指定进程的输入命令行
| 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 下载
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)}");
}
上面的代码需要引用一个 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)}");
}
}
2019-11-29-dotnet-获取指定进程的输入命令行的更多相关文章
- dotnet 获取指定进程的输入命令行
本文告诉大家如何在 dotnet 获取指定的进程的命令行参数 很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数? 我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 ...
- dotnet 通过 WMI 获取指定进程的输入命令行
本文告诉大家如何使用 WMI 通过 Process 获取这个进程传入的命令行 使用下面代码,使用 Win32_Process 拿到所有的进程,通过 WHERE 判断当前的进程,然后拿到进程传入的命令 ...
- 2019-8-31-dotnet-获取指定进程的输入命令行
title author date CreateTime categories dotnet 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:58 +0800 2019-0 ...
- 2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行
原文:2019-11-29-dotnet-通过-WMI-获取指定进程的输入命令行 title author date CreateTime categories dotnet 通过 WMI 获取指定进 ...
- 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 ...
- 获取指定进程号,并kill掉
直接上案例: 例子:获取nginx进程 方法:$ps -aux |grep nginx |grep -v grep |awk '{print $2}' 或者 $ps -ef |grep nginx ...
- python 中 使用sys模块 获取运行脚本时在命令行输入的参数
在python项目的开发的过程中, 经常需要运行各种python脚本, 有时候还需要根据不同的使用情况输入不同的参数, 如果每次都去编辑一下脚本那就太麻烦,太耗费时间了, 这时就可以使用Python自 ...
- 在Shell脚本中获取指定进程的PID
注意这条命令用反引号(Tab上面的那个键)括起来,作用类似于${ } processId = ` ps -ef | grep fms.jar | grep -v grep | awk '{print ...
随机推荐
- asp.net form submit 在Chrome里面看Form提交
Chrome中查看 request form data 在Fiddler中查看
- maven仓库,snapshot快照仓库和release发布仓库的区别
首先看下snapshot类型 建立一个maven-privider项目,版本定义为1.1-SNAPSHOT 创建一个privider类,写一个hello()方法,并推送maven私有仓库 然后再建立一 ...
- NOIP2010提高组真题部分整理(没有关押罪犯)
目录 \(NOIP2010\)提高组真题部分整理 \(T1\)机器翻译: 题目背景: 题目描述: 输入输出格式: 输入输出样例: 说明: 题解: 代码: \(T2\)乌龟棋 题目背景: 题目描述: 输 ...
- leetcode 103二叉树的锯齿形层次遍历
与102相比就增加了flag,用以确定要不要进行reverse操作 reverse:STL公共函数,对于一个有序容器的元素reverse ( s.begin(),s.end() )可以使得容器s的元素 ...
- Typography 字体
Typography 字体 我们对字体进行统一规范,力求在各个操作系统下都有最佳展示效果. ¶中文字体 和畅惠风 PingFang SC 和畅惠风 Hiragino Sans GB 和畅惠风 Micr ...
- BCNF/3NF 数据库设计范式简介
数据库设计有1NF.2NF.3NF.BCNF.4NF.5NF.从左往右,越后面的数据库设计范式冗余度越低. 满足后一个设计范式也必定满足前一个设计范式. 1NF只要求每个属性是不可再分的,基本每个数据 ...
- Maven POM 模板[z]
https://juejin.im/post/5cc826a5f265da03a33c443a [z]https://juejin.im/post/5cc826a5f265da03a33c443a S ...
- CentOS 系统安装 nodejs 及相关配置
概述 今天我在我的 AWS EC2 服务器上安装了 nodejs.没想到竟然这么麻烦,比在 windows 和 mac 上麻烦多了.所以我把心得记录下来,供以后开发时参考,相信对其他人也有用. 参考资 ...
- ControlTemplate in WPF —— ComboBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- IPython4_Notebook
目录 目录 前言 系统软件 Setup IPython Setup IPython Setup Notebook 临时指定镜像源 Install pyreadline Install pyzmq In ...