应用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 HKCRComponent 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] = '