Initial version

This commit is contained in:
Simon Rozman 2016-12-06 23:33:50 +01:00
parent 4ce715899a
commit 5e364e87dc
11 changed files with 590 additions and 1 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/*.sdf
/*.suo
/Debug
/ipch
/Release
/x64
/*.opensdf

Binary file not shown.

View File

@ -1,2 +1,30 @@
# WLANSetEAPUserData
Windows helper utility to set user data for a given WLAN connection
Windows helper utility to set user data for a given WLAN profile
##Usage
```
WLANSetEAPUserData <profile> <flags> <user data URI>
```
##Parameters
Parameter | Explanation
----------------|------------
`profile` | The name of the network profile (not neccessarely the same as SSID)
`flags` | Flags to pass to `WlanSetProfileEapXmlUserData()` function call (decimal number: 0=Current User, 1=All Users)
`user data URI` | User data XML URI. Can be a path to an XML file, web URL where user data XML can be loaded from, etc.
##Return codes
Value | Meaning
------|--------
0 | Success (on at least one WLAN interface)
100 | CommandLineToArgvW() failed
101 | Not enough arguments
200 | CoInitialize() failed
300 | CoCreateInstance(CLSID_DOMDocument2)
301 | IXMLDOMDocument::load() failed
302 | IXMLDOMDocument::load() reported an error in the XML document
304 | IXMLDOMDocument::get_xml() failed
400 | WlanOpenHandle() failed
401 | WlanEnumInterfaces() failed
402 | WlanSetProfileEapXmlUserData() failed on all WLAN interfaces for the given profile
403 | No ready WLAN interfaces found

26
WLANSetEAPUserData.sln Normal file
View File

@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WLANSetEAPUserData", "WLANSetEAPUserData\WLANSetEAPUserData.vcxproj", "{EC3084A5-CA18-498B-A962-5321AE37789B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|Win32.ActiveCfg = Debug|Win32
{EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|Win32.Build.0 = Debug|Win32
{EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|x64.ActiveCfg = Debug|x64
{EC3084A5-CA18-498B-A962-5321AE37789B}.Debug|x64.Build.0 = Debug|x64
{EC3084A5-CA18-498B-A962-5321AE37789B}.Release|Win32.ActiveCfg = Release|Win32
{EC3084A5-CA18-498B-A962-5321AE37789B}.Release|Win32.Build.0 = Release|Win32
{EC3084A5-CA18-498B-A962-5321AE37789B}.Release|x64.ActiveCfg = Release|x64
{EC3084A5-CA18-498B-A962-5321AE37789B}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

5
WLANSetEAPUserData/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/*.user
/Debug
/Release
/x64
/*.aps

View File

@ -0,0 +1,157 @@
/*
Copyright 2016 Simon Rozman
This file is part of WLANSetEAPUserData.
WLANSetEAPUserData is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
WLANSetEAPUserData is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WLANSetEAPUserData. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WLANSetEAPUserData.h"
#include <comip.h>
#include <comutil.h>
#include <msxml6.h>
#include <tchar.h>
#include <memory>
#ifdef _DEBUG
#pragma comment(lib, "comsuppwd.lib")
#else
#pragma comment(lib, "comsuppw.lib")
#endif
#pragma comment(lib, "msxml6.lib")
#pragma comment(lib, "Wlanapi.lib")
using namespace std;
///
/// Main program body
///
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
UNREFERENCED_PARAMETER(hInstance );
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine );
UNREFERENCED_PARAMETER(nCmdShow );
// Get command line arguments. (As Unicode, please.)
int nArgs;
unique_ptr<LPWSTR[], LocalFree_delete<LPWSTR[]> > pwcArglist(CommandLineToArgvW(GetCommandLineW(), &nArgs));
if (!pwcArglist) {
//_ftprintf(stderr, _T("CommandLineToArgvW() failed (error %u).\n"), GetLastError());
return 100;
}
if (nArgs <= 3) {
//_ftprintf(stderr, _T("Not enough arguments.\n"));
return 101;
}
// Initialize COM.
com_initializer com_init(NULL);
if (FAILED(com_init.status())) {
//_ftprintf(stderr, _T("CoInitialize() failed (error 0x%08x).\n"), com_init.status());
return 200;
}
// Load user data XML into memory.
// Use MSXML6 IXMLDOMDocument to load XML to offload charset detection.
_bstr_t user_data;
{
// Create XML document.
_com_ptr_t<_com_IIID<IXMLDOMDocument, &__uuidof(IXMLDOMDocument)> > doc;
HRESULT hr = doc.CreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_ALL);
if (FAILED(hr)) {
//_ftprintf(stderr, _T("CoCreateInstance(CLSID_DOMDocument2) failed (error 0x%08x).\n"), hr);
return 300;
}
doc->put_async(VARIANT_FALSE);
doc->put_validateOnParse(VARIANT_FALSE);
// Load XML from user data file.
VARIANT_BOOL succeeded = VARIANT_FALSE;
hr = doc->load(_variant_t(pwcArglist[3]), &succeeded);
if (FAILED(hr)) {
//_ftprintf(stderr, _T("IXMLDOMDocument::load(%ls) failed (error 0x%08x).\n"), pwcArglist[3], hr);
return 301;
} else if (!succeeded) {
//_ftprintf(stderr, _T("IXMLDOMDocument::load(%ls) failed.\n"), pwcArglist[3]);
return 302;
}
// Get document XML.
BSTR bstr;
hr = doc->get_xml(&bstr);
if (FAILED(hr)) {
//_ftprintf(stderr, _T("IXMLDOMDocument::get_xml() failed (error 0x%08x).\n"), hr);
return 304;
}
user_data.Attach(bstr);
}
// Open WLAN handle.
DWORD dwNegotiatedVersion;
unique_ptr<void, WlanCloseHandle_delete> wlan;
{
HANDLE hWlan;
DWORD dwResult = WlanOpenHandle(WLAN_API_MAKE_VERSION(2, 0), NULL, &dwNegotiatedVersion, &hWlan);
if (dwResult != ERROR_SUCCESS) {
//_ftprintf(stderr, _T("WlanOpenHandle() failed (error %u).\n"), dwResult);
return 400;
}
wlan.reset(hWlan);
}
// Get a list of WLAN interfaces.
unique_ptr<WLAN_INTERFACE_INFO_LIST, WlanFreeMemory_delete<WLAN_INTERFACE_INFO_LIST> > interfaces;
{
WLAN_INTERFACE_INFO_LIST *pInterfaceList;
DWORD dwResult = WlanEnumInterfaces(wlan.get(), NULL, &pInterfaceList);
if (dwResult != ERROR_SUCCESS) {
//_ftprintf(stderr, _T("WlanEnumInterfaces() failed (error %u).\n"), dwResult);
return 401;
}
interfaces.reset(pInterfaceList);
}
// Iterate over all WLAN interfaces.
bool success = false;
for (DWORD i = 0; i < interfaces->dwNumberOfItems; i++) {
if (interfaces->InterfaceInfo[i].isState == wlan_interface_state_not_ready) {
// This interface is not ready.
continue;
}
// Set user data.
DWORD dwResult = WlanSetProfileEapXmlUserData(
wlan.get(),
&(interfaces->InterfaceInfo[i].InterfaceGuid),
pwcArglist[1],
wcstoul(pwcArglist[2], NULL, 10),
user_data,
NULL);
if (dwResult == ERROR_SUCCESS) {
// At least one interface/profile succeeded.
success = true;
} else {
//_ftprintf(stderr, _T("WlanSetProfileEapXmlUserData() failed (error %u).\n"), dwResult);
}
}
return success ? 0 : interfaces->dwNumberOfItems ? 402 : 403;
}

View File

@ -0,0 +1,161 @@
/*
Copyright 2016 Simon Rozman
This file is part of WLANSetEAPUserData.
WLANSetEAPUserData is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
WLANSetEAPUserData is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WLANSetEAPUserData. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Windows.h>
///
/// Deleter for unique_ptr using LocalFree
///
template <class _Ty>
struct LocalFree_delete
{
///
/// Frees memory
///
/// \sa [LocalFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730.aspx)
///
inline void operator()(_Ty *_Ptr) const
{
LocalFree(_Ptr);
}
};
///
/// Deleter for unique_ptr to array of unknown size using LocalFree
///
template <class _Ty>
struct LocalFree_delete<_Ty[]>
{
///
/// Frees memory
///
inline void operator()(_Ty *_Ptr) const
{
LocalFree(_Ptr);
}
///
/// Frees memory
///
/// \sa [LocalFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730.aspx)
///
template<class _Other>
inline void operator()(_Other *) const
{
LocalFree(_Ptr);
}
};
#include <ObjBase.h>
///
/// Context scope automatic COM (un)initialization
///
class com_initializer
{
public:
///
/// Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA).
///
/// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx)
///
inline com_initializer(_In_opt_ LPVOID pvReserved)
{
m_result = CoInitialize(pvReserved);
}
///
/// Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.
///
/// \sa [CoInitializeEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279.aspx)
///
inline com_initializer(_In_opt_ LPVOID pvReserved, _In_ DWORD dwCoInit)
{
m_result = CoInitializeEx(pvReserved, dwCoInit);
}
///
/// Uninitializes COM.
///
/// \sa [CoUninitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms688715.aspx)
///
virtual ~com_initializer()
{
if (SUCCEEDED(m_result))
CoUninitialize();
}
///
/// Return result of `CoInitialize()` call.
///
/// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx)
///
inline HRESULT status() const
{
return m_result;
}
protected:
HRESULT m_result; ///< Result of CoInitialize call
};
#include <wlanapi.h>
///
/// Deleter for unique_ptr using WlanCloseHandle
///
struct WlanCloseHandle_delete
{
///
/// Closes the WLAN handle
///
/// \sa [WlanCloseHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706610.aspx)
///
inline void operator()(void *_Ptr) const
{
WlanCloseHandle(_Ptr, NULL);
}
};
///
/// Deleter for unique_ptr using WlanFreeMemory
///
template <class _Ty>
struct WlanFreeMemory_delete
{
///
/// Frees memory
///
/// \sa [WlanFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706722.aspx)
///
void operator()(_Ty *_Ptr) const
{
WlanFreeMemory(_Ptr);
}
};

Binary file not shown.

View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{EC3084A5-CA18-498B-A962-5321AE37789B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>WLANSetEAPUserData</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Full</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<WholeProgramOptimization>true</WholeProgramOptimization>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Full</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<WholeProgramOptimization>true</WholeProgramOptimization>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="WLANSetEAPUserData.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="WLANSetEAPUserData.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="WLANSetEAPUserData.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="WLANSetEAPUserData.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WLANSetEAPUserData.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="WLANSetEAPUserData.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by WLANSetEAPUserData.rc
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif