Hello everyone, I have one question.. should be relatively simple but I just cant seem to find any info anywhere.. maybe Im just looking in the wrong place and need guidance..

Here is my problem.. I have the following code:

EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
    DrawMenuBar(GetConsoleWindow());
    GetSystemMenu(GetConsoleWindow(), FALSE);   //Disables [(CTRL + SPACE) && (ALT + F4)]
    //HWND hWnd = GetConsoleWindow();     //Gets a handle to the console window...
    //ShowWindow( hWnd, SW_HIDE );        //Will Hide Console Itself..
    //ShowWindow( hWnd, SW_RESTORE );     //Restores Console Itself..

It disables the close(x) button on a console program but lets you minimize and maximize..
It also disables the ctrl space click and alt f4.. so they cant exit it that way either..

I have commented out the hiding and showing of the window because thats not what I want to do.. I want the user to see the program running, know that its running but not be able to exit it until its finished running!

I have gotten as far as disabling the close buttons and shortcuts that way, but they can right click the program on the taskbar and press close that way Or they can close it through task manager..

I want to disable those options while its running but KEEP the program ON Screen.
Show/Restore console makes it disappear completely.. (again not what I want).

Question: Is there a way to make it not show in task manager & to make it not right clickable on the task bar?? I do NOT want to HIDE it from that task bar because if the user minimizes it, they will not be able to restore it. I just want to disable right clicking it or closing it from the task bar and task manager.

Thank you in advance.

Recommended Answers

All 4 Replies

*sigh* Guess I needa simplify my question since there were no responses for 17 hrs straight..

I know how to do this in windows form with the command this->ShowInTaskbar = false..

How do I do this in a normal console app?!?!

I'm not sure you can do the last two. The program gets some control over its own window (so you can remove the control buttons and the keyboard shortcuts etc), but I think that the taskbar is part of the window manager, so I'm not sure how much your program can (or should) affect its behaviour. You almost certainly can't hide the program or stop it begin closed in the task manager either.

Why would you want to do this, anyway? Isn't it just going to end up annoying your users?

Well If you wanna see the source code, here it is.. It basically just constantly runs on the computer checking if a process is running.. and exits it if its running too long.. Thing is the user isnt supposed to exit the program in any form or way other than removing it from the startup folder, or through the task manager..

Yes they can close the program through the task manager, and they can stop it from running on startup by removing it from the start up folder.. But I dont want the program to be closed by any buttons.. and the button shows up on the TASKBAR allowing it to be closed that way.

If it can be done with windows form command: ShowInTaskbar = false; Why cant it be done with a simple console program?

Also saw an example where someone said:

ShowWindow(hwnd, SW_HIDE);
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) | WS_APPWINDOW);
ShowWindow(hwnd, SW_SHOW);

I guess changing the style should make it disappear from the taskbar.. but it doesnt work for my console app :S

I believe those examples only works for API, MFC, FORM..

It really isnt meant to annoy the user.. My cousin has been put on a guest account on MY computer because he 2 classes and I wanted to test my c++ skills and write a program to limit how much time he plays a game or does a specific activity. Thats y I dont want it to be exitable.

#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));
#define _WIN32_WINNT 0x0500


#pragma comment(lib, "advapi32.lib")
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <time.h>	// class needs this inclusion

using namespace std;

//Declare Check Processes...
DWORD CountProcesses(CHAR *pProcessName) 
{
    DWORD dwCount = 0;
    HANDLE hSnap = NULL;
    PROCESSENTRY32 proc32;

    if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
        return -1;
    proc32.dwSize=sizeof(PROCESSENTRY32);
    while((Process32Next(hSnap, &proc32)) == TRUE)
        if(stricmp(proc32.szExeFile,pProcessName) == 0)
            ++dwCount;
    CloseHandle(hSnap); 
    return dwCount;
}
//End Declaration...
//Declare Thread...
namespace jsw {
    namespace threading {
        class auto_event {
        public:
            auto_event(): _event(CreateEvent(0, false, false, 0)) {}

            BOOL wait(DWORD timeout = 1) const
            {
                return WaitForSingleObject(_event, timeout) == WAIT_OBJECT_0;
            }

            BOOL set() { return SetEvent(_event); }
        private:
            HANDLE _event;
        };

        class thread {
        public:
            static thread start(
                LPTHREAD_START_ROUTINE fn, LPVOID args = 0, 
                DWORD state = 0, DWORD timeout = 5000)
            {
                return thread(CreateThread(0, 0, fn, args, state, 0), timeout);
            }

            static void sleep(DWORD milliseconds) { Sleep(milliseconds); }
            static void exit(DWORD exitCode) { ExitThread(exitCode); }
        public:
            thread(HANDLE thread, DWORD timeout): _thread(thread), _timeout(timeout) {}
            ~thread() { CloseHandle(_thread); }

            DWORD exit_code() const
            {
                DWORD exitCode = NO_ERROR;

                GetExitCodeThread(_thread, &exitCode);

                return exitCode;
            }

            HANDLE handle() const { return _thread; }
            BOOL is_alive() const { return exit_code() == STILL_ACTIVE; }
            DWORD join() { return WaitForSingleObject(_thread, _timeout); }
            DWORD suspend() { return SuspendThread(_thread); }
            DWORD resume() { return ResumeThread(_thread); }
            BOOL abort(DWORD exitCode) { return TerminateThread(_thread, exitCode); }
        private:
            HANDLE _thread;
            DWORD _timeout;
        };
    }
}
//End Thread Declaration
//Star Declaration for password in thread & Password output->screen = Asterisks..
DWORD WINAPI get_password(LPVOID args)
{
  using namespace jsw::threading;
 
  int ch;
  char *pword = (char*)((LPVOID*)args)[0];
  auto_event *e = (auto_event*)((LPVOID*)args)[1];
  int s = 0;
  
  puts ("Enter The Password To Allow the program to run: ");
  fflush(stdout);
  
  while ((ch = getch()) != EOF 
          && ch != '\n' 
          && ch != '\r')
  {
    if (ch == '\b' && s > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      s--;
      pword[s] = '\0';
    }
    else if (isalnum(ch))
    {
      putchar('*');
      pword[s++] = (char)ch;
    }
  }

  pword[s] = '\0';
  e->set();
 
  return NO_ERROR;
}
//End Declaration for password in thread..

//////////////////////////////////////////
// class test program:

string playagain;
int x;
int i = 0;
int u = 0;
int p = 0;
int k = 0;

void started();
void stopped();
void pwget();
void wtf();

//int main( int, char *[] )
int main(void)
{
    fflush(stdin);
    /*
    EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED); // Disables the Close Button.
    DrawMenuBar(GetConsoleWindow());
    GetSystemMenu(GetConsoleWindow(), FALSE);   //Disables [(CTRL + SPACE) && (ALT + F4)]
    */
    
    //HWND hWnd = GetConsoleWindow();     //Gets a handle to the console window...
    //ShowWindow( hWnd, SW_HIDE );        //Will Hide Console Itself..
    //ShowWindow( hWnd, SW_RESTORE );     //Restores Console Itself..
    
    //-----------------------Second way to disable Close Button-----------------------
    
    HWND hWnd = GetConsoleWindow();
	HMENU hMnu = ::GetSystemMenu(hWnd, FALSE); //hWnd is a handle to Console you obtained
	::RemoveMenu(hMnu, SC_CLOSE, MF_BYCOMMAND);
	//WS_SYSMENU;
//ShowWindow(hWnd, SW_HIDE);
//SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
//ShowWindow(hWnd, SW_HIDE);



    fflush(stdin);
    system("cls");
    cout<<"Welcome to GameBlock v1.0\n\n";
    REPEAT
    pwget();
    
   	bool quit = false;
		
	while(! quit) {
            
               {
                    cout<<"Timer Started\n";  
                    started();
                    quit = true;
                    system("taskkill /IM notepad.exe");
                    cout<<"\n";
				}
		}
		cout << "------------------------------\n\n" << endl;
		playagain.clear();
		playagain = "y";
        fflush(stdin);
        wtf();
	UNTIL(playagain == "n");
    playagain.clear();
    system("cls");
    
    return 0;
}

