如何清除进程命令行?

Joe*_*dan 8 c++ windows winapi command-line command-line-arguments

我想从内部清除我的进程的命令行.例如,在任务管理器/进程资源管理器中查看我的进程时,命令行条目将为空.

我想在当前运行的进程中执行此操作,而不是在可能的情况下重新启动进程.

Ole*_*leg 11

我想你必须修改你的进程的PEBRTL_USER_PROCESS_PARAMETERS部分(例如,参见http://en.wikipedia.org/wiki/Process_Environment_Blockhttp://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT %20Objects/Process/PEB.html).您可以尝试使用NtQueryInformationProcess来获取PEB.然后你可以修改.我希望它能奏效.ProcessParameters.CommandLine

更新:我验证了我的建议.有用.以下测试程序证明了这一点:

#include <Windows.h>
#include <Winternl.h> // for PROCESS_BASIC_INFORMATION and ProcessBasicInformation
#include <stdio.h>
#include <tchar.h>

typedef NTSTATUS (NTAPI *PFN_NT_QUERY_INFORMATION_PROCESS) (
    IN HANDLE ProcessHandle,
    IN PROCESSINFOCLASS ProcessInformationClass,
    OUT PVOID ProcessInformation,
    IN ULONG ProcessInformationLength,
    OUT PULONG ReturnLength OPTIONAL);

int main()
{
    HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                   FALSE, GetCurrentProcessId());
    PROCESS_BASIC_INFORMATION pbi;
    ULONG ReturnLength;
    PFN_NT_QUERY_INFORMATION_PROCESS pfnNtQueryInformationProcess =
        (PFN_NT_QUERY_INFORMATION_PROCESS) GetProcAddress (
            GetModuleHandle(TEXT("ntdll.dll")), "NtQueryInformationProcess");
    NTSTATUS status = pfnNtQueryInformationProcess (
        hProcess, ProcessBasicInformation,
        (PVOID)&pbi, sizeof(pbi), &ReturnLength);
    // remove full information about my command line
    pbi.PebBaseAddress->ProcessParameters->CommandLine.Length = 0;

    getchar(); // wait till we can verify the results
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我们用一些参数启动程序,我们将看到

替代文字

而不是以前见过的

替代文字

  • @Joe:我现在可以重现你在下列情况下描述的问题:1)程序编译为32位应用程序2)程序在64位操作系统(Windows 7 x64)上启动而不是管理员3)使用64位TaskManager.如果使用"C:\ Windows\SysWOW64\taskmgr.exe"或Process Explorer或编译程序X64**而代码中没有任何内容**,可以在我的测试程序中删除程序参数.64位exe的用法是一个选项吗?您可以使用`IsWow64Process`进行测试,无论您是在64位上运行还是重新启动另一个exe,例如. (2认同)

Jim*_*hel 1

您可以尝试调用GetCommandLineAPI函数,然后将第一个字节设置为0。即:

LPTSTR cmdline = GetCommandLine();
*cmdline = '\0';
Run Code Online (Sandbox Code Playgroud)

老实说,我不知道这是否有效或可能产生什么后果,但可能值得一试。