win32: pcapif: ensure we can run with npcap as well

By default, npcap keeps its DLLs not in system32 but in system32/npcap.
To load DLLs from there, mark them as "delay load DLLs" and adjust the
DLL search path before using/loading them.

Signed-off-by: Simon Goldschmidt <goldsimon@gmx.de>
This commit is contained in:
Simon Goldschmidt 2023-06-29 22:11:55 +02:00
parent bfcbf80221
commit 1a5dffb931
4 changed files with 38 additions and 0 deletions

View File

@ -85,6 +85,7 @@
<TargetMachine>MachineX86</TargetMachine>
<GenerateMapFile>true</GenerateMapFile>
<MapFileName>$(TargetDir)$(TargetName).map</MapFileName>
<DelayLoadDLLs>Packet.dll;wpcap.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@ -125,6 +126,7 @@
<TargetMachine>MachineX86</TargetMachine>
<GenerateMapFile>false</GenerateMapFile>
<MapFileName>$(TargetDir)$(TargetName).map</MapFileName>
<DelayLoadDLLs>Packet.dll;wpcap.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemGroup>

View File

@ -1057,6 +1057,9 @@ pcapif_init(struct netif *netif)
int local_index;
SYS_ARCH_DECL_PROTECT(lev);
pcapifh_init_npcap();
SYS_ARCH_PROTECT(lev);
local_index = ethernetif_index++;
SYS_ARCH_UNPROTECT(lev);

View File

@ -114,6 +114,33 @@ pcapifh_free_readonly_mem(void *data)
}
}
/**
* Npcap keeps its DLLs in a different directory for compatiblity with winpcap.
* Make sure they get found by adding that directory to the DLL search path.
*/
void pcapifh_init_npcap(void)
{
char npcap_dir[512];
unsigned int len;
static char npcap_initialized = 0;
if (!npcap_initialized)
{
npcap_initialized = 1;
len = GetSystemDirectory(npcap_dir, 480);
if (!len) {
lwip_win32_platform_diag("Error in GetSystemDirectory: %x", GetLastError());
return;
}
strcat_s(npcap_dir, 512, "\\Npcap");
if (SetDllDirectory(npcap_dir) == 0) {
lwip_win32_platform_diag("Error in SetDllDirectory: %x", GetLastError());
return;
}
}
}
#else /* WIN32 */
/* @todo: add linux/unix implementation? */
@ -138,4 +165,8 @@ void pcapifh_linkstate_close(struct pcapifh_linkstate* state)
LWIP_UNUSED_ARG(state);
}
void pcapifh_init_npcap(void)
{
}
#endif /* WIN32 */

View File

@ -22,6 +22,8 @@ void pcapifh_linkstate_close(struct pcapifh_linkstate* state);
void *pcapifh_alloc_readonly_copy(void *data, size_t len);
void pcapifh_free_readonly_mem(void *data);
void pcapifh_init_npcap(void);
#ifdef __cplusplus
}
#endif