Get integer from Windows registry in C++/MFC



Following code gets integer from Windows registry using C++/MFC.

 
UINT RetrieveUIntFromRegistry(HKEY hKey, CString szSubkey,CString szValueName,UINT iDefault)
{
 
	HKEY retKey;
	UINT retdata;
 
	CString szFull = szSubkey;
	LONG retval = RegOpenKeyEx(hKey,szFull, 0,KEY_READ, &retKey);
 
    if (retval == ERROR_SUCCESS) {
 
		//since this is in bytes we need to multiply by size of TCHAR
		DWORD datasize = sizeof(UINT);
		DWORD dwType;
 
		if (ERROR_SUCCESS  != RegQueryValueEx(retKey,szValueName,NULL,&dwType, (BYTE *) &retdata, &datasize))
		{
			retdata = iDefault;
		}
 
		RegCloseKey(hKey);
		RegCloseKey(retKey);
 
	}else {
		return iDefault;
	}
 
	return retdata;
}


Get string from Windows registry in C++/MFC



Go to get string from Windows registry. Coded in C++/MFC.

 
CString RetrieveStringFromRegistry(HKEY hKey, CString szSubkey,CString szValueName,CString szDefault)
{
 
	HKEY retKey;
	TCHAR retdata[1024];
 
	retdata[0] = 0;
 
	CString szFull = szSubkey;
 
	LONG retval = RegOpenKeyEx(hKey,szFull, 0,KEY_READ, &retKey);
 
    if (retval == ERROR_SUCCESS) {
 
		//since this is in bytes we need to multiply by size of TCHAR
		DWORD datasize = 1024*sizeof(TCHAR);
		DWORD dwType;
 
		RegQueryValueEx(retKey,szValueName,NULL,&dwType, (BYTE *) retdata, &datasize);
 
		RegCloseKey(hKey);
		RegCloseKey(retKey);
 
	}else {
		return szDefault;
	}
 
 
	return CString(retdata);
}


Save string to Windows registry in C++/MFC



The following function saves a string in Windows registry. Coded in C++/MFC. The advantage here is that it creates the key if it doesn’t exist.

 
 
void SaveStringToRegistry(HKEY hKey, CString szSubkey,CString szValueName,CString szData)
{
	HKEY retKey;
 
	CString szFull = szSubkey;
 
    LONG retval = RegCreateKey(hKey,szFull, &retKey);
 
	if (retval == ERROR_SUCCESS) 
	{
		RegSetValueEx(retKey,szValueName,0, REG_SZ,(const BYTE *)LPCTSTR(szData),(szData.GetLength()+1)*sizeof(TCHAR));
		RegCloseKey(retKey);
	}	 
}


Check if a folder is writable using C++/MFC on Windows



This function checks if a file can be created inside a folder by creating a temporary file inside the folder. Additionally, if the whole path doesn’t exist, then it keeps going up one level until it finds a writable folder. Coded in C++/MFC.

 
//The path accepts even the folder that do not exist
//eg: C:\Program Files\A\B\C
//The function will check if C exists, if not it will check if B
//exist etc etc..until it finds a path that exists
//and then it will try to create a file in that folder
BOOL IsFolderWritable(CString strPath, BOOL bContainsFileName)
{
 
	//we will remove file name
	if (bContainsFileName)
	{
		PathRemoveFileSpec(strPath.GetBufferSetLength(1024));
		strPath.ReleaseBuffer();
	}
 
	while(!PathFileExists(strPath))
	{
		TrimPath(strPath, 1, FALSE);
	}
 
	strPath.Trim();
	if (strPath.IsEmpty()) return FALSE;
 
	//now try to create a temporary file here
	//if it creates successfully then the folder is
	//writeable
 
	CString strTempFilePath;
	UINT nRet = 0;
	nRet = GetTempFileName(strPath, _T("Tmp"), 0, strTempFilePath.GetBufferSetLength(1024));
	strTempFilePath.ReleaseBuffer();
 
	if ( 0 == nRet)
	{
		return FALSE;
	}
 
	DeleteFile(strTempFilePath);
 
	return TRUE;
 
}
 
 
 
void TrimPath(CString &strPathToTrim, int iLevel, BOOL bIncludesFileName)
{
	int iCtr = 0;
 
	// this function uses an easier logic.
	// initially it removes backshash from the end of the path
	// if a file name is at the end of the path then
	// iLevel is incremented by 1
 
	RemoveBackslash(strPathToTrim);
 
	if (bIncludesFileName)
	{
		//this is because we will have to cut the file name also
		iLevel++;
	}
 
	while ( iCtr < iLevel )
	{
		int idx = strPathToTrim.ReverseFind(_T('\\'));
 
		if (idx != 0)
		{
			//without backslash
			strPathToTrim = strPathToTrim.Left(idx);
		}
 
		iCtr++;
	}
 
	//now we will append backslash
	AppendBackslash(strPathToTrim);
 
}
 
//Appends a backslash if it exist
void AppendBackslash( CString & strPath)
{
	if ( !IsBackslashPresent(strPath) )
	{
        strPath += _T('\\');
	}
}
 
void RemoveBackslash( CString & strPath)
{
	int iLength;
 
	if ( IsBackslashPresent(strPath) )
	{
		iLength = strPath.GetLength();
        strPath = strPath.Left(iLength-1);
	}
}
 
BOOL IsBackslashPresent( CString & strPath )
{
	if (strPath.IsEmpty()) return FALSE;
 
	if (strPath.Right(1) == _T('\\')) return TRUE;
 
	return FALSE;
 
}


Get long or short date and time from CTime MFC class ( C++)



Often we have to show date/time to the user in our program. If you have enough screen space, it is better to show it in long date time format. Following is the code to get long or short date and time from CTime MFC class ( C++) in Windows.

 
CString	FormatLocaleDateTime(CTime tm, bool longDate /*= false*/)
{
	CString value;
 
	if (longDate) {
		value.Format(_T("%s %s"), FormatLocaleLongDate(tm), FormatLocaleTime(tm));
	}
	else {
		value.Format(_T("%s %s"), FormatLocaleShortDate(tm), FormatLocaleTime(tm));
	}
 
	return value;
}
 
CString	FormatLocaleShortDate(CTime tm)
{
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
}
 
CString	FormatLocaleLongDate(CTime tm)
{
 
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
 
}
 
CString	FormatLocaleTime(CTime tm)
{
 
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
 
}


Get locale short date from CTime MFC class (C++)



Here is the code to get locale short date from CTime MFC class (C++).

 
CString	FormatLocaleShortDate(CTime tm)
{
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
}


Get locale long date from CTime MFC class (C++)



Here is the code to get locale long date from CTime MFC class (C++).

 
CString FormatLocaleLongDate(CTime tm)
{
 
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
 
}


Get locale time from CTime MFC class (C++)



Here is the code to get locale time from CTime MFC class (C++).

 
CString	FormatLocaleTime(CTime tm)
{
 
	CString strValue;
	SYSTEMTIME st;
	tm.GetAsSystemTime(st);
	GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE|TIME_NOSECONDS, &st, NULL, strValue.GetBufferSetLength(SMALL_TEXT_LENGTH), SMALL_TEXT_LENGTH);
	strValue.ReleaseBuffer();
	return strValue;
 
}