请教高手关于用C#写的com组件向C#的另一个程序发送事件的问题!
我用一个vb脚本访问一个com组件,然后在脚本中调用com组件中了一个方法
这个方法中我先是用一个文件操作产生了一个文件,接着通过一个委托向别一个c#的windows应用程序发送一个消息,但是运行脚本的时侯总是提示未将对象的引用设置到对象的实例!请高手指点!谢谢!
我的三个程序如下:
vb脚本程序:
--------------------
Option Explicit
Dim objFactorial
Dim lngResult
Dim lngInputValue
Set objFactorial=CreateObject("dll.class1")
lngResult=objFactorial.out1()
---------------------
com组件代码:
--------------------
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace dll
{
public delegate void Msg();
// Step 1: Defines an event sink interface (ButtonEvents) to be
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface send
{
void show();
}
// Step 2: Connects the event sink interface to a class
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces("dll.send, dll")]
public class class1
{
public event Msg show;
public class1()
{
}
public void out1()
{
StreamWriter wr;
if(!File.Exists("c:\\dll\\aaa.txt"))
{
wr=File.CreateText("aaa.txt");
}
else
{
wr=File.AppendText("c:\\dll\\aaa.txt");
}
wr.WriteLine("----------------");
wr.Close();
show();
}
}
}
---------------
另一个windows程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace a
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public dll.class1 x=new dll.class1();
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 72);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.x.show+=new dll.Msg(x_show);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void x_show()
{
this.label1.Text="adf";
}
}
}
请高手给看看!万分感谢!
这个方法中我先是用一个文件操作产生了一个文件,接着通过一个委托向别一个c#的windows应用程序发送一个消息,但是运行脚本的时侯总是提示未将对象的引用设置到对象的实例!请高手指点!谢谢!
我的三个程序如下:
vb脚本程序:
--------------------
Option Explicit
Dim objFactorial
Dim lngResult
Dim lngInputValue
Set objFactorial=CreateObject("dll.class1")
lngResult=objFactorial.out1()
---------------------
com组件代码:
--------------------
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace dll
{
public delegate void Msg();
// Step 1: Defines an event sink interface (ButtonEvents) to be
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface send
{
void show();
}
// Step 2: Connects the event sink interface to a class
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces("dll.send, dll")]
public class class1
{
public event Msg show;
public class1()
{
}
public void out1()
{
StreamWriter wr;
if(!File.Exists("c:\\dll\\aaa.txt"))
{
wr=File.CreateText("aaa.txt");
}
else
{
wr=File.AppendText("c:\\dll\\aaa.txt");
}
wr.WriteLine("----------------");
wr.Close();
show();
}
}
}
---------------
另一个windows程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace a
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public dll.class1 x=new dll.class1();
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 72);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.x.show+=new dll.Msg(x_show);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void x_show()
{
this.label1.Text="adf";
}
}
}
请高手给看看!万分感谢!
...全文
相关推荐
医生述职报告标准模板.docx
医生述职报告标准模板.docx
VS2017 C++调用C#生成的COM组件
C#生成COM组件: VS2017 ——&gt; 新建 ——&gt; 项目 ——&gt; Visual C# ——&gt; 类库(.NET Framework) *注意:如果选择 "类库(.NET Standard)" 后面可能无法选择 "为COM互操作注册",同时记住选择的.NET Framework的版本 示例:新建的工程名为 ComExample,重命名Class1.cs为Com...
通过COM组件方式实现java调用C#写的DLL文件
通过JAVA调用C#的DLL文件实现。网上资料很多,自己整合总结了一下,做个备忘。 一、C#写com组件 开发环境 VS2013 1、新建工程:TestDemo //根据自己需要取工程名 注:VS必须以管理员身份启动!!!!! 2、右键点击工程->应用程序->程序集信息->使程序集com可见,打上勾 注:1.创建工
关于医生述职报告模板汇编十篇.docx
关于医生述职报告模板汇编十篇.docx
个人医生述职报告模板锦集七篇.docx
个人医生述职报告模板锦集七篇.docx


