MSDN 示例:未编译的打开对话框
MSDN Example: The Open Dialog Box Not Compiling
尝试编译示例代码时出现错误:
MSDN Example: The Open Dialog Box
为什么?
#include <windows.h>
#include <shobjidl.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
return 0;
}
Error C2664 'int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT)': cannot convert
argument 2 from 'PWSTR' to 'LPCSTR' MSDNTut_3-OpenFileDialogBox
...\msdntut_3-openfiledialogbox\msdntut_3-openfiledialogbox\source.cpp 34
Error (active) E0167 argument of type "const wchar_t *" is
incompatible with parameter of type
"LPCSTR" MSDNTut_3-OpenFileDialogBox ...\MSDNTut_3-OpenFileDialogBox\MSDNTut_3-OpenFileDialogBox\Source.cpp 34
Error (active) E0167 argument of type "PWSTR" is incompatible with
parameter of type
"LPCSTR" MSDNTut_3-OpenFileDialogBox ...\MSDNTut_3-OpenFileDialogBox\MSDNTut_3-OpenFileDialogBox\Source.cpp 34
微软 Visual Studio 社区 2017
版本 15.7.1
VisualStudio.15.Release/15.7.1+27703.2000
微软.NET框架
版本 4.7.02556
已安装版本:社区
I fixed it by adding
#ifndef UNICODE
#define UNICODE
#endif
to the top.. I would still like an explanation on why that was necessary, please. ;)
另一个修复方法是将 "Character Set" 设置为 "Use Unicode Character Set"
项目->(项目名称)属性->常规->项目默认值->字符集
I fixed it by adding
#ifndef UNICODE
#define UNICODE
#endif
to the top..
I would still like an explanation on why that was necessary, please. ;)
MessageBox
其实不是函数,是宏:
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE
因此,根据是否定义了 UNICODE
,MessageBox()
映射到 MessageBoxW()
(Unicode) 或 MessageBoxA()
(ANSI)。您的项目未定义 UNICODE
,因此使用了 MessageBoxA()
,并且 PWSTR
(指向宽字符串的指针)与 LPCSTR
(指向窄字符串的指针)不兼容。
您可以改用 PSTR
,或者像您一样启用 UNICODE 支持。
尝试编译示例代码时出现错误:
MSDN Example: The Open Dialog Box
为什么?
#include <windows.h>
#include <shobjidl.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
return 0;
}
Error C2664 'int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT)': cannot convert argument 2 from 'PWSTR' to 'LPCSTR' MSDNTut_3-OpenFileDialogBox ...\msdntut_3-openfiledialogbox\msdntut_3-openfiledialogbox\source.cpp 34
Error (active) E0167 argument of type "const wchar_t *" is incompatible with parameter of type "LPCSTR" MSDNTut_3-OpenFileDialogBox ...\MSDNTut_3-OpenFileDialogBox\MSDNTut_3-OpenFileDialogBox\Source.cpp 34
Error (active) E0167 argument of type "PWSTR" is incompatible with parameter of type "LPCSTR" MSDNTut_3-OpenFileDialogBox ...\MSDNTut_3-OpenFileDialogBox\MSDNTut_3-OpenFileDialogBox\Source.cpp 34
微软 Visual Studio 社区 2017 版本 15.7.1 VisualStudio.15.Release/15.7.1+27703.2000 微软.NET框架 版本 4.7.02556
已安装版本:社区
I fixed it by adding
#ifndef UNICODE #define UNICODE #endif
to the top.. I would still like an explanation on why that was necessary, please. ;)
另一个修复方法是将 "Character Set" 设置为 "Use Unicode Character Set"
项目->(项目名称)属性->常规->项目默认值->字符集
I fixed it by adding
#ifndef UNICODE #define UNICODE #endif
to the top.. I would still like an explanation on why that was necessary, please. ;)
MessageBox
其实不是函数,是宏:
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE
因此,根据是否定义了 UNICODE
,MessageBox()
映射到 MessageBoxW()
(Unicode) 或 MessageBoxA()
(ANSI)。您的项目未定义 UNICODE
,因此使用了 MessageBoxA()
,并且 PWSTR
(指向宽字符串的指针)与 LPCSTR
(指向窄字符串的指针)不兼容。
您可以改用 PSTR
,或者像您一样启用 UNICODE 支持。