2

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?

joe
  • 8,344
  • 9
  • 54
  • 80
hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • 1
    Well it does say it is a reference to a `KNOWNFOLDERID`. So the C equivalent then would require a pointer as the error shows. – Jeff Mercado Apr 02 '11 at 07:12

3 Answers3

5

How about the following?

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

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • Indeed. I hadn't used C or C++ for a few years and forgot some of the subtle differences between how references are used as compared to pointers. And I wasn't used to references being used in OS APIs. Plus it was made more opaque with Microsofts's defines and the error message talking about a GUID in one place but a KNOWNFOLDERID in the other. – hippietrail Apr 03 '11 at 01:09
1

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.

kainjow
  • 3,955
  • 1
  • 20
  • 17
0

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);
Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • 3
    ... as if casting some shit to some other shit solves any problems? I'm tempted to flag your response as offensive :-) – edgar.holleis Apr 02 '11 at 09:46
  • Well, as hippietrail said, his code works in C++ but not in C. If that is the case, then casting is enough since that is the only difference between C and C++. – Serafeim Apr 02 '11 at 16:44
  • @Sefareim: My C++ was pretty rusty and I'd forgotten the bit about references that you only mention them in the function being called and not in the code calling the function, so that's another difference. – hippietrail Apr 03 '11 at 01:07