Skip to content

Commit

Permalink
Made NPFInstall.exe's dependency on wlanapi.dll dynamically linked, s…
Browse files Browse the repository at this point in the history
…o NPFInstall.exe will not fail to launch on some systems (like Win2008) that has no wlanapi.dll.
  • Loading branch information
hsluoyz committed Jun 8, 2016
1 parent 78f35e9 commit 3bdf0e1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packetWin7/NPFInstall/NPFInstall/DriverStoreClear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ BOOLEAN ClearDriverStore()

wchar_t renameCmd[MAX_PATH];
// "pnputil.exe -d oem1.inf"
for (int i = 0; i < nInfFileNameList.size(); i++)
for (size_t i = 0; i < nInfFileNameList.size(); i++)
{
swprintf_s(renameCmd, MAX_PATH, L"pnputil.exe -d %s", nInfFileNameList[i].c_str());
executeCommand(renameCmd);
Expand Down
4 changes: 2 additions & 2 deletions packetWin7/NPFInstall/NPFInstall/LoopbackInstall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ BOOL SaveDevIDToFile(int iDevID)
}

FILE *fp;
if ((fp = fopen(strLoopbackIDFilePath, "w")) == NULL)
if (fopen_s(&fp, strLoopbackIDFilePath, "w") != 0)
{
return FALSE;
}
Expand All @@ -1382,7 +1382,7 @@ int LoadDevIDFromFile()

FILE *fp;
int iDevID;
if ((fp = fopen(strLoopbackIDFilePath, "r")) == NULL)
if (fopen_s(&fp, strLoopbackIDFilePath, "r") != 0)
{
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions packetWin7/NPFInstall/NPFInstall/LoopbackRename2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ wstring getNpcapLoopbackAdapterName()
return L"";
}

for (int i = 0; i < g_InterfaceNameList2.size(); i ++)
for (size_t i = 0; i < g_InterfaceNameList2.size(); i ++)
{
int found = 0;
for (int j = 0; j < g_InterfaceNameList1.size(); j ++)
for (size_t j = 0; j < g_InterfaceNameList1.size(); j ++)
{
if (g_InterfaceNameList2[i].compare(g_InterfaceNameList1[j]) == 0)
{
Expand Down
16 changes: 8 additions & 8 deletions packetWin7/NPFInstall/NPFInstall/NPFInstall.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX86</TargetMachine>
Expand Down Expand Up @@ -217,7 +217,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX64</TargetMachine>
Expand All @@ -240,7 +240,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
Expand Down Expand Up @@ -268,7 +268,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
Expand Down Expand Up @@ -296,7 +296,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX86</TargetMachine>
Expand Down Expand Up @@ -325,7 +325,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX64</TargetMachine>
Expand All @@ -348,7 +348,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
Expand Down Expand Up @@ -376,7 +376,7 @@
<PreprocessorDefinitions>_UNICODE;UNICODE;_WIN7;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
<Link>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;wlanapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>setupapi.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
Expand Down
85 changes: 81 additions & 4 deletions packetWin7/NPFInstall/NPFInstall/WlanRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,77 @@
#include "WlanRecord.h"
#include "RegUtil.h"


HINSTANCE hinstLib = NULL;
typedef DWORD
(WINAPI *MY_WLANOPENHANDLE)(
_In_ DWORD dwClientVersion,
_Reserved_ PVOID pReserved,
_Out_ PDWORD pdwNegotiatedVersion,
_Out_ PHANDLE phClientHandle
);

typedef DWORD
(WINAPI *MY_WLANCLOSEHANDLE)(
_In_ HANDLE hClientHandle,
_Reserved_ PVOID pReserved
);

typedef DWORD
(WINAPI *MY_WLANENUMINTERFACES)(
_In_ HANDLE hClientHandle,
_Reserved_ PVOID pReserved,
_Outptr_ PWLAN_INTERFACE_INFO_LIST *ppInterfaceList
);

typedef VOID
(WINAPI *MY_WLANFREEMEMORY)(
_In_ PVOID pMemory
);

MY_WLANOPENHANDLE My_WlanOpenHandle = NULL;
MY_WLANCLOSEHANDLE My_WlanCloseHandle = NULL;
MY_WLANENUMINTERFACES My_WlanEnumInterfaces = NULL;
MY_WLANFREEMEMORY My_WlanFreeMemory = NULL;

BOOL initWlanFunctions()
{
BOOL bRet;

// Get a handle to the packet DLL module.
hinstLib = LoadLibrary(TEXT("wlanapi.dll"));

// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
My_WlanOpenHandle = (MY_WLANOPENHANDLE)GetProcAddress(hinstLib, "WlanOpenHandle");
My_WlanCloseHandle = (MY_WLANCLOSEHANDLE)GetProcAddress(hinstLib, "WlanCloseHandle");
My_WlanEnumInterfaces = (MY_WLANENUMINTERFACES)GetProcAddress(hinstLib, "WlanEnumInterfaces");
My_WlanFreeMemory = (MY_WLANFREEMEMORY)GetProcAddress(hinstLib, "WlanFreeMemory");
// If the function address is valid, call the function.

if (My_WlanOpenHandle != NULL &&
My_WlanCloseHandle != NULL &&
My_WlanEnumInterfaces != NULL &&
My_WlanFreeMemory != NULL)
{
bRet = TRUE;
}
else
{
bRet = FALSE;
}


}
else
{
bRet = FALSE;
}

return bRet;
}

tstring printArray(vector<tstring> nstr)
{
tstring strResult;
Expand All @@ -36,7 +107,7 @@ UINT EnumInterface(HANDLE hClient, WLAN_INTERFACE_INFO sInfo[64])
__try
{
// enumerate wireless interfaces
if ((dwError = WlanEnumInterfaces(
if ((dwError = My_WlanEnumInterfaces(
hClient,
NULL, // reserved
&pIntfList
Expand All @@ -58,7 +129,7 @@ UINT EnumInterface(HANDLE hClient, WLAN_INTERFACE_INFO sInfo[64])
// clean up
if (pIntfList != NULL)
{
WlanFreeMemory(pIntfList);
My_WlanFreeMemory(pIntfList);
}
}
return 0;
Expand All @@ -79,7 +150,7 @@ PHANDLE phClient
*phClient = NULL;

// open a handle to the service
if ((dwError = WlanOpenHandle(
if ((dwError = My_WlanOpenHandle(
WLAN_API_VERSION,
NULL, // reserved
&dwServiceVersion,
Expand All @@ -106,7 +177,7 @@ PHANDLE phClient
if (hClient != NULL)
{
// clean up
WlanCloseHandle(
My_WlanCloseHandle(
hClient,
NULL // reserved
);
Expand All @@ -123,6 +194,12 @@ vector<tstring> getWlanAdapterGuids()
RPC_TSTR strGuid = NULL;
vector<tstring> nstrWlanAdapterGuids;

if (!initWlanFunctions())
{
_tprintf(_T("getWlanAdapterGuids::initWlanFunctions error.\n"));
return nstrWlanAdapterGuids;
}

if (OpenHandleAndCheckVersion(&hClient) != ERROR_SUCCESS)
{
_tprintf(_T("getWlanAdapterGuids::OpenHandleAndCheckVersion error.\n"));
Expand Down

0 comments on commit 3bdf0e1

Please sign in to comment.