Here is my code that i am working on. Upon running the exe, it asks the user for a temporary password. After that, the screen saver pops up. Upon exiting the screen saver, the console will ask you for the temporary pass that you entered in order to renable the desktop.

So my question is, everything is compiling fine with no errors or warnings, but when i run the program i have a few problems. Everything works up to when the console asks for the password again to initialize the desktop, instead of attempting to verify the correct password, the program just ends.

#include <windows.h>
#include <iostream>
#include <winable.h>

using namespace std;

int rePass(0);  //first password
int rePassb(1); //password to compare

int main()
{
    cout << "Set Recovery pass: ";
    cin >> rePass;
    cin.get();
    SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0); //Start screen saver
    
    //Saftey precaution to prevent instant screen saver disable by accedental mouse movement
    BlockInput(1);
    Sleep(2000);
    BlockInput(0);
    
    //Sloppy idle process until screen saver is disabled
        do{
              system("PAUSE");
          }
       while(SC_SCREENSAVE == 1);
    
    Sleep(2000);
    EnableWindow(GetDesktopWindow(), 0); //Deactivate desktop
    
    //Tests if the intial password that was set matches the new password entered in order to enable desktop
    PassCheck:  
              
        cout << "Enter Recovery Pass: ";
        cin >> rePassb;
                  
            if(rePassb == rePass)
            {
                 cout << "Re-Enabling Windows...";
                 EnableWindow(GetDesktopWindow(), 1); //Enable desktop if password was entered sucessfully
                 Sleep(2000);
            }
            
            else
            {
                cout << "Incorrect Password..." << endl << "Enter Recovery Pass: ";
                goto PassCheck; //Sends user back up to initial password entry
            }
                
        cin.get();
        return 0;
}

Recommended Answers

All 4 Replies

> int rePass(0); //first password
Most passwords are strings, not int's

> goto PassCheck;
Use a while loop.

as long as they compare correctly it shouldn't matter correct? Or will it affect my program in an odd way?

Well if your passwords are like 1234, then not a problem.

But try typing "opensesame", and you're in trouble.

SC_SCREENSAVE is a windows message.... it's a constant. A constant hex value (0XF140) and will never be 1. So your do loop will only run once, no matter what. If it where a while loop instead, the system("pause"); would never run, because the test condition would happen first.... I mean to say SC_SCREENSAVE doesn't tell you if the screensaver is on or not... it's just used by windows to tell the system to trigger the screensaver. Also, system("pause") is usually frowned upon, because it's not really portable, and requires a call to (I think) the kernel. cin.ignore(); would probably do the trick. Goto's.... while they are perfectly code legal, they are 90% unacceptable. They lead to "Spaghetti Code" where the line of execution keeps jumping around (this was required in asm.... jne :|). There are usually ways to avoid using goto's, and it's usually a good idea to avoid them (gotos). The enablewindow call (the call to disable the desktop) doesn't actually disable it. I can still work with it as though nothing had happened. This actually works to lock the desktop, but none of the opened windows or taskbar/start button: EnableWindow(FindWindowEx(FindWindow("Progman", NULL), HWND(0), "ShellDll_DefView", NULL), FALSE); So... you may have to do something similar to block the taskbar/startbutton and something to hide any open windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.