프로그래밍 초보 탈출

Libraries/Delphi Library

[System] Resource Library

째즈토끼 2022. 6. 24. 00:26
unit LibResourceControl;

interface

uses
  Windows, SysUtils;

type
  PFileVersionResourceData = ^TFileVersionResourceData;
  TFileVersionResourceData = record
	ResourceHandle : HRSRC;
	GlobalHandle : HGLOBAL;
	LockedPointer : Pointer;
	FixedInfo : PVSFixedFileInfo;
	Buffer : Pointer;
	ResourceSize : DWORD;
	end;

function OpenFileVersionResource(hInstance : LongWord; V : PFileVersionResourceData) : BOOL; stdcall;
procedure CloseFileVersionResource(V : PFileVersionResourceData); stdcall;

procedure GetVersionInfo_FileVersion(hInstance : LongWord; var MS, LS : DWORD); stdcall; overload;
procedure GetVersionInfo_FileVersion(const FileName : String; var MS, LS : DWORD); stdcall; overload;
function  GetVersionInfo_FileVersion(hInstance : LongWord) : String; stdcall; overload;
function  GetVersionInfo_FileVersion(const FileName : String) : String; stdcall; overload;
{$WARN SYMBOL_PLATFORM OFF}

function OpenFileVersionResource(hInstance : LongWord; V : PFileVersionResourceData) : BOOL; stdcall;
var
  Ln : DWORD;
begin
  RESULT := False;
  V^.Buffer := nil;

  V^.ResourceHandle := FindResource(hInstance, MakeIntResource(1), RT_VERSION);
  if (V^.ResourceHandle = 0) then Exit;

  V^.GlobalHandle := LoadResource(hInstance, V^.ResourceHandle);
  if (V^.GlobalHandle = 0) then Exit;
  V^.ResourceSize := SizeofResource(hInstance, V^.ResourceHandle);

  V^.LockedPointer := LockResource(V^.GlobalHandle);
  GetMem(V^.Buffer, V^.ResourceSize);
  CopyMemory(V^.Buffer, V^.LockedPointer, V^.ResourceSize);

  RESULT := VerQueryValue(V^.Buffer, '\', Pointer(V^.FixedInfo), Ln);

  if not RESULT then CloseFileVersionResource(V);
end;

procedure CloseFileVersionResource(V : PFileVersionResourceData); stdcall;
begin
  if (V^.Buffer <> nil) then begin
     FreeMem(V^.Buffer);
     V^.Buffer := nil;
     end;
end;
procedure GetVersionInfo_FileVersion(hInstance : LongWord; var MS, LS : DWORD); stdcall; overload;
var
  DV : TFileVersionResourceData;
begin
  if OpenFileVersionResource(hInstance, @DV) then begin
     MS := DV.FixedInfo.dwFileVersionMS;
     LS := DV.FixedInfo.dwFileVersionLS;
     CloseFileVersionResource(@DV)
     end
  else begin
     MS := 0;
     LS := 0;
     end;
end;

procedure GetVersionInfo_FileVersion(const FileName : String; var MS, LS : DWORD); stdcall; overload;

var
  Handle : THandle;
  Size, Len : DWORD;
  Data : Pointer;
  FI : PVSFixedFileInfo;
begin
  MS := 0;
  LS := 0;
  Size := GetFileVersionInfoSize(PChar(FileName), Handle);
  if Size = 0 then Exit;
  GetMem(Data, Size);
  if GetFileVersionInfo(PChar(FileName), Handle, Size, Data) then begin
     if VerQueryValue(Data, '\', Pointer(FI), Len) then begin
	MS := FI^.dwFileVersionMS;
	LS := FI^.dwFileVersionLS;
	end
     end;
  FreeMem(Data);
end;

function GetVersionInfo_FileVersion(hInstance : LongWord) : String; stdcall;
var
  MS, LS : DWORD;
begin
  GetVersionInfo_FileVersion(hInstance, MS, LS);
  RESULT := Format('%d.%d.%d.%d', [ HIWORD(MS), LOWORD(MS),
				    HIWORD(LS), LOWORD(LS) ]);
end;

function GetVersionInfo_FileVersion(const FileName : String) : String; stdcall;
var
  MS, LS : DWORD;
begin
  GetVersionInfo_FileVersion(FileName, MS, LS);
  RESULT := Format('%d.%d.%d.%d', [ HIWORD(MS), LOWORD(MS),
				    HIWORD(LS), LOWORD(LS) ]);
end;

end.

'Libraries > Delphi Library' 카테고리의 다른 글

[CRC] CRC-8 Library  (0) 2022.06.24
[Parser] Simple Text Parser  (0) 2022.06.24
[System] Registry Library 2  (0) 2022.06.24
[String] String Library 2  (0) 2022.06.24
[String] Wide String Library  (0) 2022.06.24