delphi – 为什么这段代码会产生异常?
发布时间:2020-12-15 09:39:53 所属栏目:大数据 来源:网络整理
导读:我今天写了一些代码,列出了PE文件中的所有部分…代码可以工作,但最后它给出了一个例外:无效的指针操作……我不知道为什么……有人可以找到错误 这是代码 procedure TForm1.Button1Click(Sender: TObject);var IDH:PImageDosHeader; buf:Pointer; INH:PImage
我今天写了一些代码,列出了PE文件中的所有部分…代码可以工作,但最后它给出了一个例外:无效的指针操作……我不知道为什么……有人可以找到错误
这是代码 procedure TForm1.Button1Click(Sender: TObject); var IDH:PImageDosHeader; buf:Pointer; INH:PImageNtHeaders; ISH:array of TImageSectionHeader; FS:TFileStream; i,total:Word; begin if OpenDialog1.Execute then begin Self.Caption:=OpenDialog1.FileName; FS:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead or fmShareDenyNone); GetMem(buf,FS.Size); FS.Read(buf^,FS.Size); FS.Free; IDH:=buf; INH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew)); ISH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew) + Sizeof(TImageNtHeaders)); total:=INH^.FileHeader.NumberOfSections - 1 ; for i:=0 to total do begin ListBox1.Items.Add(PAnsichar(@ISH[i].Name)); Application.ProcessMessages; end; end; end; 解决方法ISH:array of TImageSectionHeader; 这声明了一个动态数组.虽然动态数组是指针,但它们需要在指向的数据前面添加额外的数据,包括长度和引用计数. 因此,指向PE头中的某些数据是没有意义的: ISH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew) + Sizeof(TImageNtHeaders)); 虽然这部分似乎由于某种原因编译,但访问该数组可能会出现错误: ISH[i]//This might not work correctly since ISH does not point to a valid dynamic array. 或者如果代码幸存下来那部分(也许你已经禁用了数组边界检查或者长度信息足够大)那么delphi一旦数组超出范围,delphi就会尝试减少refcount并可能释放数组.并且该部分访问数组指向的数据前面的引用信息,这在您的情况下是无效的. 如果我没记错的话,动态数组的内存布局与此类似: -------------------------- |refcount|length|data....| -------------------------- ^ pointer goes here 这意味着你会遇到问题,因为refcount / length字段包含垃圾. 我想你想宣布它为: type TImageSectionHeaderArray=array[0..70000]TImageSectionHeader;//No idea what the limit on section headers is PImageSectionHeaderArray=^TImageSectionHeaderArray; ... var ISH:PImageSectionHeaderArray; (我的delphi有点生疏,所以可能会有一些小的语法错误) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |