Click here to Skip to main content
15,886,091 members
Articles / Programming Languages / C++

A Copy Utility using TWAIN

Rate me:
Please Sign up or sign in to vote.
4.54/5 (10 votes)
14 Jul 20042 min read 99.8K   6.8K   47  
Example for a simple encapsulation of the TWAIN interface
// RegAccess.cpp: implementation of the CRegAccess class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RegAccess.h"



CRegAccess::CRegAccess()
	: m_hKey(NULL)
{
}

CRegAccess::~CRegAccess()
{
	if (m_hKey)
		RegCloseKey(m_hKey);
}

bool CRegAccess::Open(HKEY mainKey, const char* pKey, bool bReadOnly)
{
	if (!pKey)
		return false;

	DWORD Disposition;
	return RegCreateKeyEx(mainKey, pKey, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_READ | (bReadOnly ? 0 : KEY_WRITE), NULL, &m_hKey, &Disposition) == ERROR_SUCCESS;
}

bool CRegAccess::WriteKey(const char* pSubKey, const char* pValue)
{
	if (!pSubKey || !pValue)
		return false;

	return RegSetValueEx(m_hKey, pSubKey, 0, REG_SZ, (BYTE*)pValue, lstrlen(pValue)) == ERROR_SUCCESS;
}

bool CRegAccess::WriteKey(const char* pSubKey, DWORD nValue)
{
	if (!pSubKey)
		return false;

	return RegSetValueEx(m_hKey, pSubKey, 0, REG_DWORD, (BYTE*)&nValue, sizeof(DWORD)) == ERROR_SUCCESS;
}


bool CRegAccess::ReadKey(const char* pSubKey, char* buffer, int nLen)
{
	if (!pSubKey || !buffer || nLen <= 0)
		return false;

	*buffer = 0;

	DWORD nLength = nLen;
	DWORD nType = 0;
	bool bRes = RegQueryValueEx(m_hKey, pSubKey, NULL, &nType, (BYTE*)buffer, &nLength) == ERROR_SUCCESS 
			&& nType == REG_SZ;

	return bRes;
}

bool CRegAccess::ReadKey(const char* pSubKey, DWORD& nValue)
{
	if (!pSubKey)
		return false;

	DWORD nLength = sizeof(DWORD);
	DWORD nType = 0;
	bool bRes = RegQueryValueEx(m_hKey, pSubKey, NULL, &nType, (BYTE*)&nValue, &nLength) == ERROR_SUCCESS 
			&& nType == REG_DWORD;

	return bRes;
}

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
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions