Extract correct creation date from avi file - file

I am using Delphi 10 and Windows 10 Home Edition 64-bit. I have a video file called MVI_0640.AVI. The date shown in the File Explorer is 15/04/04 which corresponds to the Media Created Date in the Properties window.
I am using the following code to extract dates.
procedure TForm1.Button1Click(Sender: TObject);
var
ADate: TDateTime;
FlHandle: integer;
MyData: TWin32FindData;
FlTime: TFileTime;
MySysTime: TSystemTime;
begin
{get date using GetCreationTime}
ADate := TFile.GetCreationTime(FlName);
Memo1.Lines.Add('GetCreationTime ' + DateToStr(ADate));
{get date using FileGetDate}
FlHandle := FileOpen(FlName,fmOpenRead);
ADate := FileDateToDateTime(FileGetDate(FlHandle));
FileClose(FlHandle);
Memo1.Lines.Add('FileGetDate ' + DateToStr(ADate));
{get date using FindFirstFile}
FindFirstFile(PChar(FlName), MyData);
FlTime := MyData.ftCreationTime;
FileTimeToSystemTime(FlTime, MySysTime);
ADate := SystemTimeToDateTime(MySysTime);
Memo1.Lines.Add('ftCreationTime ' + DateToStr(ADate));
FlTime := MyData.ftLastAccessTime;
FileTimeToSystemTime(FlTime, MySysTime);
ADate := SystemTimeToDateTime(MySysTime);
Memo1.Lines.Add('ftLastAccessTime ' + DateToStr(ADate));
FlTime := MyData.ftLastWriteTime;
FileTimeToSystemTime(FlTime, MySysTime);
ADate := SystemTimeToDateTime(MySysTime);
Memo1.Lines.Add('ftLastWriteTime ' + DateToStr(ADate));
end;
The result looks like this:
None of the dates reflect the Media Created Date. How can I extract it?
In answer to Tom Brunberg’s comment I attach an extract of the file taken with a hex editor.

The date you are looking for is in a chunk called IDIT. It is referred to e.g. in this document
Structure is simple, (sample data from a file of mine):
chunk id: IDIT // 4 ASCII chars
chunk length: 0000001A // 26 bytes
chunk data: Sun Aug 31 12:15:22 2008/n/0 // date as ascii string
The structure of an AVI file is outlined by Microsoft, as follows, with addition of the location of IDIT chunk if present
RIFF ('AVI '
LIST ('hdrl'
'avih'(<Main AVI Header>)
LIST ('strl'
'strh'(<Stream header>)
'strf'(<Stream format>)
[ 'strd'(<Additional header data>) ]
[ 'strn'(<Stream name>) ]
...
)
... (note, if present, the IDIT chunk appears here)
)
LIST ('movi'
{SubChunk | LIST ('rec '
SubChunk1
SubChunk2
...
)
...
}
...
)
['idx1' (<AVI Index>) ]
)
The above mentioned document also outlines the various structures.
Sample data:
A function to get the date (if present in the file) could be as follows:
// Note! finetuned to search only the main TLIST 'hdrl'
function GetOriginalDate(AviFileName: TFileName; out s: string): boolean;
type
TChunkId = array[0..3] of AnsiChar;
TChunk = record
chid: TChunkId;
size: cardinal;
form: TChunkId;
end;
var
fs: TFileStream;
Root: TChunk;
Chnk: TChunk;
Done: boolean;
Date: ansistring;
endpos: integer;
begin
s := 'not found';
Done := False;
result := False;
fs:= TFileStream.Create(AviFileName, fmOpenRead or fmShareDenyWrite);
try
fs.Read(Root, SizeOf(Root));
if Root.chid <> 'RIFF' then exit;
if Root.form <> 'AVI ' then exit;
fs.Read(Chnk, SizeOf(TChunk)); // main LIST
if Chnk.chid <> 'LIST' then exit;
if Chnk.form <> 'hdrl' then exit;
endpos := fs.Position + Chnk.size;
repeat
fs.Read(Chnk, SizeOf(TChunk));
if Chnk.chid = 'IDIT' then
begin
fs.Seek(-4, TSeekOrigin.soCurrent);
SetLength(Date, Chnk.size);
fs.Read(Date[1], Length(Date));
s := Date;
Done := True;
end
else
fs.Seek(Chnk.size-4, TSeekOrigin.soCurrent);
until Done or (fs.Position > endpos);
finally
fs.Free;
end;
end;
To call it, for example:
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
begin
GetOriginalDate('F:\My Video\2008-08-31\MVI_1279.AVI', s);
Memo1.Lines.Add(s);
end;
And result in Memo1
Sun Aug 31 12:15:22 2008

