Skip to content

Commit bbb4867

Browse files
VincentZyuwin11台式机VincentZyuwin11台式机
authored andcommitted
fix: delay-load wpcap.dll so binary starts without Npcap installed(build publish, pypi publish, crates publish)
Previously the npcap feature (enabled by default) caused wpcap.dll to be a load-time dependency. On Windows machines without Npcap, the OS loader terminated the process immediately no output, no error, even --help and --version were silent. Changes: - build.rs: add /DELAYLOAD:wpcap.dll for Windows MSVC targets - loopback.rs: pre-check wpcap.dll with LoadLibraryA before calling pcap APIs, return friendly error instead of crashing - bump version to 0.1.7-rc.12
1 parent 50bb44d commit bbb4867

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "winload"
3-
version = "0.1.7-rc.11"
3+
version = "0.1.7-rc.12"
44
description = "Network Load Monitor - nload-like TUI tool for Windows"
55
readme = "readme.md"
66
license = { text = "MIT" }

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "winload"
3-
version = "0.1.7-rc.11"
3+
version = "0.1.7-rc.12"
44
edition = "2021"
55
description = "Network Load Monitor — nload-like TUI tool for Windows/Linux/macOS"
66
license = "MIT"

rust/build.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
fn main() {
2-
println!("cargo:rustc-env=TARGET={}", std::env::var("TARGET").unwrap());
2+
let target = std::env::var("TARGET").unwrap();
3+
println!("cargo:rustc-env=TARGET={target}");
4+
5+
// Windows + npcap feature: delay-load wpcap.dll
6+
// Without this, the binary fails to start on machines without Npcap installed,
7+
// because the OS loader cannot find wpcap.dll at process startup.
8+
// With /DELAYLOAD, wpcap.dll is only loaded when pcap functions are first called
9+
// (i.e., only when --npcap flag is used at runtime).
10+
if target.contains("windows") && cfg!(feature = "npcap") {
11+
println!("cargo:rustc-link-lib=delayimp");
12+
println!("cargo:rustc-link-arg=/DELAYLOAD:wpcap.dll");
13+
}
314
}

rust/src/loopback.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,45 @@ pub mod platform {
5353
use super::*;
5454
use std::thread;
5555

56+
/// Check if wpcap.dll (Npcap) is available on this system.
57+
/// Must be called before any pcap API calls when using /DELAYLOAD,
58+
/// otherwise the delay-load helper will raise a fatal exception.
59+
#[cfg(feature = "npcap")]
60+
fn is_npcap_installed() -> bool {
61+
extern "system" {
62+
fn LoadLibraryA(name: *const u8) -> *mut std::ffi::c_void;
63+
fn FreeLibrary(handle: *mut std::ffi::c_void) -> i32;
64+
}
65+
unsafe {
66+
let handle = LoadLibraryA(b"wpcap.dll\0".as_ptr());
67+
if handle.is_null() {
68+
false
69+
} else {
70+
FreeLibrary(handle);
71+
true
72+
}
73+
}
74+
}
75+
5676
/// 启动 Npcap 回环捕获线程
5777
///
5878
/// 返回 Ok(info_msg) 成功时,后台线程会持续累加计数器。
5979
/// 返回 Err(msg) 如果 Npcap 不可用或打开设备失败。
6080
#[cfg(feature = "npcap")]
6181
pub fn start_npcap(counters: LoopbackCounters) -> Result<String, String> {
82+
// Pre-flight: verify wpcap.dll is loadable before calling any pcap functions.
83+
// The binary uses /DELAYLOAD:wpcap.dll, so pcap functions are not resolved
84+
// until first call. If the DLL is missing, that first call would crash.
85+
if !is_npcap_installed() {
86+
return Err(format!(
87+
"Npcap is not installed on this system.\n\n\
88+
The --npcap flag requires Npcap to capture loopback traffic.\n\
89+
Please install Npcap from: {NPCAP_URL}\n\
90+
(Enable 'Support loopback traffic capture' during installation)\n\n\
91+
Or run without the --npcap flag."
92+
));
93+
}
94+
6295
// 尝试查找 Npcap Loopback 适配器
6396
let devices = pcap::Device::list().map_err(|e| {
6497
format!(

0 commit comments

Comments
 (0)