Checking internet connection in delphi - delphi

How, i've made a program that uploads files on ftp server, the thing is that whenever i don't have access to the internet, it appears me an error, which stops running the program and says that i am not connected to the internet.
How do i make the program appear that message in a showmessage box, so that it doesn't stop running the program?
For example:
If internetconnection then
begin
end else showmessage ('You are not connected to the internet')

Please try code from this link
Ping
Also you can try to use free Internet Component Suite components that allows to implement test connection to your ftp server.
EDIT:
Since it was found that the author uses IdFTP (Indy) component for upload files on ftp server and my first answer was not good i will take courage to write as i think correct code for checking connection to the ftp server:
with IdFTP1 do begin
Host := ..;
Port := ..;
Username := ..;
Password := ..;
if Connected then Disconnect;
try
Connect;
ShowMessage('FTP IS Connected')
except
ShowMessage('FTP IS NOT Connected');
end;
end;

Related

Delphi RAD Studio 10.2, unable to browse windows system folder using FileOpenDialog

I need to be able to enumerate the folders present under C:\Windows\system32\dns on a Windows server 2016 instance running Windows DNS server.
Having tried FindFirst()/FindNext() and getting no results, I built a quick VCL Forms App to understand what was happening. I have a TButton and a TEdit, and the button's OnClick is below:
procedure TForm1.Button1Click(Sender: TObject);
begin
FDir := 'C:\Windows\System32\';
with TFileOpenDialog.Create(nil) do
try
Title := 'Select Directory';
Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem];
OkButtonLabel := 'Select';
DefaultFolder := FDir;
FileName := FDir;
if Execute then
Edit1.Text := Filename;
finally
Free;
end;
end;
When I run this - either as Administrator, or normally, on the server - and try to browse to the folder C:\Windows\system32\dns\ in the FileOpenDialog, I get an error:
Windows can't find 'C:\Windows\system32\dns'. Check the spelling and try again.
However, I know the folder exists, and I can browse it using Windows Explorer on the server, so there must be an issue with the Delphi code, or the permissions the App is running under.
Please, can anyone suggest what I need to do to fix this?
Thanks to #SertacAkyuz for reminding me about file system redirection - trying to access %Windir%\system32 from a 32bit program will be redirected to %Windir%\SysWow64 which doesn't contain the dns folder.
You can use the virtual alias %Windir%\Sysnative to gain access to the actual system32 folder from a 32bit application, and that works for the above case. so browsing to %Windir%\sysnative\dns allows me to enumerate the folders correctly.

Change file permission in FTP using TIdFTP Delphi

How do I change the permissions of a file using TidFTP in Delphi?
with IdFTP1 do
begin
try
Connect;
ChangeDir(FTPDiretorio);
Put(FArquivo, NomeOnline);
chmod(' ');//640 HOW??????????????
Disconnect;
finally
FreeAndNil(IdFTP1);
end;
end;
CHMOD is not a standard FTP command. Some FTP servers implement it as a custom command, and others do not implement it at all. As such, you have to use the TIdFTP.Site() method to send it, on FTP servers you know support it, eg:
IdFTP.Site('CHMOD 640 filename');

Downloading fails from ftp server using TMS Webcopy software in Delphi xe3

Hi i am trying to use TMS software to download a file. But cannot seem to get the file to download.
i use the following code, and have written in an exception handler which keeps telling me that the URL cannot be found. Any help would be appreciated.
main.WebCopy1.Items.Clear;
with main.WebCopy1.Items.Add do
begin
FTPHost :=fHost;
FTPUserID := fusername;
FTPPassword := fPass;
URL := '\Setup\libmysql.dll';
Protocol := wpFTP;
CopyNewerOnly := true;
TargetDir := 'c:\Program Files\myfolder\';
end;
main.WebCopy1.Execute;
May i just add, i just tested the same code and url in another project that i had written and it worked.
Thank You
Things I would try:
use the debugger and check the code which connects and sends the HTTP request
check your HTTP server log file to see which resource has been requested
use a HTTP proxy (Fiddler) to intercept and analyze the HTTP traffic between the Delphi application and your HTTP server
extract the relevant part of the two projects, then diff their sources
(both with the working and the non-working application).
Sorry guys i found the problem. The connection was not set to passive.
I checked on the server and could not find that any connection was being made to the ftp account, and realized that the problem was not the path, it was the connection.
So i checked and saw the connection was set to passive, changed it an vualla, it worked.
So Now We know, For it to work passive must be set to true.
Thanks alot anyway guys

Can TIdHTTPServer and TIdHTTP in same executable connect?

I have a program which uses a TIdHTTPServer. Now I want to write some automated tests using a TIdHTTP which talks to the TIdHTTPServer. The test code is in the program itself.
When the TIdHTTP tries to connect a 'Socket Error # 10061 Connection refused.' exception is raised. I'm guessing that's beacuse the TIdHTTPServer is using the port already.
Is it possible for a TIdHTTPServer and a TIdHTTP which are in the same executable to talk to each other at all? If so, how?
Yes, they can run in the same executable and connect to each other. Simply specify (one of) TIdHTTPServer's listening IP(s) in the URL that you pass to TIdHTTP, eg:
with IdHTTPServer1.Binding.Add do
begin
IP := '127.0.0.1';
Port := 80;
end;
IdHTTPServer1.Active := True;
...
IdHTTP1.Get('http://127.0.0.1/');

How to programatically detect if the MySql Connector/ODBC is installed? (and install it if needed)

I am thinking of the client PC here. When someone comes to run my app, they may not have the MySql Connector/ODBC installed.
Now, I could just try to connect the information scheme (or mysql) database (which is preferable?) - but, if that fails, it might only mean that the database server is down. I could ping it, but it might be up and the MySql process not currently running.
I guess I could just install the connector anyway, even though it sounds sloppy, but not if doing so is going to noticably slow my app's start-up time.
What is the best approach?
When I do install, how do I do that from Delphi, given that the connector will be available on the d/b server, who's IP address is know?
Or ... at the moment my app is a simple .EXE, with no installer. Should I create an install which also installs the ODBC connector? (if so, can anyone recommend a good freeware install builder (with no adware or toolbar installs)?
I hope that this is clear. Please ask me if not.
Thanks.
To check the ODB drivers installed you must check this windows registry key
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
from there you can easily write a delphi function to detect if the MySQL ODBC driver is installed
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
Registry,
SysUtils;
function ODBC_DriverInstalled(const DriverName:string) : Boolean;
Var
Reg : TRegistry;
Providers: TStrings;
i : Integer;
begin
Reg:=TRegistry.Create;
Result:=False;
try
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers') then
begin
Providers:=TStringList.Create;
try
Reg.GetValueNames(Providers);
for i := 0 to Providers.Count-1 do
begin
if CompareText(DriverName,Providers[i])=0 then
begin
Result:=True;
Break;
end;
end;
finally
Providers.Free;
end;
end;
finally
Reg.Free;
end;
end;
begin
try
Writeln(ODBC_DriverInstalled('MySQL ODBC 5.1 Driver'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
To install you have several options one can be use inno setup (which can read the windows registry as well to check for the odbc driver) and then install the driver included inside of your setup file. also you can donwload the driver directly from your app using a simple HTTP GET passing one the download address located here
There is no need to install the MySQL connector. You just need to put the right dll (depending on the server version) in the same folder as your application executable. I would recommend an installer like InnoSetup. This will also let you add start menu shortcut etc.

Resources