Development of C + + ActiveX controls

Source: Internet
Author: User

Recently in a LAN Web application, the use of B/s mode, but one of the modules need to read and write IC card operation, if directly in the background call Card reader interface can only trigger the server-side action on the reader, want to achieve in the browser directly manipulate the reader, You need to make ActiveX controls yourself, and then invoke some methods, properties, or events for the control in the Web page foreground.

1. Create an ActiveX control project

Enter the project name and click OK to finish

View Class View where the _d project name interface is used primarily in the "Project name Ctrl" Class and "project name Lib"

2. Project configuration

General, configuration properties, properties, right-click Project

1) "Use of MFC" Select "Use MFC in a static library" (if you choose to use in a shared DLL, you may not be able to register the component under WIN7);

2) "Character set" select "Not Configured" (select Unicode will not convert Char[n] to LPCTSTR compilation error)

3. Add a secure interface (Refer to http://blog.sina.com.cn/s/blog_5d154c460100f8z9.html)

Add a header file to the project name Ctrl.h file

#include <objsafe.h>

Find Declare_dyncreate (project name ctrl)

After that, add the following code

Declare_interface_map () Begin_interface_part (Objsafe, IObjectSafety)  Stdmethod_ (HRESULT, getinterfacesafetyoptions) (            refiid riid,            *pdwsupportedoptions,            * pdwenabledoptions  );               Stdmethod_ (HRESULT, SetInterfaceSafetyOptions) (            refiid riid,            DWORD Dwoptionsetmask,            DWORD Dwenabledoptions

Open the "project name Ctrl.cpp" Lookup

BOOL C project name CTRL::C project name Ctrlfactory::updateregistry (bool bregister) {//todo:verify that your control follows Apartment-model threading rules.//Refer to MFC TechNote-more information.//If your control does not conform to the Apartment-model rules and then//You must modify the code below, changing the 6th parameter from//afxregapartmentthreading to 0. if(bregister)returnAfxOleRegisterControlClass (AfxGetInstanceHandle (), M_clsid, M_lpszprogid, Ids_ project name,//the project name must be capitalized here.Idb_ Project Name,//the project name must be capitalized here.afxregapartmentthreading, _dwgetlocalolemisc, _tlid, _wvermajor, _wverminor); Else  returnAfxoleunregisterclass (M_clsid, M_lpszprogid);}

Replace this piece of code with the

//Interface map for IObjectSafetyBegin_interface_map (c project name CTRL, COleControl) Interface_part (c project name CTRL, Iid_iobjectsafety, Objsafe) end_interface_ MAP ()///////////////////////////////////////////////////////////////////////////////IObjectSafety member functions//Delegate AddRef, Release, QueryInterfaceULONG far EXPORT C project name Ctrl::xobjsafe::addref () {Method_prologue (c project name CTRL, Objsafe)returnPthis->externaladdref ();} ULONG far EXPORT C project name Ctrl::xobjsafe::release () {Method_prologue (c project name CTRL, Objsafe)returnPthis->externalrelease ();} HRESULT far EXPORT C project name Ctrl::xobjsafe::queryinterface (REFIID iid,voidfar* far*ppvobj) {Method_prologue (c project name CTRL, Objsafe)return(HRESULT) Pthis->externalqueryinterface (&iid, ppvobj);}ConstDWORD dwsupportedbits =Interfacesafe_for_untrusted_caller|Interfacesafe_for_untrusted_data;ConstDWORD dwnotsupportedbits = ~dwsupportedbits;///////////////////////////////////////////////////////////////////////////////cstoplitectrl::xobjsafe::getinterfacesafetyoptions//allows container to query what interfaces is safe for. We ' re//optimizing significantly by ignoring which interface the caller is//asking for.HRESULT stdmethodcalltype C project name Ctrl::xobjsafe::getinterfacesafetyoptions (REFIID riid, DWORD __rpc_far*pdwsupportedoptions, DWORD __rpc_far*pdwenabledoptions) {Method_prologue (c project name CTRL, Objsafe) HRESULT retval=Resultfromscode (S_OK);//does interface exist?IUnknown far*punkinterface; retval= Pthis->externalqueryinterface (&riid, (void* *) &punkinterface); if(retval! = e_nointerface) {//Interface existsPunkinterface->release ();//release It--just checking! }  //We support both kinds of safety and has always both set,//regardless of interface*pdwsupportedoptions = *pdwenabledoptions =dwsupportedbits;returnretval//e_nointerface if QI failed}///////////////////////////////////////////////////////////////////////////////cstoplitectrl::xobjsafe::setinterfacesafetyoptions//Since we ' re always safe, this is a no-brainer--but we does check to make//sure the interface requested exists and that the options we ' re asked to//set exist and is set on (we don't support unsafe mode).HRESULT stdmethodcalltype C project name Ctrl::xobjsafe::setinterfacesafetyoptions (REFIID riid, DWORD Dwoptionsetm Ask, DWORD dwenabledoptions) {method_prologue (c project name CTRL, Objsafe)//does interface exist?IUnknown far*punkinterface; PThis->externalqueryinterface (&riid, (void* *) &punkinterface); if(Punkinterface) {//Interface existsPunkinterface->release ();//release It--just checking! } Else{//interface doesn ' t exist  returnResultfromscode (e_nointerface);} //can ' t set bits we don ' t support if(Dwoptionsetmask &dwnotsupportedbits) {  returnResultfromscode (E_FAIL);} //can ' t set bits we do support to zeroDwenabledoptions &=dwsupportedbits;//(We already know there is no extra bits in mask) if((Dwoptionsetmask & dwenabledoptions)! =dwoptionsetmask) {  returnResultfromscode (E_FAIL);} //don ' t need to change anything since we ' re always safe returnResultfromscode (S_OK);}///////////////////////////////////////////////////////////////////////////////C project name CTRL::C project name Ctrlfactory::updateregistry-//Adds or removes system registry entries for C project name CtrlBOOL C project name CTRL::C project name Ctrlfactory::updateregistry (bool bregister) {//todo:verify that your control follows Apartment-model threading rules.//Refer to MFC TechNote-more information.//If your control does not conform to the Apartment-model rules and then//You must modify the code below, changing the 6th parameter from//afxregapartmentthreading to 0. if(bregister)returnAfxOleRegisterControlClass (AfxGetInstanceHandle (), M_clsid, M_lpszprogid, Ids_ project name,//the project here is named capital.Idb_ Project Name,//the project here is named capital.afxregapartmentthreading, _DW project name Olemisc, _tlid, _wvermajor, _wverminor); Else  returnAfxoleunregisterclass (M_clsid, M_lpszprogid);}

If you skip this step, the "object does not support this property or method" error appears when the page is called

4. Add Method Properties

1) Add method: In Class View Right-click on "project name Lib" under "_d Project Name" Add method, Pop Up Add Method Wizard, set the method name parameter, in Class View you can see the "Project name Ctrl" Class just added method, double-click the method name, add the method implementation code in the following location

void Czx_fakactrl::cardidchanged (void) {    afx_manage_state (afxgetstaticmodulestate ());     // TODO: Add Property handler code here     SetModifiedFlag ();}

2) Add properties: Because the card reader needs to read the card number to the front page, but I do not know how to pass the reference parameter to the control method, so the card number is assigned to the property, the foreground directly read the control's properties, similar to the Add method, in the Add Property Wizard set the property type name, here is set as BSTR CardID , implement type Select "Get/set method". Open the project name Ctrl.h file Locate the declared protected variable, add the CString m_cardid variable, locate the Get () method for this property under the "Project name Ctrl" Class, modify the return value to return m_ Cardid.allocsysstring (), where the M_cardid is assigned in the card reader method

BSTR Czx_fakactrl::getcardid (void) {    afx_manage_state (afxgetstaticmodulestate ());    CString strresult;     // TODO: Add the Dispatch handler code here    return m_cardid.allocsysstring ();}

(Refer to http://msdn.microsoft.com/zh-cn/library/cc451425 (v=vs.71). aspx)

Generate an OCX file to this simple ActiveX control to complete

5. Testing on the Web

Registering components: Registering with the regsvr32 command under CMD (note that some reader DLL files are to be placed in the same folder as the OCX file)

Add this control to the Web page, where the CLSID is the GUID value of the control class, which you can find in the. idl file "//C class information for project name CTRL".

<id= "Readcardcontrol"  classid= "clsid : 25717798-8bf4-4601-96a4-965c7296c733 "></OBJECT>

Methods of invoking a control using JS

<Scripttype= "Javascript#text">functionReadcard () {varRCC=document.getElementById ("Readcardcontrol")    vara=Rcc.readcardid (); varb=RCC. CardID Alert ("function returns:"+a+"\ n Card number:"+b);}</Script>

Development of C + + ActiveX controls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.