2013-01-18 37 views
0

這段代碼應該編譯沒有錯誤,你應該鏈接-lcomdlg32的對話框。如果我使用指針,程序將返回:0x22fcd868。並且應該(我認爲)在對話框中返回文件用戶類型的名稱。爲什麼lpstrFile在內存中返回地址而不是OPENFILENAMEW結構中的char數組?

#include <windows.h> 
#include <iostream> 

int main() { 
    wchar_t szFileName[MAX_PATH] = {0}; 
    OPENFILENAMEW ofn; 
    ZeroMemory(&ofn, sizeof(ofn)); 
    ofn.lStructSize = sizeof(OPENFILENAME); 
    ofn.nMaxFile = MAX_PATH; 
    ofn.lpstrFile = szFileName; 
    GetSaveFileNameW(&ofn); 

    using namespace std; 
    cout << szFileName << endl; 
    cout << *szFileName << endl; // also a number not a string 
} 

回答

6

你必須使用wcout如果你想輸出的wchar_t數組作爲空值終止的寬字符串。

2

cout不支持wchar_twchar_t*,但它支持intvoid*。編譯器最終將wchar_t轉換爲int,並將wchar_t*降級爲void*。這就是爲什麼你看到cout打印wchar_t的數值和wchar_t*的內存地址。

改爲使用wcout。它支持wchar_twchar_t*,所以它可以打印實際的數據代替:

wcout << szFileName << endl; 
wcout << *szFileName << endl; 
相關問題