0

Possible Duplicate:
how to store settings in resource

I have a record with strings, Integers, etc. This record is used as settings for a file that gets read later. How can I convert a record to binary data (or string) and save to it a RCDATA and read it back to a record? If possible native code and/or WinAPI only. Thanks for your help.

Community
  • 1
  • 1
Ben
  • 3,380
  • 2
  • 44
  • 98
  • thanks :) is it possible without the streams? – Ben Jul 19 '12 at 20:06
  • 1
    Of course it's possible. Replace the stream-writing functions with the equivalent functions of whatever other persistence mechanism you choose. – Rob Kennedy Jul 19 '12 at 20:36
  • could I use CopyMemory and ReadMemory? – Ben Jul 19 '12 at 20:40
  • 1
    Not sure why you'd use ReadMemory, but sure, if you have some other block of memory that you want to copy something to, then go ahead and use CopyMemory. Why are you asking? Go forth and do it. – Rob Kennedy Jul 19 '12 at 21:45
  • 1
    @RobKennedy if he has AnsiStiring or TObject or any other Pointer in his record - what would be the outcome ? and what would he get on reading ? – Arioch 'The Jul 19 '12 at 22:14
  • 1
    @Arioch, copying reference types as raw bytes leads to problems. Don't do that. If you want a fuller explanation, ask it as a question on its own. Answers to [the other question I linked to](http://stackoverflow.com/q/3820996/33732) address how to do it correctly. (Stack Overflow deleted my first comment when the question was closed as a duplicate because that comment linked to the duplicate question, but I'd edited it to link to a second question.) – Rob Kennedy Jul 19 '12 at 22:51
  • 1
    @RobKennedy Surely i knoww it. That is why i asked. You claimed "go ahead and use CopyMemory. Why are you asking?" And i warned that such approach only works when very narrow restricitions are applied to data, and leads to troubles in general case. I did not asked you what should i do, but rather protested your "go and do it! sure! why not?" suggestion. – Arioch 'The Jul 20 '12 at 08:54
  • 1
    @Arioch, the question about CopyMemory was in the context of alternatives to streams. The code shown for writing to streams used TStream.Write on an entire record. If that works, then CopyMemory is the general-purpose equivalent for writing to memory. I wasn't worried about pitfalls of copying reference types since the answers I linked to already addressed that. – Rob Kennedy Jul 20 '12 at 13:07

1 Answers1

1

About RCDATA:

i ask google "resource editor opensource delphi" and it told me http://melander.dk/reseditor/

This program surely can read and write resources.

The page's footer also contains links to three more opensource delphi programs that also can do it.

So i hope you would find what u need there. Among piles of other useful code :-)


But - WHAT RCDATA do you want to use ??? OF what file ? some DLL ? or your own project EXE ? RCDATA is for reading, not writing. It is not typical use. If the DLL or EXE is loaded - you would not be given rights to write into file new RCDATA. And your own running EXe is always open.


About turning to binary - that is VERY dependent on data types u use. Before you would show the data - no one would be able to tell you for sure.

Most lazy approaches would be: 1) Make mirror class from TComponent with published properties mirroring record fields. Assign those properties from record and do TComponent.SaveToStream. 2) Use JsonObject from Progdigy to make text file in JSon format 3) use some XML to save/read. For example TXMLDocument in higher Delphi versions (but it has its gotchas if created with nil Owner) or Jedi CodeLib has XML.

You can search Torry.net for a lot of XML or JSON libs.

But best of all - Google for "serializing in Delphi" and find tutorial detailing the troubles and solutions.

Arioch 'The
  • 15,799
  • 35
  • 62