Search code examples
c++com

How to create an instance of IDiaDataSource when using the #import command?


I'm trying use the #import command on msdia140.dll which is for the Debug Interface Access SDK.

Yes, I know that there is a header file for it already with the interfaces, but I want to use smart COM pointers, hence the use of the #import command.

The import command I use is:

#import "bin/msdia140.dll" \
    rename("virtual", "Virtual")\
    exclude(               \
      "IStream"            \
    , "ISequentialStream"  \
    , "_LARGE_INTEGER"     \
    , "_ULARGE_INTEGER"    \
    , "tagSTATSTG"         \
    , "_FILETIME"          \
    , "IEnumUnknown")

The rename is because of a keyword conflict, and the exclusions are to get rid of warnings of importing types whose names already exist.

In the ...App::InitInstance() function, I call CoInitialize(NULL); and in the ...App::ExitInstance() I have CoUninitialize();.

Now I'm trying to do the equivalent to:

CComPtr<IDiaDataSource> pSource;
CoCreateInstance( CLSID_DiaSource,
                   NULL,
                   CLSCTX_INPROC_SERVER,
                   __uuidof( IDiaDataSource ),
                  (void **) &pSource);

in as stated in the instructions here, but without success. The command I've tried is:

Dia2Lib::IDiaDataSourcePtr dataSource;
dataSource.CreateInstance(__uuidof(Dia2Lib::IDiaDataSource));

but the smart pointer remains as NULL. What am I doing wrong?


Solution

  • Look at the return value of CreateInstance(). It will tell you the problem, or at least the first problem.

    One problem I see off the bat is this:

    Dia2Lib::IDiaDataSourcePtr dataSource;
    dataSource.CreateInstance(__uuidof(Dia2Lib::IDiaDataSource));
    

    It should be:

    Dia2Lib::IDiaDataSourcePtr dataSource;
    HRESULT hr = dataSource.CreateInstance(__uuidof(Dia2Lib::DiaSource));
    

    You are using an interface ID instead of the CLSID of a coclass--will always be a problem.