如何在C++控制台应用程序中使用shell32.dll

Str*_*der 1 c++ linker static-linking

我需要做的是获得ApplicationData路径,我在谷歌发现有一个叫做的功能

HRESULT SHGetFolderPath(
  __in   HWND hwndOwner,
  __in   int nFolder,
  __in   HANDLE hToken,
  __in   DWORD dwFlags,
  __out  LPTSTR pszPath
);
Run Code Online (Sandbox Code Playgroud)

但它存在于shell32.dll中.在C#中,我会做类似的事情

[DllImport]
static extern HRESULT SHGetFolderPath() and so on.
Run Code Online (Sandbox Code Playgroud)

在C++ Console应用程序中我需要做什么才能调用此API?也许,我可以使用LoadLibrary()?但是这样做的正确方法是什么?

我能以某种方式静态链接这个DLL作为我的exe的一部分吗?我正在使用Visual Studio 2010.

Han*_*ant 9

你需要#include shlobj.h并链接到shell32.lib.像这样:

#include "stdafx.h"
#include <windows.h>
#include <shlobj.h>
#include <assert.h>
#pragma comment(lib, "shell32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR path[MAX_PATH];
    HRESULT hr = SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path);
    assert(SUCCEEDED(hr));
    // etc..
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

#pragma注释负责告诉链接器.

  • @Jesse,因为它*更容易解释.它并没有错. (3认同)