以前用VB的时候会,现在不知道应该用什么函数了。。
大家帮帮忙啦。。谢谢咯。。

解决方案 »

  1.   

    请参考:
      EXE文件加密器的原理:建立两个文件,一个用来添加资源到另外一个EXE文件里面,称为添加程序。另外一个被添加的EXE文件称为头文件。该程序的功能是把添加到自己里面的文件读出来。Windows下的EXE文件结构比较复杂,有的程序还有校验和,当发现自己被改变后会认为自己被病毒感染而拒绝执行。所以我们把文件添加到自己的程序里面,这样就不会改变原来的文件结构了。我们先写一个添加函数,该函数的功能是把一个文件当作一个流添加到另外一个文件的尾部。函数如下:
    function Cjt_AddtoFile(SourceFile, TargetFile: string): Boolean;
    var
      Target, Source: TFileStream;
      MyFileSize: integer;
    begin
      try
        Source := TFileStream.Create(SourceFile, fmOpenRead or fmShareExclusive);
        Target := TFileStream.Create(TargetFile, fmOpenWrite or fmShareExclusive);
        try
          Target.Seek(0, soFromEnd); //往尾部添加资源
          Target.CopyFrom(Source, 0);
          MyFileSize := Source.Size + Sizeof(MyFileSize); //计算资源大小,并写入辅程尾部
          Target.WriteBuffer(MyFileSize, sizeof(MyFileSize));
        finally
          Target.Free;
          Source.Free;
        end;
      except
        Result := False;
        Exit;
      end;
      Result := True;
    end;  有了上面的基础,我们应该很容易看得懂这个函数。其中参数SourceFile是要添加的文件, 参数TargetFile是被添加到的目标文件。比如说把a.exe添加到b.exe里面可以:Cjt_AddtoFile('a.exe', b.exe');如果添加成功就返回True否则返回假。  根据上面的函数我们可以写出相反的读出函数:function Cjt_LoadFromFile(SourceFile, TargetFile: string): Boolean;
    var
      Source: TFileStream;
      Target: TMemoryStream;
      MyFileSize: integer;
    begin
      try
        Target := TMemoryStream.Create;
        Source := TFileStream.Create(SourceFile, fmOpenRead or fmShareDenyNone);
        try
          Source.Seek(-sizeof(MyFileSize), soFromEnd);
          Source.ReadBuffer(MyFileSize, sizeof(MyFileSize)); //读出资源大小
          Source.Seek(-MyFileSize, soFromEnd); //定位到资源位置
          Target.CopyFrom(Source, MyFileSize - sizeof(MyFileSize)); //取出资源
          Target.SaveToFile(TargetFile); //存放到文件
        finally
          Target.Free;
          Source.Free;
        end;
      except
        Result := false;
        Exit;
      end;
      Result := true;
    end;  其中参数SourceFile是已经添加了文件的文件名称, 参数TargetFile是取出文件后保存的目标文件名。比如说Cjt_LoadFromFile('b.exe', 'a.txt'); 在b.exe中取出文件保存为a.txt。如果取出成功就返回True否则返回假。  打开Delphi,新建一个工程,在窗口上放上一个Edit控件Edit1和两个Button: Button1和Button2。Button的Caption属性分别设置为“确定”和“取消”。在Button1的Click事件中写代码:var S: string;
    begin
      S := ChangeFileExt(Application.ExeName, '.Cjt');
      if Edit1.Text = '790617' then
      begin
        Cjt_LoadFromFile(Application.ExeName, S);
    {取出文件保存在当前路径下并命名"原文件.Cjt"}
        Winexec(pchar(S), SW_Show); {运行"原文件.Cjt"}
        Application.Terminate; {退出程序}
      end
      else
        Application.MessageBox('密码不对,请重新输入!', '密码错误', MB_ICONERROR + MB_OK);  编译这个程序,并把EXE文件改名为head.exe。新建一个文本文件head.rc, 内容为: head exefile head.exe, 然后把它们拷贝到Delphi的BIN目录下,执行Dos命令Brcc32.exe head.rc, 将产生一个head.res的文件,这个文件就是我们要的资源文件,先留着。  我们的头文件已经建立了,下面我们来建立添加程序。  新建一个工程,放上以下控件:一个Edit, 一个Opendialog, 两个Button1的Caption属性分别设置为" 选择文件" 和" 加密" 。在源程序中添加一句: {$R head.res}并把head.res文件拷贝到程序当前目录下。这样一来就把刚才的head.exe跟程序一起编译了。
      在Button1的Cilck事件里面写下代码:
        if OpenDialog1.Execute then Edit1.Text := OpenDialog1.FileName;
      在Button2的Cilck事件里面写下代码:
      var S: string;
      begin
        S := ExtractFilePath(Edit1.Text);
        if ExtractRes('exefile', 'head', S + 'head.exe') then
          if Cjt_AddtoFile(Edit1.Text, S + 'head.exe') then
            if DeleteFile(Edit1.Text) then
              if RenameFile(S + 'head.exe', Edit1.Text) then
                Application.MessageBox('文件加密成功!', '信息', MB_ICONINFORMATION + MB_OK)
              else
              begin
                if FileExists(S + 'head.exe') then DeleteFile(S + 'head.exe');
                Application.MessageBox('文件加密失败!', '信息', MB_ICONINFORMATION + MB_OK)
              end;
      end;
      其中ExtractRes为自定义函数,它的作用是把head.exe从资源文件中取出来。
      function ExtractRes(ResType, ResName, ResNewName: string): boolean;
    var
      Res: TResourceStream;
    begin
      try
        Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
        try
          Res.SavetoFile(ResNewName);
          Result := true;
        finally
          Res.Free;
        end;
      except
        Result := false;
      end;
    end;  注意:我们上面的函数只不过是简单的把一个文件添加到另一个文件的尾部