Getting errors when trying to compile sample code:
MSDN Example: The Open Dialog Box
Why?
#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
Microsoft Visual Studio Community 2017 Version 15.7.1 VisualStudio.15.Release/15.7.1+27703.2000 Microsoft .NET Framework Version 4.7.02556
Installed Version: Community
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. ;)
Another fix is to Set the "Character Set" to "Use Unicode Character Set"
Project-> (Project Name) Properties->General->Project Defaults->Character Set