2005. szeptember 1., csütörtök

How to attach a file inside a DLL or executable


Problem/Question/Abstract:

I don't know if it is possible to attach a file to a DLL or exe. Example: You create a function (useOtherdll (bool)) in Test.dll with a parameter that tells you to use a DLL named Needed.dll. If the function parameter is 'true' then the DLL Needed.dll must be in the current directory, if the function parameter is 'false' then the DLL Needed.dll must not to be in the current directory. So if it is possible to attach in test.dll my other DLL Needed.dll, and then I can copy it if it is necessary or not.

Answer:

You can use streams to copy any data to the end of any other data - ie., copy a DLL to the end of a DLL. Example:

procedure TForm1.Button1Click(Sender: TObject);
var
  f: integer;
  fStream: TFileStream;
  mStream: TMemoryStream;
  theFiles: TStringList;
begin
  theFiles := TStringList.Create;
  try
    theFiles.Add('Needed.dll');
    theFiles.Add('TEST.dll');
    if theFiles.Count > 0 then
    begin
      mStream := TMemoryStream.Create;
      try
        for f := 0 to theFiles.Count - 1 do
        begin
          fStream := TFileStream.Create(theFiles[f], fmOpenRead);
          try
            mStream.CopyFrom(fStream, fStream.Size);
          finally
            fStream.Free;
          end;
        end;
        mStream.Seek(0, soFromBeginning);
        mStream.SaveToFile('NEW.dll');
      finally
        mStream.Free;
      end;
    end;
  finally
    theFiles.Free;
  end;
end;

You would need to mark the start of the second DLL somewhere. Then when needed, load the combined DLL into a stream. Seek to second DLL block, and copy it in a stream. Save that block steam back to the disk as the second DLL name.

Nincsenek megjegyzések:

Megjegyzés küldése