Mylon
Mylon

Reputation: 135

Routine for proxy change does not work on delphi 10.2 Tokyo

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;

Upvotes: 1

Views: 205

Answers (2)

Mylon
Mylon

Reputation: 135

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;

Upvotes: 1

LU RD
LU RD

Reputation: 34899

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)));

Upvotes: 2

Related Questions