Delphi GetLastError with execute packages - delphi

When I execute this code without runtime packages, I have a 32 code error, that is correct.
But when I activite runtime packages (for exemple just with "FireDACASADriver;YmagControlDB") the error code is always "0"
procedure TForm1.Button1Click(Sender: TObject);
Var
Stream: TStream;
iError : integer;
begin
Stream := nil;
iError := -1;
try
try
Stream := TFileStream.Create('d:\toto.docx', fmOpenRead);
except
begin
iError := GetLastError;
end;
end;
finally
if Assigned(Stream) then
Stream.Free;
end;
showmessage('Erreur : ' + inttostr(iError));
end;
How I can fix the GetLastError with runtime packages ?

It is simply not appropriate to call GetLastError there. You are mixing two different error handling models.
Call GetLastError immediately after an API call fails, if the documentation says to do so. When you call it, something other function could very well have called SetLastError and reset the value.
So it is wrong to call GetLastError since you aren't using Win32 functions, and should remove the call to GetLastError. Your code should be:
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStream;
begin
Stream := TFileStream.Create('d:\toto.docx', fmOpenRead);
try
// ....
finally
Stream.Free;
end;
end;
If there is an error, an exception will be raised which will be reported by the top level exception handler.
Runtime packages should have no bearing on how this code executes.
Possible causes of an error are that the file does not exist, or that it is locked.
You wrote:
if Assigned(Stream) then
Stream.Free;
That is always pointless since the Free method also checks for the object reference being nil. In fact your code is equivalent to:
if Assigned(Stream) then
if Assigned(Stream) then
Stream.Destroy;
So it is cleaner to rely on test inside Free and simply write:
Stream.Free;
In the comments you state that you actually want to test whether or not the file is locked. Don't use a file stream for that. Instead do the following:
Call CreateFile to open the file.
Check the returned handle against INVALID_HANDLE_VALUE to detect error.
In case of error use GetLastError to find out the cause of error.
Otherwise close the handle with CloseHandle.
However, this is not to be recommended. You might use this approach to determine that the file is not locked, but by the time you try to read it, it has been locked. There is an inherent race condition.
As a general guideline it is better to ask forgiveness than permission.

