Thouldre wrote:
So maybe there is a way to make an exception for OpenVPN but force everything else to use "private"?
You could avoid specific connections by connection name:
Code: Select all
for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(3) { ; NLM_ENUM_NETWORK_ALL
if (INetwork.GetName() == "<name of specific connection name>") { ; use MsgBox % INetwork.GetName() inside a loop like this one if you want to find out your available choices
INetwork.SetCategory(0) ; NLM_NETWORK_CATEGORY_PUBLIC
; or comment the above line to leave the setting as it is, instead of forcing the Public category to be set, and just
continue
} else {
INetwork.SetCategory(1) ; NLM_NETWORK_CATEGORY_PRIVATE
}
}
For
connected connections only, you can try this if you want to filter by adapter name:
Code: Select all
VarSetCapacity(adapterGuid, 16), VarSetCapacity(adapterGuidStr, 140), adapters := GetAdaptersAddresses()
for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1) { ; NLM_ENUM_NETWORK_CONNECTED
for k, v in INetwork.GetNetworkConnections() {
try if ((INetworkConnection := ComObjQuery(k, "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"))) {
if (DllCall(NumGet(NumGet(INetworkConnection+0)+12*A_PtrSize), "Ptr", INetworkConnection, "Ptr", &adapterGuid) == 0) { ; ::GetAdapterId
if (DllCall("ole32\StringFromGUID2", "Ptr", &adapterGuid, "WStr", adapterGuidStr, "Int", 68)) {
if (adapters[adapterGuidStr].Description == "<name of network adapter>") { ; also look at FriendlyName instead of Description
INetwork.SetCategory(0) ; NLM_NETWORK_CATEGORY_PUBLIC
noop := "or comment the above line to leave the setting as it is, instead of forcing the Public category to be set" ; for lack of a better option in AutoHotkey...
} else {
INetwork.SetCategory(1) ; NLM_NETWORK_CATEGORY_PRIVATE
}
}
}
ObjRelease(INetworkConnection)
}
}
}
; just me: https://autohotkey.com/boards/viewtopic.php?t=18768
GetAdaptersAddresses()
{
; initial call to GetAdaptersAddresses to get the size needed
If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", 0, "UIntP", Size) = 111) ; ERROR_BUFFER_OVERFLOW
If !(VarSetCapacity(Buf, Size, 0))
Return "Memory allocation failed for IP_ADAPTER_ADDRESSES struct"
; second call to GetAdapters Addresses to get the actual data we want
If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", &Buf, "UIntP", Size) != 0) ; NO_ERROR
Return "Call to GetAdaptersAddresses failed with error: " . A_LastError
Addr := &Buf
Adapters := {}
While (Addr) {
AdapterName := StrGet(NumGet(Addr + 8, A_PtrSize, "Uptr"), "CP0")
Description := StrGet(NumGet(Addr + 8, A_PtrSize * 7, "UPtr"), "UTF-16")
FriendlyName := StrGet(NumGet(Addr + 8, A_PtrSize * 8, "UPtr"), "UTF-16")
Adapters[AdapterName] := {Description: Description, FriendlyName: FriendlyName}
Addr := NumGet(Addr + 8, "UPtr") ; *Next
}
Return Adapters
}
(Also, if anybody knows the magic incantation to retrieve a GUID through INetworkConnection's IDispatch interface, compared to me going for the more simple option of calling GetAdapterId directly from the vtable, please let me know)