Funda i-INTERNET_OPTION_PROXY ku-WinINet nge-buffer ye-C# enosayizi
Thola i-INTERNET_OPTION_PROXY ngombuzo wosayizi ne-buffer yomdabu enosayizi ofanele, ihambisana ne-DWORD kanye nezinkambu ze-pointer ku-C#.
I-INTERNET_PROXY_INFO iqukethe uhlobo lokufinyelela lwe-DWORD kanye nama-pointer amabili womucu. Umbuzo womdabu udinga futhi isikhala se-buffer esanele sedatha yayo ye-proxy; i-Marshal.SizeOf yesakhiwo esincane esilawulwayo akusiyo i-buffer yomphumela ephelele yobude obushintshayo.
Umsizi wokuhambisana we-desktop ungasebenzisa indawo yokungena ye-ANSI futhi akope imicu ngaphambi kokukhulula leyo buffer yombuzo:
static class WinInetProxy
{
[System.Runtime.InteropServices.StructLayout(
System.Runtime.InteropServices.LayoutKind.Sequential)]
struct ProxyInfo
{
public uint AccessType;
public IntPtr Proxy;
public IntPtr Bypass;
}
[System.Runtime.InteropServices.DllImport(
"wininet.dll", EntryPoint = "InternetQueryOptionA",
SetLastError = true)]
[return: System.Runtime.InteropServices.MarshalAs(
System.Runtime.InteropServices.UnmanagedType.Bool)]
static extern bool Query(
IntPtr handle, uint option, IntPtr buffer, ref uint size);
public static (uint Type, string Proxy, string Bypass) Read()
{
uint size = 0;
Query(IntPtr.Zero, 38, IntPtr.Zero, ref size);
int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
if (error != 122 || size == 0)
throw new System.ComponentModel.Win32Exception(error);
IntPtr buffer = System.Runtime.InteropServices.Marshal
.AllocHGlobal(checked((int)size));
try
{
if (!Query(IntPtr.Zero, 38, buffer, ref size))
throw new System.ComponentModel.Win32Exception(
System.Runtime.InteropServices.Marshal.GetLastWin32Error());
var value = System.Runtime.InteropServices.Marshal
.PtrToStructure<ProxyInfo>(buffer);
return (value.AccessType,
System.Runtime.InteropServices.Marshal.PtrToStringAnsi(value.Proxy) ?? "",
System.Runtime.InteropServices.Marshal.PtrToStringAnsi(value.Bypass) ?? "");
}
finally
{
System.Runtime.InteropServices.Marshal.FreeHGlobal(buffer);
}
}
}
38 yi-INTERNET_OPTION_PROXY; 122 yi-ERROR_INSUFFICIENT_BUFFER. Ukushaya kokuqala kuthola inani lamabhayithi adingekayo, futhi okwesibili kugcwalisa leso sabelo. Uma umphumela ukhula phakathi kokushaya, kubikwa ukwehluleka kwesibili esikhundleni sokufunda i-buffer encane kakhulu.
Gcina i-AccessType njenge-uint. Kuqhubekeko lwe-64-bit ihlala iyi-DWORD ye-32-bit, kuyilapho izinkambu ze-pointer zikhula ngokuhambisana nesakhiwo senqubo. Ukuhlelwa okulandelanayo okuvamile kunikeza ukuqondanisa okufanele.
Lesi sibonelo sikhuluma nge-buffer yomphumela yokukhetha 38. Ungasebenzisi iphethini yobunikazi bayo ngobumpumputhe ku-INTERNET_OPTION_PER_CONNECTION_OPTION, okukhethwa kwayo kwemichu okubuyiswayo kudinga ukuhlanjululwa okuhlukile okubhaliwe.
Umphumela uchaza ukulungiselelwa kwe-WinINet ebangeni lesibambi esikhethiwe. Akusona isinqumo se-PAC sanoma iyiphi i-URL noma umbiko womzila wangempela weso sonke isiphequluli. Uma umucu we-proxy obuyiswayo uyi-balazwe eliqondene ne-protocol, gcina leso sakhiwo esikhundleni sokusithatha njengendawo eyodwa yokuphela. I-WinINet iyi-API ye-desktop; izinhlelo zokusebenza zensizakalo zidinga ukuhlelwa kwazo kwe-WinHTTP okusekelwayo.