The act of raising an exception can reset the calling thread's error code. It is simply not appropriate to call GetLastError() inside an exception handler.
That being said, if TFileStream fails to open the file, an exception is raised that contains a system-provided error message (but not the actual error code), eg:
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStream;
begin
try
Stream := TFileStream.Create('d:\toto.docx', fmOpenRead);
try
// use Stream as needed
finally
Stream.Free;
end;
except
on E: Exception do
ShowMessage('Erreur : ' + E.Message);
end;
end;
If you need access to the error code, you can't use TFileStream, you will have to use CreateFile() directly instead:
procedure TForm1.Button1Click(Sender: TObject);
var
hFile: THandle;
iError: DWORD;
begin
hFile := CreateFile('d:\toto.docx', GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
if hFile <> INVALID_HANDLE_VALUE then
begin
try
// use hFile as needed...
//
// if you need to access the file as a TStream, you can
// instantiate a THandleStream passing hFile to its constructor...
//
finally
CloseHandle(hFile);
end;
end else
begin
iError := GetLastError;
ShowMessage('Erreur : ' + IntToStr(iError));
if iError = ERROR_SHARING_VIOLATION then
begin
// do something...
end;
end;
end;
Alternatively:
procedure TForm1.Button1Click(Sender: TObject);
var
hFile: THandle;
begin
hFile := CreateFile('d:\toto.docx', GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
try
Win32Check(hFile <> INVALID_HANDLE_VALUE);
try
// use hFile as needed...
finally
CloseHandle(hFile);
end;
except
on E: EOSError do
begin
ShowMessage('Erreur : ' + IntToStr(E.ErrorCode));
if E.ErrorCode = ERROR_SHARING_VIOLATION then
begin
// do something...
end;
end;
end;
end;

Related

Delphi code completion fail with anonymous methods

Please create a new FMX application, add a button and a memo to run this example. I have this code:
procedure TForm1.Button1Click(Sender: TObject);
begin
TTask.Run(procedure
var
client: TIdHTTP;
result: string;
begin
client := TIdHTTP.Create(nil);
try
try
client.ReadTimeout := 4000;
client.ConnectTimeout := 4000;
result := client.Get('a valid url here just as test');
TThread.Synchronize(nil, procedure
begin
Memo1.Lines.Add(result);
end);
except
on E: Exception do
begin
TThread.Synchronize(nil, procedure
begin
Memo1.Lines.Add(E.Message);
end);
end
end;
finally
client.Free;
end;
end);
end;
It works as I expect but the problem is in the IDE. If I place the cursor somewhere in the body of the anonymous function, I get the closing of the finally statement automatically.
How can I fix this?
First I am here
Then I press enter and I have this!
If you put the cursor at the beginning and not at the end of the line, you can add new spaces without the completion. How to solve this problem? Well, I have discovered that the issue happens because there is this code:
TThread.Synchronize(nil, procedure
begin
Memo1.Lines.Add(result);
end);
If you remove this code, the issue doens't happen anymore. Is this a bug in the IDE?
Is this a bug in the IDE?
Yes. This is a defect. Please submit a report to Quality Portal.
Is this a bug in the IDE?
Yes, this is a bug in the IDE. Your code is syntactically valid.
How can I fix this?
The best way to avoid this is to create your code and surround it with try...except... to handle any exception:
try
MyClass := TComponent.Create(Self);
try
finally
MyClass.Free;
end;
except on E: Exception do
end;
So your code will be:
TTask.Run(procedure
var
client: TIdHTTP;
result: string;
begin
try
Client := TIdHTTP.Create(nil);
try
client.ReadTimeout := 4000;
client.ConnectTimeout := 4000;
result := client.Get('a valid url here just as test');
TThread.Synchronize(nil, procedure
begin
Memo1.Lines.Add(result);
end);
finally
Client.Free;
end;
except on E: Exception do
begin
TThread.Synchronize(nil, procedure
begin
Memo1.Lines.Add(E.Message);
end);
end;
end;
end;

Load Log.txt From other apps to Memo - Delphi7

I am trying to record the session log from other applications (Proxifier) to a Memo.
I've tried using the command :
procedure TForm1.TimerTimer(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('C:\PMASSH\Proxyfier\Profiles\Log.txt');
end;
but at certain times I get an error
Can you help my problem above ? I would really appreciate of all the answers.
Thanks
The other program has opened the file with a sharing mode that does not allow other processes to read it. Typically this happens when the other application is writing to the file.
There's not a whole lot you can do about this. This is perfectly normal behaviour, and is to be expected. You can try detecting the error, waiting for a short period of time, and re-trying.
Since you are already running this on a timer, the re-try will just happen. So perhaps you just need to suppress those exceptions:
procedure TForm1.TimerTimer(Sender: TObject);
begin
try
Memo1.Lines.LoadFromFile(...);
except
on EFOpenError do
; //swallow this error
end;
end;
Note that detecting EFOpenError is perhaps a little crude. Perhaps there are other failure modes that lead to that error. However, as a first pass, the code above is a decent start.
David's answer is correct. I just want to clarify why this is happening.
The answer lies in the code:
procedure TStrings.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
as you can see, the file is accessed for sharing but no writing is allowed.
you can solve this by creating the filestream yourself:
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
and then use the Lines.LoadFromStream() method to load the contents into the memo
Please note that the problem may subsist in cases where the other application has opened the file in exclusive mode (i.e. no sharing), so proper Exception management like in David's answer is still needed.
You can try your luck with ReadFile WinAPI. On a shared read open mode, you'll be able to sneak and read the contents of the file at last file buffer flush. If that another application (Proxifier) opened the file with CreateFile WinAPI with FILE_SHARE_READ share mode then you'll be able to open it for reading, as long as you use ReadFile API. Standart LoadFromFile method won't work here if it still was opened for share, and you'll get the same 'lock' error.
But here's the catch.. You'll have to deal with buffers, sizes and handles... You'll have to assing a handle to file for reading, get the file size with that handle, set an array with that size, do read to that array and assign, add (whatever) that array to the memo.. Pure usage of WinAPI. Some job for a simple task...
Here is a basic example of how to deal with files with WinAPI:
The key assumption of that other application's file open process:
var
Form1: TForm1;
logfile: Textfile;
h: THandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// AssignFile(logfile, 'c:\deneme.txt');
// Rewrite(logfile);
h := CreateFile('C:\deneme.txt', GENERIC_WRITE, FILE_SHARE_READ, nil,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
Timer1.enabled := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.enabled := false;
// CloseFile(logfile);
CloseHandle(h);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
str: AnsiString;
p: pointer;
buf: array of ansichar;
written: cardinal;
begin
// Writeln(logfile, 'denemeStr');
str := 'denemeStr' + #13#10;
p := pansichar(str);
SetLength(buf, length(str));
move(p^, buf[0], length(str));
WriteFile(h, buf[0], length(buf), written, nil);
FlushFileBuffers(h);
end;
And if it's been shared for reading, this is how you can read from it:
procedure TForm1.Button1Click(Sender: TObject);
var
h: THandle;
buf: array of ansichar;
size, read: cardinal;
begin
Memo1.Lines.Clear;
// Memo1.Lines.LoadFromFile('c:\deneme.txt');
h := CreateFile('C:\deneme.txt', GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
size := GetFileSize(h, nil);
SetLength(buf, size);
ReadFile(h, buf[0], size, read, nil);
CloseHandle(h);
Memo1.Lines.Add(pansichar(buf));
end;
Hope this'd help...
procedure TForm1.TimerTimer(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('C:\PMASSH\Proxyfier\Profiles\Log.txt');//// read file path error if file notfound
// if trying to record
Memo1.Lines.SaveToFile(Path...);
end;

Delphi: deny access to file for other processes

How can I deny access (only to write) to a file for other processes? I will read\write a file all time.
I use
FileOpen('c:\1.txt', fmOpenReadWrite or fmShareDenyWrite)
but after (starting to load the file to StringList) I get error
Cannot open file C:\1.txt. The process cannot access the file because it is being used by other process."
Only I open the file.
Here, the error message is actually slightly misleading. The reason you can't load into the stringlist is because you already opened the file in read/write.
if you check the implementation of TStrings.LoadfromFile:
procedure TStrings.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
You see that it tries to open the file with a "DenyWrite" condition, but you already opened the file in write mode. That is the reason why it fails.
You can work around that by using LoadFromStream instead.
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
Stringlist.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
Note that you will need to use fmShareDenyNone for this to work in that situation. Then again, you could probably reuse the Read/Write handle you got from your OpenFile, probably do something like this:
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStream;
iPosition : Int64;
begin
Stream := THandleStream.Create(FHandle); //FHandle is the read/write handle returned by OpenFile
try
iPosition := Stream.Position;
Stream.Seek(0, soFromBeginning);
Stringlist.LoadFromStream(Stream);
Stream.Position := iPosition;
//Restore stream position.
finally
Stream.Free;
end;
end;
But be advised that these approach might have a few "gotchas" I'm unaware of.

Test if disk has write access

I need to know if my program can write files to the disk (HDD, floppy, flash, CD) from where it is running.
I have tried something like:
{$I-}
Rewrite(myFile);
{$I+}
Result:= (IOResult = 0);
The problem is that if the disk is read-only, Windows gives me an error message telling me that
"appName.exe - Write Protect Error The disk cannot be written to because it is write protected. Please remove the write protection from the volume
USB_BOOT in drive D:. Cancel TryAgain Continue"
How can I test for write access without raising any error/warning messages?
Thanks.
Edit:
Ok. The "bug" has nothing to do with the above mentioned piece of code. I only thought that it appears there. I move the code to the read-only disk and ran it from there. The bug appears in a function called "CreateShortcutEx", on this line:
MyPFile.Save(PWChar(WFileName),False);
MyPFile is declared like this:
var
MyObject : IUnknown;
MySLink : IShellLink;
MyPFile : IPersistFile;
Directory : String;
WFileName : WideString;
MyReg : TRegIniFile;
begin
MyObject := CreateComObject(CLSID_ShellLink);
MySLink := MyObject as IShellLink;
MyPFile := MyObject as IPersistFile;
.....
end;
So, why is MyPFile trying to write to the application's drive (the one that is read-only) if the WFileName parameter is "C:\documents and settings\bla bla" ?
Call the Windows API SetErrorMode() function to disable the error message box.
Edit:
I just tried, and this:
procedure TForm1.Button1Click(Sender: TObject);
var
Str: TStream;
OldMode: Cardinal;
begin
OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
try
Str := TFileStream.Create('z:\foo.txt', fmOpenReadWrite);
try
finally
Str.Free;
end;
except end;
finally
SetErrorMode(OldMode);
end;
end;
works as expected.
Not really pretty but this seems to work for me.
function CanWrite(drive: string): boolean;
var
OldMode: Cardinal;
begin
OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
with TStringList.Create do
try
SaveToFile(drive + ':\file.txt');
result := true;
try
deletefile(drive + ':\file.txt');
except
end;
except
result := false;
end;
finally
SetErrorMode(OldMode);
end;
end;
Call to it with
if CanWrite('g') = true then
begin
showmessage('Can Write');
end
else
begin
showmessage('Can not write');
end;
What happens when you put your code inside an try/except?
Also, you can try (something like) this:
function CanWrite: boolean;
begin
result := true;
with TStringList.Create do
try
SaveToFile('file.txt');
except
result := false;
finally
Free;
end;
end;
Sorry, but I don't code in Delphi anymore and I don't have Delphi installed anywhere.
There exist a small freeware "Drive ready?" utility (dready.com) written by Horst Schaeffer that also can check write access. I have not tested it but as far as I can see this could be used as a solution; call it for instance as "DREADY C: /W" and check the return value.

Information required on TDataSetProvider in Delphi

I have Midas project that uses a TDataSetProvider in one of RemoteDataModules in the Server
Currently I am making use of the following events
BeforeApplyUpdates - to create an Object
BeforeUpdateRecord - to use the object
AfterApplyUpdates - to destory the object
Question:
Will ‘ AfterApplyUpdates’ always be called even if the is an update error ?
If you look at the sourcecode:
function TCustomProvider.DoApplyUpdates(const Delta: OleVariant; MaxErrors: Integer;
out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant;
begin
SetActiveUpdateException(nil);
try
try
if Assigned(FOnValidate) then
FOnValidate(Delta);
DoBeforeApplyUpdates(OwnerData);
Self.OwnerData := OwnerData;
try
Result := InternalApplyUpdates(Delta, MaxErrors, ErrorCount);
finally
OwnerData := Self.OwnerData;
Self.OwnerData := unassigned;
end;
except
on E: Exception do
begin
SetActiveUpdateException(E);
raise;
end;
end;
finally
try
DoAfterApplyUpdates(OwnerData);
finally
SetActiveUpdateException(nil);
end;
end;
end;
Yoy see that DoAfterApplyUpdates is called in the finally block. This means it is always called regardles of any exception.

Resources