INTERNET_OPTION_PROXY a leughadh ann an WinINet le bufar C# le meud
Faigh INTERNET_OPTION_PROXY le ceist meud agus bufar tùsanach air a mheudachadh gu ceart, a' freagair do raointean DWORD agus puingear C#.
Tha seòrsa ruigsinne DWORD agus dà phuingear sreang ann an INTERNET_PROXY_INFO. Feumaidh an ceist thùsanach cuideachd àite bufair gu leòr airson a dàta progsaidh; chan eil Marshal.SizeOf de structair bheag air a riaghladh na bhuFar toraidh iomlan de dh'fhaid caochlaideach.
Faodaidh neach-cuideachaidh co-fhreagarrachd deasg an t-àite inntrigidh ANSI a chleachdadh agus na sreangan a chopaigeadh mus deach am bufar ceiste sin a leigeil saor:
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);
}
}
}
Is e 38 INTERNET_OPTION_PROXY; is e 122 ERROR_INSUFFICIENT_BUFFER. Gheibh a' chiad ghairm an àireamh byte a tha a dhìth, agus lìonas an dàrna tè an sònrachadh sin. Ma dh'fhàsas an toradh eadar gairmean, thèid an dàrna fàilligeadh innse an àite bufar ro-bheag a leughadh.
Cùm AccessType mar uint. Ann am pròiseas 64-bit bidh e fhathast na DWORD 32-bit, agus leudaichidh raointean a' phuingear le ailtireachd a' phròiseis. Bheir an suidheachadh sreathach àbhaisteach an co-thaobhadh iomchaidh.
Tha an eisimpleir seo mu bhuFar toraidh roghainn 38. Na cuir am pàtran seilbhe aige gu dall air INTERNET_OPTION_PER_CONNECTION_OPTION, aig a bheil na roghainnean sreang a thèid a thilleadh a' feumachdainn an glanadh fa leth clàraichte.
Tha an toradh a' toirt cunntas air rèiteachadh WinINet aig an raon làimhseachaidh a chaidh a thaghadh. Chan e co-dhùnadh PAC a th' ann airson URL neo-riaghailteach no aithisg air slighe èifeachdach gach brabhsair. Nuair a tha an t-sreang progsaidh a thèid a thilleadh na mhapa a tha sònraichte don phròtacal, cùm a' structar sin an àite a bhith ga làimhseachadh mar aon cheann-uidhe. Is e WinINet an API deasg; feumaidh tagraidhean seirbheis an rèiteachadh WinHTTP le taic aca fhèin.