線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2101
推到 Plurk!
推到 Facebook!

WIA Device Manager 物件建立失敗

尚未結案
JammyHuang
一般會員


發表:7
回覆:2
積分:1
註冊:2004-05-20

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-26 14:03:50 IP:202.145.xxx.xxx 未訂閱
各位大大可否幫我看一下底下這段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; }
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-26 14:21:11 IP:210.68.xxx.xxx 未訂閱
您好:    PO程式碼的方式與版規說明請參考下面連結,煩請修改謝謝您的配合 >
系統時間:2024-04-28 14:37:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!