
// call with the handle you're using ... it will set up that handle
// with the IE proxy settings, if found

CURLcode curl_use_ie_proxy(CURL *curlHandle) {
	CURLcode curlError;
	unsigned long infoBufferLen;
	void *infoBuffer;
#define proxyInfo ((INTERNET_PROXY_INFO *)infoBuffer)
#define charBuffer ((char *)infoBuffer)
	
	infoBuffer = malloc(infoBufferLen = 2048);	
	if(!infoBuffer) 
		return CURLE_OUT_OF_MEMORY;
	
	if(!InternetQueryOption(NULL, INTERNET_OPTION_PROXY, infoBuffer, &infoBufferLen))
		return CURLE_FUNCTION_NOT_FOUND;

	// if a proxy is set
	if(proxyInfo->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
		// push it into the curl handle
		curlError = curl_easy_setopt(curlHandle, CURLOPT_PROXY, (void *)proxyInfo->lpszProxy);
		if(curlError != CURLE_OK) return curlError;

		infoBufferLen = 1024; // give ourselves some leeway for the password
		// try and get a proxy username - ignore a blank result
		if(InternetQueryOption(NULL, INTERNET_OPTION_PROXY_USERNAME, infoBuffer, &infoBufferLen)) {
	
			if(charBuffer[0] != '\0') {
				// try and put the password on the end of it
				strcat(charBuffer, ":");
				infoBufferLen = 2000 - strlen(charBuffer);
				if(InternetQueryOption(NULL, INTERNET_OPTION_PROXY_PASSWORD, (void *)((char *)infoBuffer + strlen(charBuffer)), &infoBufferLen)) {
					// we have it, give it to curl
					curl_easy_setopt(curlHandle, CURLOPT_PROXYUSERPWD, charBuffer);
				}
			}
		}
	}

	free(infoBuffer);
	return CURLE_OK;
}

