调用COM对象中VISUAL C ++程式

注意

你必须选择一个微软®编译器来编译和使用任何COM对象。

使用您创建如下COM对象:

  1. 创建一个Visual C ++®程序指定的文件matlab_com_example.cpp用下面的代码:

    的#include <的iostream>使用命名空间std;的#include “myComponent的\ SRC \ mycomponent_idl.h” 的#include “myComponent的\ SRC \ mycomponent_idl_i.c” INT主(){//初始化参数变量VARIANT X,Y,OUT1;//初始化COM库HRESULT HR = CoInitialize的(NULL);//创建创建Imycomponentclass * pImycomponentclass的COM对象的实例;HR = CoCreateInstance的(CLSID_mycomponentclass,NULL,CLSCTX_INPROC_SERVER,IID_Imycomponentclass,(无效**)&pImycomponentclass);//设置的输入参数到COM方法x.vt = VT_R8;y.vt = VT_R8;x.dblVal = 7.3;y.dblVal = 1946.0;//访问带有参数的方法和接收该输出端OUT1 HR =(pImycomponentclass  - > adddoubles(1,&OUT1,X,Y)); // Print the output cout << "The input values were " << x.dblVal << " and " << y.dblVal << ".\n"; cout << "The output of feeding the inputs into the adddoubles method is " << out1.dblVal << ".\n"; // Uninitialize COM CoUninitialize(); return 0; }
  2. 在MATLAB®命令窗口,如下编译程序:

    mbuild matlab_com_example.cpp

当您运行可执行文件,程序会显示两个数字,它们的总和,由COM对象的返回adddoubles