如何从C++控制台应用程序中使用SHIL32.DLL

如何从C++控制台应用程序中使用SHIL32.DLL,c++,linker,static-linking,C++,Linker,Static Linking,我需要做的是获取ApplicationData路径,我在Google中发现有一个函数名为 HRESULT SHGetFolderPath( __in HWND hwndOwner, __in int nFolder, __in HANDLE hToken, __in DWORD dwFlags, __out LPTSTR pszPath ); 但它存在于shell32.dll中 在C中,我会做一些类似的事情 [DllImport] static exter

我需要做的是获取ApplicationData路径,我在Google中发现有一个函数名为

HRESULT SHGetFolderPath(
  __in   HWND hwndOwner,
  __in   int nFolder,
  __in   HANDLE hToken,
  __in   DWORD dwFlags,
  __out  LPTSTR pszPath
);
但它存在于shell32.dll中 在C中,我会做一些类似的事情

[DllImport]
static extern HRESULT SHGetFolderPath() and so on.
<>我需要做什么在C++控制台应用程序,才能调用这个API? 也许,我可以使用LoadLibrary? 但正确的方法是什么呢

我能以某种方式将此dll静态链接为exe的一部分吗?
我正在使用Visual Studio 2010。

include和pragma commentlib,Shell32.lib应该可以工作。

include和pragma commentlib,Shell32.lib应该可以工作。

您需要包括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;
}

pragma注释负责将其告知链接器。

您需要包括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;
}

pragma注释负责将其告知链接器。

您为什么使用pragma而不只是将其添加到链接器选项下?@Jesse,因为这更容易解释。它没有什么问题,它不是代码。这只是一个指令。与include没有什么不同。你为什么使用pragma而不只是将其添加到链接器选项下?@Jesse,因为它更容易解释。它没有什么问题,它不是代码。这只是一个指令。不象包括。