1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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. |
转载请注明:exchen's blog » Delphi 自定义释放资源过程