Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C++

Virtual Desktop: A Simple Desktop Management Tool

Rate me:
Please Sign up or sign in to vote.
4.83/5 (46 votes)
25 Jul 2008CPOL5 min read 231.8K   11.9K   143  
This article gives you an overview of Windows Station, Windows Desktop and how to work with them. It also has a sample application (Virtual Desktop) demonstrating multiple desktop management.
// RegSettings.cpp: implementation of the CRegSettings class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "sample.h"
#include "RegSettings.h"


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

#define SUBKEY _T("SOFTWARE\\DigitalBlackCat\\Virtual Desktop")
extern void DebugPrintErrorMessage(TCHAR *pszErrorString = NULL, bool bDisplayMsg = false, TCHAR *pszMsgCaption = NULL);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRegSettings::CRegSettings()
{

}

CRegSettings::~CRegSettings()
{

}

BOOL CRegSettings::SetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue)
{
	ASSERT(lpszSection != NULL);
	CString strSection(lpszSection);

	CString strSubKey;
	HKEY hKey;
	strSubKey.Format(_T("%s\\%s"), SUBKEY, strSection);
	long lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, strSubKey, &hKey);
	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not create the registry key.\n"));
		return FALSE;
	}

	lReturn = RegSetValueEx(hKey, lpszEntry, NULL, REG_SZ, (LPBYTE)lpszValue, 
				(lstrlen(lpszValue)+1)*sizeof(TCHAR));
	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not set the value to key.\n"));
		RegCloseKey(hKey); 
		return FALSE;
	}

	RegCloseKey(hKey);
	return TRUE;
}


BOOL CRegSettings::SetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue)
{
	ASSERT(lpszSection != NULL);
	ASSERT(lpszEntry != NULL);
	CString strSection(lpszSection);
	
	CString strSubKey;
	HKEY hKey;
	strSubKey.Format(_T("%s\\%s"), SUBKEY, strSection);
	long lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, strSubKey, &hKey);
	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not create the registry key.\n"));
		return FALSE;
	}

	lReturn = RegSetValueEx(hKey, lpszEntry, NULL, REG_DWORD, (LPBYTE)(&nValue) , sizeof(nValue));
	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not set the value to key.\n"));
		RegCloseKey(hKey); 
		return FALSE;
	}
	RegCloseKey(hKey);
	return TRUE;
}


CString CRegSettings::ReadProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault)
{
	ASSERT(lpszSection != NULL);
	ASSERT(lpszEntry != NULL);
	CString strSection(lpszSection);
	
	CString strSubKey;
	HKEY hKey;
	strSubKey.Format(_T("%s\\%s"), SUBKEY, strSection);

	long lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, strSubKey, &hKey);

	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not create the registry key.\n"));
		return lpszDefault;
	}

	CString strValue;
	DWORD dwType, dwCount;
	LONG lResult = RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount);

	if (lResult == ERROR_SUCCESS)
	{
		ASSERT(dwType == REG_SZ);
		lResult = RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, 
			(LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
		strValue.ReleaseBuffer();
	}

	RegCloseKey(hKey);

	if (lResult == ERROR_SUCCESS)
	{
		ASSERT(dwType == REG_SZ);
		return strValue;
	}

	return lpszDefault;
}

UINT CRegSettings::ReadProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault)
{
	ASSERT(lpszSection != NULL);
	ASSERT(lpszEntry != NULL);
	CString strSection(lpszSection);
	
	CString strSubKey;
	HKEY hKey;
	strSubKey.Format(_T("%s\\%s"), SUBKEY, strSection);

	long lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, strSubKey, &hKey);

	if (lReturn != ERROR_SUCCESS)
	{
		DebugPrintErrorMessage(_T("Could not create the registry key.\n"));
		return nDefault;
	}

	DWORD dwValue;
	DWORD dwType;
	DWORD dwCount = sizeof(DWORD);
	LONG lResult = RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, 
		(LPBYTE)&dwValue, &dwCount);
	RegCloseKey(hKey);

	if (lResult == ERROR_SUCCESS)
	{
		ASSERT(dwType == REG_DWORD);
		ASSERT(dwCount == sizeof(dwValue));
		return (UINT)dwValue;
	}

	return nDefault;
}

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
India India
Hello All !
This is Mallinath S. Karkanti, from India. I'm working as a Software Developer in one of the Middle Scale Company... !

Comments and Discussions