请教高手关于用C#写的com组件向C#的另一个程序发送事件的问题!

iceezone 2004-04-29 09:55:12
我用一个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";
}
}
}


请高手给看看!万分感谢!
...全文
31 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
iceezone 2004-04-29
  • 打赏
  • 举报
回复
忘了说了,运行脚本时,com组件中的文件操作可以执行,对应的a.txt可以生成,但是后面的事件是怎么也传不出去的!

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