Registry Update

  • Thread starter Thread starter J.K. Baltzersen
  • Start date Start date
J

J.K. Baltzersen

Dear all,

I have a counter in my Windows registry. Is there any way of ensuring
that it is updated by one process/thread at a time only.

One way of updating is retrieving the counter, incrementing it, and
then writing it back. However, when doing it like this and one has a
process A and B, one risks process A retrieving the counter, then
being interrupted by process B, which retrieves the counter,
increments it, and then writing the updated counter to the registry
before process A again takes over, incrementing the old counter and
writing it to the registry.

I am using RegCreateKeyEx, RegQueryValueEx, and RegSetValueEx.

The result can be that the counter is incremented by 1 only, whereas
the wanted result is to have it incremented by 2.

Is there a simple solution to this problem?

Any help will be greatly appreciated.

Thank you very much in advance.

Best regards,
J.K. Baltzersen
 
If Process A and Process B are separate threads in the same program, you
could just use a critical section in the program to keep Process A from
updating the registry when Process B is updating it, and vice versa.

However, if Process A and Process B are separate programs, I would create a
registry "flag" which you could set to TRUE if one process was updating the
registry key, preventing the other Process from updating it, until the first
process sets the flag back to FALSE.

Help any ??

GS
 
The flag solution sound like a great idea. Thanks.

I do see one problem though. If the process that has set the flag to
TRUE somehow is terminated without having reset it, no update can be
done without manually fixing the problem -- or using a clean-up
process, which must know when to clean up.

Again thanks.
 
No matter what you do, you always run the risk of "something" screwing up
your well-thought out plan.

So, Process A could check and see if Process B is running, and if it isn't,
automatically set the registry flag to FALSE, therefore allowing Process A to
update the registry when needed.

GS
 
J.K -

Look at RegSetKeySecurity in MSDN help. This may be what you need to keep
one process from updating the registry key while the other process has it
open.

GS
 
J.K. Baltzersen said:
Dear all,

I have a counter in my Windows registry. Is there any way of ensuring
that it is updated by one process/thread at a time only.

One way of updating is retrieving the counter, incrementing it, and
then writing it back. However, when doing it like this and one has a
process A and B, one risks process A retrieving the counter, then
being interrupted by process B, which retrieves the counter,
increments it, and then writing the updated counter to the registry
before process A again takes over, incrementing the old counter and
writing it to the registry.

I am using RegCreateKeyEx, RegQueryValueEx, and RegSetValueEx.

The result can be that the counter is incremented by 1 only, whereas
the wanted result is to have it incremented by 2.

Is there a simple solution to this problem?

Any help will be greatly appreciated.

Thank you very much in advance.

Find another way. Vista won't let your software bother the registry.

Put your counter in a file. Read with lock, update the counter, put it back,
unlock the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top