2008. november 21., péntek

How to load JPG images from a resource-only DLL


Problem/Question/Abstract:

I stored jpeg's in DLL as resource, and I want to load it from the DLL using Delphi3

Answer:

You can use LoadLibrary( ' yourdllname ' ) or GetModuleHandle( ' yourdllname ' ) for retrieving the handle of your DLL and after that something like:

procedure TForm1.LoadJPGFromDLL(DLLHandle: THandle; ResName, ResType: PChar;
  JPG: TJPEGImage);

  procedure Error;
  begin
    raise Exception.Create('Filed to load resource!');
  end;

var
  ResSize: dword;
  HG, HI: LongInt;
  P: Pointer;
  MS: TMemoryStream;
begin
  HI := FindResource(DLLHandle, ResName, ResType);
  if HI = 0 then
    Error;
  HG := LoadResource(DLLHandle, HI);
  if HG = 0 then
    Error;
  ResSize := SizeOfResource(DLLHandle, HI);
  MS := TMemoryStream.Create;
  try
    P := Pointer(LockResource(HG));
    MS.Write(P^, ResSize);
    MS.Position := 0;
    JPG.LoadFromStream(MS);
    UnlockResource(HG);
  finally
    MS.Free;
    FreeResource(HG);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése