c++ - Cannot get SHGetKnownFolderPath() function working -
i having troubles using shgetknownfolderpath() function. getting following error message: type error in argument 1 'shgetknownfolderpath'; expected 'const struct _guid *' found 'struct _guid'.
in knowfolders.h
have following relevant definitions:
#define define_known_folder(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \ extern_c const guid name ... define_known_folder(folderid_programfiles,0x905e63b6,0xc1bf,0x494e,0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a);
i using pelles c compiler .
this sample code:
#include <windows.h> #include <wchar.h> #include <knownfolders.h> #include <shlobj.h> int wmain(int argc, wchar_t **argv) { pwstr path = null; hresult hr = shgetknownfolderpath(folderid_programfiles, 0, null, &path); if (succeeded(hr)){ wprintf(l"%ls", path); } cotaskmemfree(path); return 0; }
how fix error message?
edit have found code examples shgetknownfolderpath(); of them execute function without pointer. instance:
hr = shgetknownfolderpath(folderid_public, 0, null, &pszpath); if (succeeded(hr)) { wprintf(l"folderid_public: %s\n", pszpath); cotaskmemfree(pszpath); }
with comments of jonathan potter, able correct example.
the problem subtle. following code line looks c, c++.
hresult hr = shgetknownfolderpath(folderid_documents, 0, null, &path);
the shgetknownfolderpath()
function has following prototype:
stdapi shgetknownfolderpath(refknownfolderid, dword, handle, pwstr*);
its first argument refknownfolderid
.
in shtypes.h
file find following:
#ifdef __cplusplus #define refknownfolderid const knownfolderid & #else #define refknownfolderid const knownfolderid * /*__midl_const*/ #endif /* __cplusplus */
this means, in c++ refknownfolderid
reference , in c pointer. consequence, not need ampersand in c++ code first parameter. in visual c++ c code compiled c++ , distinction between languages blurred.
the second issue, unresolved external symbol 'folderid_programfiles'. error.
error fixed adding #include <initguid.h>
before #include <shlobj.h>
. reason explained in article.
so following code compiles on pelles c.
#include <windows.h> #include <initguid.h> #include <knownfolders.h> #include <shlobj.h> #include <wchar.h> int wmain(void) { pwstr path = null; hresult hr = shgetknownfolderpath(&folderid_documents, 0, null, &path); if (succeeded(hr)) { wprintf(l"%ls\n", path); } cotaskmemfree(path); return 0; }
Comments
Post a Comment