From 58155eab2c57d54db127cafd3d007660e2eb7b27 Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Sat, 2 Dec 2023 17:44:18 -0500 Subject: [PATCH 1/3] Adding dll search path for Npcap. --- .../java/pcap/jdk7/internal/NativeMappings.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java index 7eed4b0b..486f8723 100644 --- a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java +++ b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java @@ -16,9 +16,12 @@ import com.sun.jna.PointerType; import com.sun.jna.Structure; import com.sun.jna.ptr.PointerByReference; + +import java.io.File; import java.lang.reflect.Method; import java.net.InetAddress; import java.nio.ByteOrder; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -58,6 +61,16 @@ class NativeMappings { private static final Map NATIVE_LOAD_LIBRARY_OPTIONS = new HashMap(); + static final File NPCAP_DIR = Paths.get(System.getenv("SystemRoot"), "System32", "Npcap").toFile(); + + static { + if (Platform.isWindows() && System.getProperty("jna.library.path") == null) { + if (NPCAP_DIR.exists()) { + NativeLibrary.addSearchPath("wpcap", NPCAP_DIR.getAbsolutePath()); + } + } + } + static { com.sun.jna.Native.register( NativeMappings.class, NativeLibrary.getInstance(libName(Platform.isWindows()))); From cfe8482ae112af657672d8d8638ce6c7959bc391 Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Sun, 3 Dec 2023 22:21:55 -0500 Subject: [PATCH 2/3] Cleanup. Move NPCAP_DIR to static block. --- .../main/java/pcap/jdk7/internal/NativeMappings.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java index 486f8723..f2332ffd 100644 --- a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java +++ b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java @@ -61,13 +61,11 @@ class NativeMappings { private static final Map NATIVE_LOAD_LIBRARY_OPTIONS = new HashMap(); - static final File NPCAP_DIR = Paths.get(System.getenv("SystemRoot"), "System32", "Npcap").toFile(); - static { - if (Platform.isWindows() && System.getProperty("jna.library.path") == null) { - if (NPCAP_DIR.exists()) { - NativeLibrary.addSearchPath("wpcap", NPCAP_DIR.getAbsolutePath()); - } + File NPCAP_DIR = Paths.get(System.getenv("SystemRoot"), "System32", "Npcap").toFile(); + + if (Platform.isWindows() && System.getProperty("jna.library.path") == null && NPCAP_DIR.exists()) { + NativeLibrary.addSearchPath("wpcap", NPCAP_DIR.getAbsolutePath()); } } From 6068d5a26095c7279363b56acbf8aeb745580a6e Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Sun, 3 Dec 2023 22:35:19 -0500 Subject: [PATCH 3/3] Move NPCAP_DIR to if block. --- jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java index f2332ffd..543b47fe 100644 --- a/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java +++ b/jdk7/src/main/java/pcap/jdk7/internal/NativeMappings.java @@ -62,10 +62,12 @@ class NativeMappings { new HashMap(); static { - File NPCAP_DIR = Paths.get(System.getenv("SystemRoot"), "System32", "Npcap").toFile(); + if (Platform.isWindows() && System.getProperty("jna.library.path") == null) { + File NPCAP_DIR = Paths.get(System.getenv("SystemRoot"), "System32", "Npcap").toFile(); - if (Platform.isWindows() && System.getProperty("jna.library.path") == null && NPCAP_DIR.exists()) { - NativeLibrary.addSearchPath("wpcap", NPCAP_DIR.getAbsolutePath()); + if (NPCAP_DIR.exists()) { + NativeLibrary.addSearchPath("wpcap", NPCAP_DIR.getAbsolutePath()); + } } }