DLL ActiveX control WEB page call example_javascript skills
WBOYOriginal
2016-05-16 18:21:401699browse
1. Overview Due to the needs of the project, I started to learn and research VC, DLL and ActiveX controls. I found a lot of information online, but none was available or there was no example that could be understood and run. No way, just do your own research. Hard work pays off, and you will eventually achieve success. Haha, now I have summarized my own learning and dedicated it to those who need it. DLL (dynamic link library): divided into WIN32 DLL and MFC DLL ActiveX: divided into ATL control and MFC control (also a DLL) WEB: JAVASCRIPT call-> ActiveX call-> DLL Completes the addition operation and returns the value, which is displayed on the page. 2. Development (VS2008) 1. DLL library writing: File -> New -> WIN32 console -> Fill in the project name -> Select DLL -> Empty project -"Finish. (1) In the solution panel, add a header file testdll.h, content:
//extern "C" tells the compiler that this part can be used in C/C. extern "C" { DECLDIR int Add( int a, int b ); DECLDIR void Function( void ); } #endif
(2) In the solution panel, add an implementation file testdll.cpp, content:
(3) Optional. Create a new WIN32 console class and test this DLL. File -> New -> WIN32 Console -> Fill in the project name -> Select the console program -> Empty project -> Complete. In the solution panel, add an implementation file loaddll.cpp content:
AddFunc _AddFunc; FunctionFunc _FunctionFunc; cout <<"---Get DLL- --."<< endl; // L means to use the UNICODE character set, which must be consistent with the character set of the project. HINSTANCE hInstLibrary = LoadLibrary(L"E:\Project\VS\LoadDll\Release\TestDll.dll"); if (hInstLibrary == NULL) { cout <<" Dll loading [failed]."<< endl; FreeLibrary(hInstLibrary); }else{ cout <<"Dll loading [successful]."<< endl; } _AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add"); _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary, "Function"); if ((_AddFunc == NULL) || (_FunctionFunc == NULL)) { FreeLibrary(hInstLibrary);//Release }else{ cout <<"---Get DLL function [OK]---. "<< endl; } cout << _AddFunc(1, 1) << endl; // Start calling _FunctionFunc(); // cin.get (); // Get focus so that the program will not flash by. FreeLibrary(hInstLibrary);//After calling, release the memory. return(1); }
2. ActiveX control implementation: Here we choose ATL control implementation instead of MFC ActiveX. File -> New -> ATL Project -> Fill in the project name ("FROMYANTAI") -> Select the dynamic link library (DLL) -> Complete. After completion, many header H files and CPP implementation files will be generated in the "Solution Explorer" on the right. These are the default and do not need to be modified. (1) Add an ALT simple object: Click the project name (the name just given) with the mouse and select - "Add class -" select ATL simple object. Next step, give a name: "ytiicrj" -> Next step: Leave everything else unchanged, in support, select "Connection Point" and "IE Object Support" -> Complete. The next step is to add a method to "ytiicrj" so that it can be called by the WEB page. Select "iytiicrj" in "Class View" (there is a gray key icon), right-click and add -> Add method. Name the method "GetContent" -"Select IN for parameter attributes, select LONG for parameter type, parameter name A -"Add; continue; select IN for parameter attributes, select LONG for parameter type, parameter name B -"Add; continue; select OUT and parameter attributes RETVAL, select LONG* for parameter type and parameter name out –》Add---》Click to complete. This adds one (in the last line) to the ytiicrj.H header file: STDMETHOD(GetContent)(LONG a, LONG b, LONG* out); And in the ytiicrj.CPP file Added an implementation class:
STDMETHODIMP CCaluNumCtrl::GetContent( LONG a, LONG b, LONG* out) { // TODO: Add implementation code here return S_OK; }
(2), in In the ytiicrj.H file, call the DLL class library. The code is as follows: // CaluNumCtrl.h: Statement of ytiicrj The bold part is the specific implementation, and the others are untouched.
#pragma once #include "resource.h " // Main symbol #include // Add #include "AtlActiveX_i.h" #include "_ICaluNumCtrlEvents_CP.h" #if defined(_WIN32_WCE) && ! defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA) #error "Single-threaded COM objects are not properly supported on Windows CE platforms (such as Windows Mobile platforms that do not provide full DCOM support). Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support the creation of single-threaded objects. COM object implementation and allows the use of its single-threaded COM object implementation. The threading model in the rgs file has been set to "Free" because this model is the only threading model supported by non-DCOM Windows CE platforms. " #endif // ytiicrj class ATL_NO_VTABLE Cytiicrj: //Add a line: Security prompt is removed, -- when running the browser call, no security question will be prompted. public IObjectSafetyImpl, public IConnectionPointContainerImpl, public CProxy_ICaluNumCtrlEvents , public IObjectWithSiteImpl, public IDispatchImpl { public: //The following three lines of implementation definition. typedef int (*AddFunc)(int,int); //Type definition, corresponding to the DLL ADD method. Func is customized and can be written as you wish. HINSTANCE hInstLibrary; AddFunc _AddFunc; //Class mapping Cytiicrj() { //Start calling the DLL for calculation. hInstLibrary = LoadLibrary(L"TestDll.dll");//Place the written DLL file in the directory generated by this project if (hInstLibrary == NULL) { FreeLibrary( hInstLibrary);//Resource release }else{ } //Call the method and return the method handle. _AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add"); } DECLARE_REGISTRY_RESOURCEID(IDR_CALUNUMCTRL) BEGIN_COM_MAP(Cytiicrj) COM_INTERFACE_ENTRY(ICaluNumCtrl) COM_INTERFACE_ENTRY(IDispatch) COM_INTERFACE_ENTRY(IConnectionPointContainer) COM_INTERFACE_ENTRY(IObjectWithSite) //Add a line: Disable the security prompt, -- when running the browser call, no security questions will be prompted. COM_INTERFACE_ENTRY(IObjectSafety) END_COM_MAP() BEGIN_CONNECTION_POINT_MAP(Cytiicrj) CONNECTION_POINT_ENTRY(__uuidof(_ICaluNumCtrlEvents)) END_CONNECTION_POINT_MAP() DECLARE_PROTECT_FINAL_CONSTRUCT() HRESULT FinalConstruct() { return S_OK; } void FinalRelease() { FreeLibrary(hInstLibrary); } public: STDMETHOD(GetContent)(LONG a, LONG b, LONG* out); }; OBJECT_ENTRY_AUTO(__uuidof(CaluNumCtrl), Cytiicrj)
(3) Go back to the ytiicrj.PP file and add the implementation code as follows:
STDMETHODIMP CCaluNumCtrl::GetContent(LONG a, LONG b, LONG* out) { // TODO: Add implementation code here int sum = this-> ;_AddFunc(static_cast(a),static_cast(b)); *out = static_cast(sum); this->_AtlFinalRelease(); return S_OK ; }
(4) Generate DLL: This step is very simple, select Release mode, click on the project to generate (it will prompt you to select REG32 registration, then select it). This generates a lot of files in the Release directory, and what we want is a DLL file. 3. DLL and ATL ActiveX control DLL are packaged as CAB files: For example: after generating test.CAB, the WEB page will prompt for download and installation. (1) First define the setup.inf file: it describes the downloaded content and target directory as well as the version number and corresponding DLL file. This needs to be written manually. My content is as follows (please modify the name accordingly):
[version] ; version signature (same for both NT and Win95) do not remove signature="$CHICAGO$" AdvancedINF=2.0 [Add.Code] AtlActiveX.dll=AtlActiveX.dll TestDll.dll=TestDll.dll setup.inf=setup.inf [install.files] AtlActiveX.dll=AtlActiveX.dll TestDll.dll=TestDll.dll setup.inf=setup.inf [AtlActiveX.dll] clsid={4AE870B5-C7FB-4171-A47E-7F57AFD86F67} file-win32-x86 =thiscab FileVersion=1,0,0,1 DestDir=11 RegisterServer=yes [TestDll.dll] file-win32-x86=thiscab DestDir=11 FileVersion=1,0,0,1 RegisterServer=yes [setup.inf] file=thiscab [RegisterFiles] %AtlActiveX.dll ; end of INF file
(2) Integrate resources: Put all the DLLs used in one directory including the setup.inf file, and then run: IExpress command to generate the CAB package . After running, select the first, next, select the third, next, add files (select your DLL and INF files), next, select an output directory and create a CAB file name, then select Second option, next step, select the second option and then OK. This generates a CAB file. (3) The WEB page calls the ActiveX control to perform addition operations: Write a test.htm web page and put the CAB file in a directory. The content of test.htm is as follows:
Description: codeBase="test .CAB#version=9,0,0,1" codeBase represents the relative or absolute path of the file; version represents the version number. If this number is the same as the version number of the INF file, then the page will not be downloaded the second time, otherwise every time downloaded every time. CLSID is the serial number generated by the ActiveX project, which can be found in the *.rgs file of the project. Okay. All steps are completed. Now you run test.htm, the ActiveX control is prompted, you choose to allow it, and then you can call the addition operation. This is just a simple example. In the DLL, you can implement your own application.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn