Windows: lazy load wlanapi.dll

Appearently wlanapi.dll (which we use to borrow the wlan password
from the computer running Imager) is not always present on the
server editions of Windows, so only load it if it exists.

Ref #584
This commit is contained in:
Floris Bos 2023-05-21 17:22:17 +02:00
parent a7acd80908
commit bb8b097c7d
3 changed files with 45 additions and 4 deletions

View file

@ -8,8 +8,8 @@ OPTION (ENABLE_TELEMETRY "Enable sending telemetry" ON)
project(rpi-imager LANGUAGES CXX C) project(rpi-imager LANGUAGES CXX C)
set(IMAGER_VERSION_MAJOR 1) set(IMAGER_VERSION_MAJOR 1)
set(IMAGER_VERSION_MINOR 7) set(IMAGER_VERSION_MINOR 7)
set(IMAGER_VERSION_STR "${IMAGER_VERSION_MAJOR}.${IMAGER_VERSION_MINOR}.4.1") set(IMAGER_VERSION_STR "${IMAGER_VERSION_MAJOR}.${IMAGER_VERSION_MINOR}.5")
set(IMAGER_VERSION_CSV "${IMAGER_VERSION_MAJOR},${IMAGER_VERSION_MINOR},4,1") set(IMAGER_VERSION_CSV "${IMAGER_VERSION_MAJOR},${IMAGER_VERSION_MINOR},5,0")
add_definitions(-DIMAGER_VERSION_STR="${IMAGER_VERSION_STR}") add_definitions(-DIMAGER_VERSION_STR="${IMAGER_VERSION_STR}")
add_definitions(-DIMAGER_VERSION_CSV=${IMAGER_VERSION_CSV}) add_definitions(-DIMAGER_VERSION_CSV=${IMAGER_VERSION_CSV})
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
@ -48,8 +48,15 @@ elseif (UNIX)
elseif (WIN32) elseif (WIN32)
set(DEPENDENCIES acceleratedcryptographichash.cpp dependencies/mountutils/src/windows/functions.cpp dependencies/drivelist/src/windows/list.cpp set(DEPENDENCIES acceleratedcryptographichash.cpp dependencies/mountutils/src/windows/functions.cpp dependencies/drivelist/src/windows/list.cpp
windows/winfile.cpp windows/winfile.h windows/winwlancredentials.h windows/winwlancredentials.cpp windows/winfile.cpp windows/winfile.h windows/winwlancredentials.h windows/winwlancredentials.cpp
windows/rpi-imager.rc) windows/rpi-imager.rc wlanapi_delayed.lib)
set(EXTRALIBS setupapi wlanapi) set(EXTRALIBS setupapi ${CMAKE_CURRENT_BINARY_DIR}/wlanapi_delayed.lib)
add_custom_command(
OUTPUT wlanapi_delayed.lib
COMMAND ${CMAKE_DLLTOOL} --input-def "${CMAKE_CURRENT_SOURCE_DIR}/windows/wlanapi.def"
--output-delaylib "wlanapi_delayed.lib" --dllname "wlanapi.dll"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/windows/wlanapi.def
VERBATIM
)
endif() endif()
include_directories(BEFORE .) include_directories(BEFORE .)

View file

@ -8,12 +8,29 @@
#include <winioctl.h> #include <winioctl.h>
#include <wlanapi.h> #include <wlanapi.h>
#include <Windot11.h> #include <Windot11.h>
#include <delayimp.h>
#include <QDebug> #include <QDebug>
#include <QRegularExpression> #include <QRegularExpression>
#ifndef WLAN_PROFILE_GET_PLAINTEXT_KEY #ifndef WLAN_PROFILE_GET_PLAINTEXT_KEY
#define WLAN_PROFILE_GET_PLAINTEXT_KEY 4 #define WLAN_PROFILE_GET_PLAINTEXT_KEY 4
#endif #endif
static HINSTANCE hWlanApi = NULL;
/* Called by dlltool generated delaylib code that is used for lazy loading
wlanapi.dll */
FARPROC WINAPI dllDelayNotifyHook(unsigned dliNotify, PDelayLoadInfo)
{
if (dliNotify == dliNotePreLoadLibrary)
{
return (FARPROC) hWlanApi;
}
return NULL;
}
PfnDliHook __pfnDliNotifyHook2 = dllDelayNotifyHook;
inline QString unescapeXml(QString str) inline QString unescapeXml(QString str)
{ {
static const char *table[] = { static const char *table[] = {
@ -35,6 +52,14 @@ inline QString unescapeXml(QString str)
WinWlanCredentials::WinWlanCredentials() WinWlanCredentials::WinWlanCredentials()
{ {
if (!hWlanApi)
hWlanApi = LoadLibraryExA("wlanapi.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!hWlanApi)
{
qDebug() << "wlanapi.dll not available";
return;
}
/* Get both SSID and PSK in one go, and store it in variables */ /* Get both SSID and PSK in one go, and store it in variables */
HANDLE h; HANDLE h;
DWORD supportedVersion = 0; DWORD supportedVersion = 0;

9
src/windows/wlanapi.def Normal file
View file

@ -0,0 +1,9 @@
LIBRARY wlanapi
EXPORTS
WlanCloseHandle@8
WlanEnumInterfaces@12
WlanFreeMemory@4
WlanGetProfile@28
WlanGetProfileList@16
WlanOpenHandle@16
WlanQueryInterface@28