Wrong timestamp in Codesite logging - delphi

I am using Raize CodeSite to write log lines to from a Delphi desktop application to a separate log file.
All components are running on a single PC (application + Codesite)
The logging works fine, except for the date/time. The date/time which is shown in the Codesite Log viewer is sometimes EARLIER than the time the log message was sent!
TS := FormatDateTime('YYYY-MM-DD HH:NN:SS.ZZZ', NOW()); // 2019-08-20 14:22:15.780
CodeSite.Send('Log Message at ' + TS );
Which results into a log line with a (codesite-) date/time which is EARLIER than the timestamp generated in the application:
"Log message at 2019-08-20 14:22:15.780" Code site date
time: 2019/08/20 14:22:15.333

Related

Remote printing issues with Delphi and ExtEscape on Zebra printer

Our app prints directly to a Zebra UPS printer (Zebra GK420d). The list of printing commands are formatted as expected by the ExtEscape (Zebra Printer direct communication).
Printing works as expected on the local machine with the USB printer attached however nothing gets printed when using the app from a remote machine via remote desktop. All computers are running Windows 10 with latest updates.
A test was done sending the following commands from the local and remote machine while the printer is paused:
^FX
^XA
^LH30,30
^FO20,10
^ADN,18,20
^FDTest^FS
^XZ
It is possible to print the above commands correctly from the remote machine if saving it to a file and copying the file to \\printer_name.
The following is the method used to print the commands that are passed as a string argument:
procedure SendCommandsToPrinter(ACommands: string);
var
FmtLabel: AnsiString;
begin
FmtLabel := '00' + AnsiString(ACommands);
PWord(FmtLabel)^ := Length(FmtLabel) - 2; // store the length
Printer.BeginDoc;
if ExtEscape(Printer.Canvas.Handle, PASSTHROUGH, Length(FmtLabel), Pointer(FmtLabel), 0, nil) <= 0 then
raise Exception.Create('Printer error');
Printer.EndDoc;
end;
The spooled files generated on C:\Windows\System32\spool\PRINTERS folder have different content, the file generated running the app on the local machine has the exact same commands that were sent to the printer while the file generated while running the app on a remote machine have the following content:
CT~~CD,~CC^~CT~
^XA
^PW812
^POI^PQ1,0,1,Y^XZ
Anyone having similar issues with Zebra printers when printing remotelly? Any ideas on how to solve this issue?
Thanks in advance.

Strange issue with inaccurate time in TIdMessage headers and message

