dll+ ActiveX Control +web page Invoke example _javascript tips

Source: Internet
Author: User
Tags win32
I. Overview
As a result of the project needs, began to learn and research VC, DLL and ActiveX control, online data to find a lot, but no one can be used or no example to understand and run. No way, study it yourself. Kung Fu not negative, eventually have small became, hehe, now put their own study summed up, dedicated to the needs of the people.
DLLs (dynamic-link libraries): WIN32 DLLs and MFC DLLs
ActiveX: Two classes (also one DLL) for ATL controls and MFC controls
Web:javascript calls the-> ActiveX call-> DLL to complete the addition operation and returns a value that is displayed on the page.
ii. Development (VS2008)
1, DLL Library writing:
FILE-"New"-"WIN32 console-> fill in the project name-" Select dll-"Empty Project-" completed.
(1) In the Solution panel, add a header file testdll.h, content:
Copy Code code 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 section can be used in 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:
Copy Code code as follows:

#include <iostream>
#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 new WIN32 console class to 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:
Copy Code code as follows:

#include <iostream>
#include <windows.h>
using namespace Std;
typedef int (*ADDFUNC) (int,int); Defines a pointer function, an interface.
typedef void (*FUNCTIONFUNC) ();
int main ()
{
Addfunc _addfunc;
Functionfunc _functionfunc;
cout << "---get DLL---." << Endl;
L represents the use of the Unicode character set to 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 "success". << 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 the focus so that the program does not flash past.
FreeLibrary (hinstlibrary);//After the call is finished, to free the memory.
return (1);
}

2. ActiveX Control Implementation:
Here we select the ATL control implementation, not MFC ActiveX.
FILE-"New-" ATL Project-> fill in the project name ("Fromyantai")-"Select dynamic link library (DLL)-" complete.
When you are done, the Solution Explorer on the right generates many header H files and the CPP implementation files, which are not modified by default.
(1), add an alt Simple object: Mouse mail Click item name (just start name) Select-"Add Class-" Select ATL Simple objects.
Next name: "YTIICRJ"-"Next: Other unchanged, in support, select" Connection point "and" ie object Support "-" complete. "
Next, add a method to "YTIICRJ" for Web page calls. In Class View Select "Iytiicrj" (with a gray key icon) right mouse button Add-Add method. Method named "GetContent"-"parameter attribute selection in, parameter type select long parameter name a–" add; parameter attribute select in, parameter type select long parameter name b– add; Parameter properties select out and retval, parameter type selection long* Parameter name out– "Add---" click Finish.
This is the YTIICRJ. H header file added one (on last line):
STDMETHOD (GetContent) (Long A, long B, long* out);
And in YTIICRJ. A implementation class was added to the CPP file:
Copy Code code as follows:

STDMETHODIMP Ccalunumctrl::getcontent (Long A, long B, long* out)
{
TODO: Add implementation code here
return S_OK;
}

(2), in YTIICRJ. H file, invoke the DLL class library. The code is as follows:
The statement of the CALUNUMCTRL.H:YTIICRJ in bold (bold) part is a concrete implementation, others are not moved.
Copy Code code as follows:

