⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowsregistry.cpp

📁 用于查看文件被谁加锁了
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		REG_DWORD,
		REG_DWORD_LITTLE_ENDIAN,
		REG_DWORD_BIG_ENDIAN,
	};

	// Reset value
	KeyValue=0;

	// Fetch data in registry and check result type
	nResult=RawQueryValue(pKeyName, pValueBuffer, &nValueBufferSize, &nType, pSupportedTypes, sizeof(pSupportedTypes) / sizeof(DWORD));

	if (nResult==ERROR_SUCCESS) {
		// Set the Key Value
		int *pIntValue = (int *)pValueBuffer;
		KeyValue = *pIntValue;
		// Set the Key Type
		if (pValueType != NULL) {
			*pValueType=nType;
		}
	}

	if (pValueBuffer) {
		delete [] pValueBuffer;
	}
	return(nResult);
}

LONG CWindowsRegistry::QueryValue(LPCTSTR pKeyName, char **KeyValue, int &KeyArraySize, LPDWORD pValueType)
{
	return(ERROR_CALL_NOT_IMPLEMENTED);
}

LONG CWindowsRegistry::RawQueryValue(LPCTSTR pKeyName, LPBYTE pValueBuffer, DWORD *pValueBufferSize, DWORD *pType, DWORD pSupportedTypes[], size_t nNumberOfTypes)
{
	LONG nResult=ERROR_SUCCESS;
	HKEY tempKeyHandle = NULL;
	LPDWORD pReserved=NULL;
	char pSubKeyName[FWR_MAXIMUM_KEY_SIZE];
	char pSubPathName[FWR_MAXIMUM_KEY_SIZE];
	BOOL bCloseOnExit = FALSE;

    // Check parameter
    if ( pKeyName==NULL ) {
        return(ERROR_INVALID_PARAMETER);
    }

	if (!IsOpen()) {
		return(ERROR_INVALID_HANDLE);
	}

	const char *separatorPosition = strrchr(pKeyName, '\\');
	if (separatorPosition) {
		// We were given a path name with slashes: must open new handle
		DWORD ulOptions=0;
		bCloseOnExit = TRUE;
		// Fetch name of the last key in the path
		strncpy(pSubKeyName, separatorPosition+1, FWR_MAXIMUM_KEY_SIZE);
		// Fetch name of the sub-path, except the last key
		strncpy(pSubPathName, pKeyName, separatorPosition - pKeyName);
		pSubPathName[separatorPosition - pKeyName]='\0';
		nResult=RegOpenKeyEx(m_pKey,           // handle to base key
							 pSubPathName,     // name of subkey to open
							 ulOptions,        // reserved
							 KEY_QUERY_VALUE,    // security access mask
							 &tempKeyHandle    // handle of opened key
							 );
	} else {
		// We were given a direct value name: reuse currently open handle
		tempKeyHandle = m_pKey;
		strncpy(pSubKeyName, pKeyName, FWR_MAXIMUM_KEY_SIZE);
	}

	// Read the value
	if (nResult == ERROR_SUCCESS) {
		nResult = RegQueryValueEx(tempKeyHandle,
								  pSubKeyName,
								  pReserved,
								  pType,
								  pValueBuffer,
								  pValueBufferSize);

	} else {
		// Force WIN32 error (it is not set by default).
		SetLastError(nResult);
	}

	if (nResult == ERROR_SUCCESS) {
		// Check key type
		if (pSupportedTypes != NULL && pType != NULL) {
			nResult=ERROR_INVALID_DATA;
			for (unsigned int i = 0; i < nNumberOfTypes; i++) {
				if (pSupportedTypes[i]==*pType) {
					nResult=ERROR_SUCCESS;
					break;
				}
			}
		}
	}

	if (bCloseOnExit) {
		RegCloseKey(tempKeyHandle);
	}

	return(nResult);

}

