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
nikifAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
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.
Sinisa VukSoftware architectCommented:
you can use function like this:
function WriteTextToRes(sDllFile: String; sTextToWrite: String): Boolean;
var
  hResourceHandle: Cardinal;
begin
  hResourceHandle := BeginUpdateResource(PChar(sDllFile), True);
  try
    Result := UpdateResource(hResourceHandle, RT_RCDATA, 'nickname', LANG_NEUTRAL, PChar(sTextToWrite), Length(sTextToWrite));
  finally
    EndUpdateResource(hResourceHandle, False);
  end;
end;

Open in new window


https://msdn.microsoft.com/en-us/library/windows/desktop/ms648049%28v=vs.85%29.aspx

note: dll file must not be in use (opened)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nikifAuthor Commented:
Perfect!!!!
Thanks a lot

Nikiforos
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.