VC中COM组件的几种调用方式

方式一:#import导入dll库,利用VC提供的智能指针包装类

 

引入dll文件:

    #import "atl_project.dll" no_namespace

调用示例:

    CoInitialize(NULL);

    //

    // CStringModule

    ICStringModulePtr pICString= NULL;

    pICString.CreateInstance(__uuidof(CStringModule));

    BSTR bStrValue= pICString->GetFilePath();

    char *pBuf = _com_util::ConvertBSTRToString(bStrValue);

    SysFreeString(bStrValue);

    cout << "CStringModule::GetFilePath()result:" << pBuf << endl;

    delete pBuf;

    pICString.Release();


    // CMathModule

    ICMathModulePtr pICMath= NULL;

    pICMath.CreateInstance(__uuidof(CMathModule));

    double dNumber1 =5.6;

    double dNumber2 =2.7;

    double dResult  = 0.0;

    pICMath->DoAdd(dNumber1, dNumber2,&dResult);

    cout << "CMathModule::DoAdd()result:" << dResult<< endl;

    pICMath.Release();

    //

    CoUninitialize();

方式二:在工程中引入*_i.h  和*_i.c文件

 

包含头文件:

    #include <comdef.h>

    #include "atl_project_i.h"

调用示例:

    CoInitialize(NULL);

    ICStringModule* pICString= NULL;

    HRESULT hr = CoCreateInstance(CLSID_CStringModule,NULL, CLSCTX_ALL,IID_ICStringModule, (void**)&pICString);

    if (SUCCEEDED(hr)
        &&(pICString != NULL))
    {

        BSTR bStrValue= NULL;

        pICString->GetFilePath(&bStrValue);

        char *pBuf = _com_util::ConvertBSTRToString(bStrValue);

        SysFreeString(bStrValue);

        cout << "CStringModule::GetFilePath()result:" << pBuf << endl;

        delete pBuf;

        pICString->Release();
    }

    CoUninitialize();

方式三:不用CoCreateInstance,直接用CoGetClassObejct得到类厂对象接口,然后用该接口的方法CreateInstance来生成实例(前期准备如方式二)

 

调用示例:

    CoInitialize(NULL);

    IClassFactory* pIClsFactory = NULL;

    HRESULT hr = CoGetClassObject(CLSID_CStringModule,CLSCTX_ALL, NULL,IID_IClassFactory, (void**)&pIClsFactory);

    if (SUCCEEDED(hr) && (pIClsFactory!= NULL))
    {

        ICStringModule* pICString= NULL;

        hr = pIClsFactory->CreateInstance(NULL,IID_ICStringModule, (void**)&pICString);

        if (SUCCEEDED(hr) && (pICString!= NULL))
        {

            BSTR bStrValue= NULL;

            pICString->GetFilePath(&bStrValue);

            char *pBuf = _com_util::ConvertBSTRToString(bStrValue);

            SysFreeString(bStrValue);

            cout << "CStringModule::GetFilePath()result:" << pBuf << endl;

            delete pBuf;

            pICString->Release();
        }

        pIClsFactory->Release();
    }

    CoUninitialize();

方式四:不用CoCreateInstance和CoGetClassObject,直接从dll中得到DllGetClassObject,接着生成类对象及类实例(本方法适合于你想用某个组件,却不想在注册表中注册该组件)

 

调用示例:(前期准备工作如方式二,事实上只要得到CLSID和IID的定义及接口的定义就行)

    typedef HRESULT(__stdcall* pFuncGetCoClass)(REFCLSID,REFIID, void**);

    pFuncGetCoClass FuncGetCoClass= NULL;

    HINSTANCE hDllInst= LoadLibrary(L"atl_project.dll");

    FuncGetCoClass = (pFuncGetCoClass)GetProcAddress(hDllInst,"DllGetClassObject");

    if (FuncGetCoClass!= NULL)
    {

        IClassFactory* pIClsFactory= NULL;

        HRESULT hr = (FuncGetCoClass)(CLSID_CStringModule,IID_IClassFactory, (void**)&pIClsFactory);

        if (SUCCEEDED(hr) && (pIClsFactory!= NULL))
        {

            ICStringModule* pICString= NULL;

            hr = pIClsFactory->CreateInstance(NULL,IID_ICStringModule, (void**)&pICString);

            if (SUCCEEDED(hr) && (pICString!= NULL))
            {

                BSTR bStrValue= NULL;

                pICString->GetFilePath(&bStrValue);

                char *pBuf = _com_util::ConvertBSTRToString(bStrValue);

                SysFreeString(bStrValue);

                cout << "CStringModule::GetFilePath()result:" << pBuf << endl;

                deletepBuf;

                pICString->Release();
            }

            pIClsFactory->Release();
        }
    }

    FreeLibrary(hDllInst);

方式五(MFC调用):【添加】->【类】->【TypeLib 中的MFC类】,选择要调用的组件“atl_project1.0 类型库<1.0>”及其接口(前提是com组件的接口必须是派生自IDispatch)

 

包含头文件:(引入接口时自动生成)

    #include "CCStringModule.h"

调用示例:

    CoInitialize(NULL);

    CCStringModule ICString;

    // 这里可以在CCStringModule中添加一个函数GetClsid()用于返回CLSID

    IID clsId ={0xC4E98946, 0xB075, 0x4C69, {0x83, 0xBB, 0x79, 0xC9, 0x5F, 0xE5, 0xE0, 0x0F}};

    if (ICString.CreateDispatch(clsId)!= NULL)
    {

        CString strValue= ICString.GetFilePath();

        AfxMessageBox(strValue);

        ICString.ReleaseDispatch();
    }

    CoUninitialize();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值