VC6.0和VS2005:C++和C#编写调用COM组件

开发 后端
这篇文章就是关于COM组件的编写和调用的,主要包含了使用VC6.0编写和调用COM组件,VS2005中使用C#编写和调用COM组件,以及在VC6.0和VS2005之间互相调用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,项目类型为:类库

程序代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace AddDll  
  6. {  
  7.     public class Add  
  8.     {  
  9.         public int iadd(int a, int b)  
  10.         {  
  11.             int c = a + b;  
  12.             return c;  
  13.         }  
  14.     }  
  15. }  
  16.  

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

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

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

公共语言运行库设置 

C++编写调用程序:公共语言运行库设置

程序代码:

  1. #include "stdafx.h"  
  2. #include "stdio.h"  
  3.  
  4. #using "..\debug\AddDll.dll"  
  5. using namespace AddDll;  
  6.  
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.         int result;  
  10.         Add ^add = gcnew Add();  
  11.         result = add->iadd(10,90);  
  12.         printf("%d",result);  
  13.         scanf("%s");  
  14.         return 0;  
  15. }  
  16.  

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

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

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

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

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

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

VS2005中使用C#编写COM组件:COM生成设置

程序代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5.  
  6. namespace AddCom  
  7. {  
  8.     //可以通过//菜单的 “工具/guid生成”。  
  9.     //注意要选择Define Guid{….}格式,并全//部保存下来,保存到哪都行,记事本呀什么的。  
  10.     //因为在做VC程序/////////的时候要用到的。  
  11.     [Guid("298D881C-E2A3-4638-B872-73EADE25511C")]    
  12.     public interface AddComInterface  
  13.     {  
  14.         [DispId(1)]  
  15.         int iadd(int a, int b);  
  16.         [DispId(2)]  
  17.         float ladd(float a, float b);  
  18.     }  
  19.  
  20.     [Guid("2C5B7580-4038-4d90-BABD-8B83FCE***467")]  
  21.     [ClassInterface(ClassInterfaceType.None)]  
  22.     public class AddComService : AddComInterface  
  23.     {  
  24.         public AddComService()  
  25.         {  
  26.         }  
  27.         public int iadd(int a, int b)  
  28.         {  
  29.             int c = 0;  
  30.             c = a + b;  
  31.             return c;  
  32.         }  
  33.         public float ladd(float a, float b)  
  34.         {  
  35.             float c = 0;  
  36.             c = a + b;  
  37.             return c;  
  38.         }  
  39.     }  
  40. }  
  41.  

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

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

在stdafx.h添加:

  1. #import "AddCom.tlb"  
  2. using namespace AddCom; 

程序代码:

  1. void CUseComDlg::OnButtonUse()   
  2. {  
  3.         // TODO: Add your control notification handler code here  
  4.         int dresult;  
  5.         float fresult;  
  6.         CString strResult;  
  7.  
  8.         CoInitialize(NULL);//NULL换成0也可以  
  9.  
  10.         AddCom::AddComInterfacePtr p_Add(__uuidof(AddComService));  
  11.         dresult = p_Add->iadd(1,2);  
  12.         fresult = p_Add->fadd(1.2,2.3);  
  13.         strResult.Format("int:%d \nfloat:%f",dresult,fresult);  
  14.         MessageBox(strResult,"计算结果",MB_OK);  
  15.  
  16.         CoUninitialize();     
  17.           
  18. }  
  19.  

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

(1)VC6.0编写COM

使用VC6.0建立COM组件,工程类型:ATL COM AppWizard

程序代码:

接口:

  1. interface IAdd : IDispatch  
  2.         {  
  3.                 [id(1), helpstring("method iadd")] HRESULT iadd([in]int a, [in]int b, [out]int * c);  
  4.                 [id(2), helpstring("method fadd")] HRESULT fadd([in]float a, [in]float b, [out]float * c);  
  5.                 [id(3), helpstring("method isub")] HRESULT isub([in]int a, [in]int b, [out]int * c);  
  6.         };  

实现:

  1. STDMETHODIMP CAdd::iadd(int a, int b, int *c)  
  2. {  
  3.         // TODO: Add your implementation code here  
  4.         *c = a + b;  
  5.  
  6.         return S_OK;  
  7. }  
  8.  
  9. STDMETHODIMP CAdd::fadd(float a, float b, float *c)  
  10. {  
  11.         // TODO: Add your implementation code here  
  12.         *c = a + b;  
  13.  
  14.         return S_OK;  
  15. }  
  16.  
  17. STDMETHODIMP CAdd::isub(int a, int b, int *c)  
  18. {  
  19.         // TODO: Add your implementation code here  
  20.         *c = a - b;  
  21.  
  22.         return S_OK;  
  23. }  
  24.  

(2)VS2005使用C#编写调用程序(网站程序)

使用VS2005建立网站UseCom

配置:在解决方案资源管理器中的主目录点击右键,选择添加引用,选择COM,添加刚刚建立的AddCom 1.0 Type Library

在程序中要using编写的COM组件:using ADDCOMLib;

引用COM 

VS2005使用C#编写调用程序:引用COM

程序代码:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using ADDCOMLib;  
  11.  
  12. public partial class _Default : System.Web.UI.Page   
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.  
  17.     }  
  18.     protected void ButtonCom_Click(object sender, EventArgs e)  
  19.     {  
  20.         Add add = new Add();  
  21.         int iresult;  
  22.         float fresult;  
  23.         int sresult;  
  24.  
  25.         add.IAdd(10, 20, out iresult);  
  26.         add.fadd((float)1.2,(float)2.3, out fresult);  
  27.         add.isub(100, 10, out sresult);  
  28.  
  29.         TextBoxResult.Text = iresult.ToString();  
  30.         TextBoxRe2.Text = fresult.ToString();  
  31.         TextBoxRe3.Text = sresult.ToString();  
  32.     }  
  33. }  
  34.  

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

(1)VC6.0编写COM组件

使用VC6.0建立COM组件,工程类型:ATL COM AppWizard

程序代码:

接口:

  1. interface IAdd : IDispatch  
  2.         {  
  3.                 [id(1), helpstring("method iadd")] HRESULT iadd([in]int a, [in]int b, [out]int * c);  
  4.                 [id(2), helpstring("method fadd")] HRESULT fadd([in]float a, [in]float b, [out]float * c);  
  5.                 [id(3), helpstring("method isub")] HRESULT isub([in]int a, [in]int b, [out]int * c);  
  6.         };  

实现:

  1. STDMETHODIMP CAdd::iadd(int a, int b, int *c)  
  2. {  
  3.         // TODO: Add your implementation code here  
  4.         *c = a + b;  
  5.  
  6.         return S_OK;  
  7. }  
  8.  
  9. STDMETHODIMP CAdd::fadd(float a, float b, float *c)  
  10. {  
  11.         // TODO: Add your implementation code here  
  12.         *c = a + b;  
  13.  
  14.         return S_OK;  
  15. }  
  16.  
  17. STDMETHODIMP CAdd::isub(int a, int b, int *c)  
  18. {  
  19.         // TODO: Add your implementation code here  
  20.         *c = a - b;  
  21.  
  22.         return S_OK;  
  23. }  
  24.  

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

使用VC6.0建立MFC应用程序UseCOM,调用刚刚建立的COM组件

将上面程序AddCom生成的AddCom.dll放入本程序的工程目录和程序生成目录中

在StdAfx.h中加入:

  1. #import "AddCom.dll" no_namespace 

程序代码:

  1. void CUseComDlg::OnBUTTONUse()   
  2. {  
  3.         // TODO: Add your control notification handler code here  
  4.         CString strResult;  
  5.         CoInitialize(NULL);//NULL换成0也可以  
  6.         IAddPtr m_add = NULL;  
  7.         HRESULT hr = S_OK;  
  8.         hr = m_add.CreateInstance(__uuidof(Add));  
  9.  
  10.         int d_a = 90;  
  11.         int d_b = 10;  
  12.         int d_c;  
  13.         int d_d;  
  14.         float f_a = 1;  
  15.         float f_b = 2;  
  16.         float f_c;  
  17.    
  18.         m_add->_IAdd(d_a,d_b,&d_c);  
  19.         m_add->fadd(f_a,f_b,&f_c);  
  20.         m_add->isub(d_a,d_b,&d_d);  
  21.  
  22.         strResult.Format("返回结果:%d; %f; %d",d_c,f_c,d_d);  
  23.         MessageBox(strResult,"结果",MB_OK);  
  24.  
  25.         m_add.Release();  
  26.         m_add = NULL;  
  27.         CoUninitialize();     
  28.           
  29. }  
  30.  

【编辑推荐】

  1. C#程序中的数据显 示:自定义标签和XML、XSL
  2. C#自定义事件是如何生成的
  3. C# 自定义控件dll文件的生成步骤
  4. C#自定义快捷键的实现
  5. C#自定义事件的步骤介绍
责任编辑:book05 来源: csdn
相关推荐

2009-08-19 14:35:12

C++和C#相互调用C

2011-07-20 16:23:14

C++

2009-09-02 16:43:55

C#调用Excel的C

2009-08-05 16:49:42

C#中调用dll

2009-09-24 15:10:54

C#调用COM组件

2009-09-18 19:09:41

C# COM组件

2009-09-24 14:59:38

C#编写COM组件

2009-08-20 11:03:34

Visual C#使用

2009-08-05 15:10:19

C#调用GoogleE

2009-08-19 10:09:21

C#和C++

2009-08-21 17:45:40

C#调用COM对象

2009-08-03 11:32:49

C#调用COM对象

2009-07-03 10:33:07

C#创建COM组件

2009-08-11 10:12:21

2009-08-03 14:36:08

c#组件

2009-08-05 16:41:36

C#调用VC dll

2011-05-18 18:05:47

C#C++

2009-08-21 09:14:47

C# Excel CO

2010-01-18 11:05:24

C++

2011-05-18 17:56:38

C#C++
点赞
收藏

51CTO技术栈公众号