home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / CHFLZ100.ZIP / LZEXPLIC.PAS < prev    next >
Pascal/Delphi Source File  |  1996-09-05  |  12KB  |  342 lines

  1.  
  2. {$I LZDefine.inc}
  3.  
  4. unit LZExplic;
  5. { sample "explicit" or "dynamic" import unit for ChiefLZ.dll ... }
  6.  
  7. interface
  8. uses
  9. {$ifdef Win32}
  10. Windows,
  11. SysUtils,
  12. LZ_Link,
  13. {$else Win32}
  14. {$ifdef dpmi}
  15. WinAPI,
  16. {$else dpmi}
  17. WinTypes,
  18. WinProcs,
  19. {$endif dpmi}
  20. {$ifdef Delphi}
  21. Messages,
  22. SysUtils,
  23. {$else Delphi}
  24. {$ifndef dpmi}
  25. Win31,
  26. {$endif dpmi}
  27. Strings,
  28. {$endif Delphi}
  29. {$endif Win32}
  30. ChfTypes,
  31. ChfUtils;
  32.  
  33. {exported functions}
  34.  
  35. VAR IsChiefLZArchive: Function(const fName: {$ifdef Win32} string
  36.                                             {$else}        PChar
  37.                                             {$endif} ): boolean;
  38. {$ifdef Win32} stdcall; {$endif Win32}
  39.  
  40. {$ifdef Win32}
  41. VAR GetChiefLZFileName: Function(const fName: string): string;
  42.   stdcall;
  43. {$else}
  44. VAR GetChiefLZFileName: Function(fName, Dest:PChar): boolean;
  45. {$endif Win32}
  46.  
  47. VAR GetChiefLZFileSize: Function(fName: {$ifdef Win32} string
  48.                                         {$else}        PChar
  49.                                         {$endif}): LongInt;
  50. {$ifdef Win32} stdcall; {$endif Win32}
  51.  
  52. VAR GetChiefLZArchiveInfo: Function(const ArchName: {$ifdef Win32} string;
  53.                                                     {$else Win32}  PChar;
  54.                                                     {$endif Win32}
  55.                                     var   Header: TChiefLZArchiveHeader): boolean;
  56. {$ifdef Win32} stdcall; {$endif Win32}
  57.  
  58. VAR GetChiefLZArchiveSize: Function(const ArchName: {$ifdef Win32} string
  59.                                                     {$else Win32}  PChar
  60.                                                     {$endif Win32}): LongInt;
  61. {$ifdef Win32} stdcall; {$endif Win32}
  62.  
  63. VAR LZCompress: Function(const {$ifdef Win32} Source, Dest:   string
  64.                                {$else}        aSource, aDest: pChar
  65.                                {$endif};
  66.                          LZQuestion: TLZQuestionFunc;
  67.                          aProc:      TLZReportProc): LongInt;
  68. {$ifdef Win32} stdcall; {$endif Win32}
  69.  
  70. VAR LZDecompress: Function({$ifdef Win32} Source, Dest:  string
  71.                            {$else} const aSource, aDest: PChar
  72.                            {$endif};
  73.                            LZQuestion: TLZQuestionFunc;
  74.                            aProc:      TLZReportProc): LongInt;
  75. {$ifdef Win32} stdcall; {$endif Win32}
  76.  
  77.  
  78. VAR IsChiefLZFile: Function(const fName: {$ifdef Win32} string
  79.                                          {$else}        PChar
  80.                                          {$endif}): boolean;
  81. {$ifdef Win32} stdcall; {$endif Win32}
  82.  
  83.  
  84. VAR LZArchive: Function(const fSpec, ArchName: {$ifdef Win32} string
  85.                                                {$else}        PChar
  86.                                                {$endif};
  87.                         LZRecurseDirs: TLZRecurse;
  88.                         aProc:         TLZReportProc): LongInt;
  89. {$ifdef Win32} stdcall; {$endif Win32}
  90.  
  91.  
  92. VAR LZDearchive: Function(ArchName: {$ifdef Win32} string
  93.                                     {$else}        PChar
  94.                                     {$endif};
  95.                                     {$ifdef Win32} DefDir: string
  96.                                     {$else} const aDefDir: PChar
  97.                                     {$endif};
  98.                           LZQuestion: TLZQuestionFunc;
  99.                           aProc:      TLZReportProc;
  100.                           aRename:    TLZRenameFunc): LongInt;
  101. {$ifdef Win32} stdcall; {$endif Win32}
  102.  
  103.  
  104. VAR LZCompressEx: Function(const {$ifdef Win32} Name:  string
  105.                                  {$else}        aName: PChar
  106.                                  {$endif};
  107.                            ReplaceQuestion: TLZQuestionFunc;
  108.                            aProc:           TLZReportProc): LongInt;
  109. {$ifdef Win32} stdcall; {$endif Win32}
  110.  
  111.  
  112. VAR LZDecompressEx: Function({$ifdef Win32} Name:  string
  113.                              {$else}        aName: PChar
  114.                              {$endif};
  115.                              ReplaceQuestion: TLZQuestionFunc;
  116.                              aProc:           TLZReportProc): LongInt;
  117. {$ifdef Win32} stdcall; {$endif Win32}
  118.  
  119.  
  120. VAR GetLZMarkerChar: Function: Char; {$ifdef Win32} stdcall; {$endif}
  121.  
  122. VAR SetLZMarkerChar: Procedure(const NewChar: Char);
  123.                                          {$ifdef Win32} stdcall; {$endif}
  124.  
  125. VAR ChiefLZDLLVersion: Function: Integer; {$ifdef Win32} stdcall; {$endif}
  126.  
  127. Var GetFullLZName: Function(Const X: TChiefLZArchiveHeader;
  128.                               Index: Integer): String;
  129. {$ifdef Win32} stdcall; {$endif}
  130.  
  131.  
  132. Function LoadChiefLZDLL(DLLName: PChar): {$ifdef Win32} BOOL {$else} Integer {$endif};
  133. {load the ChiefLZ DLL - Win16 returns 0 if successful,
  134.                         and something else if not successful
  135.                       - Win32 returns True if successful and False otherwise
  136.                         If False, the error code is obtained by calling
  137.                         GetLastError()
  138.                       - you can supply a new DLL name to use instead of
  139.                         the default "ChiefLZ.DLL".
  140. Call this first, before doing anything}
  141.  
  142. Function UnloadChiefLZDLL: {$ifdef Win32} BOOL {$else} Boolean {$endif};
  143. {unload the DLL - call this last, after doing the LZ functions}
  144.  
  145. Function GetChiefLZDLLHandle: THandle;
  146. {return the value of the ChiefLZDLLHandle}
  147.  
  148. implementation
  149.  
  150. {$ifdef Win32}
  151. Var
  152. {$else Win32}
  153. Const
  154. {$endif Win32}
  155. ChiefLZDLLHandle: THandle = 0;
  156.  
  157. {$ifdef Win32}
  158. Var
  159. {$else Win32}
  160. Const
  161. {$endif Win32}
  162. ChiefLZDLLDefaultName: PChar = 'ChiefLZ.DLL';  {DLL Name}
  163. { This is the default in the DLL "load" function; we will use this pointer
  164.   if no other is passed in LoadChiefLZDLL() }
  165.  
  166. {/////////////////////////////////////////////}
  167. {$ifdef Win32}
  168. function LoadChiefLZProc(var   FuncAddr:  Pointer;
  169.                          const FuncIndex: Integer): boolean;
  170. var
  171.   LocalAddr: Pointer;
  172. begin
  173.   LocalAddr := GetProcAddress(ChiefLZDLLHandle, Ptr(FuncIndex));
  174.   Result := LocalAddr <> nil;
  175.   if Result then
  176.     FuncAddr := LocalAddr
  177. end;
  178. {$endif Win32}
  179. {/////////////////////////////////////////////}
  180. {/////////////////////////////////////////////}
  181. Function LoadChiefLZDLL(DLLName: PChar): {$ifdef Win32} BOOL {$else} Integer {$endif};
  182. {$ifndef DPMI}
  183. Var
  184.   OldWError: THandle;
  185. {$endif DPMI}
  186. Begin
  187.  
  188. {see if we supplied another DLL name; use default if we didn't ...}
  189.   if StrLen(DLLName) = 0 then
  190.     DLLName := ChiefLZDLLDefaultName;
  191.  
  192.  {$ifdef Win32}
  193. {
  194.   First check that a DLL hasn't already been loaded ... There is no
  195.   guarantee that the existing DLL is the same as DLLName, and loading
  196.   library DLLName will destroy ChiefLZDLLHandle ...
  197. }
  198.   if ChiefLZDLLHandle <> 0 then
  199.     RaiseError(EChiefLZDLL,SDLLReadyLoaded);
  200. {
  201.   Load DLL, putting Windows handle in ChiefLZDLLHandle ...
  202. }
  203.   OldWError := SetErrorMode(sem_NoOpenFileErrorBox);
  204.   ChiefLZDLLHandle := LoadLibrary(DLLName);
  205.   SetErrorMode(OldWError);
  206.   if ChiefLZDLLHandle = 0 then
  207.     Result := False  // Load failed
  208.   else
  209. {
  210.   Loaded successfully: put the addresses of the DLL functions into
  211.   procedural and functional variables ...
  212. }
  213.     begin
  214.       Result := LoadChiefLZProc(@LZCompress, 1) and
  215.                 LoadChiefLZProc(@LZDecompress, 2) and
  216.                 LoadChiefLZProc(@IsChiefLZFile, 3) and
  217.                 LoadChiefLZProc(@LZArchive, 4) and
  218.                 LoadChiefLZProc(@LZDearchive, 5) and
  219.  
  220.                 LoadChiefLZProc(@IsChiefLZArchive, 6) and
  221.                 LoadChiefLZProc(@GetChiefLZFileName, 7) and
  222.                 LoadChiefLZProc(@GetChiefLZFileSize, 8) and
  223.                 LoadChiefLZProc(@GetChiefLZArchiveInfo, 9) and
  224.  
  225.                 LoadChiefLZProc(@LZCompressEx, 10) and
  226.                 LoadChiefLZProc(@LZDecompressEx, 11) and
  227.  
  228.                 LoadChiefLZProc(@GetLZMarkerChar, 12) and
  229.                 LoadChiefLZProc(@SetLZMarkerChar, 13) and
  230.  
  231.                 LoadChiefLZProc(@GetFullLZName, 14) and
  232.                 LoadChiefLZProc(@ChiefLZDLLVersion, 15) and
  233.                 LoadChiefLZProc(@GetChiefLZArchiveSize, 16);
  234.       if not Result then
  235.         UnloadChiefLZDLL   // At least one load failed: backtrack out...
  236.     end
  237.  
  238.   {$else Win32}
  239.  
  240.    LoadChiefLZDLL := -1; {DLL can't be loaded}
  241.    {$ifndef DPMI}
  242.    OldWError := SetErrorMode(sem_NoOpenFileErrorBox);
  243.    {$endif DPMI}
  244.  
  245.    ChiefLZDLLHandle := LoadLibrary(ChiefLZDLLDefaultName);
  246.  
  247.    {$ifndef DPMI}
  248.    SetErrorMode(OldWError);
  249.    {$endif DPMI}
  250.    If ChiefLZDLLHandle <= 32 then Exit;
  251.  
  252.    LoadChiefLZDLL := -2; {undefined dyna-link}
  253.    @LZCompress        := GetProcAddress(ChiefLZDLLHandle, 'LZCompress');
  254.    @LZDecompress      := GetProcAddress(ChiefLZDLLHandle, 'LZDecompress');
  255.    @IsChiefLZFile     := GetProcAddress(ChiefLZDLLHandle, 'IsChiefLZFile');
  256.    @LZArchive         := GetProcAddress(ChiefLZDLLHandle, 'LZArchive');
  257.    @LZDeArchive       := GetProcAddress(ChiefLZDLLHandle, 'LZDearchive');
  258.  
  259.    @IsChiefLZArchive  := GetProcAddress(ChiefLZDLLHandle, 'IsChiefLZArchive');
  260.    @GetChiefLZFileName:= GetProcAddress(ChiefLZDLLHandle, 'GetChiefLZFileName');
  261.    @GetChiefLZFileSize:= GetProcAddress(ChiefLZDLLHandle, 'GetChiefLZFileSize');
  262.    @GetChiefLZArchiveInfo:= GetProcAddress(ChiefLZDLLHandle, 'GetChiefLZArchiveInfo');
  263.    @LZCompressEx         := GetProcAddress(ChiefLZDLLHandle, 'LZCompressEx');
  264.    @LZDeCompressEx       := GetProcAddress(ChiefLZDLLHandle, 'LZDecompressEx');
  265.    @GetLZMarkerChar      := GetProcAddress(ChiefLZDLLHandle, 'GetLZMarkerChar');
  266.    @SetLZMarkerChar      := GetProcAddress(ChiefLZDLLHandle, 'SetLZMarkerChar');
  267.    @GetFullLZName        := GetProcAddress(ChiefLZDLLHandle, 'GetFullLZName');
  268.    @ChiefLZDLLVersion    := GetProcAddress(ChiefLZDLLHandle, 'ChiefLZDLLVersion');
  269.    @GetChiefLZArchiveSize := GetProcAddress(ChiefLZDLLHandle, 'GetChiefLZArchiveSize');
  270.  
  271.    If (@LZCompress = Nil)
  272.    or (@LZDecompress = Nil)
  273.    or (@IsChiefLZFile = Nil)
  274.    or (@LZArchive = Nil)
  275.    or (@LZDeArchive = Nil)
  276.    or (@IsChiefLZArchive = Nil)
  277.    or (@GetChiefLZFileName = Nil)
  278.    or (@GetChiefLZFileSize = Nil)
  279.    or (@GetChiefLZArchiveInfo = Nil)
  280.    or (@LZCompressEx = Nil)
  281.    or (@LZDeCompressEx = Nil)
  282.    or (@GetLZMarkerChar = Nil)
  283.    or (@SetLZMarkerChar = Nil)
  284.    or (@GetFullLZName = Nil)
  285.    or (@ChiefLZDLLVersion = Nil)
  286.    or (@GetChiefLZArchiveSize = Nil)
  287.    then begin
  288.       FreeLibrary(ChiefLZDLLHandle);
  289.       ChiefLZDLLHandle := 0;
  290.       Exit;
  291.    end;
  292.  
  293.   LoadChiefLZDLL := 0; {success}
  294.  
  295.   {$endif Win32}
  296. End;
  297. {/////////////////////////////////////////////}
  298. {$ifdef Win32}
  299. Function UnloadChiefLZDLL: BOOL;
  300. Begin
  301. {
  302.   Make sure the DLL has been loaded first ...
  303. }
  304.   if ChiefLZDLLHandle = 0 then
  305.     Result := True    // DLL is (was already) unloaded
  306.   else
  307. {
  308.   WAS loaded: now to unload ...
  309. }
  310.     begin
  311.       Result := FreeLibrary(ChiefLZDLLHandle);
  312.       if Result then
  313.         ChiefLZDLLHandle := 0  // Success
  314.     end
  315. End;
  316. {$else Win32}
  317. Function UnloadChiefLZDLL: Boolean;
  318. Begin
  319.    UnloadChiefLZDLL := False; {invalid handle}
  320.    If ChiefLZDLLHandle > 32
  321.    then begin
  322.       FreeLibrary(ChiefLZDLLHandle);
  323.       UnloadChiefLZDLL := True;
  324.    end;
  325.    ChiefLZDLLHandle := 0;
  326. End;
  327. {$endif Win32}
  328. {/////////////////////////////////////////////}
  329.  
  330. Function GetChiefLZDLLHandle: THandle;
  331. {return the value of the ChiefLZDLLHandle}
  332. Begin
  333.   GetChiefLZDLLHandle := ChiefLZDLLHandle
  334. End;
  335. {/////////////////////////////////////////////}
  336. {/////////////////////////////////////////////}
  337. {/////////////////////////////////////////////}
  338. {/////////////////////////////////////////////}
  339. {/////////////////////////////////////////////}
  340. End.
  341.  
  342.