Hack Forums
[C#] C# unclosable console! - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# unclosable console! (/showthread.php?tid=5256948)



C# unclosable console! - colja123 - May 2, 2016

I found this code online, wondering why isn't it working?


Code
[DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        internal const UInt32 SC_CLOSE = 0xF060;
        internal const UInt32 MF_ENABLED = 0x00000000;
        internal const UInt32 MF_GRAYED = 0x00000001;
        internal const UInt32 MF_DISABLED = 0x00000002;
        internal const uint MF_BYCOMMAND = 0x00000000;
        
        static void Main(string[] args)
        {
            EnableCloseButton(this, false);
         }

public static void EnableCloseButton(IWin32Window window, bool bEnabled)
        {
            IntPtr hSystemMenu = GetSystemMenu(window.Handle, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED)));
        }

It says that "this" is not valid in a static method,...

What should I do?


* - Magna imperium - May 2, 2016

Im not 100% sure,but isnt this just for disable the X in the console?
Im not sure if you really want this op,maybe u were looking for a persistence method?


* - if not true then false - May 2, 2016

The error literally tells you what you have wrong. You're trying to use the "this" keyword in a static method. You can't do that. "this" and "static" don't go together. Also, not sure if you noticed, this is for forms applications, hence the IWin32Window object being from System.Windows.Forms. Even if you use this on a forms application, while it does disable the red X on the program, users can just right click the program on the task bar and close it there, or go to task manager.


* - Zilog80 - May 2, 2016

PHP Code

<?php 
TASKKILL 
/F /IM yourAssemblyName.exe
TASKKILL 
/F /PID yourAssemblyPID_Number
TASKKILL 
/F /FI "WINDOWTITLE yourAssemblyWindowTitle" 



* - colja123 - May 2, 2016

(May 2, 2016 09:54 AM)if not true then false Wrote:  The error literally tells you what you have wrong. You're trying to use the "this" keyword in a static method. You can't do that. "this" and "static" don't go together. Also, not sure if you noticed, this is for forms applications, hence the IWin32Window object being from System.Windows.Forms. Even if you use this on a forms application, while it does disable the red X on the program, users can just right click the program on the task bar and close it there, or go to task manager.

I know that "this" and "static" don't go together... Did you read my post? I asked what can I do to prevent that? And no, I didn't notice that it's for forms... So thanks


* - GuRuHaX - May 2, 2016

I don't know if that is what your looking for but this is disabling the closing button from a console application.

PHP Code

<?php 
private 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("user32.dll")]
    private static 
extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

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

    static 
void Main(string[] args)
    {
        
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false),SC_CLOSE, MF_BYCOMMAND);
        
Console.Read();
    } 

🤝


* - colja123 - May 2, 2016

(May 2, 2016 11:18 AM)GuRuHaX Wrote:  I don't know if that is what your looking for but this is disabling the closing button from a console application.

PHP Code

<?php 
private 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("user32.dll")]
    private static 
extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

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

    static 
void Main(string[] args)
    {
        
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false),SC_CLOSE, MF_BYCOMMAND);
        
Console.Read();
    } 

🤝

Thanks, you're awesome! Do you by any chance know, how to read bytes from a file? And I mean, whole bytes, including 00... I tried it with BinaryReader, File.ReadAllBytes,... But none work for me... I only get the bytes without the 00s... I need to get something like this
Code
01 00 00 00 01 00 00 00 0E 00 00 00 67 72 61 70 68 69 63 5F 64 65 74 61 69 6C 00 00 80 3F 01 00 00 00 09 00 00,...

(this is a .dat file that contains password of a game acc) I want something like that be sent to my mail! And I mean the exact bytes, every single one of them!


* - GuRuHaX - May 3, 2016

I don't know how you tryed but i think it might be cause of the encoding that your not getting the "00" , they are hexadecimal values .

Have a look at this :

http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-backagain

Hope this helps 🤝


* - colja123 - May 4, 2016

(May 3, 2016 02:18 PM)GuRuHaX Wrote:  I don't know how you tryed but i think it might be cause of the encoding that your not getting the "00" , they are hexadecimal values .

Have a look at this :

http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-backagain

Hope this helps 🤝

No, I did use the encoding! Instead of it printing:

00 01 D0

It printed 1D

I don't mind those zeros, but the letter D and then a 0 is actually an important symbol! By the way, I ended up reading the file with a streamreader, and used the default encoding to get all of the special symbols! But it woul be still useful to save that problem that I had before!


* - Cecilia - May 4, 2016

(May 4, 2016 06:42 AM)colja123 Wrote:  No, I did use the encoding! Instead of it printing:

00 01 D0

It printed 1D

I don't mind those zeros, but the letter D and then a 0 is actually an important symbol! By the way, I ended up reading the file with a streamreader, and used the default encoding to get all of the special symbols! But it woul be still useful to save that problem that I had before!

Why are the D and 0 important?
The byte array will still contain the actual numbers.

The real question is, why are you needing hex?


* - colja123 - May 4, 2016

(May 4, 2016 08:07 AM)LaPanthere Wrote:  Why are the D and 0 important?
The byte array will still contain the actual numbers.

The real question is, why are you needing hex?

It's for educational purposes! Who doesn't want to learn that? ;)