如何使用shell32.dll从C ++控制台应用程序 [英] How to use shell32.dll from C++ console application

查看:537
本文介绍了如何使用shell32.dll从C ++控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的是得到 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()等。

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



我可以以静态方式将此dll链接到我的exe中吗?
我使用Visual Studio 2010。

解决方案

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

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

int _tmain(int argc,_TCHAR * argv [])
{
TCHAR路径[MAX_PATH] ;
HRESULT hr = SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,0,path);
assert(SUCCEEDED(hr));
//等..
return 0;
}

#pragma注释告诉链接器。 >

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.

解决方案

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.

这篇关于如何使用shell32.dll从C ++控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