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

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

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

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

其中每个类型都写了两个程序,一个为COM组件程序,一个为C++和C#相互调用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. namespace AddDll
  5. {
  6. public class Add
  7. {
  8. public int iadd(int a, int b)
  9. {
  10. int c = a + b;
  11. return c;
  12. }
  13. }
  14. }

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

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

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

程序代码:

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

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

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

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

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

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

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

程序代码:

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

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

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

在stdafx.h添加:

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

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. };
  7. 实现:STDMETHODIMP CAdd::iadd(int a, int b, int *c)
  8. {
  9. // TODO: Add your implementation code here
  10. *c = a + b;
  11. return S_OK;
  12. }
  13. STDMETHODIMP CAdd::fadd(float a, float b, float *c)
  14. {
  15. // TODO: Add your implementation code here
  16. *c = a + b;
  17. return S_OK;
  18. }
  19. STDMETHODIMP CAdd::isub(int a, int b, int *c)
  20. {
  21. // TODO: Add your implementation code here
  22. *c = a - b;
  23. return S_OK;
  24. }

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

使用VS2005建立网站UseCom

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

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

程序代码:

  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. public partial class _Default : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. }
  16. protected void ButtonCom_Click(object sender, EventArgs e)
  17. {
  18. Add add = new Add();
  19. int iresult;
  20. float fresult;
  21. int sresult;
  22. add.IAdd(10, 20, out iresult);
  23. add.fadd((float)1.2,(float)2.3, out fresult);
  24. add.isub(100, 10, out sresult);
  25. TextBoxResult.Text = iresult.ToString();
  26. TextBoxRe2.Text = fresult.ToString();
  27. TextBoxRe3.Text = sresult.ToString();
  28. }
  29. }

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. };
  7. 实现:STDMETHODIMP CAdd::iadd(int a, int b, int *c)
  8. {
  9. // TODO: Add your implementation code here
  10. *c = a + b;
  11. return S_OK;
  12. }
  13. STDMETHODIMP CAdd::fadd(float a, float b, float *c)
  14. {
  15. // TODO: Add your implementation code here
  16. *c = a + b;
  17. return S_OK;
  18. }
  19. STDMETHODIMP CAdd::isub(int a, int b, int *c)
  20. {
  21. // TODO: Add your implementation code here
  22. *c = a - b;
  23. return S_OK;
  24. }

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

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

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

在StdAfx.h中加入:

#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. int d_a = 90;
  10. int d_b = 10;
  11. int d_c;
  12. int d_d;
  13. float f_a = 1;
  14. float f_b = 2;
  15. float f_c;
  16. m_add->_IAdd(d_a,d_b,&d_c);
  17. m_add->fadd(f_a,f_b,&f_c);
  18. m_add->isub(d_a,d_b,&d_d);
  19. strResult.Format("返回结果:%d; %f; %d",d_c,f_c,d_d);
  20. MessageBox(strResult,"结果",MB_OK);
  21. m_add.Release();
  22. m_add = NULL;
  23. CoUninitialize();
  24. }

C++和C#相互调用COM组件的方法简介相关推荐

  1. 来总结一下在VC中调用COM组件的方法

    来总结一下在VC中调用COM组件的方法(大家来补充) [问题点数:50分,结帖人_foo] http://bbs.csdn.net/topics/50319093 发表于: 2004-04-17 16 ...

  2. Vue中父组件调用子组件的方法

    场景 SpringBoot+Vue+Echarts实现选择时间范围内数据加载显示柱状图: SpringBoot+Vue+Echarts实现选择时间范围内数据加载显示柱状图_BADAO_LIUMANG_ ...

  3. Angular中父组件通过ViewChild调用子组件的方法

    场景 Angualr中通过原生js和ViewChild的方式获取dom: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10586 ...

  4. vue 父组件 调用 子组件的方法

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 我们都知道通过$ref可以获取到某个DOM,但是它也可以用来获取子组件的实例,调用子组件的方法 例: ...

  5. iframe调用父页面方法_5.1 vue中子组件调用父组件的方法,务必理解自定义事件的重要性...

    问题:vue中子组件调用父组件的方法 通过v-on 监听 和$emit触发来实现: 1.在父组件中 通过v-on 监听 当前实例上的 自定义事件. 2.在子组件 中 通过'$emit'触发 当前实例上 ...

  6. vue中组件之间调用方法——子组件调用父组件的方法 父组件调用子组件的方法

    vue中组件之间调用方法--子组件调用父组件的方法 & 父组件调用子组件的方法 1.vue中子组件调用父组件的方法 1.1.第一种方法是直接在子组件中通过this.$parent.event来 ...

  7. vue父组件中调用子组件中的方法 及vue父组件调用孙子组件的方法

    一:通过ref直接调用子组件的方法: 子组件child.vue: <template><div>我是子组件</div> </template> < ...

  8. 子组件调用父组件中方法的方法

    1.在子组件中用this.$parent.fn()来调用父组件的方法 父组件中: <script>export default {methods: {parentFn() {console ...

  9. vue3 setup语法糖下父组件调用子组件的方法

    vue3下,父组件调用子组件的方法,如果使用了<script setup> 这种写法,那么子组件方法需要采用defineExpose()进行修饰,才能被外界调用.上代码: 1.子组件 _p ...

最新文章

  1. RDKit | 基于相似图可视化原子贡献
  2. 12 个最佳的免费网络监控工具--转载
  3. 什么是野指针和内存泄露?如何避免野指针
  4. Struts2入门(二)——配置拦截器
  5. 如何回答性能优化的问题,才能打动阿里面试官?
  6. 未检测到其他显示器_同维工控机显示器显示器没反应维修技术精湛
  7. Oracle系列:Oracle RAC集群体系结构
  8. C语言和C++中Struct区别
  9. CodeSmith实用技巧(五):利用继承生成可变化的代码
  10. 我能想到的圆角背景的实现方法
  11. Nginx服务的ssl认证和htpasswd认证
  12. oracle监听防止连接风暴,Oracle Listener 监听风暴处理
  13. 手机照片局部放大镜_拼音输入法哪个最好?百度手机输入法——最受年轻人喜爱...
  14. 24个基本指标精粹讲解(2)--KDJ
  15. 初识Uniprot API
  16. 混jdon坛子的一些思考
  17. 互联网必备技能1-写好PPT
  18. layui 动态表格 合并单元格
  19. for/in 循环遍历对象的属性
  20. 树莓派 raspbian 系统常用命令

热门文章

  1. redis将散裂中某个值自增_redis五种数据类型
  2. psychopy 音频时长代码_PsychoPy入门_03_视频和音频的呈现
  3. batchnorm pytorch_GitHub趋势榜第一:TensorFlow+PyTorch深度学习资源大汇总
  4. php socket传递cookie,PHP Websocket在测试中验证用户(传递会话cookie)
  5. mysql relay log时间_如何得到Slave应用relay-log的时间
  6. 计算机二级vf上机试题,2016年计算机二级《VF》上机题及答案
  7. php pdo无法使用,php - php-无法使用PDO连接到数据库 - SO中文参考 - www.soinside.com
  8. linux中使用u盘和光驱的命令_Linux文件操作高频使用命令
  9. VHDL设计出租车计价器
  10. asp python 定时任务_python定时任务最强框架APScheduler详细教程