|
转自:http://www.cnblogs.com/asuran/archive/2008/08/28/1278246.html
开发环境为VS 2005, .NET framework 2.0
创建项目类别为Windows Control Library或者Class Library
1. COM Interface and Class COM Interface:IMonitor
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace ClassroomViewer

{
[
Guid("27C6D076-D0E1-4b47-AA7A-A4BFFBA9F98E"),
InterfaceType(ComInterfaceType.InterfaceIsDual),
ComVisible(true)
]
public interface IMonitor
{
void LoadControl(string sClassroom);
void UnloadControl();
}
}
COM Class:Monitor
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace ClassroomViewer

{
[
Guid("627AD403-FA50-4a08-B875-770520865DD6"),
// This is basically the programmer friendly name
// for the guid above. We define this because it will
// be used to instantiate this class. I think this can be
// whatever you want. Generally it is
// [assemblyname].[classname]
ProgId("ClassroomViewer.Monitor"),
// No class interface is generated for this class and
// no interface is marked as the default.
// Users are expected to expose functionality through
// interfaces that will be explicitly exposed by the object
// This means the object can only expose interfaces we define
ClassInterface(ClassInterfaceType.None),
// Set the default COM interface that will be used for
// Automation. Languages like: C#, C++ and VB
// allow to query for interface's we're interested in
// but Automation only aware languages like javascript do
// not allow to query interface(s) and create only the
// default one
ComDefaultInterface(typeof(IMonitor)),
ComVisible(true)
]
public partial class Monitor : UserControl, IMonitor, IObjectSafety
{
public Monitor()
{
InitializeComponent();
}
public void LoadControl(string msg)
{
if (timer.Enabled == false)
{
timer.Start();
}
}
public void UnloadControl()
{
if (timer.Enabled == true)
{
timer.Stop();
}
}
private void timer_Tick(object sender, EventArgs e)
{
label.Text = DateTime.Now.ToString();
}
}
}
1)GUID 指定该类或者接口的GUID。
2)声明属性InterfaceType(ComInterfaceType.InterfaceIsDual),ComVisible(true),以支持register 和 unregister。
3) IMonitor 是COM interface。C# 的COM Class 可以继承自COM interface 也可以不用interface.
| 谁顶了这篇日志>> 还有谁对该日志投票? | 最近读者>> | ||||||||||||||
|
|
| 发表评论:查看评论 | |||
|
|
|||
| 内 容: |
|
||
| *Ctrl+Enter快速回复 | |||