Search code examples
inno-setuppascalscriptmediainfo

Get Image File Information using MediaInfo library in Inno Setup Pascal Script


I have been trying for more than two days to get JPEG Image and MP4 Video File Information using MediaInfo.DLL in my Pascal Script.

But I keep getting error

Runtime Error (at 6:366) - Access Violation at address 0042FD23. Read of address 8065241E.'

The error mostly points to (at 6:366).

I can't think what problem is causing this exception when trying to get Media Information using MediaInfo.DLL.

The code I added to my Script:

[Files]
Source: Lamborghini_Aventador.jpg; DestDir: {tmp}; Flags: dontcopy
Source: MediaInfo.dll; DestDir: {tmp}; Flags: dontcopy

[Code]
#ifdef UNICODE
type
  PWideChar = WideString;
#endif

const
  StreamKind_Image = 5;
  InfoKind_Text = 1;

function MediaInfo_New: Cardinal;
  external 'MediaInfo_New@{tmp}\MediaInfo.dll stdcall delayload';
function MediaInfo_Open(Handle: Cardinal; File__: PWideChar): Boolean;
  external 'MediaInfo_Open@{tmp}\MediaInfo.dll stdcall delayload';
function MediaInfo_Get(Handle: Cardinal; StreamKind: Integer; StreamNumber: Integer; Parameter: PWideChar; KindOfInfo: Integer; KindOfSearch: Integer): PWideChar;
  external 'MediaInfo_Get@{tmp}\MediaInfo.dll stdcall delayload';

procedure RetrieveImageInformation;
var
  IHandle: Cardinal;
  Width: PWideChar;
begin
  ExtractTemporaryFile('Lamborghini_Aventador.jpg');
  ExtractTemporaryFile('MediaInfo.dll');
  IHandle := MediaInfo_New();
  MediaInfo_Open(IHandle, PWideChar(ExpandConstant('{tmp}\Lamborghini_Aventador.jpg')));
  Width := MediaInfo_Get(IHandle, StreamKind_Image, 0, 'Width', InfoKind_Text, 0);
  Log('Width of the JPEG Image: ' + PWideChar(Width) + '.');
end;

The line which the exception is generating is:

Width := MediaInfo_Get(IHandle, StreamKind_Image, 0, 'Width', InfoKind_Text, 0);

I expected that the compiler output will be Width of the JPEG Image: 1920.

I use latest version of Unicode Inno Setup Compiler (5.5.9 - U)

Thanks in advance for your important help.


Solution

  • You can use the MediaInfo Command Line Interface with Inno Setup Ansi or Unicode Version.

    Usage Example:

    [Files]
    Source: MediaInfo.exe; DestDir: {tmp}; Flags: Dontcopy
    
    [code]
    function InitializeSetup(): Boolean;
    var
       ErrorCode: Integer;
    begin
       ExtractTemporaryFile('MediaInfo.exe');
       ShellExec('Open', 'MediaInfo.exe', ExpandConstant('"YourFileName.mp4" --LogFile="YourFileName Prperties.log"'), ExpandConstant('{tmp}'), SW_HIDE, ewWaitUntilTerminated, ErrorCode);
       if SysErrorMessage(DLLGetLastError) = SysErrorMessage(0) then
       Result := True;  
    end;
    

    Now, navigate to the Inno Setup Temporary Directory as Administrator using Run (Windows Key + R) Command and see your Media Information Log File exists there which contains the Information about the File you given in the Command.