-1

In trying to create an IWIC Factory, the CoCreateInstance function is returning E_INVALIDARG (One or more arguments are invalid). I checked MSDN and I cannot see which of these arguments could be invalid.

IWICImagingFactory* iwicfactory = nullptr;

HRESULT IWFactHRes = CoCreateInstance(CLSID_WICImagingFactory, 
  NULL,
  CLSCTX_INPROC_SERVER, 
  IID_IWICImagingFactory, 
  (LPVOID*)iwicfactory
);

Am I missing something?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
kazama
  • 19
  • 4
  • 1
    `(void**)&iwicfactory` must be – RbMm Jun 10 '17 at 09:36
  • Oh! I added the &, and it seemed to fix that problem. It is now saying "CoInitialize was not called." This may be a completely separate issue though. – kazama Jun 10 '17 at 09:50
  • 1
    of course need call `CoInitialize` before any `CoCreateInstance` – RbMm Jun 10 '17 at 09:52
  • Ah, I didn't realize that. I'll do that now then. – kazama Jun 10 '17 at 09:53
  • 2
    The message "CoInitialize was not called" should have been a clue that maybe you need to call CoInitialize. – Raymond Chen Jun 10 '17 at 13:30
  • Ok, thanks for the sarcasm/ rudeness. Everything I searched for on MSDN and any other site, never once mentioned CoInitialize, so I had no idea. Your comment has absolutely no purpose other than to be rude to me, so next time you should keep it to yourself. – kazama Jun 10 '17 at 15:16
  • That's a ground rule for COM, that every thread must be initialized and join an apartment, before it can use any COM objects. It isn't spelled out for every COM interface. It is expected that you know how to use a technology, and since you are using Direct2D, your thread should have initialized COM already anyway. – IInspectable Jun 10 '17 at 17:46
  • I see. I didn't know that. I'm still in the process of trying to learn direct 2-d and familiarize myself with the whole COM system. I am just a beginner and a student. This site isn't just for professionals already in the field is it? – kazama Jun 10 '17 at 18:53

1 Answers1

1

The final parameter to CoCreateInstance is the

address of pointer variable that receives the interface pointer requested in riid.

Your code currently passes the pointer variable, not the address of it. You have to change

(LPVOID*)iwicfactory

to

(LPVOID*)&iwicfactory


Additional notes:
  • It is a good idea to use the IID_PPV_ARGS macro, which ensures that IID and interface pointer are in sync as well as removing the need for a cast:

    HRESULT IWFactHRes = CoCreateInstance(CLSID_WICImagingFactory, 
      NULL,
      CLSCTX_INPROC_SERVER, 
      IID_PPV_ARGS(&iwicfactory)
    );
    
  • COM needs to be initialized on the calling thread before the call to CoCreateInstance. Call either CoInitialize or CoInitializeEx to do so.

IInspectable
  • 46,945
  • 8
  • 85
  • 181