Com组件相关学习

一、C++和C#中COM组件的调用和使用问题

 

 

  前一阵在工作中做项目的时候,遇到了COM组件的调用和使用问题,当时研究和好一阵,才把中间的环节打通,现在写出来为大家提供方便,这里包含了四个类型:

  1、在VS2005中,C#编写DLL并使用C++调用

  2、在VS2005中C#编写的COM组件,使用VC6.0调用

  3、在VC6.0中编写COM组件,使用VS2005 C#调用

  4、在VC6.0中编写COM组件,使用VC6.0调用

  其中每个类型都写了两个程序,一个为COM组件程序,一个为调用程序

  程序实现:

  1、在VS2005中,C#编写DLL并使用C++调用

  (1)C#编写DLL程序

  建立C#编写的DLL程序AddDll,项目类型为:类库

  程序代码:

    using System;
  using System.Collections.Generic;
  using System.Text;
  namespace AddDll
  {
  public class Add
  {
  public int iadd(int a, int b)
  {
  int c = a + b;
  return c;
  }
  }
  }

  (2)C++编写调用程序

  建立C++的Win32控制台应用程序UseDll,项目类型为:Win32控制台应用程序

  配置:右键点击解决方案资源管理器中的UseDll,选择“属性”,将公共语言运行库支持设置为“公共语言运行库支持(/clr)”

  程序代码:

 #include "stdafx.h"
  #include "stdio.h"
  #using "../debug/AddDll.dll"
  using namespace AddDll;
  int _tmain(int argc, _TCHAR* argv[])
  {
  int result;
  Add ^add = gcnew Add();
  result = add->iadd(10,90);
  printf("%d",result);
  scanf("%s");
  return 0;
  }

 2、在VS2005中C#编写的COM组件,使用VC6.0调用

  (1)VS2005中使用C#编写COM组件

  建立C#编写的COM组件,项目类型为类库

  配置:右键点击解决方案资源管理器中的AddCom,选择“属性”,选择“生成”,选择“为COM Interop注册(_P)”

  打开AssemblyInfo.cs文件,设置[assembly: ComVisible(true)]

  这用就可以生成AddCom.tlb文件

  程序代码:

 using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Runtime.InteropServices;
  namespace AddCom
  {
  //可以通过//菜单的 “工具/guid生成”。
  //注意要选择Define Guid{….}格式,并全//部保存下来,保存到哪都行,记事本呀什么的。
  //因为在做VC程序/的时候要用到的。
  [Guid("298D881C-E2A3-4638-B872-73EADE25511C")]
  public interface AddComInterface
  {
  [DispId(1)]
  int iadd(int a, int b);
  [DispId(2)]
  float ladd(float a, float b);
  }
  [Guid("2C5B7580-4038-4d90-BABD-8B83FCE5A467")]
  [ClassInterface(ClassInterfaceType.None)]
  public class AddComService : AddComInterface
  {
  public AddComService()
  {
  }
  public int iadd(int a, int b)
  {
  int c = 0;
  c = a + b;
  return c;
  }
  public float ladd(float a, float b)
  {
  float c = 0;
  c = a + b;
  return c;
  }
  }
  }

  (2)VC6.0编写调用程序

  使用VC6.0编写建立MFC应用程序UseCom,项目类型为MFC AppWizard(exe)

  在stdafx.h添加:

 #import "AddCom.tlb"
  using namespace AddCom;
  程序代码:
  void CUseComDlg::OnButtonUse()
  {
  // TODO: Add your control notification handler code here
  int dresult;
  float fresult;
  CString strResult;
  CoInitialize(NULL);//NULL换成0也可以
  AddCom::AddComInterfacePtr p_Add(__uuidof(AddComService));
  dresult = p_Add->iadd(1,2);
  fresult = p_Add->fadd(1.2,2.3);
  strResult.Format("int:%d /nfloat:%f",dresult,fresult);
  MessageBox(strResult,"计算结果",MB_OK);
  CoUninitialize();
  }

  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();
  }

 

 

 

二、在.NET中调用dll COM组件访问USB Key例程


压缩包内文件格式: 可执行文件

附件来源: 他人共享

运行平台: Windows平台

是否经本人验证:

附件性质: 共享

注册码: rockey

详细说明: 本人两年前在用VS.NET编写Philips Mifare 1 RFID卡的读写程序时,遇到.NET中如何调用COM组件的问题,在互联网查阅资料时发现询问这个问题的还不少。现将本人找到的几个例程发布出来以供大家共享。
资料来自某公司网站,但未提供解压密码。后经本人用Tubro Zip Cracker破解得出密码为:rockey

点击下载本资源>>

三、 客户端使用COM组件

C#中可以用ComInterfaceType.InterfaceIsIDispatch来修饰一个接口为IDispatch,并用ComVisible(true)来对外界公开接口。详细见下面的例子:

将下面的MyCom.cs和test.js拷贝存盘到同一个目录下。
然后在Visual Studio工具的Dos窗口下,CD到该目录,并运行以下命令(后面有解释):
csc /target:library MyCom.cs
regasm /tlb /codebase MyCom.dll
wscript.exe test.js

C# code
   
   
// MyCom.cs
//
using System;
using System.Runtime.InteropServices;

[assembly: ComVisible(true)]
[assembly: Guid("11cb4989-6094-4a8e-9355-111111111111")]

namespace MyCom
{
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ICalc
{
int Add(int x, int y);
void SayHello();
}

[ClassInterface( ClassInterfaceType.None )]
[ProgId("MyCom.MyCalc")]
public class MyCalc : ICalc
{
public int Add(int x, int y)
{
return x + y;
}
public void SayHello()
{
System.Windows.Forms.MessageBox.Show("Hello from C# COM");
}
}
}

JScript code
//
 test.js

//
MyCom = WScript.CreateObject( " MyCom.MyCalc " );
sum
= MyCom.Add( 10 , 8 );

WScript.echo(
" The sum is " + sum);
MyCom.SayHello();




csc /target:library MyCom.cs
是将MyCom.cs源文件编译为.Net类库。注:请先确认当前的途径可以使用csc。

regasm /tlb /codebase MyCom.dll
将类库登记为COM对象。/tlb是要求产生并登记Type Library(用来具体描述该COM),/codebase指定如何找到该COM。这里可以忽略“该类库没有强命名”的警告。

wscript.exe test.js
测试在javascript下调用C#对象。

[ProgId("MyCom.MyCalc")]
给该COM对象取个容易引用的名字

[ClassInterface( ClassInterfaceType.None )]
不公开继承自object的函数

MyCom = WScript.CreateObject("MyCom.MyCalc");
在Javascript中创建一个COM对象

sum = MyCom.Add(10, 8);
调用COM对象公开的接口

 

 

//js code

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head><body>
<script language="JavaScript">
<!--
var ver ;
ver = new ActiveXObject("MyCom.MyCalc");
alert (ver.Add(10, 8));
//-->
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值