+ -
当前位置:首页 → 问答吧 → 请教MFC调COM事件的问题

请教MFC调COM事件的问题

时间:2011-12-18

来源:互联网

因项目需要要用vc++6.0调用C#写的COM组件,本人只会C#,按标准写好COM组件但不会用VC写示例.
麻烦高手帮帮忙.给出调用事件的代码
C# COM的代码
  [Guid("74623E27-B23F-4754-90B8-F7B1AEEBAA87")]
  public interface ICtrlHelper
  {
  [DispId(1)]
  string IP { get; set; }
.......
  [DispId(5)]
  string Test(int lineidx, string tagList, int flag);
.......
  } 

[Guid("9CD61034-1A39-4554-AD0F-2085D2CD34DC")]
  [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  public interface ICtrlEvent
  {
....
  [DispId(15)]
  void OnCtrlNotice(string[] ctrlNotice, string[] getdata);
....
}

  [Guid("20F7F594-F029-4cd0-A0D7-411B05459F09")]
  [ClassInterface(ClassInterfaceType.None)]
  [ComSourceInterfaces(typeof(ICtrlEvent))]
  public class CtrlHelper : ICtrlHelper
  , IObjectSafety
  {
....
[ComVisible(false)]
  public delegate void OnGetInfoHandle(string[] ctrlNotice, string[] getdata);

/// <summary>
  /// 所有系统消息
  /// </summary>
  public event OnCtrlNoticeHandle OnCtrlNotice;

string Test(int lineidx, string tagList, int flag)
{
....
这里触发了OnCtrlNotice事件
...
}
....
  }

网上找教程演示代码现在可以调用方法了,但是事件不知道怎么接受.
以下是MFC里调用COM方法的代码,已经测试成功
void CWCSPortTestDlg::OnButton1() 
{
CoInitialize(NULL);

WcsPortCOM::ICtrlHelperPtr p(__uuidof(CtrlHelper));
p->PutIP("192.168.1.11"); //初始化

MessageBox(p->Test(1,"2",1)); //调用TEST方法

CoUninitialize(); 
}
麻烦哪位再帮忙给出调用事件的代码,只要代码.应该没几句话的,只是我实在外行,不知道什么格式
麻烦不要贴链接,这几天我看了不下百篇了.

作者: xv84   发布时间: 2011-12-18

接收事件是什么意思?
在C#写的模块中触发的事件,不在本模块中解决吗?传递到MFC客户端解决?

作者: mougaidong   发布时间: 2011-12-18

相当于发布一个命令,过一段时间,COM会返回一个结果,VC要接收这个结果

作者: xv84   发布时间: 2011-12-18