当前位置: 代码网 > it编程>编程语言>C/C++ > C++中::SHCreateDirectoryEx函数使用方法

C++中::SHCreateDirectoryEx函数使用方法

2025年03月24日 C/C++ 我要评论
在 c++ 中,::shcreatedirectoryex是 windows shell api 提供的函数,用于创建多级目录(类似于mkdir -p命令)。与标准库的createdirectory不

在 c++ 中, ::shcreatedirectoryex 是 windows shell api 提供的函数,用于创建多级目录(类似于 mkdir -p 命令)。与标准库的 createdirectory 不同,它可以自动创建路径中缺失的中间目录。以下是详细使用方法及示例:

1. 函数原型与依赖项

#include <windows.h>
#include <shlobj.h>  // 包含头文件
#pragma comment(lib, "shell32.lib")  // 链接 shell32 库

int shcreatedirectoryex(
  _in_opt_ hwnd                hwnd,         // 父窗口句柄(可设为 null)
  _in_     lpctstr             pszpath,      // 要创建的目录路径
  _in_opt_ const security_attributes *psa    // 安全属性(通常为 null)
);

返回值

  • 0error_success):创建成功。
  • 非零值:错误代码(可通过 getlasterror() 获取详细错误信息)。

2. 基本使用示例

示例 1:创建单层目录

#include <windows.h>
#include <shlobj.h>
#include <iostream>

int main() {
    lpcwstr path = l"c:\\myfolder";  // 宽字符路径(unicode)

    int result = ::shcreatedirectoryex(
        null,        // 无父窗口
        path,        // 目录路径
        null         // 默认安全属性
    );

    if (result == error_success) {
        std::wcout << l"目录创建成功: " << path << std::endl;
    } else {
        std::wcerr << l"错误代码: " << result << std::endl;
    }

    return 0;
}

示例 2:创建多级目录

lpcwstr path = l"c:\\parent\\child\\grandchild";  // 自动创建中间缺失的目录
int result = ::shcreatedirectoryex(null, path, null);

3. 关键注意事项

(1) unicode 与多字节字符集

推荐使用 unicode 版本:windows api 默认优先使用宽字符(wchar/lpcwstr)。

若使用多字节字符集(char),需显式转换路径:

lpcstr patha = "c:\\myfolder";
wchar pathw[max_path];
multibytetowidechar(cp_acp, 0, patha, -1, pathw, max_path);
::shcreatedirectoryex(null, pathw, null);

(2) 路径格式

使用 双反斜杠 \\ 或 正斜杠 / 作为分隔符:

lpcwstr path1 = l"c:/myfolder/subdir";  // 正斜杠
lpcwstr path2 = l"c:\\myfolder\\subdir"; // 双反斜杠

(3) 错误处理

通过返回值判断是否成功,结合 getlasterror() 获取详细信息:

if (result != error_success) {
    dword error = ::getlasterror();
    lpwstr errormsg = nullptr;
    ::formatmessage(
        format_message_allocate_buffer | format_message_from_system,
        null,
        error,
        0,
        (lpwstr)&errormsg,
        0,
        null
    );
    std::wcerr << l"错误: " << errormsg << std::endl;
    ::localfree(errormsg);
}

4. 常见问题与解决方案

问题 1:路径权限不足

  • 表现:返回错误代码 5error_access_denied)。
  • 解决
    • 以管理员权限运行程序(右键 → 以管理员身份运行)。
    • 修改目标目录权限(右键目录 → 属性 → 安全 → 编辑权限)。

问题 2:路径无效或包含非法字符

  • 表现:返回错误代码 123error_invalid_name)。
  • 解决:检查路径是否包含 <>:"|?* 等非法字符。

问题 3:磁盘空间不足

  • 表现:返回错误代码 112error_disk_full)。
  • 解决:清理磁盘或选择其他存储位置。

5. 替代方案

(1) 使用标准库 <filesystem>(c++17 及以上)

#include <filesystem>
namespace fs = std::filesystem;

bool createdirectory(const std::wstring& path) {
    try {
        return fs::create_directories(path);
    } catch (const fs::filesystem_error& e) {
        std::wcerr << l"错误: " << e.what() << std::endl;
        return false;
    }
}

(2) 使用 boost 文件系统库

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;

bool createdirectory(const std::string& path) {
    try {
        return fs::create_directories(path);
    } catch (const fs::filesystem_error& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return false;
    }
}

总结

  • ::shcreatedirectoryex 是 windows 平台下创建多级目录的高效工具,适合需要兼容旧版系统或直接使用 shell api 的场景。
  • 使用时需注意 路径格式字符编码 及 错误处理
  • 对于新项目,优先考虑 c++17 的 <filesystem> 或 boost 库,代码更简洁且跨平台。

到此这篇关于c++中::shcreatedirectoryex函数使用方法的文章就介绍到这了,更多相关c++ ::shcreatedirectoryex内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com