Solved

[Registry] Using *.reg files...

Posted on 2006-11-14
24
1,592 Views
Last Modified: 2010-05-18
This is more a DESIGN question than a technical question!

Well, okay. Something new I have to work on. I have an application which has a lot of features. These features are set in the Registry and I happen to have several hundreds of them. Or not, in which case a default value for some setting is going to be used...

Limitation: I am NOT allowed to modify the storage of these features. I can't even modify the application which uses them.

Well, not I need to find a solution for the following... I will have two *.reg files (maybe more in the future) for two different scenarios. And I have to write an application which allowds the user to select one of these files and then installs the REG file. However, the user should be able to FLIP the settings too so when the user executes the application again then it should remove the settings from the old registry file and install the new registry file.

One nasty issue is that I just can't delete the whole registry tree of this application. There might be other keys in there which need to stay untouched. So basically, it's not as easy as it might appear at first glance.

Right now, I have two registry files to use as example and pen and paper (well, Word and Paint) to use for the DESIGN of this application. And no, I don't need any deep technical advise. I just want some good suggestions about where to start the design. What would be the best approach for this?

Right now, what I considered was just reading the REG file, parse the contents and walk through every key in it, keeping a log of keys so I can always remove them again. And I have about a week to think about this design before I have to start designing it so I have some time to discuss the design with fellow experts. :-)

So, any input?
0
Comment
Question by:Wim ten Brink
  • 9
  • 4
  • 4
  • +4
24 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17938037
Can you not just run the reg files (once selected the one you want) to modify the values in the registry.
ie. just run regsvr32.exe
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17938585
Nope. Several of the options have to be removed from the registry. You can't remove them by running a REG file, as far as I know... Otherwise I would indeed call RegSvr32 with the /Quiet parameter so the user doesn't have to click yes or no. :-)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17938653
hmm well then I guess what you have already written is what you want to do.
Step through each Registry value, and alter the registry.
I guess you are also going to have to check values assigned to ones you want to remove rather than update too, ie set string values to '' so you know that these are to be deleted.

Maybe you might find it better to use various .ini files instead for various registry settings and step through one of these files instead.

Im not sure of any other quick method that would be appropriate to your situation to be honest.
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17939036
Actually, it seems to me that a *.REG file is already some kind of INI file. It just has an additional header telling me that it's actually a REG file. And, oh, yeah... It's saved as unicode and in general quite big.

Also considered storing things in XML files or some other format but the biggest problem is time. I have one day to come up with a good design and then one day to write the whole project. :-)  And then half a day for fixing and testing. It's not much time but hey, I only have to start on it next monday and just want to save a bit of time by collecting some more experts-input.

Anyway, I think I have already the right solution in my mind. Just reading the REG file as if it's an INI file, splitting it up in roots, keys, names and values. And remembering which REG file the user used previously. (So it can be undone again.)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17939251
Sounds like a plan to me Alex

:o)
0
 
LVL 28

Expert Comment

by:TName
ID: 17939352
>Several of the options have to be removed from the registry. You can't remove them by running a REG file, as far as I know...

