VB.Net - wlanapi.dll WlanSetProfileEapXmlUserData

869 views Asked by At

We are trying to create an application for our BYOD users for them to connect effortlessly to our 802.1x network.

I've found the following code on the web :

Imports System.Runtime.InteropServices


Module Module1

    Private _stringToHex As Object

    Private Property Ret As UInteger

    Private Property StringToHex(profileName As String) As Object
        Get
            Return _stringToHex
        End Get
        Set(value As Object)
            _stringToHex = value
        End Set
    End Property

    Private Property handle As IntPtr

    Private Property Guid As Guid

    <DllImport("Wlanapi", EntryPoint:="WlanSetProfileEapXmlUserData")> _
    Public Function WlanSetProfileEapXmlUserData(<[In]()> ByVal hClientHandle As IntPtr, _
                                                        <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal interfaceGuid As Guid, _
                                                        <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal strProfileName As String, _
                                                        <[In]()> ByVal flags As ULong, _
                                                        <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal profileXml As String, _
                                                        <[In]()> ByVal pReserved As IntPtr) As UInteger
    End Function


    Sub Main()


        Dim profileName As String = "ISL"

        Dim AuthXml As String = String.Format("<?xml version=""1.0"" ?>" & _
                                                    "<EapHostUserCredentials xmlns=""http://www.microsoft.com/provisioning/EapHostUserCredentials"" xmlns:eapCommon=""http://www.microsoft.com/provisioning/EapCommon"" xmlns:baseEap=""http://www.microsoft.com/provisioning/BaseEapMethodUserCredentials"">" & _
                                                        "<EapMethod>" & _
                                                            "<eapCommon:Type>25</eapCommon:Type>" & _
                                                            "<eapCommon:AuthorId>0</eapCommon:AuthorId>" & _
                                                        "</EapMethod>" & _
                                                        "<Credentials xmlns:eapUser=""http://www.microsoft.com/provisioning/EapUserPropertiesV1"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:baseEap=""http://www.microsoft.com/provisioning/BaseEapUserPropertiesV1"" xmlns:MsPeap=""http://www.microsoft.com/provisioning/MsPeapUserPropertiesV1"" xmlns:MsChapV2=""http://www.microsoft.com/provisioning/MsChapV2UserPropertiesV1"">" & _
                                                            "<baseEap:Eap>" & _
                                                                "<baseEap:Type>25</baseEap:Type>" & _
                                                                "<MsPeap:EapType>" & _
                                                                    "<MsPeap:RoutingIdentity>username</MsPeap:RoutingIdentity>" & _
                                                                    "<baseEap:Eap>" & _
                                                                        "<baseEap:Type>26</baseEap:Type>" & _
                                                                            "<MsChapV2:EapType>" & _
                                                                                "<MsChapV2:Username>USERNAME</MsChapV2:Username>" & _
                                                                                "<MsChapV2:Password>PASSWORD</MsChapV2:Password>" & _
                                                                                "<MsChapV2:LogonDomain>DOMAIN</MsChapV2:LogonDomain>" & _
                                                                            "</MsChapV2:EapType>" & _
                                                                        "</baseEap:Eap>" & _
                                                                "</MsPeap:EapType>" & _
                                                            "</baseEap:Eap>" & _
                                                        "</Credentials>" & _
                                                    "</EapHostUserCredentials>", profileName, StringToHex(profileName))

        Ret = WlanSetProfileEapXmlUserData(handle, Guid, profileName, Convert.ToUInt64(&H1), AuthXml, IntPtr.Zero)
    End Sub




End Module

This gives me the following error : "Additional information: A call to PInvoke function 'test_wlan_conf!test_wlan_conf.Module1::WlanSetProfileEapXmlUserData' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

With my almost non-existant .net skills, I admit it would be really difficult for me to solve. Any help would be deeply appreciated !

1

There are 1 answers

1
blabla296 On

Solved it myself using an AutoIT UDF from their forums. The UDF calls the wlanapi.dll and allows to use its functions to configure WLAN access, profiles, etc.

Steps for Windows 7 are :

  1. create an exe from the AutoIT UDF, modify to be able to pass CLI args (username/password)
  2. import previously created profile using netsh
  3. add the peap creds using the previously created exe
  4. Done

Steps for Windows 8 are :

  1. create an exe from the AutoIT UDF, modify to be able to pass CLI args (username/password)
  2. import previously created profile using netsh
  3. add the peap creds using the previously created exe
  4. use netsh to connect to the SSID (W8 doesn't auto-connect...)
  5. Done

Hope this helps