What is the function to get date and time an application was executed? I'm using Delphi.
I'm not sure if there's a function or API call for this. But you can fake it pretty easily. Create a unit that looks like this:
unit AppStartTime;
interface
function GetAppStartTime: TDateTime;
implementation
uses
SysUtils;
var
fStartTime: TDateTime;
function GetAppStartTime: TDateTime;
begin
result := fStartTime;
end;
initialization
fStartTime := Now;
end.
Add it to your DPR's uses list, at the top, either first or immediately after anything that "must be first on the list", such as a custom memory manager.
You can have your app log the startup time to a text file or database either in the DPR file or in your main form's OnCreate() event. You can use Delphi's Now() function to get the current date and time, and format it as a string using FormatDateTime() or DateTimeToStr(), depending on what exactly you're looking to do.
The code below saves the startup date and time during the main form's constructor to a text file in the same folder as the application itself called StartDateTime.txt:
procedure TForm1.FormCreate(Sender: TObject);
var
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.Add(FormatDateTime('mm/dd/yyyy hh:nn:ss', Now());
SL.SaveToFile(ExtractFilePath(ParamStr(0)) + 'StartDateTime.txt');
finally
SL.Free;
end;
end;
You can use the Windows API call to GetProcessTimes (declared in Windows.pas) to get details for any process.
If it's your application, I would probably get the start time myself and log it somewhere to keep a history.
Use NtQuerySystemInformation with the SystemProcessInformation informationclass, this returns an array of SYSTEM_PROCESSES structures (records) of which the CreateTime contains the exact time when the applications was started:
_SYSTEM_PROCESSES = record // Information Class 5
NextEntryDelta: ULONG;
ThreadCount: ULONG;
Reserved1: array[0..5] of ULONG;
CreateTime: LARGE_INTEGER;
UserTime: LARGE_INTEGER;
KernelTime: LARGE_INTEGER;
ProcessName: UNICODE_STRING;
BasePriority: KPRIORITY;
ProcessId: ULONG;
InheritedFromProcessId: ULONG;
HandleCount: ULONG;
// next two were Reserved2: array [0..1] of ULONG; thanks to Nico Bendlin
SessionId: ULONG;
Reserved2: ULONG;
VmCounters: VM_COUNTERS;
PrivatePageCount: ULONG;
IoCounters: IO_COUNTERSEX; // Windows 2000 only
Threads: array[0..0] of SYSTEM_THREADS;
end;
SYSTEM_PROCESSES = _SYSTEM_PROCESSES;
PSYSTEM_PROCESSES = ^SYSTEM_PROCESSES;
TSystemProcesses = SYSTEM_PROCESSES;
PSystemProcesses = PSYSTEM_PROCESSES;
We have already translated all of this in the Jedi Apilib (JwaNative)