C++和C#编写并且相互调用COM组件
来源:优易学  2011-9-10 17:05:59   【优易学:中国教育考试门户网】   资料下载   IT书店


  3、在VC6.0中编写COM组件,使用VS2005 C#调用
  (1)VC6.0编写COM使用VC6.0建立COM组件:
  工程类型:ATL COM AppWizard
  程序代码:
  接口:
  interface IAdd : IDispatch
  {
  [id(1), helpstring("method iadd")] HRESULT iadd([in]int a, [in]int b, [out]int * c);
  [id(2), helpstring("method fadd")] HRESULT fadd([in]float a, [in]float b, [out]float * c);
  [id(3), helpstring("method isub")] HRESULT isub([in]int a, [in]int b, [out]int * c);
  };
  实现:STDMETHODIMP CAdd::iadd(int a, int b, int *c)
  {
  // TODO: Add your implementation code here
  *c = a + b;
  return S_OK;
  }
  STDMETHODIMP CAdd::fadd(float a, float b, float *c)
  {
  // TODO: Add your implementation code here
  *c = a + b;
  return S_OK;
  }
  STDMETHODIMP CAdd::isub(int a, int b, int *c)
  {
  // TODO: Add your implementation code here
  *c = a - b;
  return S_OK;
  }
  (2)VS2005使用C#编写调用程序(网站程序)
  使用VS2005建立网站UseCom
  配置:在解决方案资源管理器中的主目录点击右键,选择添加引用,选择COM,添加刚刚建立的AddCom 1.0 Type Library
  在程序中要using编写的COM组件:using ADDCOMLib;

  程序代码:
  using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using ADDCOMLib;
  public partial class _Default : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void ButtonCom_Click(object sender, EventArgs e)
  {
  Add add = new Add();
  int iresult;
  float fresult;
  int sresult;
  add.IAdd(10, 20, out iresult);
  add.fadd((float)1.2,(float)2.3, out fresult);
  add.isub(100, 10, out sresult);
  TextBoxResult.Text = iresult.ToString();
  TextBoxRe2.Text = fresult.ToString();
  TextBoxRe3.Text = sresult.ToString();
  }
  }
  4、在VC6.0中编写COM组件,使用VC6.0调用
  (1)VC6.0编写COM组件使用VC6.0建立COM组件,
  工程类型:ATL COM AppWizard
  程序代码:
  接口:
  interface IAdd : IDispatch
  {
  [id(1), helpstring("method iadd")] HRESULT iadd([in]int a, [in]int b, [out]int * c);
  [id(2), helpstring("method fadd")] HRESULT fadd([in]float a, [in]float b, [out]float * c);
  [id(3), helpstring("method isub")] HRESULT isub([in]int a, [in]int b, [out]int * c);
  };
  实现:STDMETHODIMP CAdd::iadd(int a, int b, int *c)
  {
  // TODO: Add your implementation code here
  *c = a + b;
  return S_OK;
  }
  STDMETHODIMP CAdd::fadd(float a, float b, float *c)
  {
  // TODO: Add your implementation code here
  *c = a + b;
  return S_OK;
  }
  STDMETHODIMP CAdd::isub(int a, int b, int *c)
  {
  // TODO: Add your implementation code here
  *c = a - b;
  return S_OK;
  }
  (2)VC6.0编写调用程序
  使用VC6.0建立MFC应用程序UseCOM,调用刚刚建立的COM组件
  将上面程序AddCom生成的AddCom.dll放入本程序的工程目录和程序生成目录中
  在StdAfx.h中加入:
  #import "AddCom.dll" no_namespace
  程序代码:
  void CUseComDlg::OnBUTTONUse()
  {
  // TODO: Add your control notification handler code here
  CString strResult;
  CoInitialize(NULL);//NULL换成0也可以
  IAddPtr m_add = NULL;
  HRESULT hr = S_OK;
  hr = m_add.CreateInstance(__uuidof(Add));
  int d_a = 90;
  int d_b = 10;
  int d_c;
  int d_d;
  float f_a = 1;
  float f_b = 2;
  float f_c;
  m_add->_IAdd(d_a,d_b,&d_c);
  m_add->fadd(f_a,f_b,&f_c);
  m_add->isub(d_a,d_b,&d_d);
  strResult.Format("返回结果:%d; %f; %d",d_c,f_c,d_d);
  MessageBox(strResult,"结果",MB_OK);
  m_add.Release();
  m_add = NULL;
  CoUninitialize();
  }

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训