DLL + ActiveX Control + WEB page call example _ javascript skills

Source: Internet
Author: User
Tags one more line
As a result of the project, I began to learn and study VC, DLL and ActiveX controls. I found a lot of online materials, but there was no available or no example to understand and run. No way. Study it by yourself. However, I have learned to give it to the people who need it. I. Overview
As a result of the project, I began to learn and study VC, DLL and ActiveX controls. I found a lot of online materials, but there was no available or no example to understand and run. No way. Study it by yourself. However, I have learned to give it to the people who need it.
DLL (Dynamic Link Library): WIN32 DLL and MFC DLL
ActiveX: It is divided into two types: ATL control and MFC control (also a DLL)
WEB: JAVASCRIPT call-> ActiveX call-> DLL completion addition operation and return value, displayed on the page.
Ii. Development (VS2008)
1. DLL library Compilation:
File-> New-> WIN32 console-> enter the project name-> select DLL-> Empty Project-> complete.
(1) Add a header file testdll. h to the solution panel. content:

The Code is as follows:


# Ifndef _ DLLTUT_DLL_H _
# Define _ DLLTUT_DLL_H _
# If defined DLL_EXPORT
# Define DECLDIR _ declspec (dllexport)
# Else
# Define DECLDIR _ declspec (dllimport)
# Endif
// 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) Add an implementation file testdll. cpp to the solution panel. The content is as follows:

The Code is as follows:


# Include
# Define DLL_EXPORT
# Include "testdll. h"
Extern "C"
{
// The ADD method is mainly used here.
DECLDIR int Add (int a, int B)
{
Return (a + B );
}
DECLDIR void Function (void)
{
Std: cout <"DLL Called! "<Std: endl;
}
}


(3) optional. Create a WIN32 console class and test the DLL.
File-> New-> WIN32 console-> enter the project name-> choose console Program-> Empty Project-> complete.
Add an implementation file loaddll. cpp in the solution panel:

The Code is as follows:


# Include
# Include
Using namespace std;
Typedef int (* AddFunc) (int, int); // defines pointer functions and interfaces.
Typedef void (* FunctionFunc )();
Int main ()
{
AddFunc _ AddFunc;
FunctionFunc _ FunctionFunc;
Cout <"--- get DLL ---." <endl;
// L indicates that the UNICODE character set 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 loaded [succeeded]." <endl;
}
_ AddFunc = (AddFunc) GetProcAddress (hInstLibrary, "Add ");
_ FunctionFunc = (FunctionFunc) GetProcAddress (hInstLibrary, "Function ");
If (_ AddFunc = NULL) | (_ FunctionFunc = NULL ))
{
FreeLibrary (hInstLibrary); // release
} Else {
Cout <"--- obtain the DLL function [OK] ---." <endl;
}
Cout <_ AddFunc (1, 1) <endl; // start to call
_ FunctionFunc ();//
Cin. get (); // obtain the focus, so that the program will not flash.
FreeLibrary (hInstLibrary); // after calling, the memory will be released.
Return (1 );
}


2. ActiveX control implementation:
Here we select the ATL control implementation, rather than the MFC ActiveX.
File-> New-> ATL Project-> enter the project name ("FROMYANTAI")-> select dynamic link library (DLL)-> complete.
After that, a large number of hfiles and CPP implementation files will be generated in Solution Explorer on the right side. These files are not modified by default.
(1) Add an ALT simple object: Click the project name (the name you just mentioned), select-> Add class-> select ATL simple object.
Start the next step with a name: "ytiicrj"-". Next Step: Keep the rest unchanged. In support, select" connection points "and" IE Object support "-" to complete the process.
Next, add a method to "ytiicrj" for WEB page calling. In the class view, right-click "iytiicrj" (with a gray key icon) and choose "add"> "add method. The method is named "GetContent"-". The parameter attribute is IN, and the parameter type is LONG. The parameter name is A-". The parameter attribute is IN, for the parameter type, select LONG parameter name B-> Add; continue; for the parameter property, select OUT and RETVAL; for the parameter type, select LONG * parameter name out-> Add --- and click Finish.
In this way, a header file (in the last line) is added to the ytiicrj. H file ):
STDMETHOD (GetContent) (LONG a, LONG B, LONG * out );
An Implementation class is added to the ytiicrj. CPP file:

The Code is as follows:


STDMETHODIMP CCaluNumCtrl: GetContent (LONG a, LONG B, LONG * out)
{
// TODO: add the implementation code here
Return S_ OK;
}


(2) Call the DLL class library in the ytiicrj. H file. The Code is as follows:
// CaluNumCtrl. h: the Declaration of ytiicrj in bold is specific.

The Code is as follows:


# 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 "A single-thread COM object cannot be correctly supported on Windows CE platform (for example, Windows Mobile platform that does not provide full DCOM Support. Define _ CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating a single-thread COM Object and using its single-thread COM object. The thread model in the rgs file has been set to "Free" because the model is not the only thread model supported by the DCOM Windows CE platform. "
# Endif
// Ytiicrj
Class ATL_NO_VTABLE Cytiicrj:
// Add one more line: Security Prompt removed. -- security questions are not prompted when the browser is called.
Public IObjectSafetyImpl ,
Public CComObjectRootEx ,
Public CComCoClass ,
Public IConnectionPointContainerImpl ,
Public CProxy_ICaluNumCtrlEvents ,
Public IObjectWithSiteImpl ,
Public IDispatchImpl
{
Public:
// Implement the following three definitions.
Typedef int (* AddFunc) (int, int); // type definition, corresponding to the dll add method. Func custom, write casually.
HINSTANCE hInstLibrary;
AddFunc _ AddFunc; // class ing
Cytiicrj ()
{
// Start to call the DLL for computation.
HInstLibrary = LoadLibrary (L "TestDll. dll"); // store the prepared 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 one more line: Security Prompt removed. -- security questions are not prompted when the browser is called.
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) return to the ytiicrj. PP file and add the implementation Code as follows:

The Code is as follows:


STDMETHODIMP CCaluNumCtrl: GetContent (LONG a, LONG B, LONG * out)
{
// TODO: add the 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 the Release mode and click the project to generate it (the system will prompt you to select REG32 for registration, and select "be ). In this way, many files are generated under the Release directory. What we need is a DLL file.
3. DLL and ATL ActiveX Control DLL are packaged as CAB files:
For example, after the test. CAB is generated, the WEB page prompts you to download and install it.
(1) first define the setup. inf file: it describes the downloaded content and the target directory, as well as the version number and the corresponding DLL file. To write this manually, my content is as follows (modify the corresponding name ):

The Code is as follows:


[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-4871-a47e-7f57afd86f67}
File-win32-x86 = thiscab
FileVersion =
DestDir = 11
RegisterServer = yes
[TestDll. dll]
File-win32-x86 = thiscab
DestDir = 11
FileVersion =
RegisterServer = yes
[Setup. inf]
File = thiscab
[RegisterFiles]
%11% \ AtlActiveX. dll
; End of INF file


(2) integrate resources:
Put all the DLL files used in a directory, including the setup. inf file, and then run the: IExpress command to generate the CAB package.
After running the command, select "first", "Next", "third", and "Next" to add a file (select your DLL and INF files). Next, select an output directory and create a CAB file name, select the second option, next, select the second option, and then OK. In this way, a CAB file is generated.
(3) The WEB page calls ActiveX controls for addition operations:
Write a test.htmweb page and cabin in a directory. The content of test.htm is as follows:

The Code is as follows:




New Page

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.