For 2 days now I am trying to get a simple packet live capture to work with libpcap and npcap on Windows 11. I am using MSYS2-compiled MinGW GCC compiler. I have installed libpcap through pacman and linked it. I have installed npcap with wireless support as well as the loopback driver, LWF, LWF with wireless support, and the WFP callout driver (through NPFInstall.exe). The npcap service is running and wireshark works well on its own. My Wi-Fi network adapter does not support monitor mode, so I couldn't try that with WlanHelper.exe
The code I first tested was:
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
static void init_npcap_dll_path ()
{
BOOL(WINAPI *SetDllDirectory)(LPCTSTR);
char sysdir_name[512];
int len;
SetDllDirectory = (BOOL(WINAPI *)(LPCTSTR)) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetDllDirectoryA");
if(SetDllDirectory == NULL)
{
printf("Error in SetDllDirectory\n");
}
else
{
len = GetSystemDirectory(sysdir_name, 480);
if(!len) printf("Error in GetSystemDirectory (%d)\n", GetLastError());
strcat(sysdir_name, "\\Npcap");
if(SetDllDirectory(sysdir_name) == 0)
printf("Error in SetDllDirectory(\"System32\\Npcap\")\n");
}
}
int main (void)
{
/* No error here: */
init_npcap_dll_path();
/* Modules were loaded successfully */
HMODULE lib1 = LoadLibrary("wpcap");
HMODULE lib2 = LoadLibrary("Packet");
char errbuf[PCAP_ERRBUF_SIZE];
char *dev = "\\Device\\NPF_{B4B56B26-D9F1-49CF-831F-DD58B7DA5ACC}";
//char *dev = "b4b56b26-d9f1-49cf-831f-dd58b7da5acc"; /* Tried this as well */
pcap_t* handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if(handle == NULL)
{
fprintf(stderr, "Could not open device %s: %s\n", dev, errbuf);
return 2;
}
return 0;
}
And the pcap error was that "live packet capture is not supported on this system".
Moreover, even a simple network adapter lookup doesn't work:
char *dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
The error reads "no suitable device found"
Running the application as admin does nothing. I really don't know what else I could try to get it working, except maybe using VS and linking the .lib files from the npcap SDK implicitly...
What could I be doing wrong?
The error is use of
LoadLibrary, see LoadLibraryA functioncorrect use of
LoadLibraryis:And if you use
LoadLibrary, why not useGetProcAddress, see GetProcAddress functionThen you need define a poniter of function, like this:
Then use
GetProcAddress