关于COM和QueryInterface的问题 [英] Question about COM and QueryInterface

查看:71
本文介绍了关于COM和QueryInterface的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将界面导入我的源文件。 然后我创建一个对象的实例并调用一个返回另一个对象的函数。 调用成功,但返回的对象是IDispatch对象。 因此,我尝试将
转换为预期类型(通过QueryInterface直接和间接),但它不起作用。 但是,如果我调用该对象应该具有的函数,则调用成功。

I have imported an interface into my source file.  I then create an instance of an object and call a function that is to return another object.  The call succeeds, but the returned object is an IDispatch object.  So I try and get it converted to the expected type (both directly and indirectly via QueryInterface) and it doesn't work.  However, if I call a function that the object is supposed to have, the call succeeds.

我的问题是,为什么会失败? 发布的uuid不是正确的吗? 如果我是创建此对象的公司的一部分,我将修复什么?  (我是该公司的一部分,但在另一个部门和有问题的SW获得了
,因此这些信息与帮助其他团队有关。)

My question is, why is this failing?  Is the published uuid not the correct one?  If I were part of the company that created this object, what would I be fixing?  (I am part of the company, but in a different department and the SW in question was acquired, so this information is relevant to helping out another team).




我不介意某人将帖子标记为"建议作为答案",但请勿将其标记为"已回答" ;。如果我是OP,我将决定帖子是否真的回答了我的帖子。谢谢。

I don't mind someone marking a post as "Proposed as answer", but DO NOT mark it as "Answered". If I am the OP, I will decide if a post actually answers my post or not. Thank you.

推荐答案

以下是一些示例代码:

Here is some sample code:

#include <type_traits>

#import "progid:Idea.IdeaClient"
#import "progid:Idea.TableDef"

// Including these for intellisense
#include "Debug\idea.tlh"
#include "Debug\comdb.tlh"
int main()
{
    using namespace Idea;
    using namespace COMDBLib;
    CoInitialize(nullptr);

    IIdeaClientPtr client;
    HRESULT hr = client.CreateInstance("Idea.IdeaClient");
    // hr == S_OK

    auto pDB = client->OpenDatabase("Sample-Employees.IMD");
    static_assert(std::is_same_v<decltype(pDB), IDispatchPtr>, "Not same type");
    // pDB is populated and seems to be valid

    IComDatabasePtr db;
    hr = pDB->QueryInterface(IID_PPV_ARGS(&db));
    // QueryInterface() returns E_NOINTERFACE - No such interface supported.
...

如果我使用Invoke()调用Database对象中的函数,它将起作用,如果该函数返回另一个IDispatch对象,我可以让它正确公开接口,如下所示:

If I call a function in the Database object using Invoke(), it will work and if that function returns another IDispatch object, I can get it to expose the interface correctly, like so:

...
int main()
{
    using namespace Idea;
    using namespace COMDBLib;
    CoInitialize(nullptr);

    IIdeaClientPtr client(__uuidof(Idea::IdeaClient));
    IDispatchPtr pDB = client->OpenDatabase("Sample-Employees.IMD");

    DISPID dispID;
    HRESULT hr;
    DISPPARAMS dp = { NULL, NULL, 0, 0 };
    wchar_t name[] = L"TableDef";
    LPOLESTR lpszName = name;
    hr = pDB->GetIDsOfNames(IID_NULL, &lpszName, 1, LOCALE_USER_DEFAULT, &dispID);
    VARIANT vResult;
    VariantInit(&vResult);
    hr = pDB->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, nullptr, nullptr);
    assert(vResult.vt == VT_DISPATCH);

    ITableDefPtr tabledef = vResult.pdispVal;
    // YAY! tabledef is not null!!
...


这篇关于关于COM和QueryInterface的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