从 C 代码调用 SHGetKnownFolderPath

发布于 2024-10-29 04:52:34 字数 588 浏览 2 评论 0原文

我正在尝试使用 Visual Studio 2008 从 C 调用 Vista 函数 SHGetKnownFolderPath()。该代码可以像 C++ 一样正常工作,但拒绝使用以下输出编译为 C 代码:

xyz\indexwiki.cpp(316):错误 C2440: 'function':无法转换自 'const GUID' 到 'const KNOWNFOLDERID *const ' xyz\indexwiki.cpp(316) : 警告 C4024: 'SHGetKnownFolderPath' :不同类型的正式和 实际参数1

代码非常多:

PWSTR path;

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

如果可以的话,我更愿意将其保留为 C 并将项目保留为单个源文件。这是较新的 Windows API 的已知问题吗?我通过谷歌找不到太多信息。我错过了什么吗?或者是否有一个涉及铸造或预处理器定义的简单解决方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风追烟花雨 2024-11-05 04:52:34

下面的怎么样?

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

How about the following?

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

苍景流年 2024-11-05 04:52:34

即使你在 C 中传入一个指针,智能感知仍然会抱怨找不到构造函数。我认为这是一个错误,因为我无法摆脱它。我的解决方案是将文件重命名为 .cpp。

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.

从错误消息中可以看出,您传递的是 const GUID,而 SHGetKnownFolderPath 需要 REFKNOWNFOLDERID。尝试使用这个:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文