1

我想使用 COM 函数:http: CreateInstance //msdn.microsoft.com/en-us/library/k2cy7zfz%28v=vs.80%29.aspx

像这样

IPointer p=NULL;
HRESULT hr=p.CreateInstance(xxx);

但是我没有xxx我只知道它的接口名称CLSID当 我查看文件时,我可以在 tlb 文件中看到它的接口描述。我应该怎么做才能使用它?ISubPointeroleviewCreateInstance

4

3 回答 3

0

You have a couple of options for obtaining the class ID of the object you want to create. You can use the OLE Viewer to generate the header files or you can directly import the type library into your source file using the #import directive. The CreateInstance function that you refer to is a non-static member of _com_ptr_t and requires you to use an instance of it.

The following example should get you on your way.

#include <comip.h>  // _com_ptr_t
#import "tlbname.tlb" // Change to the name of your type library


int main()
{
    CoInitialize(NULL);

    ::_com_ptr_t<ISubPointer>   ptr;

    // CoISubPointer is the class ID specified in the type library
    // you will need to change the name accordingly.
    ptr.CreateInstance(__uuid(CoISubPointer), NULL, CLSCTX_INPROC_SERVER);

    CoUninitialize();

    return 0;
 }

When main() has finished ptr will automatically release it's reference to the ISubPointer object it holds.

于 2013-05-21T07:36:55.943 回答
0

有两种方法可以做到这一点:

第一个:一个ClassFactory,和

第二:创建指针的辅助函数。

我发现了这个

int main()
{
    IMath* pIMath;
    HRESULT hr;

    // 1. Initialize COM Library
    CoInitialize(NULL);

    // 2. Call CoCreateInstance to get the IMath interface pointer
    hr = CoCreateInstance ( __uuidof(CMathComp), NULL, CLSCTX_INPROC_SERVER,
                            __uuidof(IMath), (void**) &pIMath );

    if ( FAILED(hr) )
    {
        return 0;
    }

    // 3. Call the interface functions
    int sum = pIMath->Add(1, 3);
    printf("Sum = %d \n", sum);

    int sub = pIMath->Sub(4, 3);
    printf("Sub = %d \n", sub);

    // 4. Release the interface pointer if you are done
    pIMath->Release();

    // 5. Un-Initialize COM Library
    CoUninitialize();

    return 0;
}

另请参阅MSDN

HRESULT CoCreateInstance(
  _In_   REFCLSID rclsid,
  _In_   LPUNKNOWN pUnkOuter,
  _In_   DWORD dwClsContext,
  _In_   REFIID riid,
  _Out_  LPVOID *ppv
);

如果您可以CLSID从 OLEVIEW 收集使用它,否则必须有关于此的文档。你不能在不暴露 ist 的情况下交付组件CLSID

于 2013-05-21T07:26:21.093 回答
0

你不能在不知道类 ID 的情况下创建 COM 对象。我建议阅读这篇文章中的 COM 基础http://www.codeproject.com/Articles/633/Introduction-to-COM-What-It-Is-and-How-to-Use-It

于 2013-05-21T12:01:10.130 回答