![](/img/trans.png)
[英]Deprecated conversion from string constant to LPWSTR when filling lpstrFile in OPENFILENAMEW struct
[英]why lpstrFile is returning address in memory instead of char array in OPENFILENAMEW struct?
此代碼應編譯無誤,您應該為對話框鏈接-lcomdlg32
。 程序返回: 0x22fcd8
或68
如果我使用指針)。 並且(我認為)應該在對話框中返回文件用戶類型的名稱。
#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
}
如果要輸出wchar_t
數組作為以null終止的寬字符串,則必須使用wcout
。
cout
不支持wchar_t
或wchar_t*
,但是它支持int
和void*
。 編譯器最終將wchar_t
轉換為int,並將wchar_t*
降級為void*
。 這就是為什么看到cout
打印wchar_t
的數值和wchar_t*
的內存地址的原因。
請改用wcout
。 它支持wchar_t
和wchar_t*
,因此它可以打印實際數據:
wcout << szFileName << endl;
wcout << *szFileName << endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.