I found an answer to my question. Probably not very elegant or save but it does it's job. I tested it on more than 100 files and it worked without a problem. Here is my answer:
function TForm1.GetAviMediaCreationDate(AFile: string): TDateTime;
var
FS: TFileStream;
NumOfChar: integer;
i,d: integer;
ABuffer: array of byte;
AStr: string;
DateStr: string;
sdp: integer; //start date position
dn,mn,yn: integer; //used to encode date
begin
sdp := 0;
FS := TFileStream.Create(AFile,fmOpenRead);
NumOfChar := 400;
SetLength(ABuffer,NumOfChar);
FS.Read(Pointer(ABuffer)^, NumOfChar);
{find IDIT}
for i := 0 to NumOfChar-1 do
begin
AStr := Char(ABuffer[i]) +
Char(ABuffer[i+1]) +
Char(ABuffer[i+2]) +
Char(ABuffer[i+3]);
if AStr = 'IDIT' then sdp := i+7;
end;
{extract date}
for d := 1 to 24 do
DateStr := DateStr + Char(ABuffer[sdp+d]);
{assemble TDateTime}
//123456789 123456789 123456789
//Sun Jun 28 10:13:39 2015
dn := StrToInt(Copy(DateStr,9,2));
mn := IndexText(Copy(DateStr,5,3),ShortMonthNames)+1;
yn := StrToInt(Copy(DateStr,21,4));
Result := EncodeDate(yn, mn, dn);
FS.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
ADate: TDateTime;
begin
ADate := GetAviMediaCreationDate(FlName);
Memo1.Lines.Add(DateToStr(ADate));
end;

Related

Geting SQL Server instances names from registry

I have tried to get my SQLServer instances names from the registry by using this code:
type
TRegistryHelper = class helper for TRegistry
public
function ReadMultiSz(const name: string; var Strings: TStrings): boolean;
end;
function TRegistryHelper.ReadMultiSz(const name: string;
var Strings: TStrings): boolean;
var
iSizeInByte: integer;
Buffer: array of WChar;
iWCharsInBuffer: integer;
z: integer;
sString: string;
begin
iSizeInByte := GetDataSize(name);
if iSizeInByte > 0 then begin
SetLength(Buffer, Floor(iSizeInByte / sizeof(WChar)));
iWCharsInBuffer := Floor(ReadBinaryData(name, Buffer[0], iSizeInByte) / sizeof(WChar));
sString := '';
for z := 0 to iWCharsInBuffer do begin
if Buffer[z] <> #0 then begin
sString := sString + Buffer[z];
end else begin
if sString <> '' then begin
Strings.Append(sString);
sString := '';
end;
end;
end;
result := true;
end else begin
result := false;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
cKey = '\SOFTWARE\Microsoft\Microsoft SQL Server';
var
Registry: TRegistry;
MyList: TStrings;
begin
Registry := TRegistry.Create;
Registry.RootKey := HKEY_LOCAL_MACHINE;
if Registry.OpenKeyReadOnly(cKey) then
try
MyList := TStringList.Create();
Registry.ReadMultiSz('InstalledInstances', MyList);
ListBox1.Items.Assign(MyList);
finally
MyList.Free;
end;
Registry.Free;
end;
But I have noted the iSizeInByte = -1 every time, and I failed to get those names by this way.
Also, I have noted that when trying to get those instances from my TADOQuery connection string builder, the component also failed to get those names.
Is there any way to get them?

Extract Values from key and array on JSON begins with '['

