参考网址:http://support.microsoft.com/kb/873199
在 Managed 程式碼中使用 autoproxy
如果要在 Managed 程式碼中實作 autoproxy,請依照下列步驟執行:
- 建立使用 autoproxy 的 Managed 的 DLL。
- 建立範例應用程式,以確認 autoproxy 實作。
建立使用 autoproxy 的 Managed 的 DLL
若要建立受管理的 DLL,使用 WinHTTP autoproxy 函式來擷取 Proxy 資訊,請依照下列步驟執行:
- 啟動 Microsoft Visual Studio.NET。
- 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。出現 [新增專案] 對話方塊。
- 按一下 [專案類型下的 [Visual C++ 專案,然後按一下 [範本] 下方的 [受管理的 C + + 類別庫。
如果您使用的 Visual Studio.NET 2003年,按一下 [範本] 下方的 [類別程式庫 (.NET)]。
- 在 [名稱] 方塊中輸入Autoproxy,],然後再按一下[確定]]。
- 在 [方案總管] 中Autoproxy.h,] 上按一下滑鼠右鍵,然後按一下 [開啟舊檔]。
- Autoproxy.h 檔案中現有的程式碼取代下列程式碼:
// AutoProxy.h
#define UNICODE
#include <windows.h>
#include <wchar.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment (lib, "winhttp.lib")
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace AutoProxy
{
public __gc class Class1
{
private:
TCHAR* szUrl;
TCHAR* szAutoProxyLocation;
public:
Class1 (String* szUrlParam)
{
TCHAR szMessage[1024];
szUrl = (TCHAR *)(void*)Marshal::StringToCoTaskMemUni(szUrlParam);
swprintf (szMessage, L"Initializing class for URL %s with autorpxy\n",
szUrl);
OutputDebugString (szMessage);
szAutoProxyLocation = NULL;
CoInitialize(NULL);
};
Class1 (String* szUrlParam, String* proxyUrl)
{
TCHAR szMessage[1024];
szUrl = (TCHAR *)(void*)Marshal::StringToCoTaskMemUni(szUrlParam);
szAutoProxyLocation = (TCHAR*)(void*)Marshal::StringToCoTaskMemUni(proxyUrl);
swprintf (szMessage, L"Initilizing class for url %s with proxy location %s\n",
szUrl, szAutoProxyLocation);
OutputDebugString (szMessage);
CoInitialize(NULL);
};
~Class1 ()
{
OutputDebugString (L"Class1 destruct\n");
CoUninitialize ();
};
String* GetProxyForUrl()//throw (TCHAR*)
{
TCHAR szError [1024];
HINTERNET hHttpSession = NULL;
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
WINHTTP_PROXY_INFO ProxyInfo;
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
TCHAR* szProxy;
ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) );
ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) );
hHttpSession = WinHttpOpen( L"WinHTTP AutoProxy Sample/1.0",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,0
);
// Exit if the WinHttpOpen function fails.
if( !hHttpSession )
{
// Clean the WINHTTP_PROXY_INFO structure.
if( ProxyInfo.lpszProxy != NULL )
GlobalFree(ProxyInfo.lpszProxy);
if( ProxyInfo.lpszProxyBypass != NULL )
GlobalFree( ProxyInfo.lpszProxyBypass );
}
// Set up the autoproxy call.
if (szAutoProxyLocation)
{
// The proxy auto-configuration URL is already known.
// Therefore, auto-detection is not required.
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
// Set the proxy auto configuration URL.
AutoProxyOptions. lpszAutoConfigUrl = szAutoProxyLocation;
}
else
{
// Use auto-detection because you do not know a PAC URL.
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
// Use both Dynamic Host Configuration Protocol (DHCP)
// and Domain Name System (DNS) based auto-detection.
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP
|WINHTTP_AUTO_DETECT_TYPE_DNS_A;
}
// If obtaining the PAC script requires NTLM/Negotiate
// authentication, automatically supply the domain credentials
// of the client.
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
// Call the WinHttpGetProxyForUrl function with our target URL.
if( WinHttpGetProxyForUrl( hHttpSession,szUrl,&AutoProxyOptions,&ProxyInfo))
{
switch (ProxyInfo.dwAccessType)
{
case WINHTTP_ACCESS_TYPE_DEFAULT_PROXY:
OutputDebugString (L"WINHTTP_ACCESS_TYPE_DEFAULT_PROXY\n");
break;
case WINHTTP_ACCESS_TYPE_NO_PROXY:
OutputDebugString (L"WINHTTP_ACCESS_TYPE_NO_PROXY\n");
break;
case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
OutputDebugString (L"WINHTTP_ACCESS_TYPE_NAMED_PROXY\n");
break;
}
if (ProxyInfo.lpszProxy)
{
szProxy = new TCHAR [lstrlen (ProxyInfo.lpszProxy)];
lstrcpy (szProxy, ProxyInfo.lpszProxy);
}
// Clean the WINHTTP_PROXY_INFO structure.
if (ProxyInfo.lpszProxy != NULL)
GlobalFree(ProxyInfo.lpszProxy);
if (ProxyInfo.lpszProxyBypass != NULL)
GlobalFree(ProxyInfo.lpszProxyBypass);
}
else
{
DWORD dwErr = GetLastError();
switch (dwErr)
{
case ERROR_WINHTTP_AUTODETECTION_FAILED:
swprintf(szError, L"ERROR_WINHTTP_AUTODETECTION_FAILED\n");
break;
case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT:
swprintf(szError,L"ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT\n");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
swprintf(szError,L"ERROR_WINHTTP_INCORRECT_HANDLE_TYPE\n");
break;
case ERROR_WINHTTP_INVALID_URL:
swprintf(szError,L"ERROR_WINHTTP_INVALID_URL\n");
break;
case ERROR_WINHTTP_LOGIN_FAILURE:
swprintf(szError,L"ERROR_WINHTTP_LOGIN_FAILURE\n");
break;
case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT:
swprintf(szError,L"ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT\n");
break;
case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
swprintf(szError,L"ERROR_WINHTTP_UNRECOGNIZED_SCHEME\n");
break;
default:
swprintf (szError, L"Error %d\n", dwErr);
}
throw (new Exception(szError));
}
// Close the WinHTTP handles.
if( hHttpSession != NULL )
WinHttpCloseHandle( hHttpSession );
// Return the proxy settings.
Marshal::FreeHGlobal(szUrl);
return new String (szProxy);
}
};
}
- 在 [方案總管] 中Autoproxy,] 上按一下滑鼠右鍵,然後按一下 [內容]。Autoproxy 屬性頁] 對話方塊隨即出現。
- 在左窗格中按一下 [連結器],請在 [組態屬性,] 下,然後按一下 [輸入]。
- 在右窗格中的 [其他相依性] 方塊中,鍵入path of winhttp library\winhttp.lib"
附註path of winhttp library是 Winhttp.lib 檔案在您的電腦上的預留位置。
- 在 [其他相依性] 方塊中,輸入msvcrt.lib。
- 在 [強制符號參考] 方塊鍵入__DllMainCRTStartup@12,],然後再按一下[確定]]。
- 在建置] 功能表上按一下 [建置方案] 建置專案]。
注意:運到__gc 編譯出錯問題,需要在Autoproxy屬性頁-->組態屬性-->c/c++-->命令列,其他選項處加 /clr:oldSyntax 命令
建立範例應用程式,以確認 autoproxy 實作
AutoProxy.Class1 myProxy = new AutoProxy.Class1("https://docss.cntouch.com", "http://ip/PAC/proxy.pac");
try
{
string Text = myProxy.GetProxyForUrl();
}catch(Exception e)
{
}
-
附註在此程式碼http://ip/PAC/proxy.pac是 autoproxy 組態檔的 URL 路徑。如果您沒有在電腦上設定的 autoproxy 可以依照下列步驟設定 autoproxy 供測試之用:
- 啟動 [記事本]。
- 將下列程式碼貼入 「 記事本 」 中:
-
- function FindProxyForURL(url , host)
- {
- return "PROXY 192.168.1.1:8080";
- }
-
- 這段程式碼中的附註,192.168.1.1是 Proxy 伺服器的 IP 位址,而8080是您可以透過它存取 Proxy 伺服器的連接埠。
- 將檔案儲存為 Proxy.pac。
- 建立您在步驟 6 c 建立 Proxy.pac 檔案的虛擬目錄。
如需有關建立虛擬目錄的詳細資訊,按一下 [下列面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項]:
附註內容資料夾路徑必須是您儲存 Proxy.pac 檔案的位置資料夾的路徑。
- 在建置] 功能表上按一下 [建置方案] 建置應用程式]。
- 在 [偵錯] 功能表上按一下 [開始] 執行應用程式]。您可以看到 Proxy 設定在輸出中。
//取得本机的proxy
public string getProxy()
{
string sSubKey = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
string autoConfigUrl = getKeyValue(sSubKey, "AutoConfigURL");//取得本机的自动组态指令码
if (autoConfigUrl == "")//为空则表示没有设定自动组态指令码
{
string sProxyEnable = getKeyValue(sSubKey, "ProxyEnable");//取得本机是否使用代理
if (sProxyEnable == "1")
{
return getKeyValue(sSubKey, "ProxyServer");//取得代理地址
}
return null;
}
else
{
AutoProxy.Class1 myProxy = new AutoProxy.Class1(http://blog.csdn.net/junjieking, autoConfigUrl);
return myProxy.GetProxyForUrl();
}
}
public string getKeyValue(string sSubKey, string sKey)//读取注册表
{
string value = "";
RegistryKey key = Registry.CurrentUser;
RegistryKey subKey = null;
try
{
subKey = key.OpenSubKey(sSubKey);
value = subKey.GetValue(sKey).ToString();
}
catch (Exception e)
{
}
finally
{
subKey.Close();
}
return value;
}
分享到:
相关推荐
破译组态王工程密码,6.52 我也刚刚下载,希望能给大家帮助
组态王曲线例子组态密码 组态王效果图设计 组态王效果图设计
组态软件宏指令入门篇
组态王SQL数据库删除十天前的数据库信息组态王密码解锁
组态王树形控组态密码
如何在没有组态软件的情况下传送 WinCC flexible 配置到面板中 (Pack&Go;)?pdf,如何在没有组态软件的情况下传送 WinCC flexible 配置到面板中 (Pack&Go;)?
对用户而言,操作简单易学且编程简单,参数输入与修改灵活,具有多次或重复仿真运行的控制能力,可以实时地显示参数变化前后系统的特性曲线,能很直观地显示控制系统的实时趋势曲线,这些很强的交互能力使其在自动控制系统...
工业组态软件,组态王v6.5完整版源码,工控人的福利
能含组态王的安装,使用环境及初级入门实例
MCGS 自动扶梯组态过程 实现上下行高低速动画展示,另外还有自己写的说明文档
快控组态软件可以应用于工业测控、电力自动化、机房监控、智能楼宇、物联网、石油石化、工业生产过程监控等领域。 您可以免费使用它,它简单易用,性能卓越;您也可以让我们帮您量身定制一款组态软件,通过增加独特...
基于组态软件进行自动门的设计,可以实现很多功能,对于毕业设计和一些电气设计非常好
它基于串行通讯,使用 RTU(远程终端单元)或 ASCII 码传输数据。Modbus 协议有两种模式:RTU模式和ASCII模式。RTU模式是 Modbus 的默认模式,将数据传输到远程终端单元;ASCII模式用于与其他设备进行通讯。 在组态...
组态王自动洗衣机仿真
工业组态软件最新版本完整源代码。 属于工业用软件,vc编写
繁易组态屏做点餐机用宏指令。在组态功能不够用时可以用宏指令,来实现功能扩展
组态用软件手册,比较详细。希望对大家有所帮助。。。。。。。。。。。
一款很有名的工业专用组态软件——组态王v6.5版的源代码,压缩包内包含经过5次开发完善后的最终版源码。可以对提高您的编程思路有一定帮助。
组态王驱动通用扫描枪(直接从串口获取扫入的条形码 PLC 三菱 欧姆龙 西门子 伺服 单片机 组态
TIA博途中自动获取硬件组态配置的具体方法