C++获取windows桌面的路径

WINSHELLAPI HRESULT WINAPI SHGetSpecialFolderLocation (HWND hwndOwner, int nFolder,LPITEMIDLIST * ppidl); //函数声明
hwndOwner: 指定了”所有者窗口”,在调用这个函数是可能出現的对话框或信息框.
nFolder: 是一个整数id,決定哪个目录是待查找目录,它的取值可能是
CSIDL_BITBUCKET 回收站
CSIDL_CONTROLS 控制面板
CSIDL_DESKTOP Windows桌面desktop;
CSIDL_DESKTOPDIRECTORY desktop的目录;
CSIDL_DRIVES 我的电脑
CSIDL_FONTS 字体目录
CSIDL_NETHOOD 网上邻居
CSIDL_NETWORK 网上邻居virtual folder
CSIDL_PERSONAL 我的文档
CSIDL_PRINTERS 打印机
CSIDL_PROGRAMS 程序组
CSIDL_RECENT 最近打开文档
CSIDL_SENDTO 发送到菜单项
CSIDL_STARTMENU 快启菜单
CSIDL_STARTUP 启动目录
CSIDL_TEMPLATES 临时文档
ppidl: pidl地址. SHGetSpecialFolderLocation把地址写到pidl.

#include "windows.h"
#include "shlobj.h" 
string  getDesktopPath()
{
    LPITEMIDLIST pidl;
    LPMALLOC pShellMalloc;
    char szDir[200];
    if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
    {
        if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
            // 如果成功返回true  
            SHGetPathFromIDListA(pidl, szDir);
            pShellMalloc->Free(pidl);
        }
        pShellMalloc->Release();
    }

    return string(szDir);
}
int main()
{



    string  path = getDesktopPath();
    cout << path << endl;

    system("pause");

    return 0;
}

你可能感兴趣的:(c++,C语言)