{"record":{"id":"a4d7596a917305ac","repo":"2dust/v2rayN","slug":"set-internet-proxy-failed-with-error-code-marsha","errorCode":null,"errorMessage":"Set Internet Proxy failed with error code: {Marshal.GetLastWin32Error()}","messagePattern":"Set Internet Proxy failed with error code: (.+?)","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs","lineNumber":194,"sourceCode":"        // FREE the data ASAP\n        if (list.szConnection != nint.Zero)\n        {\n            Marshal.FreeHGlobal(list.szConnection); // release mem 3\n        }\n        if (optionCount > 1)\n        {\n            Marshal.FreeHGlobal(options[1].m_Value.m_StringPtr); // release mem 1\n            if (optionCount > 2)\n            {\n                Marshal.FreeHGlobal(options[2].m_Value.m_StringPtr); // release mem 2\n            }\n        }\n        Marshal.FreeCoTaskMem(optionsPtr); // release mem 4\n        Marshal.FreeCoTaskMem(ipcoListPtr); // release mem 5\n        if (returnvalue != 0)\n        {\n            // throw the error codes, they might be helpful\n            throw new ApplicationException($\"Set Internet Proxy failed with error code: {Marshal.GetLastWin32Error()}\");\n        }\n\n        return true;\n    }\n\n    /// <summary>\n    /// Retrieve list of connections including LAN and WAN to support PPPoE connection\n    /// </summary>\n    /// <returns>A list of RAS connection names. May be empty list if no dial up connection.</returns>\n    /// <exception cref=\"ApplicationException\">Error message with win32 error code</exception>\n    private static IEnumerable<string> EnumerateRasEntries()\n    {\n        var entries = 0;\n        // attempt to query with 1 entry buffer\n        var rasEntryNames = new RASENTRYNAME[1];\n        var bufferSize = Marshal.SizeOf(typeof(RASENTRYNAME));\n        rasEntryNames[0].dwSize = Marshal.SizeOf(typeof(RASENTRYNAME));\n","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs#L176-L212","documentation":"Thrown after the WinInet InternetSetOption/InternetSetPerConnOptions call sequence returns a non-zero (failure) result. The message appends Marshal.GetLastWin32Error() so the Win32 error code is visible. It signals Windows declined to apply the system proxy configuration.","triggerScenarios":"The proxy-setting routine builds an INTERNET_PER_CONN_OPTION_LIST, calls the WinInet set-option API, and returnvalue != 0 (the API reported failure). GetLastWin32Error yields the specific code (e.g. network unavailable, access denied).","commonSituations":"Running without administrator privileges on some Windows configurations; active/networked dial-up or VPN interfering with per-connection options; another process holds the WinInet settings lock; corrupt WinInet registry entries; calling on a non-interactive/session-0 context.","solutions":["Run v2rayN as administrator when setting the system proxy.","Decode GetLastWin32Error (e.g. 'net helpmsg <code>') to identify the specific Win32 failure.","Close browsers/other proxy-managing apps that may lock WinInet settings and retry.","Ensure a network connection is active (WinInet refuses some options when no connection is up).","If running as a service/session 0, set the proxy from an interactive user session instead."],"exampleFix":"// before\nthrow new ApplicationException($\"Set Internet Proxy failed with error code: {Marshal.GetLastWin32Error()}\");\n\n// after - include the symbolic message for faster diagnosis\nvar code = Marshal.GetLastWin32Error();\nthrow new ApplicationException($\"Set Internet Proxy failed (Win32 {code}): {new System.ComponentModel.Win32Exception(code).Message}\");","handlingStrategy":"try-catch","validationCode":"// Best-effort pre-check: require admin and an active connection before setting proxy\nif (!OperatingSystem.IsWindows()) throw new PlatformNotSupportedException();\n// caller should ensure elevated session and an active network connection","typeGuard":"static bool CanSetWindowsProxy() => OperatingSystem.IsWindows();","tryCatchPattern":"try\n{\n    ProxySettingWindows.SetProxy(/* ... */);\n}\ncatch (ApplicationException ex) when (ex.Message.Contains(\"Set Internet Proxy failed\"))\n{\n    var msg = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message;\n    _log?.Error($\"WinInet set proxy failed: {msg}\");\n}","preventionTips":["Run as administrator when setting the system proxy.","Decode GetLastWin32Error with 'net helpmsg <code>' or Win32Exception for the cause.","Close other apps that lock WinInet settings before applying.","Ensure an active network connection exists."],"tags":["windows","wininet","system-proxy","interop","pinvoke"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}