1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. #define ProcessBasicInformation 0
  5.  
  6. typedef struct
  7. {
  8. USHORT Length;
  9. USHORT MaximumLength;
  10. PWSTR Buffer;
  11. } UNICODE_STRING, *PUNICODE_STRING;
  12.  
  13. typedef struct
  14. {
  15. ULONG AllocationSize;
  16. ULONG ActualSize;
  17. ULONG Flags;
  18. ULONG Unknown1;
  19. UNICODE_STRING Unknown2;
  20. HANDLE InputHandle;
  21. HANDLE OutputHandle;
  22. HANDLE ErrorHandle;
  23. UNICODE_STRING CurrentDirectory;
  24. HANDLE CurrentDirectoryHandle;
  25. UNICODE_STRING SearchPaths;
  26. UNICODE_STRING ApplicationName;
  27. UNICODE_STRING CommandLine;
  28. PVOID EnvironmentBlock;
  29. ULONG Unknown[];
  30. UNICODE_STRING Unknown3;
  31. UNICODE_STRING Unknown4;
  32. UNICODE_STRING Unknown5;
  33. UNICODE_STRING Unknown6;
  34. } PROCESS_PARAMETERS, *PPROCESS_PARAMETERS;
  35.  
  36. typedef struct
  37. {
  38. ULONG AllocationSize;
  39. ULONG Unknown1;
  40. HINSTANCE ProcessHinstance;
  41. PVOID ListDlls;
  42. PPROCESS_PARAMETERS ProcessParameters;
  43. ULONG Unknown2;
  44. HANDLE Heap;
  45. } PEB, *PPEB;
  46.  
  47. typedef struct
  48. {
  49. DWORD ExitStatus;
  50. PPEB PebBaseAddress;
  51. DWORD AffinityMask;
  52. DWORD BasePriority;
  53. ULONG UniqueProcessId;
  54. ULONG InheritedFromUniqueProcessId;
  55. } PROCESS_BASIC_INFORMATION;
  56.  
  57. // ntdll!NtQueryInformationProcess (NT specific!)
  58. //
  59. // The function copies the process information of the
  60. // specified type into a buffer
  61. //
  62. // NTSYSAPI
  63. // NTSTATUS
  64. // NTAPI
  65. // NtQueryInformationProcess(
  66. // IN HANDLE ProcessHandle, // handle to process
  67. // IN PROCESSINFOCLASS InformationClass, // information type
  68. // OUT PVOID ProcessInformation, // pointer to buffer
  69. // IN ULONG ProcessInformationLength, // buffer size in bytes
  70. // OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
  71. // // variable that receives
  72. // // the number of bytes
  73. // // written to the buffer
  74. // );
  75. typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
  76.  
  77. PROCNTQSIP NtQueryInformationProcess;
  78.  
  79. BOOL GetProcessCmdLine(DWORD dwId,LPWSTR wBuf,DWORD dwBufLen);
  80.  
  81. void main(int argc, char* argv[])
  82. {
  83. if (argc<)
  84. {
  85. printf("Usage:\n\ncmdline.exe ProcId\n");
  86. return;
  87. }
  88.  
  89. NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(
  90. GetModuleHandleA("ntdll"),
  91. "NtQueryInformationProcess"
  92. );
  93.  
  94. if (!NtQueryInformationProcess)
  95. return;
  96.  
  97. DWORD dwId;
  98. sscanf(argv[],"%lu",&dwId);
  99.  
  100. WCHAR wstr[] = {};
  101.  
  102. if (GetProcessCmdLine(dwId,wstr,sizeof(wstr)))
  103. wprintf(L"Command line for process %lu is:\n%s\n",dwId,wstr);
  104. else
  105. wprintf(L"Could not get command line!");
  106. system("pause");
  107. }
  108.  
  109. BOOL GetProcessCmdLine(DWORD dwId,LPWSTR wBuf,DWORD dwBufLen)
  110. {
  111. LONG status;
  112. HANDLE hProcess;
  113. PROCESS_BASIC_INFORMATION pbi;
  114. PEB Peb;
  115. PROCESS_PARAMETERS ProcParam;
  116. DWORD dwDummy;
  117. DWORD dwSize;
  118. LPVOID lpAddress;
  119. BOOL bRet = FALSE;
  120.  
  121. // Get process handle
  122. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,dwId);
  123. if (!hProcess)
  124. return FALSE;
  125.  
  126. // Retrieve information
  127. status = NtQueryInformationProcess( hProcess,
  128. ProcessBasicInformation,
  129. (PVOID)&pbi,
  130. sizeof(PROCESS_BASIC_INFORMATION),
  131. NULL
  132. );
  133.  
  134. if (status)
  135. goto cleanup;
  136.  
  137. if (!ReadProcessMemory( hProcess,
  138. pbi.PebBaseAddress,
  139. &Peb,
  140. sizeof(PEB),
  141. &dwDummy
  142. )
  143. )
  144. goto cleanup;
  145.  
  146. if (!ReadProcessMemory( hProcess,
  147. Peb.ProcessParameters,
  148. &ProcParam,
  149. sizeof(PROCESS_PARAMETERS),
  150. &dwDummy
  151. )
  152. )
  153. goto cleanup;
  154.  
  155. lpAddress = ProcParam.CommandLine.Buffer;
  156. dwSize = ProcParam.CommandLine.Length;
  157.  
  158. if (dwBufLen<dwSize)
  159. goto cleanup;
  160.  
  161. if (!ReadProcessMemory( hProcess,
  162. lpAddress,
  163. wBuf,
  164. dwSize,
  165. &dwDummy
  166. )
  167. )
  168. goto cleanup;
  169.  
  170. bRet = TRUE;
  171.  
  172. cleanup:
  173.  
  174. CloseHandle (hProcess);
  175.  
  176. return bRet;
  177.  
  178. }

