Link to home
Start Free TrialLog in
Avatar of nikif
nikifFlag for Greece

asked on

Save data in dll

Hi,
I am trying to make a resource only dll, load its data, modify it in runtime, finally save it back in dll.

What I've managed so far is:
I made a file abcDLL.rc with content:
  nickname RCDATA abcData.txt
(file abcData.txt contains some ascii data)

I compiled it with: brcc32 abcDLL.rc
Produced: abcDLL.RES

I created DLLtest.dpr as resource only dll (code follows) and compiled:

library DLLtest;
uses
  SysUtils, Classes;
{$R abcDLL.RES}
begin
end.

I added a button to load ascii data from dll into a TMemo. It works fine.
procedure TForm1.btLoadRCClick(Sender: TObject);
var
  h : THandle;
  RS: TResourceStream;
begin
  h := LoadLibrary ('DLLtest.DLL');
  if h <> 0 then
  begin
    RS := TResourceStream.Create (h, 'nickname', RT_RCDATA);
    Memo1.Lines.LoadFromStream (RS);
  end;
  FreeLibrary (h);
end;

Then, during runtime the user modifies text in Memo1.
The question is how could I save new content of Memo1 to DLLtest.dll?
I tried the following code. Could anyone complete it?

procedure TForm1.btSaveRCClick(Sender: TObject); // Click button
var
  h : THandle;
  RS: TResourceStream;
begin
// Save new text in dll
  RS := TResourceStream.Create (h, PChar ('nickname'), RT_RCDATA);
  //----------------------------------------------------------
  // Error: Resource "nickname" not found
  // RS.... need to load Memo1.Lines to RS. How?
  //----------------------------------------------------------
  RS.SaveToFile ( 'DLLtest.DLL');
  // Will it work? Do I need the analogous of   h := LoadLibrary ('DLLtest.DLL'); ?
  RS.Free;
end;

I use D6

Thank you for your assistance
Nikif
Avatar of jimyX
jimyX

That's not possible. You could use ini file, simple DB (SQLite?) or other alternative solution.

This will not work:
RS.SaveToFile ( 'DLLtest.DLL');

SaveToFile might create a file called DLLtest.DLL, but that will never be a new generated DLL with the updated text.

You get the text resource in RS from the DLL, that you load by handle:
    RS := TResourceStream.Create (h, 'nickname', RT_RCDATA);
    Memo1.Lines.LoadFromStream (RS);

Calling RS.SaveToFile does not make any sense.

Compiled DLL is wrapped around the included resources, you can not inject the text afterward.
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nikif

ASKER

Perfect!!!!
Thanks a lot

Nikiforos