Bonjour a tous,
Voici à qui voudra bien faire une bonne action, un petit casse-tête pour enjoliver nos belles journée engrévées.
j'utilise un system.windows.forms.webbrowser que je souhaite connecter à un proxy
vous comprendrez que si je viens à vous, c'est que la fonction Proxy(ProxyAddress,ProxyPort) ne fonctionne pas sur les composants dotnet.
j'ai trouvé sur le code en C# suivant qui me parait assez accessible.
public static bool SetConnectionProxy(string proxyServer)
{
IntPtr hInternet = InternetOpen(agentName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
INTERNET_PER_CONN_OPTION[] options = new INTERNET_PER_CONN_OPTION[2];
options[0] = new INTERNET_PER_CONN_OPTION();
options[0].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_FLAGS;
options[0].value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
options[1] = new INTERNET_PER_CONN_OPTION();
options[1].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_PROXY_SERVER;
options[1].value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(options[0]) + Marshal.SizeOf(options[1]));
IntPtr current = buffer;
for (int i = 0; i < options.Length; i++)
{
Marshal.StructureToPtr(options[i], current, false);
current = (IntPtr)((int)current + Marshal.SizeOf(options[i]));
}
INTERNET_PER_CONN_OPTION_LIST optionList = new INTERNET_PER_CONN_OPTION_LIST();
optionList.pOptions = buffer;
optionList.Size = Marshal.SizeOf(optionList);
optionList.Connection = IntPtr.Zero;
optionList.OptionCount = options.Length;
optionList.OptionError = 0;
int size = Marshal.SizeOf(optionList);
IntPtr intPtrStruct = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(optionList, intPtrStruct, true);
bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intPtrStruct, size);
Marshal.FreeCoTaskMem(buffer);
Marshal.FreeCoTaskMem(intPtrStruct);
InternetCloseHandle(hInternet);
return bReturn;
}
je rencontre ici pusieurs problèmes. J'ai partiellement rtaduit avec succès le code en WD, mais certaines fonctions dotnet ont l'air de bugger (pas toutes) comme Marshal.SizeOf ou bien Marshal.StructureToPtr
je vous partage mon code au cas ou l'un d'entre vous souhaiterais me donner un coup de mains, ainsi que pour l'ensemble de la communauté.
1) déclaration globale au démarrage du projet (il fonctionne nickel)
INTERNET_PER_CONN_OPTION_LIST est une structure
Size est un entier
Connection est un IntPtr dynamique
OptionCount est un entier
OptionError est un entier
pOptions est un IntPtr dynamique
FIN
INTERNET_OPTION est une Enumération
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
INTERNET_OPTION_SETTINGS_CHANGED = 39
INTERNET_OPTION_REFRESH = 37
FIN
INTERNET_PER_CONN_OPTIONENUM est une Enumération
INTERNET_PER_CONN_FLAGS = 1
INTERNET_PER_CONN_PROXY_SERVER = 2
INTERNET_PER_CONN_PROXY_BYPASS = 3
INTERNET_PER_CONN_AUTOCONFIG_URL = 4
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5
INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL = 6
INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS = 7
INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME = 8
INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL = 9
INTERNET_PER_CONN_FLAGS_UI = 10
FIN
CONSTANTE
INTERNET_OPEN_TYPE_DIRECT = 1
INTERNET_OPEN_TYPE_PRECONFIG = 0
agentName = "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
FIN
INTERNET_OPTION_PER_CONN_FLAGS est une Enumération
PROXY_TYPE_DIRECT = 0x00000001
PROXY_TYPE_PROXY = 0x00000002
PROXY_TYPE_AUTO_PROXY_URL = 0x00000004
PROXY_TYPE_AUTO_DETECT = 0x00000008
FIN
INTERNET_PER_CONN_OPTION_OPTIONUNION est une structure
dwValue est un entier
pszValue est un IntPtr dynamique
ftValue est un System.Runtime.InteropServices.FILETIME dynamique
FIN
INTERNET_PER_CONN_OPTION est une structure
dwOption est un entier
value est un INTERNET_PER_CONN_OPTION_OPTIONUNION
FIN
SetProcessDPIAware est une Description d'API
SetProcessDPIAware..NomDLL = "user32.dll"
SetProcessDPIAware..NomFonction = "SetProcessDPIAware"
SetProcessDPIAware..TypeRetour = apiBooléen
InternetOpen est une Description d'API
InternetOpen..NomDLL = "wininet.dll"
InternetOpen..NomFonction = "InternetOpenA"
InternetOpen..Paramètre[1]..Type = apiChaîne
InternetOpen..Paramètre[2]..Type = apiEntierSystème
InternetOpen..Paramètre[3]..Type = apiChaîne
InternetOpen..Paramètre[4]..Type = apiChaîne
InternetOpen..Paramètre[5]..Type = apiEntierSystème
InternetOpen..TypeRetour = IntPtr
InternetCloseHandle est une Description d'API
InternetCloseHandle..NomDLL = "wininet.dll"
InternetCloseHandle..NomFonction = "InternetCloseHandle"
InternetOpen..Paramètre[1]..Type = apiEntierSystème
InternetCloseHandle..TypeRetour = apiBooléen
InternetSetOption est une Description d'API
InternetSetOption..NomDLL = "wininet.dll"
InternetSetOption..NomFonction = "InternetSetOption"
InternetSetOption..Paramètre[1]..Type = apiEntierSystème
InternetSetOption..Paramètre[2]..Type = apiEntierSystème
InternetSetOption..Paramètre[3]..Type = apiEntierSystème
InternetSetOption..Paramètre[4]..Type = apiEntierSystème
InternetSetOption..TypeRetour = apiBooléen
InternetQueryOptionList est une Description d'API
InternetQueryOptionList..NomDLL = "wininet.dll"
InternetQueryOptionList..NomFonction = "InternetQueryOptionList"
InternetQueryOptionList..Paramètre[1]..Type = apiEntierSystème
InternetQueryOptionList..Paramètre[2]..Type = apiEntierSystème
InternetQueryOptionList..Paramètre[3]..Type = "INTERNET_PER_CONN_OPTION_LIST"
InternetQueryOptionList..Paramètre[4]..Type = apiEntierSystème
InternetQueryOptionList..TypeRetour = apiBooléen
2) code d'initialisation de la fenetre (il fonctionne nickel aussi)
SI PAS SetConnectionProxy("127.0.0.1:"+8080) ALORS
Info("The application could not set the default connection proxy. The browser control is not using the tor service as a proxy!", "Error")
FIN
3) le code de la fonction SetConnectionProxy traduit en windev
Procedure SetConnectionProxy(proxyServer est une chaîne) :booléen
hInternet est un IntPtr dynamique = InternetOpen(agentName, INTERNET_OPEN_TYPE_DIRECT, Null, Null, 0)
options est un tableau de 2 INTERNET_PER_CONN_OPTION
options[1].dwOption = INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_FLAGS
options[1].value.dwValue = INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY
options[2].dwOption = INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_PROXY_SERVER..Valeur
options[2].value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer)
buffer est un IntPtr dynamique = Marshal.AllocCoTaskMem(Dimension(options[1]) + Dimension(options[2]))
current est un IntPtr dynamique = buffer
NBBoucles est un entier = options.Occurrence()
POUR i = 1 À NBBoucles
Marshal.StructureToPtr(options[i], current, False)
current += Dimension(options[i])
FIN
optionList est un INTERNET_PER_CONN_OPTION_LIST
optionList.pOptions = buffer
optionList.Size = Dimension(optionList)
optionList.Connection = IntPtr.Zero
optionList.OptionCount = options.Occurrence()
optionList.OptionError = 0
size est un entier = Dimension(optionList)
intPtrStruct est un IntPtr dynamique = Marshal.AllocCoTaskMem(size)
Marshal.StructureToPtr(optionList, intPtrStruct, True)
bReturn est un booléen = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intPtrStruct, size)
Marshal.FreeCoTaskMem(buffer)
Marshal.FreeCoTaskMem(intPtrStruct)
InternetCloseHandle(hInternet)
RENVOYER bReturn
Je bloque sur la boucle ligne 37. Marshal.StructureToPtr me renvoie une erreur de chargement de mscorlib.dll mais celle-ci est bien chargée et utilisée pour les autres appels sur Marshal.StringToHGlobalAnsi par exemple
POUR i = 1 À NBBoucles
Marshal.StructureToPtr(options[i], current, False)
current += Dimension(options[i])
FIN
merci par avance sur votre coup de main.
Hugues