新聞中心
前一陣在工作中做項(xiàng)目的時(shí)候,遇到了COM組件的調(diào)用和使用問(wèn)題,當(dāng)時(shí)研究和好一陣,才把中間的環(huán)節(jié)打通,現(xiàn)在寫(xiě)出C++和C#相互調(diào)用COM組件的程序來(lái)為大家提供方便,這里包含了四個(gè)類(lèi)型:

創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供潛山企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為潛山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
1、在VS2005中,C#編寫(xiě)DLL并使用C++調(diào)用
2、在VS2005中C#編寫(xiě)的COM組件,使用VC6.0調(diào)用
3、在VC6.0中編寫(xiě)COM組件,使用VS2005 C#調(diào)用
4、在VC6.0中編寫(xiě)COM組件,使用VC6.0調(diào)用
其中每個(gè)類(lèi)型都寫(xiě)了兩個(gè)程序,一個(gè)為COM組件程序,一個(gè)為C++和C#相互調(diào)用COM組件調(diào)用程序
程序?qū)崿F(xiàn):
1、在VS2005中,C#編寫(xiě)DLL并使用C++調(diào)用
(1)C#編寫(xiě)DLL程序
建立C#編寫(xiě)的DLL程序AddDll,項(xiàng)目類(lèi)型為:類(lèi)庫(kù)
程序代碼:
- 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++編寫(xiě)調(diào)用程序
建立C++的Win32控制臺(tái)應(yīng)用程序UseDll,項(xiàng)目類(lèi)型為:Win32控制臺(tái)應(yīng)用程序
配置:右鍵點(diǎn)擊解決方案資源管理器中的UseDll,選擇“屬性”,將公共語(yǔ)言運(yùn)行庫(kù)支持設(shè)置為“公共語(yǔ)言運(yùn)行庫(kù)支持(/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#編寫(xiě)的COM組件,使用VC6.0調(diào)用
(1)VS2005中使用C#編寫(xiě)COM組件
建立C#編寫(xiě)的COM組件,項(xiàng)目類(lèi)型為類(lèi)庫(kù)
配置:右鍵點(diǎn)擊解決方案資源管理器中的AddCom,選擇“屬性”,選擇“生成”,選擇“為COM Interop注冊(cè)(_P)”
打開(kāi)AssemblyInfo.cs文件,設(shè)置[assembly: ComVisible(true)]
這用就可以生成AddCom.tlb文件
程序代碼:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace AddCom
- {
- //可以通過(guò)//菜單的 “工具/guid生成”。
- //注意要選擇Define Guid{….}格式,并全//部保存下來(lái),保存到哪都行,記事本呀什么的。
- //因?yàn)樵谧鯲C程序/////////的時(shí)候要用到的。
- [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-8B83FCE***467")]
- [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編寫(xiě)調(diào)用程序
使用VC6.0編寫(xiě)建立MFC應(yīng)用程序UseCom,項(xiàng)目類(lèi)型為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,"計(jì)算結(jié)果",MB_OK);
- CoUninitialize();
- }
3、在VC6.0中編寫(xiě)COM組件,使用VS2005 C#調(diào)用
(1)VC6.0編寫(xiě)COM使用VC6.0建立COM組件,
工程類(lèi)型: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);
- };
- 實(shí)現(xiàn):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#編寫(xiě)調(diào)用程序(網(wǎng)站程序)
使用VS2005建立網(wǎng)站UseCom
配置:在解決方案資源管理器中的主目錄點(diǎn)擊右鍵,選擇添加引用,選擇COM,添加剛剛建立的AddCom 1.0 Type Library
在程序中要using編寫(xiě)的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中編寫(xiě)COM組件,使用VC6.0調(diào)用
(1)VC6.0編寫(xiě)COM組件使用VC6.0建立COM組件,
工程類(lèi)型: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);
- };
- 實(shí)現(xiàn):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編寫(xiě)調(diào)用程序
使用VC6.0建立MFC應(yīng)用程序UseCOM,調(diào)用剛剛建立的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("返回結(jié)果:%d; %f; %d",d_c,f_c,d_d);
- MessageBox(strResult,"結(jié)果",MB_OK);
- m_add.Release();
- m_add = NULL;
- CoUninitialize();
- }
C++和C#相互調(diào)用COM組件程序的編寫(xiě)方法就給大家介紹到這里。
【編輯推薦】
- C# winForm自定義鼠標(biāo)樣式的兩種方法
- C#自定義消息框的設(shè)置圖解
- 掌握C#自定義泛型類(lèi):從初始化說(shuō)起
- C#存儲(chǔ)過(guò)程的循序漸進(jìn)
- 存儲(chǔ)過(guò)程的優(yōu)勢(shì)及其調(diào)用方法介紹
分享名稱(chēng):C++和C#相互調(diào)用COM組件的方法簡(jiǎn)介
分享路徑:http://fisionsoft.com.cn/article/dhdjpcg.html


咨詢(xún)
建站咨詢(xún)