LONG CWindowsRegistry::WriteValue(LPCTSTR pKeyName, LPCTSTR pKeyValue, DWORD nValueType)
{
	LONG nResult=ERROR_SUCCESS;
	DWORD nValueBufferSize=0;
	BYTE pValueBuffer[MaximumWriteSize+1];
	size_t nEffectiveSize=0;
	
	// If string is NULL... do not write anything (Q: should we write an empty string ?)
	if (pKeyValue == NULL) {
		return(ERROR_SUCCESS); // Not an error
	}

	nEffectiveSize=strlen(pKeyValue)+1; // Always write the '\0'

	// Set buffer for registry I/O
	if (nEffectiveSize >= MaximumWriteSize) {
		// Try not to overflow the write buffer
		nEffectiveSize=MaximumWriteSize;
	}
	memcpy((void *)pValueBuffer, (void *)pKeyValue, nEffectiveSize);
	
	// Set data in registry with low-level function
	nResult=RawWriteValue(pKeyName, pValueBuffer, nEffectiveSize, nValueType);

	return(nResult);
}

LONG CWindowsRegistry::WriteValue(LPCTSTR pKeyName, int KeyValue, DWORD nValueType)
{
	LONG nResult=ERROR_SUCCESS;
	DWORD nValueBufferSize=0;
	BYTE pValueBuffer[MaximumWriteSize+1];
	size_t nEffectiveSize=0;
	
	nEffectiveSize=sizeof(KeyValue);

	// Set buffer for registry I/O
	if (nEffectiveSize >= MaximumWriteSize) {
		// Try not to overflow the write buffer
		nEffectiveSize=MaximumWriteSize;
	}
	memcpy((void *)pValueBuffer, (void *)&KeyValue, nEffectiveSize);

	// Set data in registry with low-level function
	nResult=RawWriteValue(pKeyName, pValueBuffer, nEffectiveSize, nValueType);

	return(nResult);
}

LONG CWindowsRegistry::RawWriteValue(LPCTSTR pKeyName, LPBYTE pValueBuffer, DWORD nValueBufferSize, DWORD nType)
{
	LONG nResult=ERROR_SUCCESS;
	HKEY tempKeyHandle = NULL;
	DWORD nReserved=0;
	char pSubKeyName[FWR_MAXIMUM_KEY_SIZE];
	char pSubPathName[FWR_MAXIMUM_KEY_SIZE];
	BOOL bCloseOnExit = FALSE;

    // Check parameter
    if ( pKeyName==NULL ) {
        return(ERROR_INVALID_PARAMETER);
    }

	if (!IsOpen()) {
		return(ERROR_INVALID_HANDLE);
	}
	const char *separatorPosition = strrchr(pKeyName, '\\');
	if (separatorPosition) {
		// We were given a path name with slashes: must open new handle
		DWORD ulOptions=0;
		bCloseOnExit = TRUE;
		// Fetch name of the last key in the path
		strncpy(pSubKeyName, separatorPosition+1, FWR_MAXIMUM_KEY_SIZE);
		// Fetch name of the sub-path, except the last key
		strncpy(pSubPathName, pKeyName, separatorPosition - pKeyName);
		pSubPathName[separatorPosition - pKeyName]='\0';
		nResult=RegOpenKeyEx(m_pKey,           // handle to base key
							 pSubPathName,     // name of subkey to open
							 ulOptions,        // reserved
							 KEY_SET_VALUE,    // security access mask
							 &tempKeyHandle    // handle of opened key
							 );
	} else {
		// We were given a direct value name: reuse currently open handle
		tempKeyHandle = m_pKey;
		strncpy(pSubKeyName, pKeyName, FWR_MAXIMUM_KEY_SIZE);
	}

	if (nResult == ERROR_SUCCESS) {
		nResult = RegSetValueEx(tempKeyHandle,
								pSubKeyName,
								nReserved,
								nType,
								pValueBuffer,
								nValueBufferSize);
	} else {
		// Force WIN32 error (it is not set by default).
		SetLastError(nResult);
	}

	if (bCloseOnExit) {
		RegCloseKey(tempKeyHandle);
	}

	return(nResult);
}

const char *CWindowsRegistry::GetKeyName(void) const
{
	return(m_pSubKeyName);
}

