Windows API 第二篇 SHGetSpecialFolderPath

Posted prairieNew

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows API 第二篇 SHGetSpecialFolderPath相关的知识,希望对你有一定的参考价值。

BOOL SHGetSpecialFolderPath( HWND hwndOwner,
                                 LPTSTR lpszPath,
                                 int nFolder,
                                 BOOL fCreate );

参数解释:

hwndOwnerHandle to the owner window the client should specify if it displays a dialog box or message box.

lpszPathPointer to a null-terminated string that receives the drive and path of the specified folder. This buffer must be at least MAX_PATH characters in size

nFolder:A CSIDL that identifies the folder of interest. If a virtual folder is specified, this function will fail.

fCreate:Indicates if the folder should be created if it does not already exist. If this value is nonzero, the folder will be created. If this value is zero, the folder will not be created.

 

一个简单的test
建立控制台程序:

#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <Shlobj.h>

using namespace std;;


int _tmain(int argc, _TCHAR* argv[])
{
    WCHAR szPath[MAX_PATH + 1] = { 0 };
    wstring strMsgW;

    BOOL bRet;
    //严格一点,每个返回值要判断
    bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_PROGRAM_FILES, FALSE);
    strMsgW.append(L"CSIDL_PROGRAM_FILES: ");
    strMsgW.append(szPath);
    strMsgW.append(L"
");

    bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_PROGRAM_FILES_COMMON, FALSE);
    strMsgW.append(L"CSIDL_PROGRAM_FILES_COMMON: ");
    strMsgW.append(szPath);
    strMsgW.append(L"
");

    bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_SYSTEM, FALSE);
    strMsgW.append(L"CSIDL_SYSTEM: ");
    strMsgW.append(szPath);
    strMsgW.append(L"
");

    bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_STARTUP , FALSE);
    strMsgW.append(L"CSIDL_STARTUP: ");
    strMsgW.append(szPath);
    strMsgW.append(L"
");

    bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_APPDATA , FALSE);
    strMsgW.append(L"CSIDL_APPDATA: ");
    strMsgW.append(szPath);
    strMsgW.append(L"
");

    MessageBox(NULL, strMsgW.c_str(), L"SHGetSpecialFolderPath Test", MB_OK);

    return 0;
}

运行结果:

 

技术分享图片

 





以上是关于Windows API 第二篇 SHGetSpecialFolderPath的主要内容,如果未能解决你的问题,请参考以下文章

第二篇 如何选择操作系统

第二篇 如何选择操作系统

小白学习windows第二篇

API解读第二篇——执行SQL的核心对象(JDBC)

第二篇:数据可视化 - 基本API

python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy