Skip to content

QueryResult API

query 模块提供了用于查询已安装 MSVC 和 Windows SDK 组件的结构化 API。

核心类型

QueryOptions

查询操作的配置选项。

rust
use msvc_kit::query::{QueryOptions, QueryComponent, QueryProperty};
use msvc_kit::Architecture;

let options = QueryOptions::builder()
    .install_dir("C:/msvc-kit")
    .arch(Architecture::X64)
    .component(QueryComponent::All)
    .property(QueryProperty::All)
    .msvc_version("14.44")
    .sdk_version("10.0.26100.0")
    .build();
字段类型默认值描述
install_dirPathBuf"msvc-kit"要查询的安装目录
archArchitecture主机架构目标架构
componentQueryComponentAll要查询的组件
propertyQueryPropertyAll要获取的属性
msvc_versionOption<String>None指定 MSVC 版本(None = 最新)
sdk_versionOption<String>None指定 SDK 版本(None = 最新)

QueryComponent

rust
pub enum QueryComponent {
    All,   // 查询 MSVC 和 SDK
    Msvc,  // 仅查询 MSVC 编译器
    Sdk,   // 仅查询 Windows SDK
}

可从字符串解析:"all""msvc""sdk""winsdk"

QueryProperty

rust
pub enum QueryProperty {
    All,      // 返回所有信息
    Path,     // 安装路径
    Env,      // 环境变量
    Tools,    // 工具可执行文件路径
    Version,  // 版本信息
    Include,  // include 路径
    Lib,      // 库路径
}

支持别名的字符串解析:

  • "path" / "paths" / "install-path"
  • "env" / "environment" / "env-vars"
  • "tools" / "tool" / "executables"
  • "version" / "versions" / "ver"
  • "include" / "includes" / "include-paths"
  • "lib" / "libs" / "lib-paths"

QueryResult

查询操作的结果,包含所有发现的信息。

rust
pub struct QueryResult {
    pub install_dir: PathBuf,
    pub arch: String,
    pub msvc: Option<ComponentInfo>,
    pub sdk: Option<ComponentInfo>,
    pub env_vars: HashMap<String, String>,
    pub tools: HashMap<String, PathBuf>,
}

方法

方法返回类型描述
tool_path(name)Option<&PathBuf>获取指定工具的路径
env_var(name)Option<&String>获取指定环境变量的值
msvc_version()Option<&str>获取 MSVC 版本字符串
sdk_version()Option<&str>获取 SDK 版本字符串
msvc_install_path()Option<&Path>获取 MSVC 安装路径
sdk_install_path()Option<&Path>获取 SDK 安装路径
all_include_paths()Vec<&PathBuf>获取所有 include 路径
all_lib_paths()Vec<&PathBuf>获取所有库路径
to_json()serde_json::Value导出为 JSON
format_summary()String人类可读的摘要

ComponentInfo

单个已安装组件的信息。

rust
pub struct ComponentInfo {
    pub component_type: String,
    pub version: String,
    pub install_path: PathBuf,
    pub include_paths: Vec<PathBuf>,
    pub lib_paths: Vec<PathBuf>,
    pub bin_paths: Vec<PathBuf>,
}

函数

query_installation

rust
pub fn query_installation(options: &QueryOptions) -> Result<QueryResult>

查询已有安装的组件信息。

示例:

rust
use msvc_kit::query::{QueryOptions, query_installation};

let options = QueryOptions::builder()
    .install_dir("C:/msvc-kit")
    .build();

let result = query_installation(&options)?;

// 获取 cl.exe 路径
if let Some(cl) = result.tool_path("cl") {
    println!("cl.exe: {}", cl.display());
}

// 获取所有环境变量
for (key, value) in &result.env_vars {
    println!("{}={}", key, value);
}

可查询的工具

以下工具名可通过 tool_path() 查询:

名称可执行文件描述
clcl.exeC/C++ 编译器
linklink.exe链接器
liblib.exe静态库管理器
ml64ml64.exeMASM 汇编器 (x64)
nmakenmake.exeMake 工具
rcrc.exe资源编译器
mtmt.exe清单工具
dumpbindumpbin.exe二进制文件转储工具
editbineditbin.exe二进制文件编辑工具

环境变量

env_vars 字段包含以下标准变量:

变量示例
INCLUDEC:\msvc-kit\VC\Tools\MSVC\14.44\include;...
LIBC:\msvc-kit\VC\Tools\MSVC\14.44\lib\x64;...
PATHC:\msvc-kit\VC\Tools\MSVC\14.44\bin\Hostx64\x64;...
VCToolsVersion14.44.34823
VCToolsInstallDirC:\msvc-kit\VC\Tools\MSVC\14.44.34823
VCINSTALLDIRC:\msvc-kit\VC
WindowsSdkDirC:\msvc-kit\Windows Kits\10
WindowsSDKVersion10.0.26100.0\
WindowsSdkBinPathC:\msvc-kit\Windows Kits\10\bin\10.0.26100.0
Platformx64

Released under the MIT License.