Using Indy 10.1.5 (the version shipped with D2007), I'm composing and sending an email message as part of an automated internal process.
The email is sent fine, but when the recipient receives the message the time appears incorrectly in the email client, causing the message to incorrectly sort in the inbox. This has caused the recipient to overlook the message (because it wasn't in the inbox in the proper sequence and scrolled off the bottom), causing minor processing delays.
Both the sender and recipient in the US Eastern time zone (UTC -0400). The message header is correctly showing the message date, but without including a time zone:
Date: Mon, 3 Aug 2015 11:12:21 +0000
When the email is received and viewed in Outlook, the message is being shown with the UTC offset:
Mon 08/03/2015 7:12 AM
The code that creates the message is pretty simple (copied/pasted and then anonymized and simplified). It creates a relatively short message body (providing info about the file attached), creates the attachment, and sends the message. (try..except and try..finally removed for brevity - they exist in the actual code.)
Msg := TIdMessage.Create;
// Also tried using False and setting Date property manually
Msg.UseNowForDate := True;
for i := 0 to NumAddr do // Number of recipients
begin
Msg.Recipients.Add;
Msg.Recipients[Msg.Recipients.Count - 1] := RecipAddr[i];
end;
Msg.FromList.Add;
Msg.FromList[0].Name := Sender Name;
Msg.FromList[0].Address := SenderAddress;
Msg.Subject := 'Some text';
Msg.Body.Add('A few lines of text providing summary info.');
TIdAttachmentFile.Create(Msg.MessageParts, FileToAttach);
Mail := TIdSMTP.Create;
Mail.Host := PrimaryMailServer;
Mail.Connect;
Mail.Send(Msg);
Mail.Disconnect;
I've read through the Indy documentation and examined all of the properties I can find for both TIdSMTP and TIdMessage, and can't find anything else that would correct this time zone mismatch.
Can anyone see what it is I'm missing here?
10.1.5 is an outdated version of Indy 10. The current version is 10.6.2.
There were some UTC related bugs in earlier versions of Indy 10. In this case, there was a bug in Indy's OffsetFromUTC() function, which retrieves the local machine's UTC offset and is used when calculating the timestamp for outgoing emails. You should upgrade to a modern version of Indy 10, so you have the latest fixes, features, architectural changes, etc.
If you cannot upgrade, you will have to set the TIdMessage.UseNowForDate property to false and provide your own timezone-adjusted TDateTime value in the TIdMessage.Date property to account for Indy's faulty offset, so a proper timestamp value is sent.

Suppress an error message box appearing in Delphi for a specific error

At the moment I have a Delphi form running and upon losing connection to its Database/loss of network, it re-establishes and carries on.
The service is on a timer and each minute, it pops up with message box saying failure to connected to db on network [ip address]. This is at the start of the timer, a stored proc is ran.
I have all errors writing to a log file on the service local machine, is there a way I can suppress the message box appearing as I don't need it to appear all the time? by message box it is a windows exception box not a showMessageBox(). It automatically pops up when the exception hits but it will continuously hit whilst the machine boots up and re-establishes connection to the network.
The code I have is as follows
(*Call db Procedure*)
try
db.SQL.Clear;
db.SQL.Add('call dbProc();');
db.ExecSQL;
except
On E : Exception Do
begin
If E.ClassName = 'EIBInterBaseError' Then
begin
WriteToLog('Network Error : An error has occured whilst trying to communicate with the db outside the'
+ ' loop to catch up , please see user guide V.1.0.2.145',2);
reconnectdb;
end
else if E.ClassName = 'EADOError' then
begin
WriteToLog('Network Error : An error has occured whilst trying to communicate with the db outside the'
+ ' loop to catch up , please see user guide V.1.0.2.145',2);
reconnectDB;
end;
end;
end;
Based on your description (since you did not show any code), it sounds like each iteration of your loop is raising an exception that you are not catching, so it ends up in a default exception handler within the RTL that is then displaying the popup MessageBox. If this is the case, you need to catch the original exception using a try..except block around your code that is failing.

Incorrect boundary string in multipart/mixed message

I'm using Delphi 2006 to create and send an email message with an attachment in a personal-use-only application. I send the message with an instance of TIdSMTP, and then also put a copy into a specific IMAP folder with an instance of TIdIMAP4. This all works very nicely with the version of Indy 10 that was distributed with BDS2006, with one exception: the time is always incorrect in the email header.
I decided to fix that if I could, and after searching for a solution it seemed most reasonable to get the latest Indy 10 snapshot and use that.
That puts the correct time into the email header, but there's a new problem. The boundary string is now different in the header of the message that is added to the IMAP folder than what comes in the body of the email! (Please note that the message that was sent via SMTP is correct.)
This is the relevant header information from the older version of Indy 10:
Content-Type: multipart/mixed; boundary="XNlC6OyS4QSiHY2U=_jsXyps6TR34pFNsh"
MIME-Version: 1.0
Date: Tue, 22 Nov 2011 09:11:58 +0000
A test of the BDS2006-bundled version of Indy
--XNlC6OyS4QSiHY2U=_jsXyps6TR34pFNsh
Content-Type: application/octet-stream;
name="20111122.xls"
And this is the same header information from Indy 10.5.8 (snapshot 10_4702 which I installed yesterday):
Content-Type: multipart/mixed; boundary="CDbEncbFvL7RZdOJ3DOIRoRBs=_nBsbZms"
MIME-Version: 1.0
Date: Tue, 22 Nov 2011 07:33:46 -0600
investigating more deeply, why does the boundary indicator change?
--h=_WzGWJei29fng7SqdPpDh1nkJxJZhiGc
Content-Type: application/octet-stream;
name="20111122.xls"
The time stamp is fixed, but now the boundary string is incorrect. The result is that there appears to be nothing at all in the message that gets added to my IMAP folder.
Here is the relevant code that creates the email message and attachment, sends it, and puts a copy into the IMAP folder:
FTheMsg.Date := Now; // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.ClearBody;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' +
FormatDateTime('mm/dd/yy', FEndDate);
FTheMsg.Body.Assign(FMemo.Lines);
// create the attachment
TIdAttachmentFile.Create(FTheMsg.MessageParts, fileName);
// send the mail!
FSmtp.Host := FSMTPHost; // values set up elsewhere, FSmtp is a component on the form
FImap.Host := FIMAPHost; // FImap is also a component on the form
FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, FTheMsg.LastGeneratedHeaders, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;
As I said, the email that gets sent is fine and displays properly. But the one that is added to my IMAP folder (in FImap.AppendMsg() above) is incorrect. I've attempted to trace through the code to see where it might be going wrong, but frankly, I'm not familiar enough with Indy and the various email protocols/RFCs to be able to determine what's going wrong. About all I can tell is that the older version saves the message to a temporary file before appending it to the folder, while the newer version saves it to a memory stream instead. Obviously, something is different about that, but I'm currently too ignorant to determine what.
Is there a simple way to correct the timestamp problem in the old version? If so, that would be fine for my use, as everything else appears to be correct. If not, what else do I need to do to fix the problem exhibited here with the incorrect boundary string?
(As this is an application strictly for my own use, I can live with the incorrect date if I have to, but not with the "empty-appearing" copy in my 'Sent Items' folder.)
If more information is needed, I'll gladly supply whatever I can.
[Edit: I did incorporate something of a kludge in MY code, using the older version of Indy. I simply set the date/time of the message to UTC/GMT time before sending it, and that, at least, allows the message to contain the correct time at the receiver's end. I don't particularly care for that fix, but it does the trick.]
Don't use the TIdMessage.Body property to hold your text when an attachment is present. Put the text into a TIdText object instead. Even better, use the TIdMessageBuilder... classes, such as TIdMessageBuilderPlain, to prepare the TIdMessage body for you.
Try this:
FTheMsg.Clear;
FTheMsg.Date := Now; // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' + FormatDateTime('mm/dd/yy', FEndDate);
FTheMsg.ContentType := 'multipart/mixed';
TIdText.Create(FTheMsg.MessageParts, FMemo.Lines).ContentType := 'text/plain';
TIdAttachmentFile.Create(FTheMsg.MessageParts, fileName);
FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, nil, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;
Or:
FTheMsg.Clear;
FTheMsg.Date := Now; // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' + FormatDateTime('mm/dd/yy', FEndDate);
with TIdMessageBuilderPlain.Create do
try
PlainText.Assign(FMemo.Lines);
Attachments.Add(fileName);
FillMessage(FTheMsg);
finally
Free;
end;
FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, nil, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;
Now, with that said, it is likely that it will still not work correctly either way. TIdIMAP4.AppendMsg() calls TIdMessage.SaveToStream() internally, which regenerates the email content fresh (and thus alters the boundary used in the body). Whether you pass in the pre-existing TIdMessage.LastGeneratedHeaders or let TIdIMAP4.AppendMsg() grab the current TIdMessage.Headers, they will be out of sync with the new boundary that TIdMessage.SaveToStream() generates.
To make sure both SMTP and IMAP4 are in sync, they need to receive the same data. Try calling TIdMessage.SaveToStream() manually first with the TIdMessage.NoEncode property set to False, then set the TIdMessage.NoDecode property to True and call TIdMessage.LoadFromStream() to reload the saved data as-is into the TIdMessage.Headers and TIdMessage.Body properties, then call TIdSMTP.Send() and TIdIMAP4.AppendMsg() with the TIdMessage.NoEncode property set to True so the TIdMessage.Headers and TIdMessage.Body are sent as-is.
I know, this goes against what the TIdIMAP4.AppendMsg() comments/docs say to do. AppendMsg() does not currently take MIME into account at all, so it does not ensure the MIME boundary in the header and body match each other. I will try to check in a fix for that. For Indy 11, Indy's entire MIME handling system is going to be redesigned, so I'll make sure it is possible to preserve boundaries, and/or to specify custom boundaries, so AppendMsg() can match the body boundary to the header boundary better.
IMAP4 is a very tricky protocol to work with in general.

Setting correct printer in MS Word through automation

I have the following automation code:
lPrintSetup := fWordObject.Application.Dialogs.Item(wdDialogFilePrintSetup);
lPrintSetup.Printer := 'MyPrinter';
lPrintSetup.DoNotSetAsSysDefault := True;
lPrintSetup.Execute;
lPrintSetup := Null;
The Printer property is giving me some problems, sometimes Execute crashes with an EOleException (0x800A1460 (error code 5216): There is a printer error) because of a wrong printername.
I have printer information of all printers in a _PRINTER_INFO_2 record which I obtained by a EnumPrinters API-call. How can I compose the right printername for Word given the information in a _PRINTER_INFO_2 record? It has work with at least Windows 2000, Word 2000 and Citrix.
Some background info:
Our application first filled the Printername with a self constructed printername. This gave problems with Citrix clients, so for Citrix clients we took the _PRINTER_INFO_2.pPortName and deleted the Client:#: part.
This is working for the majority of our customers, but sometimes still the printer error shows up.
What I have tried so far (on Windows XP SP3, Word 2007):
Just take the _PRINTER_INFO_2.pPrinterName. Problem is that when you modify printernames on purpose (renaming 'PDFCreator' to 'HP DESKJET 520 on MYPC') it crashes on the latter (while selecting this printer in Word works).
Composing a printername like this: lPrintSetup.Printer := PRINTER_INFO_2.pPrinterName + ' on ' + PRINTER_INFO_2.pPortname. Seems to work always! But googling around showed that ' on ' is localized, so I'm not sure if that's going to work on non-english Windows versions. Edit: does not work always :(
Another solution I found on the web:
When reading the printername from Word it has the form of "Printername on Ne01:", where Ne01 is ranged from Ne00: to Ne99:. The solution suggested taking the printername and just try to set it while looping from Ne00: to Ne99:. When .Execute doesn't crash, you've got the right one. I'm not very fond of this 'trail and error' method.
I'm not sure if you've tried this, or if its of any use, but you can get a list of all the printers on the system from the Printer.Printers object make sure you add Printers to the Uses clause of your unit.
This should then list the actual names on the system and you may be able to use this information to do what you want.
As stated you can get a list of printer names using the Printer.Printers which is a TStringList with the name of the printer on each item.
This code gives the default printer name
Printer.Printers[Printer.PrinterIndex]
Some minutes ago I learned, that word2k not only wants Printernames like "Printername on Ne01:" it only wants the port (NEnn) uppercase "Printername on NE01:"
I figured it out. Word has the printername in the form of "Printername on NE01:". Ne01: is the printerport as specified in the devices section of win.ini. So now I compose the printername as _PRINTER_INFO_2.pPrinterName + ' on ' + <PrinterPort from win.ini> and set that name for the printer property of the FilePrintSetup dialog.
This is much better than to resort to the trail-and-error method mentioned in my question.

Resources