How to use WIA in C++?

2.6k views Asked by At

I found this tutorial on using WIA in c++, but I don't understand how to use it. The following doesn't succeed, and I don't what exactly is wrong here. Basically I copied the code from the tutorial.

void init() {
    IWiaDevMgr2* devMgr; 
    HRESULT hr = createWiaDeviceManager( &devMgr ); 

    if(!SUCCEEDED(hr))
    {
        std::cout << "couldn't create WIA Device Manager!\n"; 
        return; 
    }
   ...
}

HRESULT createWiaDeviceManager(IWiaDevMgr2** devMgr)
{
    if(devMgr == 0) return E_INVALIDARG; 

    *devMgr = 0; 

    return CoCreateInstance( CLSID_WiaDevMgr2, 0, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)devMgr); 
}

By the way, I am using Windows 7 64-bit, so using IWiaDevMgr2 should be fine.

1

There are 1 answers

0
Hans Passant On BEST ANSWER

createWiaDeviceManager always returns -2147221008

That's 0x800401f0 when you interpret/display it as an unsigned number. As you should, HRESULT is not a signed type. From the WinError.h SDK file:

//
// MessageId: CO_E_NOTINITIALIZED
//
// MessageText:
//
// CoInitialize has not been called.
//
#define CO_E_NOTINITIALIZED              _HRESULT_TYPEDEF_(0x800401F0L)

Add this line to your initialization code:

CoInitialize(NULL);