Search code examples
c++comdirectx

reinterpret_cast Fails to Compile


I am trying to gather some experience with DirectWrite but I am failing to create the factory:

I have a C++ (CLR) class

public ref class TextFormat{
internal:
    static IDWriteFactory* pBaseFactory;
.....
public:
    TextFormat(String^ FontFamilyName, FontWeights FontWeight,
      FontStyles FontStyle, FLOAT FontSize);
....
}

and a .cpp file with the code

.....
HRESULT HResult = DWriteCreateFactory(
    DWRITE_FACTORY_TYPE_SHARED,
    __uuidof(IDWriteFactory),
    reinterpret_cast<IUnknown**>(&pBaseFactory));
.....

The compiler complains about the reinterpretcast "Invalid type conversion". I think I have copied the existing examples good enough and I do not see why the cast is failing.


Solution

  • I guess you are experimenting with a C++ helper library.

    I happened to run into exactly the same problem about a year ago.

    My workaround was to create a local temporary variable, run the factory creation with that variable as target and then copy the value into my helper class, just like this:

    HRESULT HResult = DWriteCreateFactory (
      DWRITE_FACTORY_TYPE_SHARED,
      __uuidof( IDWriteFactory ),
      reinterpret_cast<IUnknown**>( &pTempFactory )
    );
    if( HResult != 0 )System::Runtime::InteropServices::Marshal::ThrowExceptionForHR (HResult);
    pBaseFactory = pTempFactory;
    

    I am not sure if this is an elegant solution but it worked for me.