10

I have the following WinRT code which works fine to discover active VPN Profiles:

VpnManagementAgent vpn;
auto profiles = vpn.GetProfilesAsync().get();
wprintf(L"Found %d profiles\n", profiles.Size());
for (auto vp : profiles)
{
    wprintf(L"Found profile %s\n", vp.ProfileName().c_str());
}

I would like to check the ConnectionStatus but this is not available on the IVpnProfile interface. I have tried adding the following to no avail as ConnectionStatus is available in the concrete VpnPlugInProfile and VpnNativeProfile classes:

VpnPlugInProfile pp = vp.as<VpnPlugInProfile>();
if (pp != nullptr)
    wprintf(L"ConnectionStatus = %d\n", pp.ConnectionStatus());

This just give an Invalid Handle error.

Is there a way of getting ConnectionStatus from IVpnProfile?

AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
Paul Dolphin
  • 758
  • 2
  • 8
  • 18
  • That doesn't sound right. The [as](https://learn.microsoft.com/en-us/uwp/cpp-ref-for-winrt/com-ptr#comptras-function) member function template either returns a valid interface pointer, or throws. What is the exact error you get, and where from? – IInspectable Oct 17 '18 at 12:43
  • Sorry, yes the Invalid Handle error happens in a surrounding catch block so as is throwing – Paul Dolphin Oct 18 '18 at 13:14
  • It's actually quite easy to get the connection status from a `IVpnProfile` - simply query from `Windows.Networking.Vpn.VpnNativeProfile` a new profile, assign to it your newly acquired profile name and then query for VpnNativeProfile2 which has a `get_ConnectionStatus`. I have C code which do it and I tested it if someone is interested (reason I'm posting this as comment and not answer). The more complex task I am unable to perform is acquire the servers to which a connection is currently connected to. – AnArrayOfFunctions Jul 17 '19 at 19:01
  • That is considering you haven't created a `rasphone.pbk` file inside your `%userprofile%\AppData\Local\Packages\%fullpackagename%\LocalState`. Because if you have so - `IVpnProfile` **can** be queried for `IVpnNativeProfile`. The program is designed clever and in case of missing `rasphone.pbk` inside your app LocalState it'll only fill the profile name and forbid further queries to nativeprofile. God bless Microsoft. However I've found this with some heavy reverse - engineering - it would be helpful if instead there was a good documentation as well as good code. – AnArrayOfFunctions Jul 18 '19 at 14:55

1 Answers1

0

Hint: the invalid handle error is a bug in Windows; it's been fixed and the fix was serviced down-level (possibly to RS5 and 19H1)

PESMITH_MSFT
  • 350
  • 1
  • 9