Skip to content

Commit

Permalink
win32: pcapif: ensure we can run with npcap as well
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
goldsimon committed Jun 29, 2023
1 parent bfcbf80 commit 1a5dffb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contrib/ports/win32/msvc/lwIP_Test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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'">
Expand Down Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions contrib/ports/win32/pcapif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions contrib/ports/win32/pcapif_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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? */
Expand All @@ -138,4 +165,8 @@ void pcapifh_linkstate_close(struct pcapifh_linkstate* state)
LWIP_UNUSED_ARG(state);
}

void pcapifh_init_npcap(void)
{
}

#endif /* WIN32 */
2 changes: 2 additions & 0 deletions contrib/ports/win32/pcapif_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a5dffb

Please sign in to comment.