Developing ActiveX controls under VS2010 using C #

Source: Internet
Author: User

1. Create a class library

2. Project Properties-Application-assembly information-"Make assembly COM visible" tick;

3. Project Properties-Generate-"Register for COM Interop" tick. (This toss day, otherwise the registration event is not available);

4. Create Interface: IObjectSafety (note that the GUID cannot be changed);

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.InteropServices;
Using System.Text;

Namespace My6
{
[Guid ("E3a49460-230d-4727-9efd-a1e9303d4ee2")]
[InterfaceTypeAttribute (Cominterfacetype.interfaceisiunknown)]
public interface IObjectSafety
{
[PreserveSig]
int getinterfacesafetyoptions (ref Guid riid, [MarshalAs (UNMANAGEDTYPE.U4)] ref int pdwsupportedoptions, [MarshalAs ( UNMANAGEDTYPE.U4)] ref int pdwenabledoptions);

[PreserveSig ()]
int SetInterfaceSafetyOptions (ref Guid riid, [MarshalAs (UNMANAGEDTYPE.U4)] int dwoptionsetmask, [MarshalAs ( UNMANAGEDTYPE.U4)] int dwenabledoptions);
}
}

5. Create a class library with the following code

Using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

Namespace My6
{
//define event Delegate
public delegate void Showinputevent (String str);
[Guid ("bb9df6c9-0c30-41c8-a568-91484c67b03c")]
[InterfaceTypeAttribute (Cominterfacetype.interfaceisidispatch)]
public interface controlevents
{
[ DispIdAttribute (0x001)]
void Onshowinputevent (String str);
}
[Guid ("Abb26c85-5521-4d68-a70a-9d7050980a1c")]
[ComVisible (True)]
[Comsourceinterfacesattribute ( typeof (ControlEvents)]
public partial class Usercontrol1:usercontrol, IObjectSafety
{
#region
public E Vent Showinputevent onshowinputevent;
public void Showrandomnum ()
{
Random r = new Random ();
int num = r.next (0, +);
Label1. Text = "Random number:" + Num. ToString ();
}
public void Showinput (String str)
{
Label1. Text = str;
Onshowinputevent (str);
}

#endregion
Public UserControl1 ()
{
InitializeComponent ();
Label1. Text = "";
Label2. Text = "";
}
#region IObjectSafety member

Private 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 int interfacesafe_for_untrusted_caller = 0x00000001;
Private Const int interfacesafe_for_untrusted_data = 0x00000002;
Private Const int S_OK = 0;
Private Const int E_FAIL = unchecked ((int) 0x80004005);
Private Const int E_NOINTERFACE = unchecked ((int) 0x80004002);

private bool _fsafeforscripting = true;
private bool _fsafeforinitializing = true;

public int getinterfacesafetyoptions (ref Guid riid, ref int pdwsupportedoptions, ref int pdwenabledoptions)
{
int Rslt = E_FAIL;

String strguid = 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
}

return Rslt;
}

public int setinterfacesafetyoptions (ref Guid riid, int dwoptionsetmask, int dwenabledoptions)
{
int Rslt = E_FAIL;
String strguid = 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
}

return Rslt;
}

#endregion

}

}

6. Web page Invoke Event method

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<object id= "ActiveX" classid= "Clsid:abb26c85-5521-4d68-a70a-9d7050980a1c" ></object>
<br/>
<input type= "text" id= "Userval"/>
<input type= "button" value= "Call the plug-in method" onclick= "Callmethod ()"/>
<input type= "button" value= "Invoke plug-in event" onclick= "CallEvent ()"/>
<!--Event Registration--
<script language= "JavaScript" for= "ActiveX" event= "onshowinputevent (value)" >alert (value);</script>
<!--how to call plug-ins--
<script type= "Text/javascript" >
var callmethod = function () {
Activex.showrandomnum ();
},
CallEvent = function () {
var str = document.getElementById ("Userval"). Value;
Activex.showinput (str);
}
</script>
</body>

Developing ActiveX controls under VS2010 using C #

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.