Hi, maybe I misunderstand your intention, but I think you can remove keys with a reg file  by adding a "-" before the path, e.g.
[-HKEY_...
instead of
[HKEY_...
0
 
LVL 28

Expert Comment

by:TName
ID: 17939579
And to delete a value:
[HKEY...]
"SomeValue"=-

See http://support.microsoft.com/kb/310516
0
 
LVL 28

Assisted Solution

by:TName
TName earned 125 total points
ID: 17942750
I've tested it and it seems to work (sorry if it's an old hat, but I had never tried it ;)

After adding:
[HKEY_CURRENT_USER\Test]
"TestOption"="One"
"TestOption2"="Two"

this will delete the second option:
[HKEY_CURRENT_USER\Test]
"TestOption2"=-

and this will delete the key "Test"
[-HKEY_CURRENT_USER\Test]

So if you're allowed to edit the reg files, you could do everyting with the 2 reg files, each deleting, overwriting and adding what's necessary.
If you can't edit them (for whatever reason), you'd need 4 files, I guess (e.g. run DeleteStuffFromA.reg and then B.reg if you go from A to B)
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17945354
Thanks, TName! That's one interesting thing to keep in mind. :-)

Unfortunately, I heard one additional specification for this tool from our customer and the reason why this tool is required. Many users in their system are not allowed to execute RegSvr32.exe so I can't use RegSvr32 to register a REG file. Maybe through some API call which accepts a complete REG file but as it appears now, I just have to write code to parse the REG file... :-(
(And worse, the REG files are unicode...)
0
 
LVL 28

Expert Comment

by:TName
ID: 17945600
>one additional specification for this tool from our customer

Yes, I think that's being taught in some secret place: "How to prevent any simple solution by strategic use of specifications"  :)
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17945620
> Yes, I think that's being taught in some secret place: "How to prevent any simple solution by strategic use of specifications"  :)

Which is exactly why I'm asking for other experts input here. :-) They're very likely to come up with several other pitfalls for this application. :-) Oh, well... I first have to come up with the design and when that's done, checked and approved, the tool can be written. Just need the simplest-to-implement solution here. :-)

Am still waiting to hear if it also has to support Windows 98 or now... :-(
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17946323
Did I say RegSvr32? Well, I meant RegEdit. :-)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Expert Comment

by:dvplayltd
ID: 17948720
Win 98 is dead :)... from some years ! do NOT think about it.
0
 
LVL 4

Assisted Solution

by:BedouinDN
BedouinDN earned 125 total points
ID: 17951335
Have you taken a look at Russells' (RLibby) Regtransfer code? It may be of use to you.
You can find it on his web page: http://users.adelphia.net/~rllibby/
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17954143
dvplayltd said:
>  Win 98 is dead :)... from some years ! do NOT think about it.

You should tell that to some of the customers that I have to deal with. Quite a few of them are still using a computer which happens to be 8 years old or so. They might have upgraded the hardware and increased the memory and disk space but never bothered to upgrade the operating system since it just continues to work well enough for them.

Well, okay... We're trying to make Windows 2000 the minimum requirement for our products but this might cost us a few customers because they don't see the need of upgrading their hardware and operating system just for our own upgrades. It's one reason why we haven't upgraded to a newer Delphi version already. We're still using Delphi 6 (not 2006!) for development. :-(
Not to worry, though. Privately, I use Delphi 2006. :-)

@BedouinDN,
I actually forgot about the existance of that library. Thanks for reminding me. :-)
0
 
LVL 9

Expert Comment

by:alkisg
ID: 17958248
Some thoughts:

1) Windows 98 .reg files are ANSI files, not Unicode. The file format is Regedit 4, while in 2000/XP it's Regedit 5 (and they're unicode).
That's not a problem, there are several functions out there that let you read a unicode OR ansi text file (e.g. synedit.sf.net, tnt unicode controls...)

2) What you're planning so far is implementing an app that can parse .reg files and put them (or remove them) from registry. This has the same functionality as regedit /s.
So, all you have to do is rename regedit.exe to myapp.exe to overcome the security restrictions for the users you mentioned ;-)

3) If you need to modify keys in HKLM, the users will need administrative rights or elevated permissions. Did you ask about that?
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17959813

And my source code handles (1), (2), and (3) natively without the use of Regedit.

Russell
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17963391
Well,

1) Only ANSI support on Windows '98 might be a bit of an annoyance but it will be more of a customer issue to begin with. If need be, the REG file can always be converted back to ANSI. But for the moment I can assume the user will have Windows 2000 as a minimum.

2) Renaming regedit and providing it as separate executable is also a possibility. But the main customer who forced this limitation won't be happy about such a solution. The only Registry files that this tool is allowed to process are the ones that are part of our product.