void pwget()
{    
     string password;
     password.clear();
     REPEAT
     char cProcess[80] = "notepad.exe";
     DWORD dwReturn;
     dwReturn = CountProcesses(cProcess);
     if(dwReturn != -1)
     {
                           if(dwReturn == 1)
                           {
                              cout<<"User Attempted to Open Program!\n\n";
                              
                              using namespace jsw::threading;
                              char pword[1024];
                              auto_event e;
                              LPVOID args[2] = {pword, &e};
                              thread worker = thread::start(get_password, args);
                              
                              if (e.wait(10000)){
                              string password(pword);
                                 if(password == "brandon")
                                 {
                                  system("cls");
                                  cout<<"Password was entered correctly.\n\n";
                                  Sleep(1000);
                                  system("cls");
                                  break;
                                  }
                                  else
                                  {
                                      system("cls");
                                      cout<<"Password is Invalid...\n\n";
                                      Sleep(1000);
                                      system("taskkill /IM notepad.exe");
                                      cout<<"\n";
                                      system("cls");
                                      cout<<"Welcome to GameBlock v1.0\n\n";
                                      pwget();
                                  }
                              }
                              else {
                              keybd_event(VK_RETURN, 0x3d, 0, 0);
                              keybd_event(VK_RETURN, 0x3d, KEYEVENTF_KEYUP, 0);
                              worker.join();
                              cout<<"\n\nUser Timedout..\n\n";
                              Sleep(1000);
                              system("taskkill /IM notepad.exe");
                              cout<<"\n";
                              system("cls"); 
                              cout<<"Welcome to GameBlock v1.0\n\n";
                              pwget();
                              }
                           }
                           else if(dwReturn == 0)
                           {
                               cout<<"Program Not Running\n";
                               Sleep(1000);
                               wtf();
                           }
     }
    UNTIL((password == "brandon"));
    password.clear();
    cout<<"In Minutes, enter the length of time the program should run: ";
    cin>>x;
    cin.ignore();
    x = x*60;
}

void stopped()
{
	bool quit = false;
	
	char cProcess[80] = "notepad.exe";
	DWORD dwReturn;
	dwReturn = CountProcesses(cProcess);
    if(dwReturn != -1)
     {
                           if(dwReturn == 0)
                           {
                              cout<<"Program Not running, User taking a break.. Timer Paused...\n\n";
                              Sleep(2000);
                              started();
                           }
                           else if(dwReturn == 1)
                           {
                                u = u;
                                //cout<<"Program Started/Resumed.. Timer Resumed..\n\n";
                           }
     }
}

void started()
{
     bool quit = false;
	
	char cProcess[80] = "notepad.exe";
	DWORD dwReturn;
	dwReturn = CountProcesses(cProcess);
					do
					{
                           Sleep(1000);
                           cout <<u/3600<<" hrs & "<<k<<" mins "<<p<< " s" << endl;

                           DWORD dwReturn;
                           char cProcess[80] = "notepad.exe";
                           dwReturn = CountProcesses(cProcess);
                           if(dwReturn != -1)
                           {
                             stopped();
                             if(dwReturn == 1)
                             {
                              u++;
                               if(p > 59)
                               {
                                    p = 0;
                                    p++;
                               }
                               else
                               {
                                    p++;
                               }
                               if((u/60) > 59)
                               {
                                  k = 0;
                               }
                               else
                               {
                                   k = u/60;
                               }
                             }
                           }
                           else if(dwReturn == 0)
                           {
                                Sleep(2000);
                               cout<<"Program Not Running... \n\n";
                           }
                      i++;
                    }while(i <= x);
                    fflush(stdin);
                    u = 0;
                    i = 0;
                    k = 0;
                    p = 0;
                    fflush(stdin);
}

void wtf()
{
    fflush(stdin);
    system("cls");
    cout<<"Welcome to GameBlock v1.0\n\n";
    REPEAT
    pwget();
    
   	bool quit = false;
		
	while(! quit) {
            
               {
                    cout<<"Timer Started\n";  
                    started();
                    quit = true;
                    system("taskkill /IM notepad.exe");
                    cout<<"\n";
				}
		}
		cout << "------------------------------\n\n" << endl;
		playagain.clear();
		playagain = "y";
        fflush(stdin);
        //main();
	UNTIL(playagain == "n");
    playagain.clear();
    system("cls");
}

Sigh Ill mark this one as solved too.. no one has an answer so no point keeping the thread open.. I assume they think im coding a virus or malicious program :S.. Though I'd question if I was capable of doing such a thing, i'd have answered my own thread and questions already.

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.