各位大大可否幫我看一下底下這段Code為什麼總是無法順利建立pWiaDevMgr這個物件(pWiaDevMgr都是NULL)...到底是哪裡有做錯呢?? #include
#pragma hdrstop #include "frmMain.h"
#include IWiaDevMgr *pWiaDevMgr;
IEnumWIA_DEV_INFO *pWiaEnumDevInfo; //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm" TfrmWIA *frmWIA;
//---------------------------------------------------------------------------
__fastcall TfrmWIA::TfrmWIA(TComponent* Owner) : TForm(Owner)
{
HRESULT hr = S_FALSE; pWiaDevMgr = NULL;
hr = CoInitialize(0);
if(SUCCEEDED(hr))
{
hr = CreateWiaDeviceManager(pWiaDevMgr);
if (SUCCEEDED(hr))
{
EnumerateWiaDevices(pWiaDevMgr);
}
}
CoUninitialize();
} //---------------------------------------------------------------------------
HRESULT __fastcall TfrmWIA::CreateWiaDeviceManager(IWiaDevMgr *pWiaDevMgr)
{
// Create an instance of the device manager
HRESULT hrWiaDevMgr = CoCreateInstance(CLSID_WiaDevMgr,NULL,CLSCTX_LOCAL_SERVER,IID_IWiaDevMgr,(void**)&pWiaDevMgr);
return hrWiaDevMgr;
} //---------------------------------------------------------------------------
HRESULT __fastcall TfrmWIA::EnumerateWiaDevices(IWiaDevMgr *pWiaDevMgr)
{
// Validate arguments
if (NULL == pWiaDevMgr) return E_INVALIDARG; // Get a device enumerator interface
HRESULT hrWiaEnumDevInfo = pWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL,&pWiaEnumDevInfo);
//if (S_OK == hrWiaEnumDevInfo)
if (SUCCEEDED(hrWiaEnumDevInfo))
{
// Loop until you get an error or pWiaEnumDevInfo->Next returns S_FALSE to signal the end of the list.
while (S_OK == hrWiaEnumDevInfo)
{
// Get the next device's property storage interface pointer
IWiaPropertyStorage *pWiaPropertyStorage = NULL;
hrWiaEnumDevInfo = pWiaEnumDevInfo->Next(1,&pWiaPropertyStorage,NULL); // pWiaEnumDevInfo->Next will return S_FALSE when the list is exhausted, so check for S_OK before using the returned value.
if (hrWiaEnumDevInfo == S_OK)
{
// Do something with the device's IWiaPropertyStorage* ,Release the device's IWiaPropertyStorage*
ReadSomeWiaProperties(pWiaPropertyStorage);
pWiaPropertyStorage->Release();
pWiaPropertyStorage = NULL;
}
}
// If the result of the enumeration is S_FALSE (which is normal), change it to S_OK.
if (S_FALSE == hrWiaEnumDevInfo) hrWiaEnumDevInfo = S_OK; // Release the enumerator
pWiaEnumDevInfo->Release();
pWiaEnumDevInfo = NULL;
} return hrWiaEnumDevInfo;
} //---------------------------------------------------------------------------
HRESULT __fastcall TfrmWIA::ReadSomeWiaProperties(IWiaPropertyStorage *pWiaPropertyStorage)
{
AnsiString aStr; // Validate arguments
if (NULL == pWiaPropertyStorage) return E_INVALIDARG; // Declare PROPSPECs and PROPVARIANTs, and initialize them to zero.
PROPSPEC PropSpec[3] = {0};
PROPVARIANT PropVar[3] = {0}; // How many properties are you querying for?
const ULONG c_nPropertyCount = sizeof(PropSpec)/sizeof(PropSpec[0]); // Define which properties you want to read:
// Device ID. This is what you would use to create the device.
PropSpec[0].ulKind = PRSPEC_PROPID;
PropSpec[0].propid = WIA_DIP_DEV_ID; // Device Name
PropSpec[1].ulKind = PRSPEC_PROPID;
PropSpec[1].propid = WIA_DIP_DEV_NAME; // Device description
PropSpec[2].ulKind = PRSPEC_PROPID;
PropSpec[2].propid = WIA_DIP_DEV_DESC; // Ask for the property values
HRESULT hrReadWiaProperties = pWiaPropertyStorage->ReadMultiple(c_nPropertyCount,PropSpec,PropVar);
if (SUCCEEDED(hrReadWiaProperties))
{
// IWiaPropertyStorage::ReadMultiple will return S_FALSE if some
// properties could not be read, so you have to check the return
// types for each requested item. // Check the return type for the device ID
if (VT_BSTR == PropVar[0].vt)
{
// Do something with the device ID
aStr = aStr.sprintf("WIA_DIP_DEV_ID: %ws",PropVar[0].bstrVal);
} // Check the return type for the device name
if (VT_BSTR == PropVar[1].vt)
{
// Do something with the device name
aStr = aStr.sprintf("WIA_DIP_DEV_NAME: %ws",PropVar[1].bstrVal);
} // Check the return type for the device description
if (VT_BSTR == PropVar[2].vt)
{
// Do something with the device description
aStr = aStr.sprintf("WIA_DIP_DEV_DESC: %ws",PropVar[2].bstrVal);
} // Free the returned PROPVARIANTs
FreePropVariantArray(c_nPropertyCount,PropVar);
}
return hrReadWiaProperties;
}