VS2010 集成标准com组件

#include <Windows.h>
#include <stdio.h>

// import后,会提示找不到对应的SignCtrl.tlh文件,只需要对项目重新编译一下,在项目的Debug目录下,就会有SignCtrl.tlh文件
#import "C:/Users/Administrator/Desktop/jzbank/CallComSln/CallCom/CallCom/SignCtrl.ocx" no_namespace

extern "C"
{
    typedef HRESULT (__stdcall *pFunc)(REFCLSID rclsid,REFIID riid,LPVOID * ppv);
}    

void main(){

     //谨记谨记,必须先调用 CoInitialize(NULL);
    if (!SUCCEEDED(CoInitialize(NULL)))  
    { 
        return;
    }

    HINSTANCE hdllInst = LoadLibrary("SignCtrl.ocx");  

    pFunc func=(pFunc)GetProcAddress(hdllInst,"DllGetClassObject");  
    
    if (func != NULL)  
    {  
        
        CLSID CLSID_SignCtrl_ocx;  
        HRESULT hr = CLSIDFromString(L"{7CDD83B9-75FA-4C7A-89A6-1ABC55126D57}", &CLSID_SignCtrl_ocx);
        if (!SUCCEEDED(hr))
        {
            return;
        }

        IClassFactory* pcf = NULL; 
        hr = func(CLSID_SignCtrl_ocx, IID_IClassFactory,(void**)&pcf);  

        if (SUCCEEDED(hr) && (pcf != NULL))  
        {  
            // 创建 IID
            IID CLSID_DSignCtrl;

            //_DSignCtrl的标识{05c0ba8b-20f1-40ee-ae8c-cb78d9809203} 在tlh文件里
            CLSID_DSignCtrl.Data1=0x05c0ba8b;  
            CLSID_DSignCtrl.Data2=0X20f1;   
            CLSID_DSignCtrl.Data3=0X40ee;  

            CLSID_DSignCtrl.Data4[0]=0Xae;  
            CLSID_DSignCtrl.Data4[1]=0X8c;  
            CLSID_DSignCtrl.Data4[2]=0Xcb;  
            CLSID_DSignCtrl.Data4[3]=0X78;  
            CLSID_DSignCtrl.Data4[4]=0Xd9;  
            CLSID_DSignCtrl.Data4[5]=0X80;  
            CLSID_DSignCtrl.Data4[6]=0X92;  
            CLSID_DSignCtrl.Data4[7]=0X03;  
                
            // SignCtrl.ocx向外暴露的
            _DSignCtrl* pCall = NULL;

            // 拿到实例
            hr = pcf->CreateInstance(NULL, CLSID_DSignCtrl, (void**)&pCall);  

            if (SUCCEEDED(hr)   && (pCall != NULL))  
            {
                pCall->AboutBox();
            }

            pcf->Release();

        } else{
            printf("false");
            
        }
    }   

    FreeLibrary(hdllInst);

    // 谨记谨记, 调用 CoInitialize(NULL);后要销毁
    CoUninitialize(); 

    getchar();
}

 

posted @ 2018-03-14 15:57  cnblogs_z_s  阅读(415)  评论(0编辑  收藏  举报