Необходимо выбрать компилятор Microsoft®, чтобы скомпилировать и использовать любой COM-объект.
Используйте COM-объект, который вы создали можно следующим образом:
Создайте Визуальный C ++® программа в файле с именем matlab_com_example.cpp со следующим кодом:
#include <iostream>
using namespace std;
#include "mycomponent\src\mycomponent_idl.h"
#include "mycomponent\src\mycomponent_idl_i.c"
int main() {
// Initialize argument variables
VARIANT x, y, out1;
//Initialize the COM library
HRESULT hr = CoInitialize(NULL);
//Create an instance of the COM object you created
Imycomponentclass *pImycomponentclass;
hr=CoCreateInstance
(CLSID_mycomponentclass, NULL, CLSCTX_INPROC_SERVER,
IID_Imycomponentclass,(void **)&pImycomponentclass);
// Set the input arguments to the COM method
x.vt=VT_R8;
y.vt=VT_R8;
x.dblVal=7.3;
y.dblVal=1946.0;
// Access the method with arguments and receive the output 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;
} В Командном окне MATLAB® скомпилируйте программу можно следующим образом:
mbuild matlab_com_example.cpp
Когда вы запускаете исполняемый файл, программа отображает два числа и их сумму, как возвращено adddoubles COM-объекта.