BOOL CWindowsRegistry::EnumerateSubKeys(const DWORD subkeyIndex, char * pSubKeyName)
{
	LONG nResult=ERROR_SUCCESS;

	DWORD size_of_subkey_name_string = FWR_MAXIMUM_KEY_SIZE;

	::ZeroMemory(pSubKeyName,FWR_MAXIMUM_KEY_SIZE);

	nResult = RegEnumKey(m_pKey, 
                         subkeyIndex, 
                         pSubKeyName, 
                         size_of_subkey_name_string
						);
	if ( nResult == ERROR_SUCCESS )
	{
		// There are still subkeys
		return( TRUE );
	}
	else
	{
		// No more subkeys
		return( FALSE );
	}
}

BOOL CWindowsRegistry::EnumerateValues( const DWORD subValueIndex, char *pSubValueName)
{
	LONG nResult=ERROR_SUCCESS;

	DWORD size_of_subkey_name_string = FWR_MAXIMUM_KEY_SIZE;

	::ZeroMemory(pSubValueName,FWR_MAXIMUM_KEY_SIZE);

	nResult = RegEnumValue(m_pKey, 
						   subValueIndex, 
                           pSubValueName, 
                           &size_of_subkey_name_string,
						   NULL,   // Reserved
						   NULL,   // value type: not required
						   NULL,   // value data: not required
						   NULL    // value data size: not required
						   );
	if ( nResult == ERROR_SUCCESS )
	{
		// There are still subkeys
		return( TRUE );
	}
	else
	{
		// No more subkeys
		return( FALSE );
	}
}

LONG CWindowsRegistry::CopyKeys(LPCTSTR srcKey, LPCTSTR dstKey)
{
	LONG nResult=ERROR_SUCCESS;

  // Open the source
	CWindowsRegistry srcRegistry;
	nResult = srcRegistry.OpenKey(srcKey, NULL, KEY_READ);
  if (nResult != ERROR_SUCCESS) {
	  // Error while opening source
		return(nResult);
	}
	// Open or create the destination
	CWindowsRegistry dstRegistry;
	nResult = dstRegistry.OpenKey(dstKey, NULL, KEY_WRITE);
	if (nResult != ERROR_SUCCESS) {
		nResult = dstRegistry.CreateKey(dstKey, NULL, KEY_WRITE);
		if (nResult != ERROR_SUCCESS) {
			// Can't open & can't create: error
			return(nResult);
		}
	}

	// Loop on all subkeys
	char srcSubKey[FWR_MAXIMUM_KEY_SIZE];
	char fullDstKey[FWR_MAXIMUM_KEY_SIZE];
	char fullSrcKey[FWR_MAXIMUM_KEY_SIZE];
  for (int i=0; srcRegistry.EnumerateSubKeys(i, srcSubKey)==TRUE; i++) {
	  strcpy(fullDstKey, dstKey);
		strcat(fullDstKey, "\\");
		strcat(fullDstKey, srcSubKey);
	  strcpy(fullSrcKey, srcKey);
		strcat(fullSrcKey, "\\");
		strcat(fullSrcKey, srcSubKey);
		// Recursive call for subkeys
		nResult=CopyKeys(fullSrcKey, fullDstKey);
		if (nResult != ERROR_SUCCESS) {
			return(nResult);
		}
  }
	// Copy values
  for (int j=0; srcRegistry.EnumerateValues(j, srcSubKey)==TRUE; j++) {
		// Copy value
    char *pStringValue;
    DWORD IntegerValue;

		nResult = srcRegistry.OpenKey(srcKey,NULL,KEY_READ);
    if (nResult == ERROR_SUCCESS) {
			nResult = dstRegistry.OpenKey(dstKey,NULL,KEY_WRITE);
			if (nResult == ERROR_SUCCESS) {
				// Reads the value (if it's a string)
				nResult = srcRegistry.QueryValue(srcSubKey,&pStringValue);
				if (nResult == ERROR_SUCCESS) {
					nResult = dstRegistry.WriteValue(srcSubKey, pStringValue);
					if (pStringValue) {
						delete [] pStringValue;
					}
				} else {
					// Reads the value (if it's an integer)
					nResult = srcRegistry.QueryValue(srcSubKey,IntegerValue);
					if (nResult == ERROR_SUCCESS) {
						nResult = dstRegistry.WriteValue(srcSubKey, IntegerValue);
					} else {
						return(nResult);
					}
				}
			}
		}
	}

	return(nResult);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -