CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    How reliable is SystemParametersInfo?

    Guys, I know I've been asking unordinary questions lately but I can't help but come across these bugs. Here's another one.

    I run the following code on my Vista laptop from a user-mode process:

    Code:
    BOOL bSecure = -100;
    if(::SystemParametersInfo(SPI_GETSCREENSAVESECURE, NULL, &bSecure, 0))
    {
        int success = 1;
    }
    The purpose is to obtain the "On resume, display logon screen" screensaver setting (circled in red in the screenshot below.)

    Name:  3.jpg
Views: 925
Size:  40.5 KB

    SystemParametersInfo succeeds but bSecure is not changed.

    For those who may not believe me, I'm attaching the screenshot from the VS debugger.

    Name:  1.jpg
Views: 851
Size:  10.4 KB

    Upon further debugging it turns out that SystemParametersInfo actually returns 0xC0000002 when on my other computer it returns 1 when it succeeds. I thought it is not supposed to return HRESULT, or is it?

    Anyway, the question -- is there a reliable way to call it?

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

    Re: How reliable is SystemParametersInfo?

    Upon further debugging it turns out that SystemParametersInfo actually returns 0xC0000002 when on my other computer it returns 1 when it succeeds. I thought it is not supposed to return HRESULT, or is it?
    The only reliable value returned is a 0 for fail. For success, Microsoft guarantees that the return value will not be 0 but that is all they guarantee. They may return a 1, -1, 1000 or any other non-zero value. This value can be different for different versions of Windows - and could even change between service packs as Microsoft makes no guarantee other than a non-zero value for success.
    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)

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: How reliable is SystemParametersInfo?

    @2kaud: So how shall I interpret the results I get?

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

    Re: How reliable is SystemParametersInfo?

    Which versions of Windows are you using it for?
    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)

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

    Re: How reliable is SystemParametersInfo?

    For this test program

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    BOOL bsecure = 100;
    
    int ret;
    
    	if (!(ret = SystemParametersInfo(SPI_GETSCREENSAVESECURE, NULL, &bsecure, 0))) {
    		cout << "Problem with SystemParametersInfo. Error: " << GetLastError();
    		return 1;
    	}
    
            cout << "ret: " << ret << endl;
    	cout << "value: " << (int)bsecure;
    	
    	return 0;
    }
    On both Windows 7 and Windows 8.1 - both x64 fully patched - it shows

    not require a password - 0
    requires a password - 1

    I haven't got immediate access to a Vista system so can't test on that yet.

    Note that for Windows Server 2003 and earlier and Windows XP and earlier, SPI_GETSCREENSAVESECURE is not supported.
    Last edited by 2kaud; December 13th, 2013 at 07:43 AM.
    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)

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: How reliable is SystemParametersInfo?

    Your GetLastError() part will never be executed because SystemParametersInfo returns 0xC0000002. If I do call it right after that API the last error is not changed from what it was before the call.

    At this point I cannot confirm if this behavior is specific for my laptop (running Vista) or that OS in general.

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

    Re: How reliable is SystemParametersInfo?

    Quote Originally Posted by dc_2000 View Post
    Your GetLastError() part will never be executed because SystemParametersInfo returns 0xC0000002. If I do call it right after that API the last error is not changed from what it was before the call.

    At this point I cannot confirm if this behavior is specific for my laptop (running Vista) or that OS in general.
    I've now had an opportunity to try it on a Vista SP2 x64 system. I agree with you. I'm getting the same result as you on Vista. The same .exe works correctly on Windows 7, Windows 8.1 and XP (returns 0 with an error code). But vista is returning 0xC0000002 the same as you found!
    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)

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

    Re: How reliable is SystemParametersInfo?

    I haven't at the moment got access to a Vista system, but on Windows 7/8.1 the return value is 1 and on XP the return value is 0.

    Can any guru confirm the return value for this on a vista system?
    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)

  9. #9
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: How reliable is SystemParametersInfo?

    Thank you for sticking with it. Yes, it must be some bug that never got fixed. The question though is how many more SystemParametersInfo parameters do the same thing?

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

    Re: How reliable is SystemParametersInfo?

    Quote Originally Posted by dc_2000 View Post
    Thank you for sticking with it. Yes, it must be some bug that never got fixed. The question though is how many more SystemParametersInfo parameters do the same thing?
    I looked at the value returned on Vista to see if there was some way of knowing whether screen secure was set or not but in both cases the info returned is the same and the value of bSecure doesn't change from its initial value. So I can't see how this parameter can be used for vista.

    However, there is a registry setting which can be checked for this info

    key
    HKEY_CURRENT_USER\Control Panel\Desktop

    name
    ScreenSaverIsSecure

    type
    Reg_SZ

    value
    notsecure - 0
    secure - 1

    I've checked, and this works on XP, Vista, Windows 7 and Windows 8.1

    I've tried it and the program below will display the correct value for XP, Vista, Windows 7 and Windows 8.1.

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    HKEY	dt;
    
    LONG	res;
    
    const DWORD dsize = 5;
    char	data[dsize];
    
    	if ((res = RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_QUERY_VALUE, &dt)) != ERROR_SUCCESS) {
    		cout << "Error opening 'Control Panel\\Desktop: " << res << endl;
    		return 2;
    	}
    
    	DWORD bufsz = dsize;
    	if ((res = RegQueryValueEx(dt, "ScreenSaverIsSecure", NULL, NULL, (LPBYTE)data, &bufsz)) != ERROR_SUCCESS) {
    		cout << "cannot query ScreenSaverIsSecure: " << res << endl;
    		return 3;
    	}
    
    	cout << "Screen Saver Secure is: " << data << endl;
    
    	return 0;
    }
    Hope this helps.
    Last edited by 2kaud; December 14th, 2013 at 05:17 PM.
    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)

  11. #11
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: How reliable is SystemParametersInfo?

    Thanks. That is actually my current workaround for that method. I do this:

    Code:
    BOOL bSecure = 0xdead;
    if(VistaOrLaterOS)
    {
        ::SystemParametersInfo(SPI_GETSCREENSAVESECURE, NULL, &bSecure, 0);
    }
    
    if(bSecure == 0xdead)
    {
        //Use your registry method
    }
    My major concern about this is this article and this statement:

    However in Vista, you will find a glitch with this method: the UI in the Screen Saver Settings dialog remains in the state before you make the change.

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

    Re: How reliable is SystemParametersInfo?

    When I tested this on Vista I didn't experience any problem. Ticking or unticking on-resume password protect and then clicking Apply caused the registry entry to be correctly changed. I think the article is referring to the other way - trying to change the on-resume password protect setting via changing the registry.

    Why not just use the registry method for all versions of the OS as it works for all versions? That could simply your code somewhat rather than having version checks etc.
    Last edited by 2kaud; December 15th, 2013 at 06:12 AM.
    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)

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