How to use shell32.dll from C++ console application

做~自己de王妃 提交于 2019-12-24 10:47:49

问题


What I need to do is to get ApplicationData path , I've found in Google that there is function called

HRESULT SHGetFolderPath(
  __in   HWND hwndOwner,
  __in   int nFolder,
  __in   HANDLE hToken,
  __in   DWORD dwFlags,
  __out  LPTSTR pszPath
);

But it exists in shell32.dll In C# I'd do something like

[DllImport]
static extern HRESULT SHGetFolderPath() and so on.

What do I need to do in C++ Console application, to be able to call this API? Maybe, I can use LoadLibrary()? But what is the right way to do this?

Can I somehow statically link this dll to be part of my exe? I am using Visual Studio 2010.


回答1:


You need to #include shlobj.h and link to shell32.lib. Like this:

#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;
}

The #pragma comment takes care of telling the linker about it.




回答2:


#include <Shlobj.h> and #pragma comment(lib,"Shell32.lib") should work.



来源:https://stackoverflow.com/questions/9863674/how-to-use-shell32-dll-from-c-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!