CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Sep 2012
    Posts
    25

    Problem Reading/Writing Registry in Windows 7?

    This may be in the wrong forum – but here goes.

    I have a 32-bit application that I've been maintaining for about 12 years and it runs on every Windows platform up to Windows 7. In all that time I've been using CWinApp::GetProfileXxxx() and CWinApp::WriteProfileXxxx() calls to read and write my program settings (about 70 settings) in the Registry. The settings are read from the Registry at start-up and written to the Registry when the program closes. There is also a method for the user to read and write the program settings to an INI file using the same code as the Registry access.

    On my Windows 7 system the Registry read/write works just like it always has on all previous Windows versions. However, on some customer's Windows 7 machines there appears to be a problem with the Registry access. The program settings are either not being read from the Registry or are not being written to the Registry. I think the settings are not being written – but I don't know that. When the user uses the INI file the settings appear to be read and written.

    The customer has complained a bit (I'd complain too) but doesn't have the time or doesn't want to take the time to run some simple tests for me to find out what's going on with his Windows 7 system.

    So here is my question: Are there any user account settings or permissions that can block the program's access to the Registry? He claims he is an Administrator but I can't even get him to verify that.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem Reading/Writing Registry in Windows 7?

    Quote Originally Posted by Mike Douglas View Post
    This may be in the wrong forum – but here goes.

    I have a 32-bit application that I've been maintaining for about 12 years and it runs on every Windows platform up to Windows 7.
    You must test your legacy apps separately on Windows 7, at least that is my experience. Since Windows 7 changed a lot around, you can't assume your app is compatible with Win 7.
    In all that time I've been using CWinApp::GetProfileXxxx() and CWinApp::WriteProfileXxxx() calls to read and write my program settings (about 70 settings) in the Registry.
    This is an example of the changes made.

    Windows 7 has more or less changed the way that the registry works. Look up "Registry Virtualization".

    Second, are you checking your return codes from your functions and calling GetLastError()? If so, what is the error you get when a registry function fails? You didn't mention this.

    Regards,

    Paul McKenzie

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem Reading/Writing Registry in Windows 7?

    Try to use some of registry classes like CRegKey instead of these obsolete CWinApp::GetProfileXxxx() and CWinApp::WriteProfileXxxx() methods.

    BTW, do you check the return values of GetProfileXxxx / WriteProfileXxxx calles?
    Victor Nijegorodov

  4. #4
    Join Date
    Sep 2012
    Posts
    25

    Re: Problem Reading/Writing Registry in Windows 7?

    I did check my app in my Windows 7 machine and didn't have any problems with the GetProfileXxxx() and WriteProfileXxxx () Registry access calls. So like a moron I assumed that everything was OK.

    Again, like a moron I didn't check the return value of WriteProfileXxxx() because on my Win7 system it works. I think I'll go back and add a little 'check it' routine and check the return value to see what, if anything, is reported as an error. But, on my Win7 everything works so I'll have to figure out how to break it.

    I think the problem is related to the registry key permissions, but I'm not sure yet.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem Reading/Writing Registry in Windows 7?

    Quote Originally Posted by Mike Douglas View Post
    I did check my app in my Windows 7 machine and didn't have any problems with the GetProfileXxxx() and WriteProfileXxxx () Registry access calls. So like a moron I assumed that everything was OK.

    Again, like a moron I didn't check the return value of WriteProfileXxxx() because on my Win7 system it works. I think I'll go back and add a little 'check it' routine and check the return value to see what, if anything, is reported as an error. But, on my Win7 everything works so I'll have to figure out how to break it.
    Regardless whether if it works or not, you must check every return value when you call a Windows API, MFC, or any third-party library function that states that the function returns an error code. This is especially the case if your application is going to be used by customers who will be running your software on hardware you have no idea about.

    Your case is a classic example -- you don't know what hardware or software your users are running, how their systems are set up, etc. You never release an app that is meant to be used by the public and not check to make sure that every Windows call is valid. If an error occurs, then report the error with the combination of GetLastError() / FormatMessage() and a log file of some type.

    Without these checks, what winds up happening is that your assumption is wrong, and then your program takes the "everything is OK" path when everything is not OK, potentially causing damage. Can you imagine if your program was supposed to remove some registry keys, and that failure to check a return code caused some sort of memory corruption to a string variable, causing wrong keys to be removed?

    I once had to debug an app where a GDI call was failing and was never checked -- what wound up happening to one customer was that the ink out of a cheap inkjet printer would get used up due to totally black pages being outputted -- all assuming that a good image was being printed.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 19th, 2013 at 08:16 PM.

  6. #6
    Join Date
    Mar 2016
    Posts
    1

    Re: Problem Reading/Writing Registry in Windows 7?

    Mike I am hoping you got this resolved because I have a similar problem with our application. I also think it is related to registry key permission somehow.

  7. #7
    Join Date
    Sep 2012
    Posts
    25

    Re: Problem Reading/Writing Registry in Windows 7?

    Jeff,

    Never did find a satisfactory answer. The one customer that reported problems didn't want to help me find an answer. Sorry.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem Reading/Writing Registry in Windows 7?

    Quote Originally Posted by Mike Douglas View Post
    Jeff,

    Never did find a satisfactory answer. The one customer that reported problems didn't want to help me find an answer. Sorry.
    Even if your customer is an administrator, depending on what keys your program writes to, the program itself may need to run as an administrator.

  9. #9
    Join Date
    Sep 2012
    Posts
    25

    Re: Problem Reading/Writing Registry in Windows 7?

    Jeff,

    I thought about this after I posted my first reply today. One poster advised using CRegKey instead of WriteProfileXxxx() and ReadProfileXxxx() but CRegKey in my VC++6.0 doesn't have QueryBinaryValue() or SetBinaryValue() members and CRegKey is deprecated anyway (from what I've read). So I stayed with
    WriteProfileXxxx() and ReadProfileXxxx().

    During program initialization I used WriteProfileInt() to write a time value to the
    Registry, immediately read the value back with ReadProfileInt(), checked for a successful write, and made sure the value written was the same as the value read back. If the test failed then my program uses an INI file to save and restore its settings. I also notified the user but no one has said anything about it (yet). It's been about 3 years.

  10. #10
    Join Date
    Sep 2012
    Posts
    25

    Re: Problem Reading/Writing Registry in Windows 7?

    Hi Arjay,

    Shouldn't be a problem. I'm using HKEY_CURRENT_USER.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem Reading/Writing Registry in Windows 7?

    Quote Originally Posted by Mike Douglas View Post
    Hi Arjay,

    Shouldn't be a problem. I'm using HKEY_CURRENT_USER.
    Is the app a 32bit app, but the customer with the failure is running on a 64bit system?

  12. #12
    Join Date
    Sep 2012
    Posts
    25

    Re: Problem Reading/Writing Registry in Windows 7?

    32-bit app developed on a Win7-32-bit machine. Runs just fine on my WinXP, Vista 32-bit, Win7 32-bit, Win7 64-bit, and Win8. Don't know what the problem machine was because the user never responded to my emails.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem Reading/Writing Registry in Windows 7?

    In case customer is not willing to cooperate, hardly you can do anything but testing on all possible platforms with all possible culture settings and be lucky enough to run into the same issue the customer really has. Taking this route or not, depends on your resources. And willing to help this particular customer who never responds to your requests.
    Best regards,
    Igor

  14. #14
    Join Date
    May 2017
    Posts
    12

    Re: Problem Reading/Writing Registry in Windows 7?

    Quote Originally Posted by Mike Douglas View Post
    32-bit app developed on a Win7-32-bit machine. Runs just fine on my WinXP, Vista 32-bit, Win7 32-bit, Win7 64-bit, and Win8. Don't know what the problem machine was because the user never responded to my emails.
    I have also similar situation 32 bit app on 32 bit Windows 7. When I open registery using CRegKey::Open() function, I get error code 2 (=file not found). I can't make sense why I get this error. I tried other test paths as well but same results.

    Code:
    int res = regKey.Open( HKEY_CURRENT_USER, _T("HKEY_CURRENT_USER\\Software\\Intel\\Display\\Igfxcui\\HotKeys"), KEY_WRITE );

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem Reading/Writing Registry in Windows 7?

    Try this

    Code:
    res = regKey.Open( HKEY_CURRENT_USER, _T("Software\\Intel\\Display\\Igfxcui\\HotKeys"), KEY_WRITE );
    The name of the key to be opened/created needs to be a sub-key of the parent. See https://msdn.microsoft.com/en-us/lib...#cregkey__open
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured