Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / MFC

How to write a simple but effective TCP/IP port scanner for Win32

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
27 Oct 20017 min read 163.1K   7.3K   101  
An article on how to write a TCP/IP port scanner with a GUI, based on the MFC's property sheet paradigm
/*
	CRegKey.cpp
	Classe base per l'accesso al registro (SDK).
	Riadattata dal codice originale ATL (M$) (vedi http://codeguru.earthweb.com/system/CRegKey.shtml).
	Luca Piergentili, 14/07/99
	lpiergentili@yahoo.com
	http://www.geocities.com/lpiergentili/
*/
#include "env.h"
#include "pragma.h"
#include <stdlib.h>
#include "window.h"
#include <winreg.h>
#include "CRegKey.h"

/*
	Detach()
*/
HKEY CRegKey::Detach(void)
{
	HKEY hKey = m_hKey;
	m_hKey = NULL;
	return(hKey);
}

/*
	Create()
*/
LONG CRegKey::Create(HKEY hKeyParent,LPCSTR lpcszKeyName,LPSTR lpszClass/*=REG_NONE*/,DWORD dwOptions/*=REG_OPTION_NON_VOLATILE*/,REGSAM samDesired/*=KEY_ALL_ACCESS*/,LPSECURITY_ATTRIBUTES lpSecAttr/*=NULL*/,LPDWORD lpdwDisposition/*=NULL*/)
{
	DWORD dw;
	HKEY hKey = NULL;
	LONG lRes = ::RegCreateKeyEx(	hKeyParent,
							(LPCTSTR)lpcszKeyName,
							0L,
							(LPTSTR)lpszClass,
							dwOptions,
							samDesired,
							lpSecAttr,
							&hKey,
							&dw
							);

	if(lpdwDisposition!=NULL)
		*lpdwDisposition = dw;

	if(lRes==ERROR_SUCCESS)
	{
		lRes = CRegKey::Close();
		m_hKey = hKey;
	}

	return(lRes);
}

/*
	Open()
*/
LONG CRegKey::Open(HKEY hKeyParent,LPCSTR lpcszKeyName,REGSAM samDesired/*=KEY_ALL_ACCESS*/)
{
	HKEY hKey = NULL;
	LONG lRes = ::RegOpenKeyEx(hKeyParent,(LPCTSTR)lpcszKeyName,0,samDesired,&hKey);

	if(lRes==ERROR_SUCCESS)
	{
		lRes = CRegKey::Close();
		m_hKey = hKey;
	}

	return(lRes);
}

/*
	Close()
*/
LONG CRegKey::Close(void)
{
	LONG lRes = ERROR_SUCCESS;
	
	if(m_hKey!=NULL)
	{
		lRes = ::RegCloseKey(m_hKey);
		m_hKey = NULL;
	}

	return(lRes);
}

/*
	QueryValue()
*/
LONG CRegKey::QueryValue(LPSTR lpszValue,LPCSTR lpcszValueName,DWORD* pdwCount)
{
	DWORD dwType = 0L;
	return(::RegQueryValueEx(m_hKey,(LPTSTR)lpcszValueName,NULL,&dwType,(LPBYTE)lpszValue,pdwCount));
}

/*
	QueryValue()
*/
LONG CRegKey::QueryValue(DWORD& dwValue,LPCSTR lpcszValueName)
{
	DWORD dwType = 0L;
	DWORD dwCount = sizeof(DWORD);
	return(::RegQueryValueEx(m_hKey,(LPTSTR)lpcszValueName,NULL,&dwType,(LPBYTE)&dwValue,&dwCount));
}

/*
	SetValue()
*/
HRESULT CRegKey::SetValue(LPCSTR lpcszValue,LPCSTR lpcszValueName/*=NULL*/)
{
//	return(::RegSetValueEx(m_hKey,(LPCTSTR)lpcszValueName,NULL,REG_SZ,(CONST BYTE *)lpcszValue,(lstrlen(lpcszValue)+1)*sizeof(TCHAR)));
	return(::RegSetValueEx(m_hKey,(LPCTSTR)lpcszValueName,NULL,REG_SZ,(CONST BYTE *)lpcszValue,(DWORD)(lstrlen(lpcszValue)+1)));
}

/*
	SetValue()
*/
LONG CRegKey::SetValue(DWORD dwValue,LPCSTR lpcszValueName)
{
	return(::RegSetValueEx(m_hKey,(LPCTSTR)lpcszValueName,NULL,REG_DWORD,(CONST BYTE *)&dwValue,sizeof(DWORD)));
}

/*
	SetBinaryValue()
*/
LONG CRegKey::SetBinaryValue(DWORD dwValue,LPCSTR lpcszValueName)
{
	return(::RegSetValueEx(m_hKey,(LPCTSTR)lpcszValueName,NULL,REG_BINARY,(CONST BYTE *)&dwValue,sizeof(DWORD)));
}

/*
	SetValue()
*/
LONG WINAPI CRegKey::SetValue(HKEY hKeyParent,LPCSTR lpcszKeyName,LPCSTR lpcszValue,LPCSTR lpcszValueName/* = NULL */)
{
	CRegKey key;
	LONG lRes = key.Create(hKeyParent,lpcszKeyName);

	if(lRes==ERROR_SUCCESS)
		lRes = key.SetValue(lpcszValue,lpcszValueName);

	return(lRes);
}

/*
	SetKeyValue()
*/
LONG CRegKey::SetKeyValue(LPCSTR lpcszKeyName,LPCSTR lpcszValue,LPCSTR lpcszValueName/*=NULL*/)
{
	CRegKey key;
	LONG lRes = key.Create(m_hKey,lpcszKeyName);

	if(lRes==ERROR_SUCCESS)
		lRes = key.SetValue(lpcszValue,lpcszValueName);
	
	return(lRes);
}

/*
	DeleteValue()
*/
BOOL CRegKey::DeleteValue(LPCSTR lpcszValue)
{
	return(::RegDeleteValue(m_hKey,(LPCTSTR)lpcszValue)==ERROR_SUCCESS);
}

/*
	DeleteKey()
*/
BOOL CRegKey::DeleteKey(LPCSTR lpcszKey)
{
	CRegKey key;
	
	if(key.Open(m_hKey,lpcszKey)!=ERROR_SUCCESS)
		return(FALSE);

	FILETIME time;
	char szBuffer[_MAX_PATH+1];
	DWORD dwSize = sizeof(szBuffer);
	
	// cerca le eventuali sotto-chiavi presenti eliminandole singolarmente perche' su NT
	// ::RegDeleteKey() non elimina la chiave se quest'ultima possiede sotto-chiavi
	while(::RegEnumKeyEx(key.m_hKey,0L,(LPTSTR)szBuffer,&dwSize,NULL,NULL,NULL,&time)==ERROR_SUCCESS)
	{
		if(!key.DeleteKey(szBuffer))
			return(FALSE);
		
		dwSize = sizeof(szBuffer);
	}

	key.Close();

	return(DeleteSubKey(lpcszKey));
}

/*
	DeleteSubKey()
*/
BOOL CRegKey::DeleteSubKey(LPCSTR lpcszSubKey)
{
	return(::RegDeleteKey(m_hKey,(LPCTSTR)lpcszSubKey)==ERROR_SUCCESS);
}

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
Italy Italy
I like C and C++, Acid Jazz, James Brown, gli Spaghetti Aglio e Olio, alla Bolognesa, alla Puttanesca e le Fettuccine alla Matriciana ('Maccaroni' over the world). Of course I like beautiful big tits girls too, my little car, Frank Zappa, the art of Zen, italian coffee and much more...

Comments and Discussions