3) The users will have administrator rights since the product for which they need this tool also requires administrator rights. There won't be any real problems at this point. :-)

Still, good points!
0
 
LVL 9

Expert Comment

by:alkisg
ID: 17963486
Another approach:
Suppose the "root" key of your app is
HKLM\Software\Company\Product

1) COPY Product to Product.settings#2
2) To switch between settings, instead of importing keys,
just rename Product to Product.settings#1
and Product.settings#2 to Product
(and Product.settings#1 to Product.settings#2 to make a complete "swap")

This way the two settings are completely different, something like two different profiles. This may or MAY NOT be desirable, depending on the settings and the app. You'll have to check the settings to see if this approach suits the clients.
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17963872
Interesting solution, Alkis. One flaw though. The project has both leys in HKEY_LOCAL_MACHINE as in HKEY_CURRENT_USER. :-( And lots of branches and subbranches. And some of those subbranches are to be left unaltered.

Basically we're trying to make one product to appear as if it's more than one product simply based upon several features in the Registry. Think about color scheme, logo but also some other functionality. Due to legacy we're stuck with the registry for this purpose otherwise I'd have dumped them all in an XML file.
0
 
LVL 9

Assisted Solution

by:alkisg
alkisg earned 125 total points
ID: 17967672
> Both keys in HKLM, HKCU
You may swap both keys.

> Subbranches are to be left unaltered
If you initially COPY the whole branch as I said, the subbranches will be left unaltered when you swap between the two versions.

The only flaw I can think of, is that PERMANENT changes to the program will have to be applied to both versions.
E.g. if the app was autocad, and if you did an autocad update, and it modified HKLM\Software\Autocad, you should also switch to the second branch (HKLM\Software\Autocad.settings#2) and perform a second update... :-(
That's what I meant when I said it may not be desirable...

But if permanent changes are not an issue, it'll save you a lot of time: you won't have to locate which keys to save/restore, because this way you'll swap all of them.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 17973682

Given the comments, and the direction that you are thinking about taking, I would recommend taking a hard look at the source I have done in my RegTransfer unit.

Breaking the specifics down, we have:

(1) Thinking about reading and parsing a .reg file.
(2) Possible need to log keys / value changes made to the registry from a .reg file.
(3) Programmatically importing a .reg file into the registry
(4) Support for unicode (UTF 16) and ansi files
(5) Advanced analysis of .reg file (values exist, modified, key exists, etc)
(6) Privileges that are required (and held), but are not enabled by default for a process, in order to push data into HKLM.

-----

Addressing each of these:

(1) While the format itself is fairly simple, there are a number of nuances such as name and value escaping (" and \ chars), embedded ] chars in the key name, error code handling, etc, that you will most likely miss while trying to come up with general purpose parsing code. Something that my RegTransfer code already handles (with a great deal of time invested to handle ALL nuances). Not only does RegTransfer do it well, it also does it very quickly; up to 10MB/sec parsing time, depending on system of course.

(2) You can make it as simple or as complex as you want. Most users of RegTransfer just use the RegImport(File) functions to import a .reg file into the registry. A win32 result code is returned to inform of success/failure. This is similar to the regedit import, which Works fine, but provides no detailed info (keys/values) of the import. The RegTransfer code exposes a class wrapper hierarchy set (TRegFile/TRegKey/TRegValue) that allows you to evaluate each key, and the values for each. You can even determine if a value read from a .reg file is the same as the data in the registry. Using the classes, you can also pick and choose what keys, or even specific values to write/delete from the registry (for deletes, set the .MarkDeleted property to true). For example, in less than 40 lines of code you can:

- parse / load a reg file in
- remove all values that were written from a .reg file (you need to determine if it was first imported)
- remove all empty keys afterwards

var  dwKeys:        Integer;
     dwValues:      Integer;
begin

  // Create reg file wrapper
  with TRegFile.Create do
  begin
     // Resource protection
     try
        // Load file
        LoadFromFile('c:\testing.reg');
        // Set all values to MarkDeleted
        for dwKeys:=0 to Pred(KeyCount) do
           for dwValues:=0 to Pred(Keys[dwKeys].ValueCount) do
              Keys[dwKeys].Values[dwValues].MarkDeleted:=True;
        // Update registry
        if (UpdateRegistry = ERROR_SUCCESS) then
        begin
           // Walk the keys in reverse order and remove empty ones (they are auto sorted for you)
           for dwKeys:=Pred(KeyCount) downto 0 do
           begin
              // Check if empty
              if Keys[dwKeys].IsEmptyInRegisty then
              begin
                 // Set mark deleted
                 Keys[dwKeys].MarkDeleted:=True;
                 // Remove key
                 Keys[dwKeys].UpdateRegistry;
              end;
           end;
        end;
     finally
        Free;
     end;
  end;

end;

(3) This has already been touched on, but can be done using RegImportFile, RegImport (stream based), use of the extended versions: RegImportFileEx, RegImportEx which allow a callback function to be passed, or by using the TRegFile/TRegKey/TRegValue UpdateRegistry function. All importing/removing is done via the same internal code, and has been heavily tested and in use by others for quite awhile.

(4) Code is written to detect UTF 16 encoding on loading of either a file or stream, and will recode the data to ansi for handling. Nothing needs to be done by the developer. The developer can also specify (when saving to file) if they wish to have unicode or ansi file contents; you can even specify if you want the old style REGEDIT4 header or the new one on export.

(5) Advanced analysis provided through the use of the .reg file class wrappers. Features such as:

TRegValue:
   ExistsInRegistry
   SameAsRegistry
   UpdateRegistry

TRegKey:
   ExistsInRegistry
   IsEmptyInRegisty
   UpdateRegistry

TRegFile:
   RemapKeys
   UpdateRegistry

The TRegFile class also allows you to load a file/stream, make any number of changes, remap keys, add / remove items, etc and then save the state out to a new .reg file.

(6) Being an admin user means that you hold the SeBackupPrivilege and SeRestorePrivilege rights, but by default these privs will not be enabled (by default) for a process. Without these being enabled, some HKLM keys/values will return ERROR_ACCESS_DENIED when attempting to open / read them. Again, this is handled in the code I mention.

----

As far as "copying" keys, etc, even if you choose this route, you still don't get away from parsing the .reg file yourself. Given that my source is free for all use, I have no vested interest in you using / not using the RegTransfer code. All I can say is that given your requirements (especially not using RegEdit) you will be forced to use source to handle what you need to do. If you plan on implementing this yourself, just make sure you test all REG_ data types, all different chars in keys/names/values, and double test your work. Working with the registry tends to be a little unforgiving ;-)

Regards,
Russell
0
 
LVL 9

Expert Comment

by:alkisg
ID: 17973719
Guys of course if Alex is going to locate all the needed keys one by one and generate/parse .reg files, he'd better use Russell's library.

Just to clarify something about what I've said: copying the branches initially should be part of the INSTALLATION process, not a part of his program. What I propose it that his program may be a wrapper that only swaps two branches in the registry before running the actual app, IF THAT SUFFICES. If not, he'll have to mess with individual keys/parsing .reg/comparing/importing etc, no argument there. In this case, I think the most of the work is not the actual code, but LOCATING the keys that need replacing vs the keys that need to stay intact (at design time, not at runtime)...
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17977643
Well, I have to start designing now. And as suggested, I'll end up parsing the REG file provided by Russell. Points for BedouinDN  for reminding me about this library, for rlibby for his additional comments (and for creating that nice library), for alkisg for his alternative view and for TName for his REG file hack. :-) These advises were most useful for me. Why I accepted Russells post as answer? Well, just to point out how useful his library is, even though someone else pointed it out to me. :-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now