1

I am using using the Raises routine to configure use proxy within the system. It works perfectly in delphi in version 7. In Delphi 10.2 (Tokyo), even compiling without errors, when calling the routine informs that the proxy is not responding (being that the proxy is ok and worked in delphi 7 call).

Would anyone have any idea what might be going on?

function ApplyProxy(proxy: string):Boolean;
var
  MyInternetProxyInfo: PInternetProxyInfo;
begin
 try
  Result:=False;
  proxy:=Trim(proxy);
  MyInternetProxyInfo:=New(PInternetProxyInfo);
  try
    if proxy = EmptyStr then
      MyInternetProxyInfo^.dwAccessType := INTERNET_OPEN_TYPE_DIRECT else
    begin
      MyInternetProxyInfo^.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
      MyInternetProxyInfo^.lpszProxy := PAnsiChar(Trim(proxy));
      MyInternetProxyInfo^.lpszProxyBypass := PAnsiChar('<local>');
    end;
    Result:=InternetSetOption(nil, INTERNET_OPTION_PROXY, MyInternetProxyInfo, 
      SizeOf(MyInternetProxyInfo^));
  finally
    Dispose(MyInternetProxyInfo);
  end;
 except
  Result:=False;
 end;
end;
LU RD
  • 34,438
  • 5
  • 88
  • 296
Mylon
  • 115
  • 1
  • 2
  • 8
  • In Delphi 10.2 Tokyo strings are unicode, and the compiler will hint that `PAnsiChar(Trim(proxy));` is *W1044 Suspicious typecast of string to PAnsiChar* and this will not work when executed. Convert the string to an ansistring first. – LU RD Jul 13 '18 at 20:42
  • I am extraordinarily grateful. Thank you very much. I left it that way and it worked, I needed it a lot:       MyInternetProxyInfo ^ .lpszProxy: = PAnsiChar (AnsiString (Trim (proxy))); – Mylon Jul 13 '18 at 20:49
  • 2
    Why are you using the ANSI version of the API and not the UNICODE version? `InternetSetOption()` should be mapping to `InternetSetOptionW()` and not to `InternetSetOptionA()` under D2009+. And you should be using `INTERNET_OPTION_PER_CONNECTION_OPTION` to set the proxy instead of using `INTERNET_OPTION_PROXY`, as the MSDN documentation says. – Remy Lebeau Jul 14 '18 at 03:15

2 Answers2

2

In Delphi 10.2 Tokyo strings are unicode, and the compiler will warn that

PAnsiChar(Trim(proxy)); 

is

W1044 Suspicious typecast of string to PAnsiChar.

and this will not work when executed. Convert the string to an AnsiString first.

For example:

MyInternetProxyInfo^.lpszProxy := PAnsiChar(AnsiString(Trim(proxy)));
LU RD
  • 34,438
  • 5
  • 88
  • 296
1

As suggested by LU-RD, I changed the routine and started running on tokio 10.2 update 3.

function ApplyProxy(proxy: string):Boolean;
var
  MyInternetProxyInfo: PInternetProxyInfo;
begin
 try
  Result:=False;
  proxy:=Trim(proxy);
  MyInternetProxyInfo:=New(PInternetProxyInfo);
  try
    if proxy = EmptyStr then
      MyInternetProxyInfo^.dwAccessType := INTERNET_OPEN_TYPE_DIRECT else
    begin
      MyInternetProxyInfo^.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
      MyInternetProxyInfo^.lpszProxy := PAnsiChar(AnsiString(Trim(proxy)));
      MyInternetProxyInfo^.lpszProxyBypass := PAnsiChar('<local>');
    end;
    Result:=InternetSetOption(nil, INTERNET_OPTION_PROXY, MyInternetProxyInfo, SizeOf(MyInternetProxyInfo^));
  finally
    Dispose(MyInternetProxyInfo);
  end;
 except
  Result:=False;
 end;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
Mylon
  • 115
  • 1
  • 2
  • 8
  • Note that this will fail if the proxy address contains a Unicode character (won't find the IP address from the proxy address). Would be better to use xxxW() API calls instead of xxxA() which would not need the type conversions and also not fail on Unicode characters in the address. – Brian Jul 17 '18 at 20:45