TIdFTP.Put() and TIdFTP.Get() give unexpected results - delphi

I have Delphi 7 running on XP SP3.
On my server, I have this directory:
root/public_html/TESTTEST
When I do this:
procedure TForm1.Button3Click(Sender: TObject); // connect
begin
ftp.Host := 'URL';
ftp.port := 21;
ftp.Username := 'xxxxxxx';
ftp.password := 'pppppp';
ftp.Connect;
...
The TIdFTP component connects wonderfully.
And when I expand the code to this:
...
ftp.ChangeDir('/public_html');
ftp.ChangeDir('/public_html/TESTTEST');
ShowMessage(ftp.RetrieveCurrentDir);
...
It shows me:
public_html/TESTTEST
Just for test, I did this:
FTP.makedir('TESTDIR');
And the directory does exist.
public_html/TESTTEST/TESTDIR
Back to public_html/TESTTEST, if I try to use ftp.Put(file1,file2,true); I get this error message:
I won't open connection to 100.126.38.39 (only 77.106.146.15)
Same error when I try ftp.Get(file1,file2,true);
File1 and 2 are adjusted accordingly to I/O, True/False switched - no difference, same error.
When I call ftp.Get(...), the resulting file is created, but it is EMPTY.
To be honest, I don't know what to do. How can I make this work?

Related

Combining PDFs via gsdll32.dll with Delphi 11

