COM组件编写与调用.doc

上传人:李医生 文档编号:10826660 上传时间:2021-06-06 格式:DOC 页数:11 大小:180.51KB
返回 下载 相关 举报
COM组件编写与调用.doc_第1页
第1页 / 共11页
COM组件编写与调用.doc_第2页
第2页 / 共11页
COM组件编写与调用.doc_第3页
第3页 / 共11页
COM组件编写与调用.doc_第4页
第4页 / 共11页
COM组件编写与调用.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《COM组件编写与调用.doc》由会员分享,可在线阅读,更多相关《COM组件编写与调用.doc(11页珍藏版)》请在三一文库上搜索。

1、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

2、 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 .debugAddDll.dllusing namespace AddDll;int _tmai

3、n(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: ComVisibl

4、e(true)这用就可以生成AddCom.tlb文件图二 COM生成设置程序代码: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 int

5、erface 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;

6、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.tlbusing namespace AddCom; 程序代码:void CUseComDlg:OnButtonUse() / TODO: Add your control notification handler code here int dresult;

7、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组件,

8、使用VS2005 C#调用(1)VC6.0编写COM使用VC6.0建立COM组件,工程类型:ATL COM AppWizard程序代码:接口:interface IAdd : IDispatch id(1), helpstring(method iadd) HRESULT iadd(inint a, inint b, outint * c); id(2), helpstring(method fadd) HRESULT fadd(infloat a, infloat b, outfloat * c); id(3), helpstring(method isub) HRESULT isub(in

9、int a, inint b, outint * 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, i

10、nt *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;图三 引用COM程序代码:using System;using System.Data;using System.Configuration;using Syste

11、m.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 ButtonC

12、om_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.Tex

13、t = 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(inint a, inint b, outint * c); id(2), helpstring(method fadd) HRESULT fadd(infloat a, infloat b, outfloat * c); id(3),

14、helpstring(method isub) HRESULT isub(inint a, inint b, outint * 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

15、;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

16、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();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1