Active|activex| Control
In my last blog, I have implemented a most basic ActiveX control. Of course, the task we have written has not yet been completed. First of all, we first try to implement and JS interactive capabilities.
We add the ShowMessage method to the demo:
public void ShowMessage (String msg)
{
if (msg!= null)
{
MessageBox.Show (msg);
}
}
We recompile. Before we re-access the page, we'll modify the HTML code:
<body bgcolor= ' #223344 ' >
<object id= "HelloWorld"
Classid= "CLSID:9551B223-6188-4387-B293-C7D9D8173E3A" width= "184" height= "96"
>
</object>
<br>
<input type= ' button ' onclick= ' HelloWorld. ShowMessage ("Hello world!") ' value= ' click ' >
</body>
Now, if you re-access http://localhost/helloworld.htm, you should be able to interact by clicking the click button.
But the result is very regrettable, we found IE jumped out of the dialog box, as shown in the picture
Click OK, we found JS error. According to the prompts, we judge that the control can be run by modifying the settings of IE. Open IE tools--〉internet option--〉 Security--〉 local intranet--〉 Custom Level--〉 Initialize and run ActiveX controls that are not marked as safe, and set their values to Enabled. We refreshed the page and now we can finally run it correctly.
Of course, we can't expect our clients to modify this value as much as we do. After all, the first is the operation of trouble, the second is to bring a great security risks to the computer. After searching on the internet, it is found that the IObjectSafety interface must be implemented to mark ActiveX controls as safe ActiveX controls. After searching for MSDN, I found the definition of the IObjectSafety interface. It's a good run. First we use C # to implement this interface:
[Guid ("cb5bdc81-93c1-11cf-8f20-00805f2cd064"), InterfaceType (Cominterfacetype.interfaceisiunknown)]
public interface IObjectSafety
{
Methods
void Getinterfaccesafyoptions (
System.Int32 riid,
Out System.Int32 Pdwsupportedoptions,
Out System.Int32 pdwenabledoptions);
void SetInterfaceSafetyOptions (
System.Int32 riid,
System.Int32 Dwoptionssetmask,
System.Int32 dwenabledoptions);
}
Note that this GUID cannot be changed. Then, we implement this interface in the demo class. Add code:
IObjectSafety member #region IObjectSafety member
public void Getinterfaccesafyoptions (Int32 riid, out Int32 pdwsupportedoptions, out Int32 pdwenabledoptions)
{
TODO: Add webcamcontrol.getinterfaccesafyoptions implementation
Pdwsupportedoptions = 1;
Pdwenabledoptions = 2;
}
public void SetInterfaceSafetyOptions (Int32 riid, Int32 dwoptionssetmask, Int32 dwenabledoptions)
{
TODO: Add webcamcontrol.setinterfacesafetyoptions implementation
}
#endregion
Recompile, and then change the settings in IE back. Now, we find that the interaction with JS is no longer a problem.
In this way, one of the most basic ActiveX controls has been written. You can add any functionality you need on the basis of this control. Here, the task of writing the control is complete, and our next goal is to publish it.