Problem with the "in" operator in Delphi

Hi guys, I have a strange problem and I don’t know where I am doing wrong ...

I have the following code, please look at its end, that where it fails, I commented on this ...

var IDH:PImageDosHeader; INH:PImageNtHeaders; ISH:PImageSectionHeader; buf:Pointer; FS:TFileStream; ep,tmp1,tmp2:DWORD; i:Word; begin if OpenDialog1.Execute then begin FS:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead or fmShareDenyNone); GetMem(buf,FS.size); FS.Read(buf^,FS.Size); FS.Free; IDH:=PImageDosHeader(buf); INH:=PImageNtHeaders(DWORD(buf) + DWORD(IDH^._lfanew)); ep:=INH^.OptionalHeader.AddressOfEntryPoint; for i:=0 to INH^.FileHeader.NumberOfSections - 1 do begin ISH:=PimageSectionHeader(DWORD(INH) + sizeof(TImageNtHeaders) + i * sizeof(TImageSectionHeader)); tmp1:=ISH^.VirtualAddress; tmp2:=ISH^.VirtualAddress + ISH^.Misc.VirtualSize; ShowMessageFmt('%d -> %d .. %d',[ep,tmp1,tmp2]); if ep in [tmp1..tmp2] then ShowMessage('Got it'); //This fails even if ep is in the defined interval. Why? end; end; end; 

Of course I can replace this line

 if (ep>=tmp1) and (ep<=tmp2) 

but I want to know what I'm doing wrong.

+6
source share
1 answer

A set is a collection of values ​​of the same type. This type must be ordinal, and a variable of this type must have no more than 256 possible values. ( Official documentation ) Therefore, a set cannot contain integers, since there are more than 256 possible integers.

You can use the InRange function:

 if InRange(ep, tmp1, tmp2) then 

( uses Math ).

+12
source

Source: https://habr.com/ru/post/895586/


All Articles