I want to run an application with a timestamp as parameter. In C, I use something like that:
char startCommand[64];
sprintf_s(startCommand, 64, "l2.bin %d", time(NULL));
HANDLE hProcess = CreateProcess( NULL, startCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
Is it possible to add a timestamp parameter in this Delphi code?
var
Play : string;
Par : string;
begin
Play := 'myfile.exe';
Par := '??'; // the parameter - Timestamp
ShellExecute(TForm(Owner).Handle, nil, PChar(Play), PChar(Par), nil, SW_SHOWNORMAL);
end;
Do I have to do DateTimeToStr first?
The C time() function "returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds". You can use Delphi's DateUtils.SecondsBetween() function to get a similar value, eg:
uses
..., Windows, DateUtils;
function CTime: Int64;
var
SystemTime: TSystemTime;
LocalTime, UTCTime: TFileTime;
NowUTC, EpochUTC: TDateTime;
begin
// get Jan 1 1970 UTC as a TDateTime...
DateTimeToSystemTime(EncodeDate(1970, 1, 1), SystemTime);
if not SystemTimeToFileTime(SystemTime, LocalTime) then RaiseLastOSError;
if not LocalFileTimeToFileTime(LocalTime, UTCTime) then RaiseLastOSError;
if not FileTimeToSystemTime(UTCTime, SystemTime) then RaiseLastOSError;
EpochUTC := SystemTimeToDateTime(SystemTime);
// get current time in UTC as a TDateTime...
GetSystemTime(SystemTime);
with SystemTime do
NowUTC := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
// now calculate the difference in seconds...
Result := SecondsBetween(NowUTC, EpochUTC);
end;
Alternatively, you can use the DateUtils.DateTimeToUnix() function:
uses
..., Windows, DateUtils;
function CTime: Int64;
var
SystemTime: TSystemTime;
NowUTC: TDateTime;
begin
// get current time in UTC as a TDateTime...
GetSystemTime(SystemTime);
with SystemTime do
NowUTC := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
// now calculate the difference from Jan 1 1970 UTC in seconds...
Result := DateTimeToUnix(NowUTC);
end;
Either way, you can then do this:
var
Play : string;
Par : string;
begin
Play := 'myfile.exe';
Par := IntToStr(CTime());
ShellExecute(TForm(Owner).Handle, nil, PChar(Play), PChar(Par), nil, SW_SHOWNORMAL);
end;
Or, using CreateProcess() instead, similar to what the C code is doing:
var
startCommand : string;
hProcess: THandle;
si: TStartupInfo;
pi: TProcessInformation;
begin
startCommand := Format('%s %d', ['myfile.exe', CTime()]);
...
ZeroMemory(@si, sizeof(si));
si.cb := sizeof(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_SHOWNORMAL;
if CreateProcess(nil, PChar(startCommand), nil, nil, False, 0, nil, nil, si, pi) then
begin
hProcess := pi.hProcess;
...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
end;
...
end;
time(NULL) returns time elapsed in seconds since 01/01/1970 UTC (Unix time)
You can convert TDateTime into needed format using DateUtils.DateTimeToUnix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With