// EnumProcessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EnumProcess.h"
#include "EnumProcessDlg.h"

#include <Tlhelp32.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CEnumProcessDlg dialog



CEnumProcessDlg::CEnumProcessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEnumProcessDlg::IDD, pParent)
	, m_iTadad(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEnumProcessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_ctlList);
	DDX_Text(pDX, IDC_EDIT_TADAD, m_iTadad);
	DDX_Control(pDX, IDC_LIST_ID, m_lID);
}

BEGIN_MESSAGE_MAP(CEnumProcessDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_RUN, OnBnClickedButtonRun)
	ON_LBN_DBLCLK(IDC_LIST1, OnLbnDblclkList1)
	ON_WM_CLOSE()
	ON_LBN_SELCHANGE(IDC_LIST1, OnLbnSelchangeList1)
	ON_LBN_SELCHANGE(IDC_LIST_ID, OnLbnSelchangeListId)
	

END_MESSAGE_MAP()


// CEnumProcessDlg message handlers

BOOL CEnumProcessDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEnumProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CEnumProcessDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEnumProcessDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CEnumProcessDlg::OnBnClickedButtonRun()
{
	int t = m_ctlList.GetCount();
	if( t > -1)
	{
		for( int j=0; j<t; j++)
		{
			m_ctlList.DeleteString(j);
			m_lID.DeleteString(j);
		}
	}

	
	
	//OSVERSIONINFO osver;
	//ZeroMemory(&osver, sizeof(OSVERSIONINFO));
		
	//osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	//GetVersionEx(&osver);
	/*if(osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
	{//windows NT
		EnumProcesses(NULL,0,&dw);
		DWORD *dwPID = new DWORD((int)dw/sizeof(DWORD));
		DWORD dwcb = dw;
		EnumProcesses(dwPID, dwcb,&dw);
	
	}
	else if(osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && 
		(osver.dwMinorVersion > 4 || 
		(osver.dwMajorVersion == 4 && osver.dwMinorVersion >0)))
	{//win98
		

	}*/
	//DWORD dw;
	CString s;
	h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	
	PROCESSENTRY32 pe32;
	ZeroMemory(&pe32, sizeof(PROCESSENTRY32));
	pe32.dwSize = sizeof(PROCESSENTRY32);
	BOOL b = Process32First(h, &pe32);

	while(b)
	{
		m_iTadad++;
		

		TCHAR th[10];
		_ltoa((unsigned long)pe32.th32ProcessID, th, 10);
		s  = th;
		s += " ### ";
		s += pe32.szExeFile;
		m_ctlList.AddString(s);
		m_lID.AddString( th );
		b = Process32Next(h, &pe32);
		
	}
	UpdateData(false);

	
/****************************************************************	
	PROCESS_MEMORY_COUNTERS pmc;
	ZeroMemory(&pmc, sizeof(PROCESS_MEMORY_COUNTERS ));
	pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS );
	dw = pmc.cb;
	GetProcessMemoryInfo(hp, &pmc, dw);
****************************************************************/
	
/***************************************************************
	MODULEINFO model;
    ZeroMemory(&model, sizeof(MODULEINFO));
	EnumProcessModules(hp, NULL, 0, &dw);
	HMODULE *hm = new HMODULE[(int)dw / sizeof(HMODULE)];
	EnumProcessModules(hp, hm, dw, &dw);
	GetModuleInformation(hp, hm[0], &model,sizeof(MODULEINFO));
	delete[] hm;

****************************************************************/

    /*SYSTEM_INFO sys;
	ZeroMemory(&sys, sizeof(SYSTEM_INFO));
	GetSystemInfo(&sys);
	
	MEMORY_BASIC_INFORMATION pbi;
	ZeroMemory(&pbi, sizeof(MEMORY_BASIC_INFORMATION));
	VirtualQueryEx(hp, sys.lpMinimumApplicationAddress, &pbi, sizeof(MEMORY_BASIC_INFORMATION));*/


	/****************************************************
	TCHAR filename[MAX_PATH] = {0};
	dw = MAX_PATH;
	dw = GetMappedFileName(hp, sys.lpMinimumApplicationAddress  , filename, dw );
	dw = GetLastError();
	****************************************************/
	
/*	MessageBox("ALLAH");
	
	MessageBox("ALLAH");
	TerminateProcess(hp, 0);
	CloseHandle(hp);*/
	CloseHandle(h);

}

void CEnumProcessDlg::OnLbnDblclkList1()
{
	int i = 0;
	i = m_ctlList.GetCurSel();
	CString str;
	m_lID.GetText(i, str);
	unsigned long l = atol(str);
	HANDLE hp = OpenProcess(PROCESS_ALL_ACCESS, TRUE,(DWORD)l);
	DWORD dw = GetLastError();
	if (!hp && dw == 5)
	{
		MessageBox("ERROR_ACCESS_DENIED");
		return;
	}
	int dc = MessageBox("Are you sure","Warning", MB_OKCANCEL);
	if (dc == IDOK)
	{
		BOOL b = TerminateProcess(hp, 0);
		if (b)
		{
			MessageBox("Successful.");
			m_lID.DeleteString(i);
			m_ctlList.DeleteString(i);
		}
		else
		{
			MessageBox("Cannot Trminate Process");
			CloseHandle( hp );
		}
	}

}

void CEnumProcessDlg::OnClose()
{
	CDialog::OnClose();
}

void CEnumProcessDlg::OnLbnSelchangeList1()
{
	m_lID.SetCurSel( m_ctlList.GetCurSel() );
}

void CEnumProcessDlg::OnLbnSelchangeListId()
{
	m_ctlList.SetCurSel( m_lID.GetCurSel() );
}