原文转自:http://blog.donews.com/zwell/archive/2004/09/30/114988.aspx

<转>得到其它进程的命令行的更多相关文章

  1. C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )

    Subject: C#中如何获取其他进程的命令行参数 ( How to get other processes&apos;s command line argument )From: jian ...

  2. 获取其他进程的命令行(ReadProcessMemory其它进程的PPROCESS_PARAMETERS和PEB结构体)

    type   UNICODE_STRING = packed record     Length: Word;     MaximumLength: Word;     Buffer: PWideCh ...

  3. Docker命令行与守护进程如何交互?

    译者按: Docker是典型的C/S架构,其守护进程(daemon)与命令行(CLI)是通过REST API进行交互的. 原文: Understanding how the Docker Daemon ...

  4. windows上,任务管理器中,进程命令行太长怎么办

    一.前言 在windows上,有时候需要查看进程命令行,但是有的进程的命令行太长了,很难看全 此时,可以使用下面的方法解决(红框改为自己要查看的进程即可): C:\Users\Gaoyu>wmi ...

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

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

  6. 2019-11-29-dotnet-获取指定进程的输入命令行

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

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

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

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

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

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

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

随机推荐

  1. [BZOJ4836]二元运算(分治FFT)

    4836: [Lydsy1704月赛]二元运算 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 202[Submit][Stat ...

  2. [TCO2009]NumberGraph

    题意:给你一些带权的节点和一个正整数集合$S$,$S$中每一个数的二进制后缀$0$个数相同,节点$x$的权值为$v_x$,如果对于$x,y$存在$t\in S$使得$|v_x-v_y|=t$,那么连边 ...

  3. 修改request的parameter的几种方式(转载)

    转载地址:https://blog.csdn.net/xieyuooo/article/details/8447301

  4. 动态扩展php组件(mbstring为例)

    1.进入源码包中的mbstring目录 cd ~/php-/ext/mbstring/ 2.启动phpize /usr/local/php/bin/phpize 3.配置configure ./con ...

  5. fedora19/opensuse13.1 配置svn client

    Date: 20140208Auth: Jin 一.install zypper install  subversion yum install  subversion 二.操作 1.将文件check ...

  6. hdu3401 Trade 单调队列优化dp

    Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  7. DEBUG : Eclipse Debug 时出现 Cannot connect to VM select failed错误

    Eclipse在执行Debug操作时, 出现“Eclipse Debug 时出现 "Cannot connect to VM select failed"”错误, 在网上查找该错误 ...

  8. 翻译:Spring-Framework-Reference Document:15.2-DispatcherServlet

    写在前面的话:   最近被项目的代码折腾的死去活来的,其实框架也没有那么难理解,只是自己的Web基础太差,被Request和Response这一对神雕侠侣坑到泪流满面!今天捣腾了一下Spring We ...

  9. Hive:用Java代码通过JDBC连接Hiveserver

    参考https://www.iteblog.com/archives/846.html 1.hive依赖hadoop,将hdfs当作文件存储介质,那是否意味着hive需要知道namenode的地址? ...

  10. [Todo]各种语言包管理工具

    看到一篇文章不错: http://harttle.com/2015/05/29/pkg-manager.html 包管理和构建系统是现代的软件开发团队中必不可少的工具,也是Linux软件系统的常见组织 ...