Skip to content

Instantly share code, notes, and snippets.

@scola
Created October 1, 2014 03:25
Show Gist options
  • Save scola/e07a9f6cc9a21e331ded to your computer and use it in GitHub Desktop.
Save scola/e07a9f6cc9a21e331ded to your computer and use it in GitHub Desktop.
set auto proxy url for IE or windows system proxy
#include "stdio.h"
#include "windows.h"
#include "wininet.h"
#pragma comment(lib, "wininet.lib")
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
LPWSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
if (szArgList == NULL)
{
MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK);
return 10;
}
if (argCount == 1) return 1;
char buff[256];
strcpy_s(buff, szCmdLine);
int buffSize = MultiByteToWideChar(CP_ACP, 0, buff, -1, NULL, 0);
LPWSTR proxy = new WCHAR[buffSize];
MultiByteToWideChar(CP_ACP, 0, buff, -1, proxy, buffSize);
INTERNET_PER_CONN_OPTION_LIST List;
INTERNET_PER_CONN_OPTION Option[2];
unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
Option[0].Value.pszValue = proxy;
Option[1].dwOption = INTERNET_PER_CONN_FLAGS;
Option[1].Value.dwValue = PROXY_TYPE_AUTO_PROXY_URL;
if (strcmp(szCmdLine,"off"))
Option[1].Value.dwValue = PROXY_TYPE_AUTO_PROXY_URL;
else{
Option[1].Value.dwValue = PROXY_TYPE_DIRECT;
Option[0].Value.pszValue = NULL;
}
DWORD ConnectedState = 0;
LPDWORD lpdwFlags = (LPDWORD)&ConnectedState;
char ConnectedName[4096] = {'0',};
LPTSTR lpszConnectionName = (LPTSTR)ConnectedName;
BOOL internetconn = InternetGetConnectedStateEx(lpdwFlags, lpszConnectionName, 4096, NULL);
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
if ((ConnectedState & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN ){
List.pszConnection = NULL;
}else{
List.pszConnection = lpszConnectionName;
}
List.dwOptionCount = 2;
List.dwOptionError = 0;
List.pOptions = Option;
if(!InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, nSize))
printf("InternetSetOption failed! (%d)\n", GetLastError());
InternetSetOption(NULL,INTERNET_OPTION_SETTINGS_CHANGED,NULL,NULL);
InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL,NULL);
//The connection settings for other instances of Internet Explorer.
return 0;
}
@scola
Copy link
Author

scola commented Oct 1, 2014

ieSetAutoProxyURL http://127.0.0.1:8086/proxy.pac //set goagent auto proxy on
ieSetAutoProxyURL http://127.0.0.1:7777/pac //set cow auto proxy on
ieSetAutoProxyURL off //direct connect

@stevezhougs
Copy link

windows 10 INTERNET_OPTION_SETTINGS_CHANGED and INTERNET_OPTION_REFRESH not work,the browser need to restart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment