Delphi自定义释放资源过程

program Project2;

uses
  Windows;

{$R mydll.RES}

function ExtractRes( ResType , ResName , OutName : string) : Boolean;
var
  HResInfo : THandle;
  HGlobal : THandle;
  HFile : THandle;
  Ptr : Pointer;
  Size , N : Integer;
begin
  HFile := INVALID_HANDLE_VALUE;
  repeat
  Result := False;
  HResInfo := FindResource( HInstance , PChar( ResName) , PChar( ResType));
  if HResInfo = 0 then Break;
  HGlobal := LoadResource( HInstance , HResInfo);
  if HGlobal = 0 then Break;
  Ptr := LockResource( HGlobal);
  Size := SizeOfResource( HInstance , HResInfo);
  if Ptr = nil then Break;
  HFile := CreateFile( PChar( OutName) , GENERIC_READ or GENERIC_WRITE , 0 , nil , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , 0);
  if HFile = INVALID_HANDLE_VALUE then Break;
  if WriteFile( HFile , Ptr ^, Size , LongWord(N) , nil) then Result := True;
until True;
  if HFile <> INVALID_HANDLE_VALUE then CloseHandle( HFile);
end;

begin
  ExtractRes( 'dll' , 'mydll' , 'C:/123.dll' );   //资源类型 资源名 输出文件名
end .

你可能感兴趣的:(Delphi自定义释放资源过程)