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

📄 registry.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
// registry.cpp
#include <objbase.h>
#include <assert.h>
#include "registry.h"

BOOL setKeyAndValue(const char* pszPath, const char* szSubkey, const char* szValue);
void CLSIDtochar(const CLSID& clsid, char* szCLSID, int length);
LONG recursiveDeleteKey(HKEY hKeyParent, const char* szKeyChild);

const int CLSID_STRING_SIZE = 39;	// Size of a CLSID as a string

// Register the component in the registry.
HRESULT RegisterServer(HMODULE hModule,	const CLSID& clsid, const char* szNiceName,
						const char* szVerIndProgID,	const char* szProgID) 
{
	char szModule[512];							// get server location
	DWORD dwResult = ::GetModuleFileName(hModule, szModule,	sizeof(szModule)/sizeof(char));
	assert(dwResult != 0);

	char szCLSID[CLSID_STRING_SIZE];			// convert the CLSID into a char
	CLSIDtochar(clsid, szCLSID, sizeof(szCLSID));

	char szKey[64];								// Build the key CLSID\\{...}
	strcpy(szKey, "CLSID\\");
	strcat(szKey, szCLSID);
	setKeyAndValue(szKey, NULL, szNiceName);	// Add the CLSID to the registry

	// Add the server filename, ProgID and version-independent subkeys under the CLSID key.
	setKeyAndValue(szKey, "InprocServer32", szModule);
	setKeyAndValue(szKey, "ProgID", szProgID);
	setKeyAndValue(szKey, "VersionIndependentProgID", szVerIndProgID);

	// Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT.
	setKeyAndValue(szVerIndProgID, NULL, szNiceName); 
	setKeyAndValue(szVerIndProgID, "CLSID", szCLSID);
	setKeyAndValue(szVerIndProgID, "CurVer", szProgID);

	// Add the versioned ProgID subkey under HKEY_CLASSES_ROOT.
	setKeyAndValue(szProgID, NULL, szNiceName); 
	setKeyAndValue(szProgID, "CLSID", szCLSID);

	return S_OK;
}

// Remove the component from the registry.
LONG UnregisterServer(const CLSID& clsid, const char* szVerIndProgID, 
					  const char* szProgID) 
{
	char szCLSID[CLSID_STRING_SIZE];		// Convert the CLSID into a char.
	CLSIDtochar(clsid, szCLSID, sizeof(szCLSID));

	char szKey[64];							// Build the key CLSID\\{...}
	strcpy(szKey, "CLSID\\");
	strcat(szKey, szCLSID);
											// Delete the CLSID Key - CLSID\{...}
	LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey);
	assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); 

											// Delete the version-independent ProgID Key.
	lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID);
	assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); 

											// Delete the ProgID key.
	lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID);
	assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND));
	
	return S_OK;
}

// Convert a CLSID to a char string.
void CLSIDtochar(const CLSID& clsid, char* szCLSID, int length) 
{
	assert(length >= CLSID_STRING_SIZE);

	LPOLESTR wszCLSID = NULL;
	HRESULT hr = StringFromCLSID(clsid, &wszCLSID);		// get CLSID
	assert(SUCCEEDED(hr));
	wcstombs(szCLSID, wszCLSID, length);				// convert from wide to non-wide

	CoTaskMemFree(wszCLSID);							// free memory
}

// Delete a key and all of its descendents.
LONG recursiveDeleteKey(HKEY hKeyParent, const char* lpszKeyChild) 
{
	HKEY hKeyChild;										// open the child key
	LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, KEY_ALL_ACCESS, &hKeyChild);
	if (lRes != ERROR_SUCCESS)
		return lRes;

	FILETIME time;
	char szBuffer[256];
	DWORD dwSize = 256;								// enumerate all descendents of this child
	while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time) == S_OK) {
		lRes = recursiveDeleteKey(hKeyChild, szBuffer);	// delete this child's descendents
		if (lRes != ERROR_SUCCESS) {
			RegCloseKey(hKeyChild);
			return lRes;
		}
		dwSize = 256;
	}
	RegCloseKey(hKeyChild);

	return RegDeleteKey(hKeyParent, lpszKeyChild);		// delete this child
}

// Create a key and set its value.
BOOL setKeyAndValue(const char* szKey, const char* szSubkey, const char* szValue) 
{
	HKEY hKey;
	char szKeyBuf[1024];

	strcpy(szKeyBuf, szKey);							// copy keyname into buffer
	if (szSubkey != NULL) 
	{													// append subkey name
		strcat(szKeyBuf, "\\");
		strcat(szKeyBuf, szSubkey );
	}

														// Create and open key and subkey.
	long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT, szKeyBuf, 0, NULL, 
		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
	if (lResult != ERROR_SUCCESS)
		return FALSE;

	if (szValue != NULL)								// set the value
		RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)szValue, 
		(DWORD)strlen(szValue)+1);

	RegCloseKey(hKey);
	return TRUE;
}

⌨️ 快捷键说明

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