一、相关结构体与API:
- typedef struct {
- DWORD dwAccessType;
- LPCTSTR lpszProxy;
- LPCTSTR lpszProxyBypass;
- } INTERNET_PROXY_INFO, *LPINTERNET_PROXY_INFO;
- HRESULT UrlMkSetSessionOption(
- DWORD dwOption,
- LPVOID pBuffer,
- DWORD dwBufferLength,
- DWORD dwReserved
- );
- HRESULT UrlMkSetSessionOption(
- DWORD dwOption,
- LPVOID pBuffer,
- DWORD dwBufferLength,
- DWORD dwReserved
- );
二、VC源码说明
1、新建一个对话框工程,并添加如下控件、关联变量:
2、部分源码及说明:
- void CProxyDlg::OnButtonSet()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- if(m_IP.IsEmpty() || m_Port.IsEmpty())
- {
- MessageBox("IP地址或端口不能为空!","提示",MB_ICONERROR | MB_OK);
- GetDlgItem(IDC_EDIT1)->SetFocus();
- }
- // lpszProxy格式为“192.168.1.1:8080”
- m_IP = m_IP + ":" + m_Port;
- INTERNET_PROXY_INFO proxy;
- proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
- proxy.lpszProxy = m_IP;
- proxy.lpszProxyBypass = NULL;
- HRESULT hr = ::UrlMkSetSessionOption(INTERNET_OPTION_PROXY,&proxy,sizeof(proxy),0);
- if(S_OK == hr)
- {
- SetDlgItemText(IDC_STAT,"代理设置完成!");
- return;
- }
- if(E_INVALIDARG == hr)
- {
- SetDlgItemText(IDC_STAT,"代理参数错误!");
- return;
- }
- MessageBox("设置失败!");
- }
- void CProxyDlg::OnButtonUpdate()
- {
- // TODO: Add your control notification handler code here
- m_Web.Navigate("http://www.ip138.com",NULL,NULL,NULL,NULL);
- }
3、程序运行效果:
在 Process Explorer中查看属性:
三、源码下载: