请注意下面的第二个示例,该示例使用 C++。
请注意,第三个示例是在 C# 中。
好吧,经过一番挖掘之后,似乎问题可能出在某种权限问题上,因为在调试二进制文件并跳过了一些随机检查之后 - 它显示了我为我的帐户设置的单个配置文件。是的,有人GetCurrentPackageFamilyName
肯定它正在查询某种权限。
没有背景信息不是我的错 - 唯一的公共文档在这里。我只是想ping接口。
C 变体很复杂,因为我必须调用RoCreateInstance
thenInspectable_QueryInterface
并定义我自己的 IID,因为缺少原始 IID 。WindowsCreateStringReference
我还需要使用自定义 COM 处理程序创建自己的字符串。标题中包含的名称长度为数千个字符,这无济于事。
基本上,我尝试从VPNProfile
我查询和填充的界面和 XML 中添加配置文件——这些尝试都不起作用。最近我能够更新来自对象的现有配置文件。
但是,我似乎也无法连接到一个并断开连接。
我可以对所述界面做的唯一有用的事情是UpdateProfileFromXmlAsync
使应用程序崩溃:
#define COBJMACROS
#include <roapi.h>
#include <windows.networking.vpn.h>
#include <stdbool.h>
#include <stdio.h>
//Had to manually define my IIDs
//because the definitions are missing from the library
const IID IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent = {0x193696CD,0xA5C4,0x4ABE,0x85,0x2B,0x78,0x5B,0xE4,0xCB,0x3E,0x34};
const IID IID___FIAsyncOperationCompletedHandler_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile = {0xdac6dd72,0xa5d1,0x56d4,0xaf,0xc4,0x98,0x9f,0x84,0xdc,0xb2,0xb3};
const IID IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile = {0x7875B751,0xB0D7,0x43DB,0x8A,0x93,0xD3,0xFE,0x24,0x79,0xE5,0x6A};
const IID IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnNativeProfile = {0xA4AEE29E,0x6417,0x4333,0x98,0x42,0xF0,0xA6,0x6D,0xB6,0x98,0x02};
const IID IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnChannel = {0x4AC78D07,0xD1A8,0x4303,0xA0,0x91,0xC8,0xD2,0xE0,0x91,0x5B,0xC3};
const IID IID___FIAsyncOperationCompletedHandler_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus = {0x31229f8c,0x709d,0x5017,0x86,0x29,0x57,0xef,0x12,0x89,0xe6,0x16};
const WCHAR pVPNXmlCode[] =
L"<SyncML xmlns=\"SYNCML:SYNCML1.2\" xmlns:A=\"syncml:metinf\">"
L" <SyncBody>"
L" <Atomic>"
L" <CmdID>10000</CmdID>"
L""
L" <!-- Configure VPN Server Name or Address (PhoneNumber=) [Comma Separated]-->"
L" <Add>"
L" <CmdID>10001</CmdID>"
L" <Item>"
L" <Target>"
L" <LocURI>./Vendor/MSFT/VPNv2/VPN_Demo/ProfileXML</LocURI>"
L" </Target>"
L" <Data><VPNProfile>"
L" <ProfileName>VPN_Demo</ProfileName>"
L" <NativeProfile>"
L" <Servers>VPNServer.contoso.com</Servers>"
L" <NativeProtocolType>Automatic</NativeProtocolType>"
L" <Authentication>"
L" <UserMethod>Eap</UserMethod>"
L" <Eap>"
L" <Configuration>"
L"<EapHostConfig xmlns=\"http://www.microsoft.com/provisioning/EapHostConfig\"> <EapMethod> <Type xmlns=\"http://www.microsoft.com/provisioning/EapCommon\">25</Type> <VendorId xmlns=\"http://www.microsoft.com/provisioning/EapCommon\">0</VendorId> <VendorType xmlns=\"http://www.microsoft.com/provisioning/EapCommon\">0</VendorType> <AuthorId xmlns=\"http://www.microsoft.com/provisioning/EapCommon\">0</AuthorId> </EapMethod> <Config xmlns=\"http://www.microsoft.com/provisioning/EapHostConfig\"> <Eap xmlns=\"http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1\"> <Type>25</Type> <EapType xmlns=\"http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV1\"> <ServerValidation> <DisableUserPromptForServerValidation>false</DisableUserPromptForServerValidation> <ServerNames></ServerNames> </ServerValidation> <FastReconnect>true</FastReconnect> <InnerEapOptional>false</InnerEapOptional> <Eap xmlns=\"http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1\"> <Type>13</Type> <EapType xmlns=\"http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV1\"> <CredentialsSource> <CertificateStore> <SimpleCertSelection>false</SimpleCertSelection> </CertificateStore> </CredentialsSource> <ServerValidation> <DisableUserPromptForServerValidation>false</DisableUserPromptForServerValidation> <ServerNames></ServerNames> </ServerValidation> <DifferentUsername>false</DifferentUsername> <PerformServerValidation xmlns=\"http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV2\">false</PerformServerValidation> <AcceptServerName xmlns=\"http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV2\">false</AcceptServerName> <TLSExtensions xmlns=\"http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV2\"> <FilteringInfo xmlns=\"http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV3\"> <EKUMapping> <EKUMap> <EKUName>Unknown Key Usage</EKUName> <EKUOID>1.3.6.1.4.1.311.87</EKUOID> </EKUMap> </EKUMapping> <ClientAuthEKUList Enabled=\"true\"> <EKUMapInList> <EKUName>Unknown Key Usage</EKUName> </EKUMapInList> </ClientAuthEKUList> </FilteringInfo> </TLSExtensions> </EapType> </Eap> <EnableQuarantineChecks>false</EnableQuarantineChecks> <RequireCryptoBinding>false</RequireCryptoBinding> <PeapExtensions> <PerformServerValidation xmlns=\"http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV2\">false</PerformServerValidation> <AcceptServerName xmlns=\"http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV2\">false</AcceptServerName> </PeapExtensions> </EapType> </Eap> </Config> </EapHostConfig>"
L" </Configuration>"
L" </Eap>"
L" </Authentication>"
L" <RoutingPolicyType>SplitTunnel</RoutingPolicyType>"
L" </NativeProfile>"
L" <DomainNameInformationList>"
L" <DomainName>.contoso.com</DomainName>"
L" <DNSServers>10.5.5.5</DNSServers>"
L" </DomainNameInformationList>"
L" <TrafficFilter> "
L" <App>%ProgramFiles%Internet Exploreriexplore.exe</App> "
L" </TrafficFilter> "
L" <TrafficFilter> "
L" <App>Microsoft.MicrosoftEdge_8wekyb3d8bbwe</App> "
L" </TrafficFilter>"
L" <Route>"
L" <Address>10.0.0.0</Address>"
L" <PrefixSize>8</PrefixSize>"
L" </Route>"
L" <Route>"
L" <Address>25.0.0.0</Address>"
L" <PrefixSize>8</PrefixSize>"
L" </Route>"
L" <RememberCredentials>true</RememberCredentials>"
L" </VPNProfile></Data>"
L" </Item>"
L" </Add>"
L""
L" </Atomic>"
L" <Final/>"
L" </SyncBody>"
L"</SyncML>"
;
//Completion Handlers
HRESULT QueryInterface(This,riid,ppvObject)void * This;REFIID riid;void **ppvObject; {if(!ppvObject) return E_POINTER;
return !memcmp(riid, &IID_IUnknown, sizeof *riid) ||
!memcmp(riid, &IID___FIAsyncOperationCompletedHandler_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus, sizeof *riid) ||
!memcmp(riid, &IID___FIAsyncOperationCompletedHandler_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile, sizeof *riid) ? *ppvObject=This, S_OK
: E_NOINTERFACE;}
HANDLE hReady;
(AddRef)(in) void*in; {;}
(Release)(in) void*in; {;}
__x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile *pProfile;
//Error Handler
HRESULT (InvokeError)(in,asyncInfo,status) void*in;
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus *asyncInfo;AsyncStatus status;
{
enum __x_ABI_CWindows_CNetworking_CVpn_CVpnManagementErrorStatus disconnectstatus;
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_GetResults(asyncInfo,&disconnectstatus);
printf("SET0:%d\n%x\n",disconnectstatus, GetLastError()),SetEvent(hReady);
return S_OK;}
//Profile Sets Handler
HRESULT (InvokeCollect)(in,asyncInfo,status) void*in;
__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile *asyncInfo;AsyncStatus status;
{__FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile *pResults;
unsigned int size;
printf("SET1:%d\n",status),
printf("%x\n",__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_GetResults(asyncInfo, &pResults)),
__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_Release(asyncInfo),
__FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_get_Size(pResults,&size),
printf("Amount of profiles collected %d\n",size),
__FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_Release(pResults),
SetEvent(hReady);return S_OK;}
__FIAsyncOperationCompletedHandler_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile ProfilesRetrieverHandler =
{(__FIAsyncOperationCompletedHandler_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfileVtbl[1])
{{QueryInterface,AddRef,Release,InvokeError}}};
(main)()
//Long complex,boring initialization
{__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent *pVpnMan; HSTRING_HEADER header;
ULONG idCount; IID*pIdds;
hReady = CreateEvent(0,true,false,0); __x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile *pProfile;
__x_ABI_CWindows_CNetworking_CVpn_CIVpnNativeProfile *pNativeProfile;
ULONG strSize; WCHAR *pRawString;__x_ABI_CWindows_CNetworking_CVpn_CIVpnChannel *pChannel;
enum __x_ABI_CWindows_CNetworking_CVpn_CVpnManagementErrorStatus disconnectstatus;
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus *pVpnManErr;
IInspectable *pVpnObjInspect; HSTRING string; RoInitialize(RO_INIT_MULTITHREADED),
//Query for IVpnManagementAgent
WindowsCreateStringReference(RuntimeClass_Windows_Networking_Vpn_VpnManagementAgent,
sizeof RuntimeClass_Windows_Networking_Vpn_VpnManagementAgent/sizeof(WCHAR)-1,
&header, &string), RoActivateInstance(string,&pVpnObjInspect), WindowsDeleteString(string), IInspectable_QueryInterface(pVpnObjInspect,
&IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent,&pVpnMan),IInspectable_Release(pVpnObjInspect),
//Query for IVpnProfile
WindowsCreateStringReference(RuntimeClass_Windows_Networking_Vpn_VpnNativeProfile,
sizeof RuntimeClass_Windows_Networking_Vpn_VpnNativeProfile/sizeof(WCHAR)-1,
&header, &string), RoActivateInstance(string,&pVpnObjInspect),WindowsDeleteString(string),IInspectable_GetIids(pVpnObjInspect,&idCount,&pIdds),
IInspectable_QueryInterface(pVpnObjInspect, &IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile,&pProfile),IInspectable_Release(pVpnObjInspect),
//Set Profile name to currently active VPN Profile in my system
WindowsCreateStringReference(pVPNXmlCode, sizeof pVPNXmlCode/sizeof(WCHAR)-1,&header, &string),WindowsCreateStringReference(L"VPN Connection",
sizeof L"VPN Connection"/sizeof(WCHAR)-1,&header, &string),__x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile_put_ProfileName(pProfile,string),
WindowsDeleteString(string),
//Query to IVpnNativeProfile and the back to IVpnProfile - because I can
printf("%x\n",__x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile_QueryInterface(pProfile,&IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnNativeProfile,&pNativeProfile)),
__x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile_Release(pProfile),
printf("%x\n",__x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile_QueryInterface(pNativeProfile,&IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnProfile,&pProfile)),
__x_ABI_CWindows_CNetworking_CVpn_CIVpnNativeProfile_Release(pNativeProfile),
//The interesting part
//Try to add profile from XML
WindowsCreateStringReference(pVPNXmlCode,sizeof pVPNXmlCode/
sizeof(WCHAR)-1,&header, &string),__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent_AddProfileFromXmlAsync(pVpnMan,string,&pVpnManErr),
WindowsDeleteString(string),__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_put_Completed(pVpnManErr,&ProfilesRetrieverHandler),
WaitForSingleObject(hReady, INFINITE), ResetEvent(hReady),
//Try to add profie from previously queried profile
__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent_AddProfileFromObjectAsync(pVpnMan,pProfile,&pVpnManErr),
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_put_Completed(pVpnManErr,&ProfilesRetrieverHandler),
WaitForSingleObject(hReady, INFINITE), ResetEvent(hReady),
//See how many profiles we have
ProfilesRetrieverHandler.lpVtbl->Invoke = InvokeCollect;
__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile *pProfiles;
__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent_GetProfilesAsync(pVpnMan,&pProfiles),
__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_put_Completed(pProfiles,&ProfilesRetrieverHandler),
__FIAsyncOperation_1___FIVectorView_1_Windows__CNetworking__CVpn__CIVpnProfile_Release(pProfiles),
WaitForSingleObject(hReady, INFINITE),ResetEvent(hReady);
//Try connectiong to our previously named profile
ProfilesRetrieverHandler.lpVtbl->Invoke = InvokeError,
__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent_ConnectProfileAsync(pVpnMan,pProfile,&pVpnManErr),
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_put_Completed(pVpnManErr,&ProfilesRetrieverHandler),
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_Release(pVpnManErr),
WaitForSingleObject(hReady, INFINITE),ResetEvent(hReady);
//For fun crash the application
ProfilesRetrieverHandler.lpVtbl->Invoke = InvokeError,
WindowsCreateStringReference(pVPNXmlCode,sizeof pVPNXmlCode/
sizeof(WCHAR)-1,&header, &string),__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent_UpdateProfileFromXmlAsync(pVpnMan,string,&pVpnManErr),
WindowsDeleteString(string),
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_put_Completed(pVpnManErr,&ProfilesRetrieverHandler),
__FIAsyncOperation_1_Windows__CNetworking__CVpn__CVpnManagementErrorStatus_Release(pVpnManErr),
WaitForSingleObject(hReady, INFINITE),ResetEvent(hReady);
printf("never reached\n");
}
这是我编译上述内容的方式:
cl stackoverflow.c /link RuntimeObject.lib Ole32.lib OleAut32.lib
这是我的 C++ 变体,适用于无法查看上述代码的任何人:
#include <stdio.h>
#include <..\cppwinrt\winrt\windows.networking.vpn.h>
using namespace winrt::Windows::Networking::Vpn;
const wchar_t pVPNXmlCode[] =
L"<SyncML xmlns=\"SYNCML:SYNCML1.2\" xmlns:A=\"syncml:metinf\">"
L" <SyncBody>"
L" <Atomic>"
L" <CmdID>10000</CmdID>"
L""
L" <!-- Configure VPN Server Name or Address (PhoneNumber=) [Comma Separated]-->"
L" <Add>"
L" <CmdID>10001</CmdID>"
L" <Item>"
L" <Target>"
L" <LocURI>./Vendor/MSFT/VPNv2/VPN_Demo/ProfileXML</LocURI>"
L" </Target>"
L" <Data><VPNProfile>"
L" <ProfileName>VPN_Demo</ProfileName>"
L" <NativeProfile>"
L" <Servers>VPNServer.contoso.com</Servers>"
L" <NativeProtocolType>Automatic</NativeProtocolType>"
L" <Authentication>"
L" <UserMethod>Eap</UserMethod>"
L" </Authentication>"
L" <RoutingPolicyType>SplitTunnel</RoutingPolicyType>"
L" </NativeProfile>"
L" <DomainNameInformationList>"
L" <DomainName>.contoso.com</DomainName>"
L" <DNSServers>10.5.5.5</DNSServers>"
L" </DomainNameInformationList>"
L" <TrafficFilter> "
L" <App>%ProgramFiles%Internet Exploreriexplore.exe</App> "
L" </TrafficFilter> "
L" <TrafficFilter> "
L" <App>Microsoft.MicrosoftEdge_8wekyb3d8bbwe</App> "
L" </TrafficFilter>"
L" <Route>"
L" <Address>10.0.0.0</Address>"
L" <PrefixSize>8</PrefixSize>"
L" </Route>"
L" <Route>"
L" <Address>25.0.0.0</Address>"
L" <PrefixSize>8</PrefixSize>"
L" </Route>"
L" <RememberCredentials>true</RememberCredentials>"
L" </VPNProfile></Data>"
L" </Item>"
L" </Add>"
L""
L" </Atomic>"
L" <Final/>"
L" </SyncBody>"
L"</SyncML>"
;
int wmain()
{
winrt::init_apartment();
VpnNativeProfile profile;
profile.AlwaysOn(false);
profile.EapConfiguration(L"<EapHostConfig></EapHostConfig>");
profile.NativeProtocolType(VpnNativeProtocolType::L2tp);
profile.ProfileName(L"VPN Connection");
profile.RememberCredentials(true);
profile.RequireVpnClientAppUI(false);
profile.RoutingPolicyType(VpnRoutingPolicyType::ForceAllTrafficOverVpn);
profile.Servers().Append(L"vlus-af1.pointtoserver.com");
profile.RequireVpnClientAppUI(true);
profile.UserAuthenticationMethod(VpnAuthenticationMethod::Certificate);
profile.TunnelAuthenticationMethod(VpnAuthenticationMethod::Certificate);
VpnManagementAgent agent;
wprintf(L"%d\n", agent.AddProfileFromXmlAsync(pVPNXmlCode).get());
wprintf(L"%d\n", agent.AddProfileFromObjectAsync(profile).get());
auto profiles = agent.GetProfilesAsync().get();
wprintf(L"Found %d profiles\n", profiles.Size());
for (auto vp : profiles)
{
wprintf(L"%d\n",agent.DisconnectProfileAsync(vp).get());
wprintf(L"Found profile %s\n", vp.ProfileName().c_str());
}
wprintf(L"%d\n", agent.ConnectProfileAsync(profile).get());
wprintf(L"%d\n", agent.UpdateProfileFromXmlAsync(pVPNXmlCode).get());
wprintf(L"never reached\n");
}
这是我编译上述内容的方式:
cl so.cpp /std:c++latest /link RuntimeObject.lib Ole32.lib OleAut32.lib
这是一个 C# 变体:
using System;
using Windows;
using Windows.Networking.Vpn;
using System.Threading.Tasks;
using Windows.Networking;
using System.Threading;
public class entry
{public static async Task Main()
{VpnManagementAgent mgr = new VpnManagementAgent();
mgr.GetProfilesAsync().Completed = (asyncInfo, asyncStatus) => {Console.WriteLine($"{asyncInfo.GetResults().Count}\n");};
Thread.Sleep(-1);}}
我正在编译这个:
csc same.cs /r:"C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd" /r:System.Runtime.dll /r:"C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd" /langversion:preview
我正在使用 Visual Studio 2019 构建工具,并且正在运行 Windows Server 2019。
我正在寻找 MSFT 的答案。如何从所述界面利用任何 VPN 功能。
这是我的 C 程序的输出:
0
0
SET0:1
0
SET0:1
0
SET1:1
0
Amount of profiles collected 0
SET0:1
0
1 的意思VpnManagementErrorStatus_Other
。这是 C++ 程序的输出:
1
1
Found 0 profiles
1
这是 C# 变体输出:
0