주요 콘텐츠

Visual C++ 프로그램에서 COM 객체 호출하기

참고

COM 객체를 컴파일하고 사용하려면 Microsoft® 컴파일러를 선택해야 합니다.

생성한 COM 객체를 사용하려면 다음을 수행하십시오.

  1. 다음 코드를 사용해 matlab_com_example.cpp라는 파일에 Visual C++® 프로그램을 만듭니다.

    #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;
    }    
  2. MATLAB® 명령 창에서 다음과 같이 프로그램을 컴파일합니다.

    mbuild matlab_com_example.cpp

실행 파일을 실행하면, 프로그램은 두 숫자와 COM 객체의 adddoubles가 반환한 합계를 표시합니다.