#pragma once
#include "resource.h"//main symbol
#include <windows.h>//Add
#include "atlactivex_i.h"
#include "_icalunumctrlevents_cp.h"
#if defined (_WIN32_WCE) &&!defined (_ce_dcom) &&!defined (_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
single-threaded COM objects are not supported correctly on the Windows CE platform #error, such as Windows Mobile platforms that do not provide full DCOM support. Defining _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA can force ATL support to create single-threaded COM object implementations and allow their single-threaded COM object implementations. The threading model in the RGS file has been set to free because the model is the only threading model supported by the DCOM Windows CE platform. "
#endif
Ytiicrj
Class Atl_no_vtable CYTIICRJ:
Add one line: The security prompt is lifted--the security issue is not prompted when you run the browser call.
Public IOBJECTSAFETYIMPL&LT;CYTIICRJ, interfacesafe_for_untrusted_caller| Interfacesafe_for_untrusted_data>,
Public ccomobjectrootex<ccomsinglethreadmodel>
Public CCOMCOCLASS&LT;CYTIICRJ, &clsid_calunumctrl>
Public iconnectionpointcontainerimpl<cytiicrj>
Public cproxy_icalunumctrlevents<cytiicrj>
Public iobjectwithsiteimpl<cytiicrj>
Public Idispatchimpl<icalunumctrl, &iid_icalunumctrl, &libid_atlactivexlib, 1, 0>
{
Public
The following three lines implement the definition.
typedef int (*ADDFUNC) (int,int); Type definition, corresponding to the DLL Add method. Func custom, write casually.
HINSTANCE hinstlibrary;
Addfunc _addfunc; Class mappings
CYTIICRJ ()
{
Starts calling the DLL for evaluation.
Hinstlibrary = LoadLibrary (L "TestDll.dll");//Put the written DLL file in the directory generated by this project
if (hinstlibrary = NULL)
{
FreeLibrary (hinstlibrary);//Resource release
}else{
}
The method is called, and the method handle is returned.
_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 line: The security prompt is lifted--the security issue is not prompted when you run the browser call.
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), back in YTIICRJ. In the PP file, add the implementation code as follows:
Copy Code code as follows:

STDMETHODIMP Ccalunumctrl::getcontent (Long A, long B, long* out)
{
TODO: Add implementation code here
int sum = This->_addfunc (static_cast<int> (a),static_cast<int> (b));
*out = static_cast<long> (sum);
This->_atlfinalrelease ();
return S_OK;
}

(4), build DLL:
This step is very simple, select release mode, click on the project to generate (will be prompted to choose REG32 Registration, then choose to be). So in the release directory generated a lot of files, we want is a DLL file.
3. dll and ATL ActiveX control DLLs packaged as CAB files:
For example: Build Test. Cab, the Web page prompts you to download the installation.
(1) First define the Setup.inf file: It describes the contents of the download and the target directory, as well as the version number and the corresponding DLL file. This will be manually written, my content is as follows (the corresponding name to modify it):
Copy Code code as follows:

[Version]
; Version signature (same for both NT and Win95) does 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]
%11%\atlactivex.dll
; End of INF file

(2) Integration of resources:
Put all the DLLs you used into one directory including the Setup.inf file, and then start running: IExpress command to generate the CAB package.
After running, select the first, next, select the third, next, add the file (select your DLL and INF file), next, select an output directory and create a CAB file name, then select the second option, next, select the second option, and then OK. This generates a CAB file.
(3) The Web page invokes an ActiveX control for addition operations:
Write a test.htm Web page and a CAB file in a directory, test.htm content as follows:
Copy Code code as follows:

<HTML>
<HEAD>
<title>new page</title>
<object Id=calunumctrl align= "CENTER" width=0 height=0 codebase= "test. cab#version=9,0,0,1 "classid=" clsid:b6d4b406-9cc4-4c80-b7a2-248bbb07f682 "></OBJECT>
<script language= "JavaScript" >
function Dotest ()
{
var sum = calunumctrl.getcontent (1,1);
alert (sum);
}
</script>
</HEAD>
<BODY>
<input type= "button" value= "Renjie" id= "Btnok" onclick= "Dotest" (); " ></input>
</BODY>
</HTML>

Description: codebase= "Test." cab#version=9,0,0,1 "codebase indicates the relative or absolute path of the file; version represents the revision number, and if this number is the same as the version number of the INF file, then the second access page will not be downloaded, otherwise it is downloaded each time. The CLSID is the serial number generated by the ActiveX project, which can be found in the project's *.rgs file.
All right. All the steps are complete, when you run Test.htm, prompt ActiveX control, you choose to allow, then you can invoke the addition operation.
This is just a simple example of a DLL in which you can implement your own application.

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.