whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

34 Visitors Online


 
...fetch the portable executable's checksum using ImageHelp?
Autor: Elias Bachaalany
[ Print tip ]  

Tip Rating (24):  
     


program exeChecksum;

uses Windows, ImageHlp;


{$APPTYPE CONSOLE}

function ComputePEChecksum(FileName: string): DWORD;
var
  
h, hMap: Cardinal;
  pMem: Pointer;
  headersum, checksum, fsizehigh, fsizelow: DWORD;
  nth: PImageNtHeaders;
Label
  
cleanup;
begin
  
pMem := nil;

  Result := 0;

  headersum := 0;
  checksum  := 0;

  h := Windows.CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ,
    nil, OPEN_EXISTING, 0, 0);
  if (h = INVALID_HANDLE_VALUE) then
    
Exit;

  fsizelow := Windows.GetFileSize(h, Pointer(@fsizehigh));

  hMap := Windows.CreateFileMapping(h, nil, PAGE_READONLY, fsizeHigh, fsizeLow, nil);
  if (hMap = 0) then
    goto 
cleanup;

  pMem := Windows.MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
  if (pMem = nilthen
    goto 
cleanup;

  nth := CheckSumMappedFile(pMem, fsizeLow, @headersum, @checksum);

  if (nth = nilthen
    
checksum := 0;

  cleanup:
  if (pMem <> nilthen
    
Windows.UnmapViewOfFile(pMem);
  if (hMap <> 0) then
    
Windows.CloseHandle(hMap);
  if (h <> 0) then
    
Windows.CloseHandle(h);

  Result := checksum;
end;

var
  
x1, x2: DWORD;
begin
  
x1 := ComputePEChecksum('c:\1.exe'); // original filename
  
x2 := ComputePEChecksum('c:\2.exe');
  // original filename but has a string in it lightly modified

  
WriteLn('Checksum 1: ', x1, #13#10'Checksum 2: ', x2);
end.


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners