在 C# 中连接到 Wi-Fi - EAP TTLS 用户数据和 WlanSetProfileEapXmlUserData

Connecting to Wi-Fi in C# - EAP TTLS user data & WlanSetProfileEapXmlUserData

我正在尝试连接到 C# 中的 EAP-TTLS Wi-Fi 配置文件。

我有以下本机方法:

[DllImport("Wlanapi.dll", SetLastError = true)]
public static extern uint WlanSetProfileEapXmlUserData(
    IntPtr hClientHandle,
    ref Guid pInterfaceGuid,
    string strProfileName,
    uint dwFlags,
    string strEapXmlUserData,
    IntPtr pReserved);

我这样称呼它:

var result = NativeMethods.WlanSetProfileEapXmlUserData(clientHandle, ref interfaceGuid, "MyWifiProfileName", WLAN_SET_EAPHOST_DATA_ALL_USERS, profileData, IntPtr.Zero);

个人资料如下:

<?xml version="1.0" encoding="UTF-8" ?>
<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>21</eapCommon:Type>
<eapCommon:AuthorId>311</eapCommon:AuthorId>
</EapMethod>
<Credentials xmlns="http://www.microsoft.com/provisioning/EapHostUserCredentials">
<EapTtls xmlns="http://www.microsoft.com/provisioning/EapTtlsUserPropertiesV1">
<Username>%%%USERNAME%%%</Username>
<Password>%%%PASSWORD%%%</Password>
</EapTtls>
</Credentials>
</EapHostUserCredentials>

但我似乎永远无法得到成功的回应。 'result' 始终为 1168 - ERROR_NOT_FOUND - 未找到元素。 LastWin32Error 始终为 1008 - ERROR_NO_TOKEN。

即使我将配置文件名称更改为不存在的配置文件,或者将 XML 更改为明显无效的内容,我仍然会收到相同的退出代码和最后一个错误。

我做错了什么?

收到来自 MSDN 论坛的答复 (link)

解决方法是本机方法的不同声明:

    [DllImport("wlanapi.dll")]
    public static extern int WlanSetProfileEapXmlUserData(
        [In] IntPtr clientHandle,                                   // The client's session handle, obtained by a previous call to the WlanOpenHandle function.
        [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, // The GUID of the interface.
        [In, MarshalAs(UnmanagedType.LPWStr)] string profileName,   // The name of the profile associated with the EAP user data. Profile names are case-sensitive. This string must be NULL-terminated.
        [In] SetEapUserDataMode dwFlags,                            // A set of flags that modify the behavior of the function.
        [In, MarshalAs(UnmanagedType.LPWStr)] string userDataXML,   // A pointer to XML data used to set the user credentials, The XML data must be based on the EAPHost User Credentials schema. To view sample user credential XML data, see EAPHost User Properties: http://msdn.microsoft.com/en-us/library/windows/desktop/bb204765(v=vs.85).aspx
        IntPtr reservedPtr
    );