Click here to Skip to main content
15,886,799 members
Articles / Desktop Programming / MFC

How to Register a Program

Rate me:
Please Sign up or sign in to vote.
3.95/5 (9 votes)
28 Jan 2009CPOL8 min read 61.7K   2K   61  
How to register a program on end-user machine only and without end-user interference.
const bool CRegistryAccess::CreateKey(const CString &cstrBranch, const CString &cstrKeyName)
// This is to create a variable in the registry (without setting it). Return T on success and F if not.
{
	TRY
	{
		HKEY hKey = (HKEY)NULL;
		TCHAR szName[MAX_KEY_LENGTH] = _T("");
		::wsprintf(szName, TEXT("%s\\%s"), (LPCTSTR)cstrBranch, (LPCTSTR)cstrKeyName); 
		DWORD dwDisp = 0;
		bool bResult = (::RegCreateKeyEx(
			m_hKeyCurrentUser, 
			szName, 
			0, 
			NULL, 
			REG_OPTION_NON_VOLATILE, 
			KEY_FULL_ACCESS, 
			NULL, 
			&hKey, 
			&dwDisp) == ERROR_SUCCESS);
		if(!bResult)
		{
			#ifdef _DEBUG
			if(m_bHiddenKey)
				bResult = (::GetLastError() == 0);
			else
				::ShowLastError(_T("RegCreateKeyEx"));
			#endif
		}
		// Return T on success and F if not.
		return bResult;
	}
	CATCH_ALL(e)
	{
		WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "CreateKey", "", e);
		THROW_LAST();
	}
	END_CATCH_ALL
	return false;
}

const bool CRegistryAccess::EnumKey(CONST DWORD dwIndex, HKEY &hKey, CString &cstrKeyName)
// Enum one key on a open key handle.
{
	TRY
	{
		LONG lResult = ERROR_SUCCESS;
		TCHAR szName[MAX_KEY_LENGTH] = _T("");
		DWORD dwSize = MAX_KEY_LENGTH;
		FILETIME ftWrite;
		cstrKeyName.Empty();
		lResult = ::RegEnumKeyEx(
			hKey, 
			dwIndex, 
			szName, 
			&dwSize, 
			NULL, 
			NULL, 
			NULL, 
			&ftWrite);
		if(lResult == ERROR_SUCCESS)
		{
			// Got the one we wanted.
			cstrKeyName.Format(TEXT("%s"), szName);
			return true;
		}
		#ifdef _DEBUG
		if(!m_bHiddenKey)
			::ShowLastError(_T("RegEnumKeyEx"));
		#endif
		return false;
	}
	CATCH_ALL(e)
	{
		WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "EnumKey", "", e);
		THROW_LAST();
	}
	END_CATCH_ALL
	return false;
}

const void CRegistryAccess::EnumKeys(const CString &cstrBranch, CStringArray &sarrKeys)
// Return keys under this branch as string array.
{
	TRY
	{
		HKEY hKey = (HKEY)NULL;
		sarrKeys.RemoveAll();
		sarrKeys.SetSize(0, 1);
		if(OpenKey(cstrBranch, hKey))
		{
			CString cstrKeyName(_T(""));
			for(DWORD dwIndex = 0; EnumKey(dwIndex, hKey, cstrKeyName); dwIndex++)
				// Collect it.
				sarrKeys.Add(cstrKeyName);
			CloseKey(hKey);
		}
	}
	CATCH_ALL(e)
	{
		WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "EnumKeys", "", e);
		THROW_LAST();
	}
	END_CATCH_ALL
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Denmark Denmark
c/c++/c# -developer.

Comments and Discussions