前一陣在工作中做項目的時候,遇到了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 "..debugAddDll.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文件
圖二 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 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)
使用VS2005建立網站UseCom 配置:在解決方案資源管理器中的主目錄點擊右鍵,選擇添加引用,選擇COM,添加剛剛建立的AddCom 1.0 Type Library 在程序中要using編寫的COM組件:using ADDCOMLib; 圖三 引用COM 程序代碼: using System; |
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();
}