Development environment: VS2008
The first step is to create the project
Create a new project, select the Windows Forms Control Library, creating a User control project "Activexdemo" (note that the name here is not in Chinese, otherwise there will be a problem), there is a user control class UserControl1.cs
Write the business logic code that you need in your class to save
The second step sets the project properties
Add [Assembly:allowpartiallytrustedcallers ()] in AssemblyInfo.cs, you need to refer to using System.Security; namespaces
Set project Properties, right-click Project-Properties
Select "Application" to open "assembly information".
Tick "make assembly COM visible", click "OK"
Then select "Generate", scroll down to the bottom, tick "register for COM Interop"
Then save and recompile the project, and at this point the "ActiveXDemo.dll" becomes an ActiveX control
Step three Install external tools
Install External Tools "ole/com Object Viewer" and "Create GUID" (this step can be ignored for these two tools)
1.ole/com Object Viewer
This tool is used to view ActiveX controls, that is, to verify that your project has not become an ActiveX control
Installation method: Point to open vs top menu Bar "Tools"--"external tools"
Then, add a "ole/com Object Viewer", the corresponding command program is generally placed in the C:\Program Files\Microsoft Sdks\windows\v6.0a\bin\oleview.exe
Click "Apply", "OK", this is the installation is complete, and then re-click on the top of the "Tools" menu to see, there is a "Ole/com Object Viewer"
Click "Ole/com Object Viewer" to expand the ". NET Category" on the Left
You'll find that there's an ActiveX control you just created
That's what the Ole/com Object Viewer does.
2. Create a GUID
The higher version of VS seems to have this tool, but in some of the lower version or incomplete installation such as Vs2008 not necessarily have this tool, so in the absence of time need to install
The installation method is the same as above, except that the command program's address is generally C:\Program Files (x86) \microsoft Visual Studio 9.0\common7\tools\guidgen.exe
The purpose of this tool is described in the following steps
Fourth step to improve the security level of the ActiveX plugin
How does ie know that a plugin is script-safe? It is through the following two methods.
One is to query whether the ActiveX component implements the IObjectSafety interface, and returns the script security;
The second is whether the query ActiveX component shows that it implements CATID_SafeForInitializing and catid_safeforscripting in the component Category manager of the registry.
Here we only say the first implementation of the IObjectSafety interface
First, add a GUID for the control class UserControl1, which will be used for the client call of the B/s system (you can create a GUID using the Tools-Create GUID menu):
Using system;using system.collections.generic;using system.text;using system.windows.forms;using System.runtime.interopservices;namespace activexdemo{ [Guid ("bb725724-65d6-4e71-aa11-dedfafe9248f"), ProgId ( "Activexdemo.usercontrol1"), ComVisible (true)] public partial class Usercontrol1:usercontrol,iobjectsafety {
Note that you want to introduce System.Runtime.InteropServices; namespaces
Second, in order for the ActiveX control to gain trust from the client, the control class also needs to implement an interface named "IObjectSafety" , so add an interface class to the project IObjectSafety
Directly copy and paste the following code, do not make any changes, especially the GUID, are fixed, can not be changed
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceactivexdemo{[ComImport, GuidAttribute ("cb5bdc81-93c1-11cf-8f20-00805f2cd064")] [InterfaceTypeAttribute (Cominterfacetype.interfaceisiunknown)] Public InterfaceIObjectSafety {[PreserveSig]intGetInterfaceSafetyOptions (refGuid riid, [MarshalAs (UNMANAGEDTYPE.U4)]ref intPdwsupportedoptions, [MarshalAs (UNMANAGEDTYPE.U4)]ref intpdwenabledoptions); [PreserveSig ()]intSetInterfaceSafetyOptions (refGuid riid, [MarshalAs (UNMANAGEDTYPE.U4)]intDwoptionsetmask, [MarshalAs (UNMANAGEDTYPE.U4)]intdwenabledoptions); }}
Similarly, you need to introduce System.Runtime.InteropServices; namespaces
Next, implement the IObjectSafety interface in the control class UserControl1
#regionIObjectSafety membersPrivate Const string_iid_idispatch ="{00020400-0000-0000-c000-000000000046}"; Private Const string_iid_idispatchex ="{A6EF9860-C720-11D0-9337-00A0C90DCAA9}"; Private Const string_iid_ipersiststorage ="{0000010a-0000-0000-c000-000000000046}"; Private Const string_iid_ipersiststream ="{00000109-0000-0000-c000-000000000046}"; Private Const string_iid_ipersistpropertybag ="{37d84f60-42cb-11ce-8135-00aa004bb851}"; Private Const intInterfacesafe_for_untrusted_caller =0x00000001; Private Const intInterfacesafe_for_untrusted_data =0x00000002; Private Const intS_OK =0; Private Const intE_fail =unchecked((int)0x80004005); Private Const intE_nointerface =unchecked((int)0x80004002); Private BOOL_fsafeforscripting =true; Private BOOL_fsafeforinitializing =true; Public intGetInterfaceSafetyOptions (refGuid riid,ref intPdwsupportedoptions,ref intpdwenabledoptions) { intRSLT =E_fail; stringstrGUID = riid. ToString ("B"); Pdwsupportedoptions= Interfacesafe_for_untrusted_caller |Interfacesafe_for_untrusted_data; Switch(strguid) { Case_iid_idispatch: Case_iid_idispatchex:rslt=S_OK; Pdwenabledoptions=0; if(_fsafeforscripting = =true) Pdwenabledoptions=Interfacesafe_for_untrusted_caller; Break; Case_iid_ipersiststorage: Case_iid_ipersiststream: Case_iid_ipersistpropertybag:rslt=S_OK; Pdwenabledoptions=0; if(_fsafeforinitializing = =true) Pdwenabledoptions=Interfacesafe_for_untrusted_data; Break; default: Rslt=E_nointerface; Break; } returnRslt; } Public intSetInterfaceSafetyOptions (refGuid riid,intDwoptionsetmask,intdwenabledoptions) { intRSLT =E_fail; stringstrGUID = riid. ToString ("B"); Switch(strguid) { Case_iid_idispatch: Case_iid_idispatchex:if(((dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_caller) && (_fsafeforscripting = =true)) Rslt=S_OK; Break; Case_iid_ipersiststorage: Case_iid_ipersiststream: Case_iid_ipersistpropertybag:if(((dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_data) && (_fsafeforinitializing = =true)) Rslt=S_OK; Break; default: Rslt=E_nointerface; Break; } returnRslt; } #endregion
This code is also fixed, can not be changed, directly copy and paste on the line
The fifth step is to make the installation file
Create a new Setup project in the original project solution, which is done in VS2008
New Project--Other project types--Installation and deployment--installation projects
Note that the "solution" below here is to select " Add to Solution "
Then, right-click on the project--add--project output
Set our project Activexdemo as the primary output
Click OK, then right-click on the main output file--Properties
Set the Register property to vsdrpCOM
Recompile the Setup project to open the directory where the installation project is located
At this point, the ActiveX browser plug-in installation package is ready, double-click the Setup.exe file or Activexdemosetup.msi file can install the browser plug-in to your computer
Sixth step using the ActiveX plugin
Create a new Web project or an HTML file, and add the following code to the page where you want to use the browser plugin:
The emphasis is on the ClassID attribute in the OBJECT tag, which corresponds to the GUID specified in the fourth step in the property GUID .
Is it convenient to invoke the method in the UserControl1 class directly using the id attribute defined by object?
In order to more convenient and intuitive to see if the plug-in has normal loading, you can add some detection code, as follows:
<body><object id= "Csharpactivex" classid= "clsid:bb725724-65d6-4e71-aa11-dedfafe9248f" width= "100%" height = "Max" ></object> <form id= "Form1" runat= "Server" > <div> <input type= ' button ' Onclick= ' csharpactivex.test () ' value= ' I am button '/> </div> </form></body><script type= "Text/javascript" > var objcard = document.getElementById ("Csharpactivex"); if (objcard.object==null) { alert ("Csharpactivex plugin not installed!) "); } else{ alert ("The Csharpactivex plugin has been detected! "); } </script>
Then, since only IE supports ActiveX browser plug-ins, so open this page in IE browser, look at the effect
At this time, do not rest assured that, you can check our ActiveX plug-in whether there is no installation on
Click the Toolbar "Tools"--"Manage Add-ons"
You can see one of the Activexdemo.usercontrol1, which is the browser plugin we installed.
At this point, you have successfully mastered the ability to create ActiveX browser plugins ~
C # making ActiveX browser plug-ins. Net