最近在做一款局域网的web应用,采用B/S模式,但是其中一个模块需要在网页端做出读写IC卡的操作,如果在后台直接调用读卡器接口只能触发服务器端对读卡器的动作,想要实现在浏览器端直接操作读卡器,就需要自己制作activeX控件,然后在网页前台调用控件的一些方法、属性或事件。
1、创建ActiveX控件项目
输入项目名称,点击确定->完成
查看类视图其中主要用到“工程名Ctrl”类和“工程名Lib”下的“_D工程名”接口
2、项目配置
右键项目->属性->配置属性->常规:
1)“MFC的使用”选择“在静态库中使用MFC”(如果选择在共享DLL中使用,可能在Win7下无法注册组件);
2)“字符集”选择“未配置”(选择unicode会报无法将char[n]转化为LPCTSTR的编译错误)
3、添加安全接口(参考http://blog.sina.com.cn/s/blog_5d154c460100f8z9.html)
在"工程名Ctrl.h"文件中添加头文件
#include <objsafe.h>
查找DECLARE_DYNCREATE(工程名Ctrl)
在其后添加如下代码
DECLARE_INTERFACE_MAP() BEGIN_INTERFACE_PART(ObjSafe, IObjectSafety) STDMETHOD_(HRESULT, GetInterfaceSafetyOptions) ( REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions ); STDMETHOD_(HRESULT, SetInterfaceSafetyOptions) ( REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions
打开"工程名Ctrl.cpp"查找
BOOL C工程名Ctrl::C工程名CtrlFactory::UpdateRegistry(BOOL bRegister) { // TODO: Verify that your control follows apartment-model threading rules. // Refer to MFC TechNote 64 for more information. // If your control does not conform to the apartment-model rules, then // you must modify the code below, changing the 6th parameter from // afxRegApartmentThreading to 0. if (bRegister) return AfxOleRegisterControlClass( AfxGetInstanceHandle(), m_clsid, m_lpszProgID, IDS_工程名, //这里的工程名必须为大写 IDB_工程名, //这里的工程名必须为大写 afxRegApartmentThreading, _dwGetLocalOleMisc, _tlid, _wVerMajor, _wVerMinor); else return AfxOleUnregisterClass(m_clsid, m_lpszProgID); }
将此段代码替换为
// Interface map for IObjectSafety BEGIN_INTERFACE_MAP( C工程名Ctrl, COleControl ) INTERFACE_PART(C工程名Ctrl, IID_IObjectSafety, ObjSafe) END_INTERFACE_MAP() ///////////////////////////////////////////////////////////////////////////// // IObjectSafety member functions // Delegate AddRef, Release, QueryInterface ULONG FAR EXPORT C工程名Ctrl::XObjSafe::AddRef() { METHOD_PROLOGUE(C工程名Ctrl, ObjSafe) return pThis->ExternalAddRef(); } ULONG FAR EXPORT C工程名Ctrl::XObjSafe::Release() { METHOD_PROLOGUE(C工程名Ctrl, ObjSafe) return pThis->ExternalRelease(); } HRESULT FAR EXPORT C工程名Ctrl::XObjSafe::QueryInterface( REFIID iid, void FAR* FAR* ppvObj) { METHOD_PROLOGUE(C工程名Ctrl, ObjSafe) return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj); } const DWORD dwSupportedBits = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; const DWORD dwNotSupportedBits = ~ dwSupportedBits; ///////////////////////////////////////////////////////////////////////////// // CStopLiteCtrl::XObjSafe::GetInterfaceSafetyOptions // Allows container to query what interfaces are safe for what. We‘re // optimizing significantly by ignoring which interface the caller is // asking for. HRESULT STDMETHODCALLTYPE C工程名Ctrl::XObjSafe::GetInterfaceSafetyOptions( REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions) { METHOD_PROLOGUE(C工程名Ctrl, ObjSafe) HRESULT retval = ResultFromScode(S_OK); // does interface exist? IUnknown FAR* punkInterface; retval = pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface); if (retval != E_NOINTERFACE) { // interface exists punkInterface->Release(); // release it--just checking! } // we support both kinds of safety and have always both set, // regardless of interface *pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits; return retval; // E_NOINTERFACE if QI failed } ///////////////////////////////////////////////////////////////////////////// // CStopLiteCtrl::XObjSafe::SetInterfaceSafetyOptions // Since we‘re always safe, this is a no-brainer--but we do check to make // sure the interface requested exists and that the options we‘re asked to // set exist and are set on (we don‘t support unsafe mode). HRESULT STDMETHODCALLTYPE C工程名Ctrl::XObjSafe::SetInterfaceSafetyOptions( REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions) { METHOD_PROLOGUE(C工程名Ctrl, ObjSafe) // does interface exist? IUnknown FAR* punkInterface; pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface); if (punkInterface) { // interface exists punkInterface->Release(); // release it--just checking! } else { // interface doesn‘t exist return ResultFromScode(E_NOINTERFACE); } // can‘t set bits we don‘t support if (dwOptionSetMask & dwNotSupportedBits) { return ResultFromScode(E_FAIL); } // can‘t set bits we do support to zero dwEnabledOptions &= dwSupportedBits; // (we already know there are no extra bits in mask ) if ((dwOptionSetMask & dwEnabledOptions) != dwOptionSetMask) { return ResultFromScode(E_FAIL); } // don‘t need to change anything since we‘re always safe return ResultFromScode(S_OK); } ///////////////////////////////////////////////////////////////////////////// // C工程名Ctrl::C工程名CtrlFactory::UpdateRegistry - // Adds or removes system registry entries for C工程名Ctrl BOOL C工程名Ctrl::C工程名CtrlFactory::UpdateRegistry(BOOL bRegister) { // TODO: Verify that your control follows apartment-model threading rules. // Refer to MFC TechNote 64 for more information. // If your control does not conform to the apartment-model rules, then // you must modify the code below, changing the 6th parameter from // afxRegApartmentThreading to 0. if (bRegister) return AfxOleRegisterControlClass( AfxGetInstanceHandle(), m_clsid, m_lpszProgID, IDS_工程名, //这里的工程名为大写 IDB_工程名, //这里的工程名为大写 afxRegApartmentThreading, _dw工程名OleMisc, _tlid, _wVerMajor, _wVerMinor); else return AfxOleUnregisterClass(m_clsid, m_lpszProgID); }
如果跳过此步,网页调用时会出现“对象不支持此属性或方法”的错误
4、添加方法属性
1)添加方法:在类视图中右键“工程名Lib”下的“_D工程名”添加方法,弹出添加方法向导,设置好方法名称参数后,在类视图中可以看到“工程名Ctrl”类下刚才添加的方法,双击方法名,在如下位置添加方法的实现代码
void CZX_fakaCtrl::CardIDChanged(void) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // TODO: 在此添加属性处理程序代码 SetModifiedFlag(); }
2)添加属性:因为读卡需要把读到的卡号传给网页前台,但是本人不知道如何给控件方法传递引用参数,所以就将卡号赋给属性,让前台直接读取控件的属性,跟添加方法类似,在添加属性向导设置属性类型名称,这里设置为BSTR CardID,实现类型选择“get/set方法”。打开“工程名Ctrl.h”文件找到声明的protected变量位置,添加CString m_cardID变量,在“工程名Ctrl”类下找到此属性的get()方法,修改返回值return m_cardID.AllocSysString();,其中在读卡方法中对m_cardID进行赋值
BSTR CZX_fakaCtrl::GetCardID(void) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strResult; // TODO: 在此添加调度处理程序代码 return m_cardID.AllocSysString(); }
(参考http://msdn.microsoft.com/zh-cn/library/cc451425(v=vs.71).aspx)
生成ocx文件,到此一个简单的activeX控件完成
5、在网页进行测试
注册组件:在cmd下利用regsvr32命令注册(注意一些读卡器DLL文件要跟ocx文件放在同一文件夹下)
在网页中添加此控件,其中clsid为控件类的GUID值,在.idl文件中“// C工程名Ctrl 的类信息”后可以找到
<OBJECT id="ReadcardControl" classid="clsid:25717798-8BF4-4601-96A4-965C7296C733"> </OBJECT>
利用js调用控件的方法
<script type="javascript#text"> function readCard(){ var rcc=document.getElementById("ReadcardControl") var a=rcc.readCardID(); var b=rcc.CardID alert("函数返回:"+a+"\n卡号:"+b); } </script>