📄 readothercmdlinedlg.cpp
字号:
// ReadOtherCmdLineDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ReadOtherCmdLine.h"
#include "ReadOtherCmdLineDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReadOtherCmdLineDlg dialog
CReadOtherCmdLineDlg::CReadOtherCmdLineDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReadOtherCmdLineDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReadOtherCmdLineDlg)
m_szCmdLine = _T("");
m_nPID = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CReadOtherCmdLineDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReadOtherCmdLineDlg)
DDX_Text(pDX, IDC_CMDLINE, m_szCmdLine);
DDX_Text(pDX, IDC_PID, m_nPID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReadOtherCmdLineDlg, CDialog)
//{{AFX_MSG_MAP(CReadOtherCmdLineDlg)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_GET, OnGet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReadOtherCmdLineDlg message handlers
BOOL CReadOtherCmdLineDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_nPID = GetCurrentProcessId();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CReadOtherCmdLineDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
DWORD g_GetCmdLine(DWORD dwPID,TCHAR* pCmdLine,DWORD dwBufLen)
{
#define BUFFER_LEN 512 //reading buffer for the commandline
HANDLE hProc = OpenProcess(PROCESS_VM_READ,FALSE,dwPID);
if(hProc == NULL)
{
return GetLastError();
}
DWORD dwRet = -1;
DWORD dwAddr = *(DWORD*)((DWORD)GetCommandLine + 1);//第2个字节开始才是我们要读的地址
TCHAR tcBuf[BUFFER_LEN] = {0};
DWORD dwRead = 0;
//判断平台
DWORD dwVer = GetVersion();
try
{
if(dwVer < 0x80000000) // Windows NT/2000/XP
{
if(ReadProcessMemory(hProc,(LPVOID)dwAddr,&dwAddr,4,&dwRead))
{
if(ReadProcessMemory(hProc,(LPVOID)dwAddr,tcBuf,BUFFER_LEN,&dwRead))
{
_tcsncpy(pCmdLine,tcBuf,dwBufLen); //最好检查一下dwRead和dwBufLen的大小,使用较小的那个
dwRet = 0;
}
}
}
else // Windows 95/98/Me and Win32s
{
while(true) //使用while是为了出错时方便跳出循环
{
if(!ReadProcessMemory(hProc,(LPVOID)dwAddr,&dwAddr,4,&dwRead)) break;
if(!ReadProcessMemory(hProc,(LPVOID)dwAddr,&dwAddr,4,&dwRead)) break;
if(!ReadProcessMemory(hProc,(LPVOID)(dwAddr + 0xC0),tcBuf,BUFFER_LEN,&dwRead)) break;
if(*tcBuf == 0)
{
if(!ReadProcessMemory(hProc,(LPVOID)(dwAddr + 0x40),&dwAddr,4,&dwRead)) break;
if(!ReadProcessMemory(hProc,(LPVOID)(dwAddr + 0x8),&dwAddr,4,&dwRead)) break;
if(!ReadProcessMemory(hProc,(LPVOID)dwAddr,tcBuf,BUFFER_LEN,&dwRead)) break;
}
_tcsncpy(pCmdLine,tcBuf,dwBufLen); //最好检查一下dwRead和dwBufLen的大小,使用较小的那个
dwRet = 0;
break;
}
}
}
catch(...)
{
dwRet = ERROR_INVALID_ACCESS; //exception
}
CloseHandle(hProc);
return dwRet;
}
void CReadOtherCmdLineDlg::OnGet()
{
UpdateData();
if(m_nPID < 0)
{
AfxMessageBox("pid not valid");return;
}
const int BUF_LEN = 512;
if(g_GetCmdLine(m_nPID,m_szCmdLine.GetBuffer(BUF_LEN),BUF_LEN)!= 0)
{
AfxMessageBox("error occured.");return;
}
m_szCmdLine.ReleaseBuffer();
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -