開發ActiveX時候,原本程式執行好好的,註冊OCX/反註冊OCX都很正常,當照著MSDN步驟進行安全碼加註後,每次返註冊OCX都會出現DllUnregisterServer函數出錯,錯誤代碼:0x80070002 可能不敢相信,但以下是MSDN程式碼:
STDAPI DllUnregisterServer(void)
{
HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleUnregisterTypeLib(_tlid))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);
// Remove entries from the registry.
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
if (FAILED(hr)) return hr;
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
if (FAILED(hr)) return hr;
return NOERROR;
}
錯誤代碼:0x80070002,錯誤描述爲:系統找不到指定的文件。但看看注冊表,其實反注冊是成功的,下面說一下錯誤的原因,安全組件其實是在注冊表中添加一些注冊,表明自己安全罷了,關于安全性的注冊是依賴與原組件的,所以上面函數反注冊的順序不正確,應該先反注冊掉安全組件,再反注冊掉原組件,修改後的代碼如下:
STDAPI DllUnregisterServer(void)
HRESULT hr; // HResult used by Safety Functions
{
AFX_MANAGE_STATE(_afxModuleAddrThis);
hr = UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
if (SUCCEEDED(hr))
hr = UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);
return hr;
}