diff --git a/src/icf_mount.cpp b/src/icf_mount.cpp index 6af092d..be147f6 100644 --- a/src/icf_mount.cpp +++ b/src/icf_mount.cpp @@ -104,8 +104,7 @@ namespace icf_mount { // overlay.vhd已存在 → 直接挂载(数据已在里面,无需重新拷贝) if (all_ok && !app_entries.empty()) { auto *last = app_entries.back(); - std::wstring exe_dir = utils::get_exe_dir(); - std::wstring overlay_vhd = std::format(L"{}\\overlay.vhd", exe_dir); + std::wstring overlay_vhd = std::format(L"{}\\overlay.vhd", utils::get_cwd()); std::wstring last_tag = std::format(L"APP_{}", last->part_index); std::wstring parent_for_create = std::format(L"\\\\?\\FscryptDisk_{}\\internal_{}.vhd", last_tag, last->part_index); @@ -318,8 +317,7 @@ namespace icf_mount { } // 3) overlay.vhd:移除盘符 + detach(保留文件供下次沿用) - std::wstring exe_dir = utils::get_exe_dir(); - std::wstring overlay_vhd = std::format(L"{}\\overlay.vhd", exe_dir); + std::wstring overlay_vhd = std::format(L"{}\\overlay.vhd", utils::get_cwd()); { char drive = static_cast(cfg.mount_letter); std::wstring mount_point = std::format(L"{}:\\", cfg.mount_letter); diff --git a/src/main.cpp b/src/main.cpp index e183341..c329d55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,12 +82,12 @@ int main(int argc, char *argv[]) { return 1; } - // 确定config.ini路径:优先命令行参数,否则使用exe所在目录 + // 确定config.ini路径:优先命令行参数,否则使用当前运行目录 std::wstring config_path; if (argc >= 3) { config_path = utils::to_wstring(argv[2]); } else { - config_path = std::format(L"{}\\config.ini", utils::get_exe_dir()); + config_path = std::format(L"{}\\config.ini", utils::get_cwd()); } // 解析config.ini diff --git a/src/utils/files.cpp b/src/utils/files.cpp index f250501..002577c 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -34,6 +34,14 @@ namespace utils { return get_directory(std::wstring(buf, len)); } + // 获取当前运行目录(进程工作目录CWD) + std::wstring get_cwd() { + wchar_t buf[32768]; + DWORD len = GetCurrentDirectoryW(_countof(buf), buf); + if (len == 0 || len >= _countof(buf)) return L"."; + return std::wstring(buf, len); + } + // 递归拷贝目录 bool copy_dir_overwrite(const std::wstring &src_dir, const std::wstring &dst_dir, diff --git a/src/utils/files.h b/src/utils/files.h index 78e2c35..eb8e6db 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -15,6 +15,9 @@ namespace utils { // 获取当前exe所在目录(末尾不带反斜杠) std::wstring get_exe_dir(); + // 获取当前运行目录(进程工作目录CWD,末尾不带反斜杠) + std::wstring get_cwd(); + // 递归拷贝 src_dir -> dst_dir,文件已存在时强制覆盖 // 返回:成功返回true,out_files_copied 输出已拷贝文件数 bool copy_dir_overwrite(const std::wstring &src_dir, diff --git a/src/vhd_mount.h b/src/vhd_mount.h index c9cc132..b147f4d 100644 --- a/src/vhd_mount.h +++ b/src/vhd_mount.h @@ -26,7 +26,7 @@ namespace vhd_mount { std::string *error); // 创建差分VHD(overlay层),父盘指向parent_vhd_path - // vhd_path: 输出VHD文件路径(将被创建,如exe目录\overlay.vhd) + // vhd_path: 输出VHD文件路径(将被创建,如当前运行目录\overlay.vhd) // parent_vhd_path: 父盘NT路径,如 \\?\FscryptDisk_APP_N\internal_N.vhd // error: 输出错误信息 // 返回:成功返回true