RegSvr32 加载失败,找不到指定的模块
应用ocx注册时, 出现下图错误:
解决方案(检测缺少库):
0. 使用管理员运行cmd,注册ocx
1. mfc100.dll、mfc100u.dll、msvcp100.dll、msvcr100.dll之类的库(可能是ocx工程属性为Use MFC in a Shared DLL)
2. 可能是缺少应用程序需要的库,可以写个简单的win32控制台程序。
(ocx依赖的库全部#pragma comment进来,调用一个库函数),运行demo,会提示缺少库的名称
2. 或者使用win32程序直接调用ocx,查看错误提示
3. 使用depends工具查看缺少库
4. 工程间使用不同的MD(MDD)、MT(MTD), 也可能导致程序无法运行
附ocx注册为安全组件代码
1 // Hello.cpp : Implementation of CHelloApp and DLL registration. 2 3 #include "stdafx.h" 4 #include <objsafe.h> 5 6 #ifdef _DEBUG 7 #define new DEBUG_NEW 8 #endif 9 10 11 CHelloApp theApp; 12 13 const GUID CDECL _tlid = { 0x872CF3E6, 0x5EBF, 0x4436, { 0xA5, 0x25, 0x58, 0xCE, 0xF, 0xAA, 0x38, 0x5B } }; 14 //注意与idl middleocx uuid(51DF1D91-DA4E-47DA-A5BE-84A96ADD2425)相同, 之前注册了ocx,需要卸载后重新注册 15 // 注册ocx: regsvr32 middleocx.ocx 16 // 卸载ocx: regsvr32 /u middleocx.ocx 17 const CATID CLSID_SafeItem = { 0x51DF1D91, 0xDA4E, 0x47DA, { 0xA5, 0xBE, 0x84, 0xA9, 0x6A, 0xDD, 0x24, 0x25 } }; 18 const WORD _wVerMajor = 1; 19 const WORD _wVerMinor = 0; 20 21 22 23 // CHelloApp::InitInstance - DLL initialization 24 25 BOOL CHelloApp::InitInstance() 26 { 27 BOOL bInit = COleControlModule::InitInstance(); 28 29 if (bInit) 30 { 31 // TODO: Add your own module initialization code here. 32 } 33 return bInit; 34 } 35 36 37 38 // CHelloApp::ExitInstance - DLL termination 39 int CHelloApp::ExitInstance() 40 { 41 // TODO: Add your own module termination code here. 42 return COleControlModule::ExitInstance(); 43 } 44 45 46 47 // 创建组件种类 48 HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription) 49 { 50 ICatRegister* pcr = NULL ; 51 HRESULT hr = S_OK ; 52 53 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, 54 NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr); 55 if (FAILED(hr)) 56 return hr; 57 58 // Make sure the HKCR\Component Categories\{..catid...} 59 // key is registered. 60 CATEGORYINFO catinfo; 61 catinfo.catid = catid; 62 catinfo.lcid = 0x0409 ; // english 63 64 // Make sure the provided description is not too long. 65 // Only copy the first 127 characters if it is. 66 int len = wcslen(catDescription); 67 if (len>127) 68 len = 127; 69 wcsncpy_s(catinfo.szDescription, catDescription, len); 70 // Make sure the description is null terminated. 71 catinfo.szDescription[len] = '\0'; 72 73 hr = pcr->RegisterCategories(1, &catinfo); 74 pcr->Release(); 75 76 return hr; 77 } 78 79 // 注册组件种类 80 HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 81 { 82 // Register your component categories information. 83 ICatRegister* pcr = NULL ; 84 HRESULT hr = S_OK ; 85 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, 86 NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr); 87 if (SUCCEEDED(hr)) 88 { 89 // Register this category as being "implemented" by the class. 90 CATID rgcatid[1] ; 91 rgcatid[0] = catid; 92 hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid); 93 } 94 if (pcr != NULL) 95 pcr->Release(); 96 return hr; 97 } 98 99 // 卸载组件种类 100 HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 101 { 102 ICatRegister* pcr = NULL ; 103 HRESULT hr = S_OK ; 104 105 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, 106 NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr); 107 if (SUCCEEDED(hr)) 108 { 109 // Unregister this category as being "implemented" by the class. 110 CATID rgcatid[1] ; 111 rgcatid[0] = catid; 112 hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid); 113 } 114 115 if (pcr != NULL) 116 pcr->Release(); 117 118 return hr; 119 } 120 121 // DllRegisterServer - Adds entries to the system registry 122 123 STDAPI DllRegisterServer(void) 124 { 125 AFX_MANAGE_STATE(_afxModuleAddrThis); 126 127 if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid)) 128 return ResultFromScode(SELFREG_E_TYPELIB); 129 130 if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE)) 131 return ResultFromScode(SELFREG_E_CLASS); 132 133 // 标记控件初始化安全. 134 // 创建初始化安全组件种类 135 HRESULT hr = CreateComponentCategory(CATID_SafeForInitializing, 136 L"Controls safely initializable from persistent data!"); 137 if (FAILED(hr)) 138 return hr; 139 // 注册初始化安全 140 hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); 141 if (FAILED(hr)) 142 return hr; 143 144 // 标记控件脚本安全 145 // 创建脚本安全组件种类 146 hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!"); 147 if (FAILED(hr)) 148 return hr; 149 // 注册脚本安全组件种类 150 hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting); 151 if (FAILED(hr)) 152 return hr; 153 154 return NOERROR; 155 } 156 157 158 159 // DllUnregisterServer - Removes entries from the system registry 160 161 STDAPI DllUnregisterServer(void) 162 { 163 AFX_MANAGE_STATE(_afxModuleAddrThis); 164 165 if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor)) 166 return ResultFromScode(SELFREG_E_TYPELIB); 167 168 if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE)) 169 return ResultFromScode(SELFREG_E_CLASS); 170 171 return NOERROR; 172 }