I have been using this code for more than 10 years and it suddenly stopped working.
The program reads a list of pdf files and output a single pdf file containing all the files in the list. It uses gsapi.pas wrapper unit. over the years I have only had to download an updated version gsdll32.dll file from Ghostscript.to be compatible with newer versions of pdf file format. but it is not working anymore
Any ideas of what is going on?
procedure PDFMerge(input : Array of String;output : string);
var
code:integer;
instance:Pointer;
argv:PPChar;
N,I : smallint;
begin
new(instance);
N:=length(input);
setlength(argv,6+N);
code:=gsapi_new_instance(#instance,nil);
if code<>0 then
begin
raise Exception.Create('Impossible to open an instance of ghostscript. Error code: '+IntToStr(code));
end;
try
//try adding dNothing as first parameter argv[1] := pchar('dNothing');
argv[0] := pchar('AppendPdf');
argv[1] := pchar('-dNOPAUSE');
argv[2] := pchar('-dBATCH');
argv[3] := pchar('-dSAFER');
argv[4] := pchar('-sDEVICE=pdfwrite');
argv[5] := pchar('-sOutputFile='+ output);
argv[6] := pchar('-c');
argv[7] := pchar('.setpdfwrite');
argv[8] := pchar('-f');
for I := 0 to N - 1 do
argv[9+I] := pchar(input[I]);
//Call gs32.dll
code := gsapi_init_with_args(instance, length(argv), argv);
if code<0 then
raise Exception.Create('ERROR: init_args: '+IntToStr(code));
gsapi_exit(instance);
gsapi_delete_instance(instance);
finally
end;
end;
I am using Ghostscript V10.0. I found that they have a new interpreter perhaps it has to do with That? Although I did try addidng the switch "-dNEWPDF=false" but no luck. I keep getting error code -100.
Note using the equivalent command line in Ghostscript itself works fine. It seems to me they changed the way the API needs to be called. Whould that be the case?
I would appreciate any help.
I did try addidng the switch "-dNEWPDF=false" but no luck.

TMemIniFile.Create hanging when called in ServiceStart

In a service running under system account the code below hangs in the TMemIniFile.Create without errors.
If we replace it with TIniFile, it works fine.
It's a Delphi Tokyo 10.2.3 Win32 app running under Windows Server 2012R2. There's no concurrent access to the INI file.
This is the first time (first client) we see this, it has been running fine on many machines.
I have no idea what to look for further. Any ideas?
It 'works' now because we switched to TIniFile, but I'd like to find the cause. From other posts I read here, TINIfile seems to be more finicky than TMemINIfile, my situation is the reverse.
There are no special characters in the INI file and it is created with an ASCII editor.
// This is set in the ServiceCreate:
FIniFileNaam := ChangeFileExt(ParamStr(0),'.INI');
// This is called from the ServiceStart:
procedure TTimetellServiceBase.LeesINI;
var lIniFile : TMemIniFile;
begin
LogMessage(FIniFileNaam, EVENTLOG_INFORMATION_TYPE, cCatInfo, cReadINI); // Logs to event log, we see this
FStartDir := ExtractFilePath(ParamStr(0));
if assigned(FLaunchThreadLijst) then FreeAndNil(FLaunchThreadLijst);
FLaunchThreadLijst := TStringList.Create;
try
if FileExists(FIniFileNaam) then
begin
// Lees waarden uit INI file
lIniFile := TMemIniFile.Create(FIniFileNaam); // This is the call that hangs. The service is unresponsive now.
try
FLaunchThreadLijst.CommaText := lIniFile.ReadString(INISECTIE_SERVICETASKS,'RunIniFiles','');
FMaxTaskDuration := lIniFile.ReadInteger(INISECTIE_SERVICETASKS,'MaxTaskDuration',FMaxTaskDuration);
finally
FreeAndNil(lIniFile);
end;
end;
finally
if (FLaunchThreadLijst.Count = 0) and FileExists(FStartDir + FExeName) then
FLaunchThreadLijst.Add(SDEFAULTTHREADNAME);
LogMessage(Format('FLaunchThreadLijst.CommaText: %s (%d items)',[FLaunchThreadLijst.CommaText,FLaunchThreadLijst.Count]), EVENTLOG_INFORMATION_TYPE, cCatInfo, cLaunchList);
end;
end;
FWIW, INI file contents:
[TASKMANAGER]
RunIniFiles=TTTasks.ini
MaxTaskDuration=2
RestartIniFiles=
KillIniFiles=

Accessing Text File from Service

I'm tryin to write a simple service in Delphi XE7, in a Win7 64bit virtual machine where Delphi is installed.
All what i need for now is to open/create a text file and write something into it every second. It should be simple ... should ...
Immediately after creating the service, and installing it, it runs well.
I addedd this code:
const
LogName = 'C:\GFLog.txt';
var FLogFile : TextFile;
procedure TServiceTest.ServiceExecute(Sender: TService);
begin
ServiceThread.ProcessRequests(False);
try
AssignFile(FLogFile, LogName);
if not FileExists(LogName)
then Append(FlogFile)
else Rewrite(FlogFile);
WriteLn(FLogFile,'Start '+TimeToStr(Now));
while not Terminated do
begin
WriteLn(FLogFile,TimeToStr(Now));
Sleep(1000);
ServiceThread.ProcessRequests(False);
end;
CloseFile(FLogFile);
except
on E:Exception do
ShowMessage(E.Message)
end;
end;
As a result, the file is not created, no errors are shown, and I can't understand why.
Of course, I've made something wrong, but what?
Someone can help?
Ok, solved ... copy and paste error ... i feelVERY stupid, but ...
if not FileExists(LogName)
then Append(FlogFile)
else Rewrite(FlogFile);
I was tryin to append to not exixting file ...

Delphi7, Save User's Changes or other User's Information / Notes

In my program, the user completes a form and then presses Submit. Then, a textfile or a random extension file is created, in which all the user's information is written. So, whenever the user runs the application form, it will check if the file, which has all the information, exists, then it copies the information and pastes it to the form. However, it is not working for some reason (no syntax errors):
procedure TForm1.FormCreate(Sender: TObject);
var
filedest: string;
f: TextFile;
info: array[1..12] of string;
begin
filedest := ExtractFilePath(ParamStr(0)) + 'User\Identity\IdentityofMyself.txt';
if FileExists(filedest) then
begin
AssignFile(f,filedest);
Reset(f);
ReadLn(info[1], info[2], info[3], info[4], info[5], info[6], info[7],
info[8], info[9], info[10], info[11], info[12]);
Edit1.Text := info[1];
Edit2.Text := info[2];
ComboBox1.Text := info[3];
ComboBox5.Text := info[4];
ComboBox8.Text := info[4];
ComboBox6.Text := info[5];
ComboBox7.Text := info[6];
Edit3.Text := info[7];
Edit4.Text := info[8];
Edit5.Text := info[11];
Edit6.Text := info[12];
ComboBox9.Text := info[9];
ComboBox10.Text := info[10];
CloseFile(f);
end
else
begin
ShowMessage('File not found');
end;
end;
The file exists, but it shows the message File not found. I don't understand.
I took the liberty of formatting the code for you. Do you see the difference (before, after)? Also, if I were you, I would name the controls better. Instead of Edit1, Edit2, Edit3 etc. you could use eFirstName, eLastName, eEmailAddr, etc. Otherwise it will become a PITA to maintain the code, and you will be likely to confuse e.g. ComboBox7 with ComboBox4.
One concrete problem with your code is this line:
readln(info[1], info[2], info[3], info[4], info[5], info[6], info[7],
info[8], info[9], info[10], info[11], info[12]);
You forgot to specify the file f!
Also, before I formatted your code, the final end of the procedure was missing. Maybe your blocks are incorrect in your actual code, so that ShowMessage will be displayed even if the file exists? (Yet another reason to format your code properly...)
If I encountered this problem and wanted to do some quick debugging, I'd insert
ShowMessage(BoolToStr(FileExists(filedest), true));
Exit;
just after the line
filedest := ...
just to see what the returned value of FileExists(filedest) is. (Of course, you could also set a breakpoint and use the debugger.)
If you get false, you probably wonder what in the world filedest actually contains: Well, replace the 'debugging code' above with this one:
ShowMessage(filedest);
Exit;
Then use Windows Explorer (or better yet: the command prompt) to see if the file really is there or not.
I'd like to mention an another possibility to output a debug message (assuming we do not know how to operate real debugger yet):
{ ... }
filedest := ExtractFilePath(ParamStr(0)) + 'User\Identity\IdentityofMyself.txt';
AllocConsole; // create console window (uses Windows module) - required(!)
WriteLn('"' + filedest + '"'); // and output the value to verify
if FileExists(filedest) then
{ ... }

How to properly initialize Codec in Runtime (Turbo Power Lock Box 3)?

I use following procedure to encode stream.
procedure SaveEncodedStream(Strm:TStream; LicFileName:String);
var
C:TCodec;
CL:TCryptographicLibrary;
Sg:TSignatory;
KFS,DFS:TFileStream;
Dir:String;
begin
CL:=TCryptographicLibrary.Create(nil);
C:=TCodec.Create(nil);
SG:=TSignatory.Create(nil);
Dir := ExtractFilePath(ParamStr(0));
KFS:=TFileStream.Create(Dir+PublicKeyFile,fmOpenRead);
DFS:=TFileStream.Create(LicFileName,fmCreate);
try
C.CryptoLibrary:=CL;
C.BlockCipherId := 'native.RSA';
C.ChainModeId := 'native.CBC';
C.AsymetricKeySizeInBits := 1024;
SG.Codec:=C;
SG.LoadKeysFromStream(KFS,[partPublic]);
C.EncryptStream(Strm,DFS);
finally
CL.Free;
C.Free;
SG.Free;
KFS.Free;
DFS.Free;
end;
end;
And receive "Wrong Mode" error on
C.EncryptStream(Strm,DFS); call
Stepping into the code I discovered that it even does not try to load keys as Codec is not initialized. When I place componets on the form - everything works. But I do not need Form or DataModule.
Have not found solution to get rid of DataModule. It looks like components need one to properly initialize themselves. So as workaround I have created global DataModule with all components configured in design mode. I use that module in SaveEncodedStream like that:
uses
... EncryptDataModule;
...
var
BeenHere:Boolean = false;
...
procedure SaveEncodedStream(Strm:TStream; LicFileName:String);
var
KFS,DFS:TFileStream;
Dir:String;
begin
Dir := ExtractFilePath(ParamStr(0));
KFS:=TFileStream.Create(Dir+PublicKeyFile,fmOpenRead);
DFS:=TFileStream.Create(LicFileName,fmCreate);
try
DataModule.SG.LoadKeysFromStream(KFS,[partPublic]);
if BeenHere then DataModule.C.Reset;
DataModule.C.EncryptStream(Strm,DFS);
BeenHere:=true;
finally
KFS.Free;
DFS.Free;
end;
end;

Resources