Click to See Complete Forum and Search --> : Probel in retrieving proxy information from .pac file


Zargh
July 18th, 2007, 04:55 PM
Hello, I have this piece of code:

int main()
{
char url[100] = "http://localhost/proxy.pac";
char server[100] = "google.com";


WINHTTP_AUTOPROXY_OPTIONS proxyOptions;
WINHTTP_PROXY_INFO proxyInfo;

// Zero memory
ZeroMemory(&proxyOptions, sizeof(proxyOptions));
ZeroMemory(&proxyInfo, sizeof(proxyInfo));

// Open temp internet session
HINTERNET hInternet = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

// Set proxy options
proxyOptions.lpszAutoConfigUrl = LPWSTR(url);
proxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
proxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
proxyOptions.dwReserved = NULL;
proxyOptions.lpvReserved = 0;
proxyOptions.fAutoLogonIfChallenged = TRUE;

// Get proxy info
if (!WinHttpGetProxyForUrl(hInternet, LPWSTR(server), &proxyOptions, &proxyInfo))
{
// Close temp internet session
WinHttpCloseHandle(hInternet);
// Failed
return FALSE;
}

MessageBox(0, (char*)proxyInfo.lpszProxy, 0, 0);

// Close temp internet session
WinHttpCloseHandle(hInternet);
}


But for some reason, it doesn't work. I think the problem is in the ascii to unicode conversion. Can someone help me out?

Thanks :)

ericloe
April 16th, 2009, 03:02 PM
Hi,

LPWSTR(url) and LPWSTR(server) are simply casts from ascii pointer to wide-char pointer. This means that your string is not actually being converted to wide-char only the pointer type and is not a valid string when passed into WinHttpGetProxyForUrl. Instead, define your variables as wide-char and remove the casts:

wchar url[100] = L"http://localhost/proxy.pac";
wchar server[100] = L"google.com";

Better late than never.

Eric Loewenthal

Hello, I have this piece of code:

int main()
{
char url[100] = "http://localhost/proxy.pac";
char server[100] = "google.com";


WINHTTP_AUTOPROXY_OPTIONS proxyOptions;
WINHTTP_PROXY_INFO proxyInfo;

// Zero memory
ZeroMemory(&proxyOptions, sizeof(proxyOptions));
ZeroMemory(&proxyInfo, sizeof(proxyInfo));

// Open temp internet session
HINTERNET hInternet = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

// Set proxy options
proxyOptions.lpszAutoConfigUrl = LPWSTR(url);
proxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
proxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
proxyOptions.dwReserved = NULL;
proxyOptions.lpvReserved = 0;
proxyOptions.fAutoLogonIfChallenged = TRUE;

// Get proxy info
if (!WinHttpGetProxyForUrl(hInternet, LPWSTR(server), &proxyOptions, &proxyInfo))
{
// Close temp internet session
WinHttpCloseHandle(hInternet);
// Failed
return FALSE;
}

MessageBox(0, (char*)proxyInfo.lpszProxy, 0, 0);

// Close temp internet session
WinHttpCloseHandle(hInternet);
}


But for some reason, it doesn't work. I think the problem is in the ascii to unicode conversion. Can someone help me out?

Thanks :)