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
310 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 33

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 33

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 33

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 33

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

14 Experts available now in Live!

Get 1:1 Help Now