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
295 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
  • 4
  • 3
7 Comments
 
LVL 32

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 32

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 32

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 32

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now