Use C # To compile ActiveX Controls (2)

Source: Internet
Author: User
In my previous blog, a basic ActiveX control has been implemented. Of course, the task we have compiled is not completed yet. First, we try to implement interaction with Js.

Add the showmessage method in the demo: Public void showmessage (string MSG)
{
If (MSG! = NULL)
{
MessageBox. Show (MSG );
}
}


Recompile. Before accessing the page again, modify the htmlCode: <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, access http: // localhost/helloworld.htm again and click the click button to implement interaction.

However, we are sorry for the result. We found that IE jumped out of the dialog box,

After clicking OK, we found the JS error. According to the prompt, we can modify the IE settings to run the control. Open the IE tool --> Internet Options --> Security --> Local intranet --> Custom Level --> to initialize and Run ActiveX controls that are not marked as secure, set the value to enable. We refreshed the page and now we can run it correctly.

Of course, we cannot expect our customers to change this value just like us. After all, it is difficult to operate and brings great security risks to the computer. After searching on the internet, you must implement the IObjectSafety interface to mark ActiveX controls as secure ActiveX controls. After searching for msdn, I found the definition of the IObjectSafety interface. This is easy to handle. 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 the following code:

IObjectSafety member # region IObjectSafety Member

Public void getinterfaccesafyoptions (int32 riid, out int32 pdwsupportedoptions, out int32 pdwenabledoptions)
{
// Add webcamcontrol. getinterfaccesafyoptions to implement the task
Pdwsupportedoptions = 1;
Pdwenabledoptions = 2;
}

Public void setinterfacesafetyoptions (int32 riid, int32 dwoptionssetmask, int32 dwenabledoptions)
{
// Add webcamcontrol. setinterfacesafetyoptions to implement
}

# Endregion

Re-compile, and then change the settings in IE back. Now, we find that there is no problem with JS interaction.

In this way, a basic ActiveX control has been written. You can add any functions you need on the basis of this control. At this point, the task of writing controls has been completed, and our next goal is to release it.

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.