偶想通过IMoniker接口,获得一个已经存在的COM对象的接口指针,请问各位DX,如何为之?

解决方案 »

  1.   

    IMoniker->QueryInterface(IID_IUnknown, (void**)&pUnk);
      

  2.   

    http://64.233.167.104/search?q=cache:ZGe3fM9WE2gJ:www.huihoo.com/ppt/com/10.ppt+BindToObject&hl=zh-CN&lr=lang_zh-CN
      

  3.   

    aspnetwuxueyou:那IMoniker指针应该怎么创建呢?是创建成“Pointer Moniker”吗?可不可以再详细一些?偶很笨滴,谢谢啦!另外,你后来贴出来滴网址怎么进不去呀?
      

  4.   

    void CustomRpt(char *pszObject) 

        HRESULT             hr; 
        WCHAR               szObject[128]; 
        WCHAR               wszMsg[128] = {L"Your Message Here...\n"}; 
        IMoniker            *pmkObject = NULL; 
        IUnknown            *pIUnk = NULL; 
        IPersistFile        *pIPersistFile = NULL; 
        ICustomInterface    *pICustomInterface = NULL; 
     
        // Create a wide-character version of the object's file name. 
        wsprintf(wszObject, L"%hs", pszObject); 
     
        // Get a file moniker for the object (a *.smp file). 
        hr = CreateFileMoniker(wszObject, &pmkObject); 
     
        if(FAILED(hr)) 
        { 
            printf("Client: CreateFileMoniker for Object failed"); 
            return; 
        } 
     
        // BindMoniker is equivalent to calling CreateBindCtx() followed by 
        // a call to BindToObject(). It has the net result of binding the 
        // interface (specified by the IID) to the moniker. 
     
        hr = BindMoniker(pmkObject, 0, IID_IUnknown, (void **)&pIUnk); 
        if (FAILED(hr)) 
        { 
            printf("Client: BindMoniker failed (%x)\n", hr); 
            return; 
        } 
     
        // Try a couple QueryInterface calls into the object code; first a 
        // QueryInterface to IPersistFile... 
     
        hr = pIUnk->QueryInterface(IID_IPersistFile, 
           (void **)&pIPersistFile); 
     
        if (FAILED(hr)) { 
            printf("Client: QueryInterface IPersistFile failed (%x)\n", hr); 
            pIUnk->Release(); 
            return; 
        } 
     
        // Followed by a QueryInterface to ICustomInterface. 
        hr = pIUnk->QueryInterface(IID_ICustomInterface, 
                        (void **)&pICustomInterface); 
     
        if (FAILED(hr)) { 
            printf("Client: QueryInterface failed (%x)\n", hr); 
            pIUnk->Release(); 
            pIPersistFile->Release(); 
            return; 
        } 
     
        // CustomReport() is the object function that displays the time and 
        // date information on the object. 
        hr = pICustomInterface->CustomReport(); 
     
        if (FAILED(hr)) 
        { 
            printf("Client: pICustomInterface->CustomReport failed (%x)\n", 
                 hr); 
            pIUnk->Release(); 
            pIPersistFile->Release(); 
            return; 
        } 
     
        // Clean up resources by calling release on each of the interfaces. 
        pIPersistFile->Release(); 
        pICustomInterface->Release(); 
        pIUnk->Release(); 
        return; 

      

  5.   

    1. create
     // Get a file moniker for the object (a *.smp file). 
        hr = CreateFileMoniker(wszObject, &pmkObject); 
    2. bind to object
       // BindMoniker is equivalent to calling CreateBindCtx() followed by 
        // a call to BindToObject(). It has the net result of binding the 
        // interface (specified by the IID) to the moniker. 
     
        hr = BindMoniker(pmkObject, 0, IID_IUnknown, (void **)&pIUnk); 
      

  6.   

    3. you can call functions thru pIUnk pointer
      

  7.   

    试了一下,还是不行。请问void CustomRpt(char *pszObject)中的参数应该是什么数据呀?
      

  8.   

    内部没有做,你是query不出来的