Sorry, I searched here for This problem on the site
But the Json I have is unlike any other here on the site Because it begins with '['
and I got tired trying to extract the values
Then how I can Extract Values from key and array on JSON begins with '['
[{"date":"12/11/1990","name":"Delphi7"},{"date":"03/05/2012","name":"Delphi 10.4"}]
Or extract Form This :
[{"User":{"date":"12/11/1990","name":"Delphi7"}},{"User":{"date":"03/05/2012","name":"Delphi 10.4"}}]
In JSON, [] denotes an array. In this case, both examples you have provided represent an array of objects.
If you parse these strings using TJSONObject.ParseJSONValue(), it will return a TJSONValue pointer to a TJSONArray object that is holding TJSONObject elements, eg:
uses
..., System.JSON;
var
JSONStr, DateStr, NameStr: string;
JSONVal: TJSONValue;
JSONArr: TJSONArray;
JSONObj: TJSONObject;
I: Integer;
begin
JSONStr := '[{"date":"12/11/1990","name":"Delphi7"},{"date":"03/05/2012","name":"Delphi 10.4"}]';
JSONVal := TJSONObject.ParseJSONValue(JSONStr);
try
JSONArr := JSONVal as TJSONArray;
for I := 0 to JSONArr.Count-1 do
begin
JSONObj := JSONArr[I] as TJSONObject;
DateStr := JSONObj.GetValue('date').Value;
NameStr := JSONObj.GetValue('name').Value;
...
end;
finally
JSONVal.Free;
end;
end;
uses
..., System.JSON;
var
JSONStr, DateStr, NameStr: string;
JSONVal: TJSONValue;
JSONArr: TJSONArray;
JSONObj, JSONUser: TJSONObject;
I: Integer;
begin
JSONStr := '[{"User":{"date":"12/11/1990","name":"Delphi7"}},{"User":{"date":"03/05/2012","name":"Delphi 10.4"}}]';
JSONVal := TJSONObject.ParseJSONValue(JSONStr);
try
JSONArr := JSONVal as TJSONArray;
for I := 0 to JSONArr.Count-1 do
begin
JSONObj := JSONArr[I] as TJSONObject;
JSONUser := JSONObj.GetValue('User') as TJSONObject;
DateStr := JSONUser.GetValue('date').Value;
NameStr := JSONUser.GetValue('name').Value;
...
end;
finally
JSONVal.Free;
end;
end;

TurboPowern OnGuard create license file with machine modifier

