Calling SHGetKnownFolderPath from C code

5.7k Views Asked by At

I'm trying to call the Vista function SHGetKnownFolderPath() from C using Visual Studio 2008. The code works fine as C++ but refuses to compile as C code with this output:

xyz\indexwiki.cpp(316) : error C2440: 'function' : cannot convert from 'const GUID' to 'const KNOWNFOLDERID *const ' xyz\indexwiki.cpp(316) : warning C4024: 'SHGetKnownFolderPath' : different types for formal and actual parameter 1

The code is pretty much:

PWSTR path;

HRESULT hr = SHGetKnownFolderPath(
  FOLDERID_Profile,
  0,
  NULL,
  &path
);

I'd prefer to keep it as C and keep the project as a single source file if I can. Is this a known problem with newer Windows APIs? I couldn't find much via Google. Am I missing something? Or is there perhaps a simple workaround involving casting or preprocessor defines?

3

There are 3 best solutions below

1
On BEST ANSWER

How about the following?

HRESULT hr = SHGetKnownFolderPath(&FOLDERID_Profile, 0, NULL, &path);

0
On

Even if you pass in a pointer in C, the intellisense will still complain about not finding a constructor. I think this is a bug, because I couldn't get rid of it. My solution was to just rename the file to .cpp.

3
On

As you can see from the error message, you are passing a const GUID, while the SHGetKnownFolderPath wants a REFKNOWNFOLDERID. Try using this:

HRESULT hr = SHGetKnownFolderPath( (REFKNOWNFOLDERID)FOLDERID_Profile,  0,  NULL,  &path);