Solved

ConsoleNoClose.exe for Console Apps on Windows to eliminate the X to Close - Has anyone used this app and could you give me some feedback on the results?

Posted on 2014-10-20
7
335 Views
Last Modified: 2014-10-30
I have a windows Console application that runs on XP, Win7, Win8, WinServer2003+

It has always been a problem when users us the X in the upper right to close the application.  I saw a posting about this utility called ConsoleNoClose.exe which runs in the background and will disable the X to close.  

I wondered if any expert that watches this list has used this utility and if there are any gotcha's that might be an issue.

When I searched the web, I saw more hits about Removing the utility than installing and it got me a bit worried that the use of the utility caused issues that I might not be anticipating.

If you have a better solution to eliminating the X in a Windows Console application, I would be glad to know what else I might investigate.
0
Comment
Question by:nlpalmquist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40393186
Is this your console application (something you wrote) or a third-party console application?

-saige-
0
 

Author Comment

by:nlpalmquist
ID: 40395649
A custom application that I wrote.  It runs as a Microsoft 32bit console application in the environment that we are considering using this utility.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40395740
If the environment supports using a windows form application and the user does not need to interact with the console environment, you could change your application to be a windows form application that just displays the output returned from the console environment.

If this is a possibility, let me know, I can provide some sample code.

-saige-
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:nlpalmquist
ID: 40404883
That Change is not possible. But thank you for your responses.  I think I will recommend that we do not use this utility, as much as it might be helpful for poorly trained users.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40404985
I understand.  Otherwise, this is the best I could come up with.  It does not remove the close button but it does disable it:
using System;
using System.Runtime.InteropServices;

public class Program
{
	public static void Main()
	{
		NativeMethods.DeleteMenu(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_CLOSE, NativeMethods.MF_BYCOMMAND);
		Console.ReadLine();
	}
}

public static class NativeMethods
{
	public const int MF_BYCOMMAND = 0x00000000;
	public const int SC_CLOSE = 0xF060;

	[DllImport("user32.dll")]
	public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);

	[DllImport("kernel32.dll", ExactSpelling = true)]
	public static extern IntPtr GetConsoleWindow();

	[DllImport("user32.dll")]
	public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
}

Open in new window


In my testing, the Close button does not show as disabled, but hitting the close button does nothing.  Also Alt-F4 does not work nor does the close option show in the system taskbar menu, e.g -Close button is not marked as disabled but clicking does not close the console application.Right-clicking on the taskbar icon, does not show a close option.The same is true by right-clicking on the menu bar of the console window.  No close option.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40405010
Ok I found a way to gray the Close button as well (this code also includes disabling the minimize and maximize options and removing their menu entries):
using System;
using System.Runtime.InteropServices;

public class Program
{
	public static void Main()
	{
		NativeMethods.EnableMenuItem(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_CLOSE, NativeMethods.MF_GRAYED); 
		NativeMethods.DeleteMenu(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_CLOSE, NativeMethods.MF_BYCOMMAND);
		NativeMethods.EnableMenuItem(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_MAXIMIZE, NativeMethods.MF_GRAYED);
		NativeMethods.DeleteMenu(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_MAXIMIZE, NativeMethods.MF_BYCOMMAND);
		NativeMethods.EnableMenuItem(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_MINIMIZE, NativeMethods.MF_GRAYED);
		NativeMethods.DeleteMenu(NativeMethods.GetSystemMenu(NativeMethods.GetConsoleWindow(), false), NativeMethods.SC_MINIMIZE, NativeMethods.MF_BYCOMMAND);
		Console.ReadLine();
	}
}

public static class NativeMethods
{
	public const int MF_BYCOMMAND = 0x00000000;
	public const int MF_GRAYED = 0x1;
	public const int MF_ENABLED = 0x00000000;
	public const int MF_DISABLED = 0x00000002;
	public const int SC_CLOSE = 0xF060;
	public const int SC_MINIMIZE = 0xF020; //for minimize button on forms
	public const int SC_MAXIMIZE = 0xF030; //for maximize button on forms

	[DllImport("user32.dll")]
	public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);

	[DllImport("user32.dll")]
	public static extern int EnableMenuItem(IntPtr hMenu, int tItem, int tStatus);

	[DllImport("kernel32.dll", ExactSpelling = true)]
	public static extern IntPtr GetConsoleWindow();

	[DllImport("user32.dll")]
	public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
}

Open in new window

Produces the following -Disabled and greyed out close button.  Minimize and maximize show as grey but are still selectable, again they do not acutally function.Close, Minimize and Maximize options do not appear on the menu either.
-saige-
0
 

Author Closing Comment

by:nlpalmquist
ID: 40414013
I appreciate your suggestions.  Thank you very much.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question