I use TurboPower onGuard for licensing and there is an option to specify and generate the machine specific keys for license validation. With following code I create machine modifier based on machineID
const
Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);
procedure TForm1.FormCreate(Sender: TObject);
var
InfoSet : TEsMachineInfoSet;
MachineID : Longint;
begin
{ initialize the machine information set }
InfoSet := [midUser, midSystem, midNetwork, midDrives];
{ create the machine ID and display in hex }
try
MachineID := CreateMachineID(InfoSet);
Edit1.Text := '$' + BufferToHex(MachineID, SizeOf(MachineID));
except on E:Exception do
ShowMessage(E.Message);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
modifier :LongInt;
key :TKey;
releasecode,unlockcode :TCode;
inexpirydate: TdateTime;
begin
modifier := GenerateMachineModifierPrim ;
ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));
//HexToBuffer(Ckey, key, SizeOf(TKey));
InitRegCode(Ckey, edit1.Text, inExpiryDate, releaseCode);
Edit2.text := BufferToHex(unlockCode, SizeOf(releaseCode));
end;
On the other hand to validate serial number and relase code I use the following code on client side to validate application, I place SerialNumberCode component to implement that.
Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);
procedure TForm1.OgSerialNumberCode1GetKey(Sender: TObject; var Key: TKey);
begin
Key := Ckey;
end;
procedure TForm1.OgSerialNumberCode1GetModifier(Sender: TObject;
var Value: LongInt);
begin
Value := GenerateMachineModifierPrim;
end;
function TForm1.GetSNData(var S: string): integer;
var
TC : TCode;
SNC : string;
modifier :LOngInt;
begin
try
modifier := GenerateMachineModifierPrim;
ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));
IniSNVal := StrToInt(InputBox('Serial number', 'Enter serial Value ', '12345678'));
SNC := (InputBox('release code', 'Enter code ', ''));
{Check that Release Code was entered correctly}
HexToBuffer(SNC, TC, SizeOf(TCode));
if not (IsSerialNumberCodeValid(CKey, TC)) then begin
S := 'Release code not entered correctly';
Result := mrCancel;
end else begin
IniFile := TIniFile.Create(TheDir + 'test.INI');
try
IniFile.WriteInteger('Codes', 'SN', IniSNVal);
IniFile.WriteString('Codes', 'SNCode', SNC);
finally
IniFile.Free;
end;
end;
finally
end;
end;
procedure TForm1.OgSerialNumberCode1GetCode(Sender: TObject; var Code: TCode);
var
S1 : string;
L : integer;
begin
if not (FileExists(TheDir + 'test.INI')) then
Exit;
{open Ini File}
IniFile := TIniFile.Create(TheDir + 'test.INI');
try
{try to read release code}
S1 := IniFile.ReadString('Codes', 'SNCode', '');
IniSNVal := IniFile.ReadInteger('Codes', 'SN', 0);
{convert retrieved string to a code}
HexToBuffer(S1, Code, SizeOf(Code));
finally
IniFile.Free;
end;
end;
procedure TForm1.OgSerialNumberCode1Checked(Sender: TObject; Status: TCodeStatus);
var
S,
C1,
C2 : string;
TC : TCode;
LI : longint;
begin
case Status of
ogValidCode : begin
{check if retrieved Serial Number matches Code}
LI := OgSerialNumberCode1.GetValue;
if (LI <> IniSNVal) then begin
Status := ogInvalidCode;
S := 'The serial number has been changed';
end else begin
ShowMessage('Serial #: ' + IntToStr(IniSNVal));
Exit;
end;
end;
ogInvalidCode : begin
{if INI file doesn't exist, presume this is first run}
if not (FileExists(TheDir + 'test.INI')) then
begin
if not (GetSNData(S) = mrCancel) then begin
{Check the SN/ReleaseCode}
OgSerialNumberCode1.CheckCode(True);
{must Exit since line above began a recursive call}
Exit;
end;
end else
S := 'Invalid Code';
end;
ogCodeExpired : S := 'Evaluation period expired';
end;
ShowMessage(S);
Application.Terminate;
end;
But at the client side machine never accepts the validation and throws Release code not entered correctly. Probably I don't understand usage of machineID exmaple in onguard folder. Please also note that the key for client side and generation for relaease code is same so there shouldn't be any mismatch.

open multiple files via shell context menu as params

I want to select more than one text file in the windows explorer and open the files via context menu in my app. For one file I found the solution but for more files there some ideas but no (working) solutions.
Anyone here that has the answer?
Here is an example that i've just searched and collected from internet.
Aim: Select multiple folders in Windows Explorer and get list of these folders' names via a shell context menu item "SelectedFolders", or using SendTo menu or drag-and-drop folders from shell onto the application form.
Please put a listbox named lstSelectedFolders and a speed button named sbClearList.
The main form name is frmSelectedFolders.
Here we go.
/////////////////////////////////////////////////////////////
program selectedfolders;
uses
Windows, Messages, SysUtils, Forms,
uSelectedFolders in 'uSelectedFolders.pas' {frmSelectedFolders};
{$R *.res}
var
receiver: THandle;
i, result: integer;
s: string;
dataToSend: TCopyDataStruct;
Mutex : THandle;
begin
Mutex := CreateMutex(nil, True, 'SelectedFolders');
if (Mutex <> 0) and (GetLastError = 0) then
begin
Application.Initialize;
Application.Title := 'Selected Folders';
Application.CreateForm(TfrmSelectedFolders, frmSelectedFolders);
Application.Run;
if Mutex <> 0 then CloseHandle(Mutex);
end
else
begin
receiver := FindWindow(PChar('TfrmSelectedFolders'), PChar('Selected Folders'));
if receiver <> 0 then
begin
for i:=1 to ParamCount do
begin
s := trim(ParamStr(i));
if s <> '' then
begin
dataToSend.dwData := 0;
dataToSend.cbData := 1 + Length(s);
dataToSend.lpData := PChar(s);
result := SendMessage(receiver, WM_COPYDATA, Integer(Application.Handle), Integer(#dataToSend));
//sleep(100);
//if result > 0 then
// ShowMessage(Format('Sender side: Receiver has %d items in list!', [result]));
end;
end; // for i
end;
end;
end.
/////////////////////////////////////////////////////////////
unit uSelectedFolders;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, ActiveX, ComObj, ShlObj, Registry, Buttons;
type
TfrmSelectedFolders = class(TForm)
lstSelectedFolders: TListBox;
sbClearList: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure sbClearListClick(Sender: TObject);
private { Private declarations }
public { Public declarations }
procedure WMDROPFILES(var Message: TWMDROPFILES); message WM_DROPFILES;
procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
function GetTarget(const LinkFileName: string): string;
end;
var
frmSelectedFolders: TfrmSelectedFolders;
implementation
{$R *.dfm}
procedure RegisterContextMenuForFolders();
const
Key = 'Directory\shell\SelectedFolders\command\';
begin
with TRegistry.Create do
try
// for all users, class registration for directories
RootKey := HKEY_CLASSES_ROOT;
if OpenKey(Key, true) then
WriteString('', '"' + Application.ExeName + '" "%l"');
finally
Free;
end;
end;
procedure TfrmSelectedFolders.WMDROPFILES(var Message: TWMDROPFILES);
var
N, i: integer;
buffer: array[0..255] of Char;
s: string;
begin
try
N := DragQueryFile(Message.Drop, $FFFFFFFF, nil, 0);
for i:=1 to N do
begin
DragQueryFile(Message.Drop, i-1, #buffer, SizeOf(buffer));
s := buffer;
if UpperCase(ExtractFileExt(s)) = '.LNK' then
s := GetTarget(s);
if lstSelectedFolders.Items.IndexOf(s) < 0 then
lstSelectedFolders.Items.Add(s);
end;
finally
DragFinish(Message.Drop);
end;
end;
function TfrmSelectedFolders.GetTarget(const LinkFileName: string): string;
var
//Link : String;
psl : IShellLink;
ppf : IPersistFile;
WidePath : Array[0..260] of WideChar;
Info : Array[0..MAX_PATH] of Char;
wfs : TWin32FindData;
begin
if UpperCase(ExtractFileExt(LinkFileName)) <> '.LNK' then
begin
Result := 'NOT a shortuct by extension!';
Exit;
end;
CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, psl);
if psl.QueryInterface(IPersistFile, ppf) = 0 Then
Begin
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(LinkFileName), -1, #WidePath, MAX_PATH);
ppf.Load(WidePath, STGM_READ);
psl.GetPath(#info, MAX_PATH, wfs, SLGP_UNCPRIORITY);
Result := info;
end
else
Result := '';
end;
procedure TfrmSelectedFolders.WMCopyData(var Msg: TWMCopyData);
var
s: string;
begin
s := trim(PChar(Msg.copyDataStruct.lpData));
if s = '' then
begin
msg.Result := -1;
exit;
end;
if UpperCase(ExtractFileExt(s)) = '.LNK' then
s := GetTarget(s);
if lstSelectedFolders.Items.IndexOf(s) < 0 then
lstSelectedFolders.Items.Add(s);
msg.Result := lstSelectedFolders.Items.Count;
end;
procedure TfrmSelectedFolders.FormCreate(Sender: TObject);
var
i: integer;
s: string;
begin
RegisterContextMenuForFolders();
DragAcceptFiles(Handle, TRUE);
lstSelectedFolders.Clear;
s := ExtractFileDir(Application.ExeName);
lstSelectedFolders.Items.Add(s);
for i:=1 to ParamCount do
begin
s := trim(ParamStr(i));
if UpperCase(ExtractFileExt(s)) = '.LNK' then
s := GetTarget(s);
if lstSelectedFolders.Items.IndexOf(s) < 0 then
lstSelectedFolders.Items.Add(s);
end;
end;
procedure TfrmSelectedFolders.sbClearListClick(Sender: TObject);
begin
lstSelectedFolders.Clear;
end;
end.

Create multiple text files

what would be a neat way to create multiple *.txt files on application
startup i.e check if they exist if not create them.
I need to create about 10 text files.
Will I have to do like this for every single file:
var
MyFile: textfile;
ApplicationPath: string;
begin
ApplicationPath := ExtractFileDir(Application.ExeName);
if not FileExists(ApplicationPath + '\a1.txt') then
begin
AssignFile(MyFile, (ApplicationPath + '\a1.txt'));
Rewrite(MyFile);
Close(MyFile);
end
else
Abort;
end;
If you only want to create the empty files (or rewrite the existing) with subsequently numbered file names, you might try something like this. The following examples use the CreateFile API function. But pay attention that several things may forbid your file creation attempts!
If you want to create (overwrite) them in all circumstances, use CREATE_ALWAYS disposition flag
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Name: string;
Path: string;
begin
Path := ExtractFilePath(ParamStr(0));
for I := 1 to 10 do
begin
Name := Path + 'a' + IntToStr(I) + '.txt';
CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
end;
end;
Or if you want to create the files only if they doesn't exist, use the CREATE_NEW disposition flag
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Name: string;
Path: string;
begin
Path := ExtractFilePath(ParamStr(0));
for I := 1 to 10 do
begin
Name := Path + 'a' + IntToStr(I) + '.txt';
CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0));
end;
end;
Something like this, perhaps:
var
ApplicationDir: string;
I: Integer;
F: TextFile;
begin
ApplicationDir := ExtractFileDir(Application.ExeName);
for I := 1 to 10 do
begin
Path := ApplicationDir + '\a' + IntToStr(I) + '.txt';
if not FileExists(Path) then
begin
AssignFile(F, Path);
Rewrite(F);
Close(F);
end
end;
procedure CreateFile(Directory: string; FileName: string; Text: string);
var
F: TextFile;
begin
try
AssignFile(F, Directory + '\' + FileName);
{$i-}
Rewrite(F);
{$i+}
if IOResult = 0 then
begin
Writeln(F, Text);
end;
finally
CloseFile(f);
end;
end;
...
for i := 0 to 10 do
CreateFile(Directory, Filename, Text);

Resources