Skip to content

Instantly share code, notes, and snippets.

@ChronoMonochrome
Last active August 10, 2023 18:58
Show Gist options
  • Save ChronoMonochrome/fd7398d98c4f0da686fc22d229a04042 to your computer and use it in GitHub Desktop.
Save ChronoMonochrome/fd7398d98c4f0da686fc22d229a04042 to your computer and use it in GitHub Desktop.
Wifi tethering on Windows
#include <Windows.h>
#include <wlanapi.h>
#include <iostream>
#pragma comment(lib, "wlanapi.lib")
typedef DWORD(WINAPI* P_WlanOpenHandle)(DWORD, PVOID, PDWORD, PHANDLE);
typedef DWORD(WINAPI* P_WlanCloseHandle)(HANDLE, PVOID);
typedef DWORD(WINAPI* P_WlanHostedNetworkStartUsing)(HANDLE, PWLAN_HOSTED_NETWORK_REASON, PVOID);
typedef DWORD(WINAPI* P_WlanHostedNetworkStopUsing)(HANDLE, PWLAN_HOSTED_NETWORK_REASON, PVOID);
int main(int argc, char* argv[]) {
DWORD res = 0;
HANDLE wlanHandle = NULL;
HMODULE wlanDll = LoadLibrary("wlanapi.dll");
if (wlanDll == NULL) {
std::cout << "error loading wlanapi.dll" << std::endl;
return 1;
}
P_WlanOpenHandle pWlanOpenHandle = (P_WlanOpenHandle)GetProcAddress(wlanDll, "WlanOpenHandle");
P_WlanCloseHandle pWlanCloseHandle = (P_WlanCloseHandle)GetProcAddress(wlanDll, "WlanCloseHandle");
P_WlanHostedNetworkStartUsing pWlanHostedNetworkStartUsing = (P_WlanHostedNetworkStartUsing)GetProcAddress(wlanDll, "WlanHostedNetworkStartUsing");
P_WlanHostedNetworkStopUsing pWlanHostedNetworkStopUsing = (P_WlanHostedNetworkStopUsing)GetProcAddress(wlanDll, "WlanHostedNetworkStopUsing");
if (pWlanOpenHandle == NULL)
std::cout << "WlanOpenHandle symbol missing" << std::endl;
if (pWlanCloseHandle == NULL)
std::cout << "WlanCloseHandle symbol missing" << std::endl;
if (pWlanHostedNetworkStartUsing == NULL)
std::cout << "WlanHostedNetworkStartUsing symbol missing" << std::endl;
if (WlanHostedNetworkStopUsing == NULL)
std::cout << "WlanHostedNetworkStopUsing symbol missing" << std::endl;
if (pWlanOpenHandle == NULL || pWlanCloseHandle == NULL || pWlanHostedNetworkStartUsing == NULL || pWlanHostedNetworkStopUsing == NULL) {
FreeLibrary(wlanDll);
return 1;
}
DWORD version;
if (res = pWlanOpenHandle(WLAN_API_VERSION, NULL, &version, &wlanHandle) != ERROR_SUCCESS) {
std::cout << "error opening WLAN handle, error code " << res << std::endl;
FreeLibrary(wlanDll);
return 1;
}
WLAN_HOSTED_NETWORK_REASON reason;
if (res = pWlanHostedNetworkStartUsing(wlanHandle, &reason, NULL) != ERROR_SUCCESS) {
std::cout << "error starting hostednetwork, error code " << res << std::endl;
goto cleanup;
}
std::cout << "hostednetwork started successfully" << std::endl;
while (true) {
Sleep(5000);
}
if (res = pWlanHostedNetworkStopUsing(wlanHandle, &reason, NULL) != ERROR_SUCCESS) {
std::cout << "error stopping hostednetwork, error code " << res << std::endl;
goto cleanup;
}
std::cout << "hostednetwork stopped successfully" << std::endl;
cleanup:
pWlanCloseHandle(wlanHandle, NULL);
FreeLibrary(wlanDll);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment