1.打开visual studio 2013
2.新建ocx项目: 文件–>新建–>项目–>visual C++ –>MFC –>MFC ActiveX控件–>确定 –>完成
3.快速添加对外开放的方法和属性:切换到类视图–>选中HelloWorldLib下_DHelloWorld右击–>添加–> 添加方法
4.设置不需要MSVCP120*.dll支持 ,解决oxc放到其他电脑上注册不需要MSVCP120*.dll :项目名称右击–>属性 –>(活动debug)(RElease)—>常规 –>MFC 的使用 –>在静态库中使用MFC
5.将ocx设置为安全的(IE调用时不会弹窗提示).对XXX.cpp(项目名.cpp)进行修改
参考和引用该博文http://blog.youkuaiyun.com/waxgourd0/article/details/7411620(1)添加头文件
#include "ComCat.h"
#include "strsafe.h"
#include "ObjSafe.h"
(2)添加 CLSID_SafeItem 值与XXXCtrl.cpp 文件中IMPLEMENT_OLECREATE_EX 的值一致
const GUID CDECL CLSID_SafeItem =
{ 0x1535c313, 0xcbb8, 0x4e88, 0x84, 0x40, 0xe7, 0x8d, 0xa1, 0x68, 0x4c, 0x3f };
(3)添加3个方法
//创建Component Categories中的初始化安全和脚本安全项
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)
{
ICatRegister *pcr = NULL;
HRESULT hr = S_OK;
hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
if (FAILED(hr))
return hr;
// 确认HKCR\Component Categories\{..catid...}键值被注册
CATEGORYINFO catinfo;
catinfo.catid = catid;
catinfo.lcid = 0x0409; // english
//size_t len;
// 确认描述不是太长。