703
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "Registry.h"
#include "Global.h"
const char *g_RegTable[][3] = {
// format is { key, value name, value}
{ "CLSID\\{F8771B89-7971-4085-8D5F-88F7C140698B}", 0, "FastAddition" },
{ "CLSID\\{F8771B89-7971-4085-8D5F-88F7C140698B}\\InprocServer32", 0,
(const char*)-1 // rogue value indicating file name
},
{ "CLSID\\{F8771B89-7971-4085-8D5F-88F7C140698B}\\ProgID", 0, "Fooo.FastAddition.1" },
{ "Fooo.FastAddition.1", 0, "FastAddition" },
{ "Fooo.FastAddition.1\\CLSID", 0, "{F8771B89-7971-4085-8D5F-88F7C140698B}" },
};
STDAPI DllRegisterServer(void){
HRESULT hr = S_OK;
// look up server's file name
char szFileName[MAX_PATH];
GetModuleFileNameA(g_hModule, szFileName, MAX_PATH);
// register entries from table
int nEntries = sizeof(g_RegTable) / sizeof(*g_RegTable);
for (int i = 0; SUCCEEDED(hr) && i < nEntries; i++) {
const char *pszKeyName = g_RegTable[i][0];
const char *pszValueName = g_RegTable[i][1];
const char *pszValue = g_RegTable[i][2];
// map rogue value to module file name
if (pszValue == (const char*)-1)
pszValue = szFileName;
HKEY hkey;
// create the key
long err = RegCreateKeyA(HKEY_CLASSES_ROOT,
pszKeyName, &hkey);
if (err == ERROR_SUCCESS) {
// set the value
err = RegSetValueExA(hkey, pszValueName, 0,
REG_SZ, (const BYTE*)pszValue,
(strlen(pszValue) + 1));
RegCloseKey(hkey);
}
else {
// if cannot add key or value, back out and fail
DllUnregisterServer();
hr = SELFREG_E_CLASS;
}
}
return hr;
}
STDAPI DllUnregisterServer(void){
HRESULT hr = S_OK;
int nEntries = sizeof(g_RegTable) / sizeof(*g_RegTable);
for (int i = nEntries - 1; i >= 0; i--){
const char *pszKeyName = g_RegTable[i][0];
long err = RegDeleteKeyA(HKEY_CLASSES_ROOT, pszKeyName);
if (err != ERROR_SUCCESS)
hr = S_FALSE;
}
return hr;
}
regsvr32 e:\FoooSln\Release\Fooo.dll