程序会停在“FileText.Text := StrPas(Buf);”procedure TfrmPEView.LoadFileToText;
var
  Buf: PChar;
  MS: TMemoryStream;
begin
  try
    MS := TMemoryStream.Create;
    MS.LoadFromFile('abc.exe');
    MS.Position := 0;    Buf := AllocMem(MS.Size + 1);
    CopyMemory(Buf,MS.Memory,MS.Size);    for i := 0 to MS.Size - 1 do
      begin
        if Buf[i] = #0 then
          Buf[i] := #20;      end;
      
    Buf[MS.Size] := char(0);    FileText.Lines.Clear;
    FileText.Text := StrPas(Buf);
  finally
    FreeMem(Buf);
    MS.Destroy;
  end;
end;

解决方案 »

  1.   

    StrPas是将PChar转换为String;
    你直接写
    FileText.Text := Buf;procedure TForm1.Button1Click(Sender: TObject); //测试
    var
      str : PChar ;
    begin
      str := 'Delphi';
      //ShowMessage(str);
      Edit1.Text := str ;
    end;
      

  2.   

    var
      tmpStr: string;
      ms: TMemoryStream;
    begin
      ms := TMemoryStream.Create;
      ms.LoadFromFile('abc.exe');
      ms.Position := 0;
      SetLength(tmpStr,ms.siZe);
      ms.Read(tmpStr[1],ms.Size);
      FileText.Text := tmpStr;
      ms.free;
    end;顺手写的,你试一下
      

  3.   

    procedure TfrmPEView.LoadFileToText;
    var
      Buf: PChar;
      Data: String;
      MS: TMemoryStream;
    begin
      try
        MS := TMemoryStream.Create;
        MS.LoadFromFile('abc.exe');
        Buf := MS.Memory;
        SetString(Data,Buf,MS.Size);
        FileText.Lines.Clear;
        FileText.Text := Data;
      finally
        MS.Free;
      end;
    end;
      

  4.   

    结果一样的,程序到“FileText.Text := Data;”的时候就没反应了,窗口也没出现,程序也没退出
      

  5.   

    文件有多少大?另外,开始的时候,我都没有注意:
    procedure TfrmPEView.LoadFileToText;
    var
      Buf: PChar;
      Data: String;
      MS: TMemoryStream;
    begin
      try
        MS := TMemoryStream.Create;
        MS.LoadFromFile('abc.exe');
        Buf := MS.Memory;
        SetString(Data,Buf,MS.Size);
        FileText.Lines.Clear;
        FileText.Text := Data;
      finally
        MS.Free;//由于MS是在try里面Create的,假设是在Create的过程当中异常,即MS = Nil,那么这一句就会出现问题
      end;
    end;
    正确的是:procedure TfrmPEView.LoadFileToText;
    var
      Buf: PChar;
      Data: String;
      MS: TMemoryStream;
    begin
      MS := TMemoryStream.Create;
      try
        //MS := TMemoryStream.Create;    MS.LoadFromFile('abc.exe');
        Buf := MS.Memory;
        SetString(Data,Buf,MS.Size);
        FileText.Lines.Clear;
        FileText.Text := Data;
      finally
        MS.Free;
      end;
    end;
      

  6.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TfrmPEView = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        ComboBox1: TComboBox;
        procedure LoadFileToText;
        procedure Button1Click(Sender: TObject);end;var
      frmPEView: TfrmPEView;implementation{$R *.dfm}procedure TfrmPEView.LoadFileToText;
    var
      Buf: array[0..$800] of char;
      F: TFileStream;
      Size: int64;
      readbytes, i: integer;
      str: string;
    begin
      try
        F:=TFileStream.Create(ExtractFilePath(Application.ExeName)+'abc.exe', fmOpenRead);
        Size:=F.Size;
        F.Position := 0;
        Memo1.Lines.Clear;
        while F.Position<Size do
        begin
            readbytes:=F.Read(buf,$800);
            if readbytes>0 then
            begin
                     for i := 0 to readbytes - 1 do
                     begin
                          if Buf[i] = #0 then Buf[i] := #20;
                          if ComboBox1.ItemIndex=1 then
                          str:=str+inttohex(ord(buf[i]),2)+' ' else
                          str:=str+buf[i];
                     end;
                     Memo1.Lines.Add(str);
            end;
        end;
       Finally
        F.Free;
       end;
    end;procedure TfrmPEView.Button1Click(Sender: TObject);
    begin
         LoadFileToText;
    end;end.
      

  7.   


    object frmPEView: TfrmPEView
      Left = 192
      Top = 107
      Width = 697
      Height = 456
      Caption = 'frmPEView'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Memo1: TMemo
        Left = 72
        Top = 56
        Width = 521
        Height = 241
        TabOrder = 0
      end
      object Button1: TButton
        Left = 360
        Top = 328
        Width = 75
        Height = 25
        Caption = #35835#21462
        TabOrder = 1
        OnClick = Button1Click
      end
      object ComboBox1: TComboBox
        Left = 176
        Top = 328
        Width = 145
        Height = 21
        ItemHeight = 13
        ItemIndex = 0
        TabOrder = 2
        Text = 'char'#23383#31526
        Items.Strings = (
          'char'#23383#31526
          'ASCII'#30721)
      end
    end
      

  8.   

    7楼   代码保存成   Unit1.pas
    8楼                Unit1.dfm
    ---------------
    program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {frmPEView};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TfrmPEView, frmPEView);
      Application.Run;
    end.
    --------------------
      

  9.   

    文件太大的缘故,导致text赋值时进行的操作比较久。代码本身问题不大。可以拿个几十K的程序试一下就知道了。