Author: Jonas Bilinkevicius
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:
1 procedure TForm1.Button1Click(Sender: TObject);
2 var3 f: integer;
4 fStream: TFileStream;
5 mStream: TMemoryStream;
6 theFiles: TStringList;
7 begin8 theFiles := TStringList.Create;
9 try10 theFiles.Add('Needed.dll');
11 theFiles.Add('TEST.dll');
12 if theFiles.Count > 0 then13 begin14 mStream := TMemoryStream.Create;
15 try16 for f := 0 to theFiles.Count - 1 do17 begin18 fStream := TFileStream.Create(theFiles[f], fmOpenRead);
19 try20 mStream.CopyFrom(fStream, fStream.Size);
21 finally22 fStream.Free;
23 end;
24 end;
25 mStream.Seek(0, soFromBeginning);
26 mStream.SaveToFile('NEW.dll');
27 finally28 mStream.Free;
29 end;
30 end;
31 finally32 theFiles.Free;
33 end;
34 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.