array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 [转]C++和C#编写并且相互调用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,项目类型为:类库

程序代码:

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 "../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#编写的COM组件,使用VC6.0调用

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

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

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

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

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

程序代码:

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)
{
        // 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#编写调用程序(网站程序)

使用VS2005建立网站UseCom

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

在程序中要using编写的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中编写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();   
        
}
      

结束语:希望能对大家有帮助!

相关文章: