internal static IntPtr Dial(string entryName, string user, string pwd, Action <uint, RasConnectionState, uint, uint> action) { var radDialParams = new RASDIALPARAMS { size = Marshal.SizeOf <RASDIALPARAMS>(), domain = "", userName = user, password = pwd, entryName = entryName, }; var rasDialExtensions = new RASDIALEXTENSIONS { size = Marshal.SizeOf <RASDIALEXTENSIONS>(), devSpecificInfo = new RASDEVSPECIFICINFO { size = Marshal.SizeOf <RASDEVSPECIFICINFO>(), } }; var callback = new VpnNative.RasDialFunc2((a, b, c, messageId, connectionState, error, extendedError) => { action(messageId, connectionState, error, extendedError); return(true); }); PinCallback(callback); Checked("RasDial", VpnNative.RasDial(ref rasDialExtensions, null, ref radDialParams, 2, callback, out var handle)); return(handle); }
private static void PinCallback(VpnNative.RasDialFunc2 callback) { // we pin callbacks so they are not garbage collected _pinnedCallbacks.Add(callback); // remove old to avoid memory leaks if (_pinnedCallbacks.Count > 10) { _pinnedCallbacks.RemoveAt(0); } }