Click here to Skip to main content
15,885,032 members
Articles / Desktop Programming / MFC

EnetstatX

Rate me:
Please Sign up or sign in to vote.
4.42/5 (26 votes)
9 Jun 20041 min read 117.2K   4K   41  
Enhance netstat and packet filtering.
// Base.cpp: implementation of the CBase class.
//
//////////////////////////////////////////////////////////////////////
#include "Base.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#include "Tlhelp32.h"

//init static members
	DWORD CBase::m_dwLastError = 0;
	HMODULE CBase::m_hModuleTcp = NULL;
	HMODULE CBase::m_hModuleUdp = NULL;
	HMODULE CBase::m_hModuleTH = NULL;
	pAllocateAndGetTcpExTableFromStack CBase::m_pGetTcpTableEx = NULL; 
	pAllocateAndGetUdpExTableFromStack CBase::m_pGetUdpTableEx = NULL;
	pCreateToolhelp32Snapshot CBase::m_pToolhelp = NULL;
	pProcess32First CBase::m_pProc32First = NULL;
	pProcess32Next CBase::m_pProc32Next = NULL;
	bool CBase::m_bInit = false;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBase::CBase()
{
}
/////////////////////////////////////////////////////////////////////////
//
CBase::~CBase()
{	
}
//////////////////////////////////////////////////////////////////////
//
DWORD CBase::Initialize(void)
{
	if (m_bInit == true)
		return 0; //no need to initialize 

	//prepare the path
	CString strSysPath = GetSysPath();

	//load "iphlpapi.dll"
	m_hModuleTcp = ::LoadLibrary(strSysPath + "\\system32\\iphlpapi.dll");
	if(m_hModuleTcp == NULL)
		return m_dwLastError = ::GetLastError();

	m_hModuleUdp = ::LoadLibrary(strSysPath + "\\system32\\iphlpapi.dll");
	if(m_hModuleUdp == NULL)
		return m_dwLastError = ::GetLastError();

	//point to the magic method e.g TCP
	m_pGetTcpTableEx = (pAllocateAndGetTcpExTableFromStack) GetProcAddress(
		m_hModuleTcp, "AllocateAndGetTcpExTableFromStack");
	if(m_pGetTcpTableEx == NULL)
		return m_dwLastError = ::GetLastError();

	//point to the magic method e.g UDP
	m_pGetUdpTableEx = (pAllocateAndGetUdpExTableFromStack) GetProcAddress(
		m_hModuleUdp, "AllocateAndGetUdpExTableFromStack");
	if(m_pGetUdpTableEx == NULL)
		return m_dwLastError = ::GetLastError();

	//-------------------------------------------------------------------

	//load "psapi functions from kernel32.dll"
	m_hModuleTH = ::LoadLibrary(strSysPath + "\\system32\\kernel32.dll");
	if(m_hModuleTH == NULL)
		return m_dwLastError = ::GetLastError();
	
	//get address of imported functions
	m_pToolhelp = (pCreateToolhelp32Snapshot) GetProcAddress(
		m_hModuleTH, "CreateToolhelp32Snapshot");
	if(m_pToolhelp == NULL)
		return m_dwLastError = ::GetLastError();

	m_pProc32First = (pProcess32First) GetProcAddress(
		m_hModuleTH, "Process32First");
	if(m_pProc32First == NULL)
		return m_dwLastError = ::GetLastError();
	
	m_pProc32Next = (pProcess32Next) GetProcAddress(
		m_hModuleTH, "Process32Next");
	if(m_pProc32Next == NULL)
		return m_dwLastError = ::GetLastError();
	
	m_bInit = true;

	return 0;
}
//////////////////////////////////////////////////////////////////////
//
DWORD CBase::Unitialize(void)
{
	if (m_bInit == false)
		return 0; //no need to freed

	if (FALSE == FreeLibrary(m_hModuleTcp))
		return m_dwLastError = ::GetLastError();

	if (FALSE == FreeLibrary(m_hModuleUdp))
		return m_dwLastError = ::GetLastError();

	if (FALSE == FreeLibrary(m_hModuleTH))
		return m_dwLastError = ::GetLastError();

	m_bInit = false;

	return 0;
}
/////////////////////////////////////////////////////////////////////////
//
CString CBase::GetSysPath(void)
{
	//vars
	char buffPath[MAX_PATH];
	memset(buffPath, '0', sizeof(buffPath));
	
	//get windows path
	//UINT uiPathLen = ::GetSystemWindowsDirectory(buffPath, MAX_PATH+1);
	UINT uiPathLen = ::GetWindowsDirectory(buffPath, MAX_PATH+1);
	if(uiPathLen == 0)
	{
		m_dwLastError = ::GetLastError();
		return "error ";
	}
	
	//prepare the path
	CString strPath = buffPath;
	strPath.TrimRight();
	return strPath;
}
/////////////////////////////////////////////////////////////////////////
//
int CBase::GetSysVer(void)
{
	OSVERSIONINFO stOsVer;
	stOsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	GetVersionEx(&stOsVer);
	if(stOsVer.dwMajorVersion < 5)
		return WIN_NT;	
	else
		return WIN_XX;
}
/////////////////////////////////////////////////////////////////////////
//
CString CBase::GetProcById(DWORD dwPid)
{
	switch(GetSysVer())
	{
		case WIN_XX:
			return GetProcById4Other(dwPid);
		
		case WIN_NT:
		default:
			return "Invalid OS";
	}
}
/////////////////////////////////////////////////////////////////////////
//
CString CBase::GetProcById4Other(DWORD dwPid)
{
	if ((m_pToolhelp == NULL) || \
		(m_pProc32First == NULL) || \
		(m_pProc32Next == NULL))
	return "error";

	//vars
	HANDLE hProcSnap = NULL;
	CString strProcName(L"Unknown");
	PROCESSENTRY32_MY stProc32Entry;
	stProc32Entry.dwSize = sizeof(PROCESSENTRY32_MY);

	//get a snapshoot of the running proc
	hProcSnap = m_pToolhelp(TH32CS_SNAPPROCESS_MY, 0);
	if (hProcSnap == INVALID_HANDLE_VALUE)
	{
		m_dwLastError = ::GetLastError();
		return "error ";
	}	

	//gathering info
	if (FALSE == m_pProc32First(hProcSnap, &stProc32Entry))
	{
		m_dwLastError = ::GetLastError();
		return "error ";
	}

	if(dwPid == stProc32Entry.th32ProcessID)
	{
		return strProcName = stProc32Entry.szExeFile;
	}
	else
	{
		do
		{
			if (stProc32Entry.th32ProcessID == dwPid)
			{
				//free
				CloseHandle(hProcSnap);
				return strProcName = stProc32Entry.szExeFile;
			}
		} while(m_pProc32Next(hProcSnap, &stProc32Entry));
	}
	
	return strProcName;
}
/////////////////////////////////////////////////////////////////////////
//
CString CBase::GetProcPath(DWORD dwPid)
{
    HANDLE        hModuleSnap = NULL; 
    MODULEENTRY32 me32        = {0}; 
 
    // Take a snapshot of all modules in the specified process. 

    hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPid); 
    if (hModuleSnap == INVALID_HANDLE_VALUE) 
	{
		return GetProcPathEx(dwPid);
	}
 
    // Fill the size of the structure before using it. 
    me32.dwSize = sizeof(MODULEENTRY32); 
 
    // Walk the module list of the process, and find the module of 
    // interest. Then copy the information to the buffer pointed 
    // to by lpMe32 so that it can be returned to the caller. 

    if (FALSE == Module32First(hModuleSnap, &me32)) 
	{
		CloseHandle (hModuleSnap); 
        return "error";           // could not walk module list 
	}
 
    // Do not forget to clean up the snapshot object. 
    CloseHandle (hModuleSnap); 
    return me32.szExePath; 
}
/////////////////////////////////////////////////////////////////////////
//
CString CBase::GetProcPathEx(DWORD dwPid)
{
		//query the whole path
		HANDLE hndProc = ::OpenProcess(
			PROCESS_QUERY_INFORMATION,
			FALSE,
			dwPid);
	
		if(hndProc == NULL)
			return "error";
	
		//vars
		char buffPath[MAX_PATH];
		memset(buffPath, '0', sizeof(buffPath));
	
		::GetProcessImageFileName(
			hndProc,
			buffPath,
			MAX_PATH);
	
		::CloseHandle(hndProc);
		TRACE1("\n%s\n", buffPath);
		return buffPath;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions