I have a MFC C++ application which uses command line arguments. But when the program is running, sensitive information entered on the command line will be displayed in the Command Line column of the Task Manager. 1.So how to delete them? 2.How to change command line arguments in MFC C++?
I use following function in my x32 InitInstance , but it does not work. The Param.CommandLine.Buffer has been changed to empty, because Task Manager still display Command Line. Is there any mistake?
#include <Windows.h>
#include <Winternl.h>
#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);
void ClearCommandLine()
{
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
PROCESS_BASIC_INFORMATION pbi = {0};
RTL_USER_PROCESS_PARAMETERS Param = {0};
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), NULL);
wchar_t* lpwszCmd=L"";
USHORT usCmdLen = 2 + 2 * (wcslen(lpwszCmd));
ReadProcessMemory(hProcess, pbi.PebBaseAddress, &peb, sizeof(peb), NULL);
ReadProcessMemory(hProcess, peb.ProcessParameters, &Param, sizeof(Param), NULL);
WriteProcessMemory(hProcess, Param.CommandLine.Buffer, lpwszCmd, usCmdLen,NULL);
WriteProcessMemory(hProcess,&Param.CommandLine.Length, &usCmdLen, sizeof(usCmdLen), NULL);
CloseHandle(hProcess);
}