2014-11-02 36 views
0

我想獲取有關用戶使用FileOpenPicker選擇的文件的某些文件信息,但所有信息(如路徑和名稱)均爲空。當我嘗試查看對象的斷點我得到了以下信息:Windows 8應用程序FileOpenPicker np文件信息

文件= 0x03489cd4 <沒有資料,沒有加載shell32.dll中的符號>

我用下面的代碼調用FileOpenPicker和HANDELING文件

#include "pch.h" 
#include "LocalFilePicker.h" 

using namespace concurrency; 
using namespace Platform; 
using namespace Windows::Storage; 
using namespace Windows::Storage::Pickers; 

const int LocalFilePicker::AUDIO = 0; 
const int LocalFilePicker::VIDEO = 1; 
const int LocalFilePicker::IMAGES = 2; 

LocalFilePicker::LocalFilePicker() 
{ 
    _init(); 
} 

void LocalFilePicker::_init() 
{ 
    _openPicker = ref new FileOpenPicker(); 
    _openPicker->ViewMode = PickerViewMode::Thumbnail; 
} 

void LocalFilePicker::askFile(int categorie) 
{ 
    switch (categorie) 
    { 
    case 0: 
     break; 
    case 1: 
     _openPicker->SuggestedStartLocation = PickerLocationId::VideosLibrary; 
     _openPicker->FileTypeFilter->Append(".mp4"); 
     break; 
    case 2: 
     break; 
    default: 
     break; 
} 

create_task(_openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
{ 
    if (file) 
    { 
     int n = 0; 
     wchar_t buf[1024]; 
     _snwprintf_s(buf, 1024, _TRUNCATE, L"Test: '%s'\n", file->Path); 
     OutputDebugString(buf); 
    } 
    else 
    { 
     OutputDebugString(L"canceled"); 
    } 
}); 
} 

任何人都可以看到什麼是錯的代碼或一些問題,爲應用程序爲什麼不能得到預期的工作環境中。

回答

4

首先解釋爲什麼你在調試時遇到麻煩,當你編寫WinRT程序時,這會發生很多事情。首先,確保啓用了正確的調試引擎。工具+選項,調試,常規。確保「使用託管兼容模式」處於關閉狀態。

現在,您可以檢查「文件」選項,它應該像這樣:

enter image description here

很難解釋當然。你在看什麼是代理。它是一個COM術語,是一個COM對象的包裝器,它不是線程安全的或者存在於另一個進程或機器中。代理實現存在於shell32.dll中,因此是混淆的診斷消息。根本看不到實際的對象,訪問其屬性需要調用代理方法。調試器無法做到的事情,代理會將調用從一個線程編組到另一個線程,而另一個線程在調試器中斷處於活動狀態時被凍結。

這使得你很盲目,在困難的情況下,你可能想寫一個littler幫助程序代碼來將該屬性存儲在局部變量中。像:

auto path = file->Path; 

沒有檢查或觀察那個麻煩。你現在應該有信心文件沒有問題,你會得到一個非常好的路徑。請注意,如何編寫const wchar_t* path = file->Path;可以讓您從編譯器中得到響亮的投訴。

這可以幫助您找到該錯誤,但無法將Platform :: String傳遞給printf()樣式函數。就像你不能跟std :: wstring一樣。你需要使用一個存取函數來轉換它。修復:

_snwprintf_s(buf, 1024, _TRUNCATE, 
       L"Test: '%s'\n", 
       file->Path->Data()); 
+0

感謝您的回答,這正是我一直在尋找的。 – furrie 2014-11-10 17:22:28

相關問題