Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 27 Oct 2001
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

Share

About the Author

Luca Piergentili
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...

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 28 Oct 2001
Article Copyright 2001 by Luca Piergentili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid