Hide cmd console to my program in C # with Visual Studio

0

So far I have been able to change certain aspects from the console, but not hide it, so that it does not appear. I mean I have a program in console mode, but I do not want to visualize the cmd anymore, how could I make it so that the console CMD does not appear in my main program?

public class Program
{
    static Pool _pool = null;
    static Work _work = null;
    static uint _nonce = 0;
    static long _maxAgeTicks = 20000 * TimeSpan.TicksPerMillisecond;
    static uint _batchSize = 100000;

     public static void Main(string[] args)
    {
        while (true)
        {
            try
            {
                _pool = SelectPool();
                _work = GetWork();
                while (true)
                {
                    if (_work == null || _work.Age > _maxAgeTicks)
                        _work = GetWork();

                    if (_work.FindShare(ref _nonce, _batchSize))
                    {
                        SendShare(_work.Current);
                        _work = null;
                    }
                    else
                        PrintCurrentState();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.Write("ERROR: ");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine();
            Console.Write("Hit 'Enter' to try again...");
            Console.ReadLine();
            System.Threading.Thread.Sleep(5000);
        }
    }


    private static void ClearConsole()
    {
        Console.Clear();
        Console.WriteLine("*****************************");
        Console.WriteLine("*** Minimal Bitcoin Miner ***");
        Console.WriteLine("*****************************");
        Console.WriteLine();
    }

    private static Pool SelectPool()
    {
        ClearConsole();
        Print("Chose a Mining Pool 'user:password@url:port' or leave empty to skip.");
        //Console.Write("Select Pool: ");
        string login = "";
        //string login = ReadLineDefault("lithander_2:[email protected]:8332");
        return new Pool(login);
    }

    private static Work GetWork()
    {
        ClearConsole();
        Print("Requesting Work from Pool...");
        Print("Server URL: " + _pool.Url.ToString());
        Print("User: " + _pool.User);
        Print("Password: " + _pool.Password);
        return _pool.GetWork();
    }

    private static void SendShare(byte[] share)
    {
        ClearConsole();
        Print("*** Found Valid Share ***");
        Print("Share: " + Utils.ToString(_work.Current));
        Print("Nonce: " + Utils.ToString(_nonce));
        Print("Hash: " + Utils.ToString(_work.Hash));
        Print("Sending Share to Pool...");
        if (_pool.SendShare(share))
            Print("Server accepted the Share!");
        else
            Print("Server declined the Share!");

        Console.Write("Hit 'Enter' to continue...");
        Console.ReadLine();
    }

    private static DateTime _lastPrint = DateTime.Now;
    private static void PrintCurrentState()
    {
        ClearConsole();
        Print("Data: " + Utils.ToString(_work.Data));
        string current = Utils.ToString(_nonce);
        string max = Utils.ToString(uint.MaxValue);
        double progress = ((double)_nonce / uint.MaxValue) * 100;
        Print("Nonce: " + current + "/" + max + " " + progress.ToString("F2") + "%");
        Print("Hash: " + Utils.ToString(_work.Hash));
        TimeSpan span = DateTime.Now - _lastPrint;
        Print("Speed: " + (int)(((_batchSize) / 1000) / span.TotalSeconds) + "Kh/s"); 
        _lastPrint = DateTime.Now;
    }

    private static void Print(string msg)
    {
        Console.WriteLine(msg);
        Console.WriteLine();
    }

    private static string ReadLineDefault(string defaultValue)
    {
        //Allow Console.ReadLine with a default value
        string userInput = Console.ReadLine();
        Console.WriteLine();
        if (userInput == "")
            return defaultValue;
        else
            return userInput;
    }
}

The error with the suggested vbs method:

  

Problem signature: Problem Event Name: CLR20r3 Problem Signature   01: ConsoleApplication1.exe Problem Signature 02: 1.0.0.0 Problem   Signature 03: 5824bc4b Problem Signature 04: ConsoleApplication1
  Problem Signature 05: 1.0.0.0 Problem Signature 06: 5824bc4b
  Problem Signature 07: 1 Problem Signature 08: 1 Problem Signature   09: System.MissingMethodException OS Version: 6.1.7601.2.1.0.256.1
  Locale ID: 1043 Additional Information 1: 0a9e Additional   Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional   Information 3: 0a9e Additional Information   4: 0a9e372d3b4ad19135b953a78882e789

     

Read our privacy statement online:
link

     

If the online privacy statement is not available, please read our   privacy statement offline: C: \ Windows \ system32 \ en-US \ erofflps.txt

    
asked by Perl 10.11.2016 в 18:47
source

3 answers

1

A very simple way to delete the console is by changing the project type in Visual Studio.

Stages:

  • Open the Properties window of your project (click on the right to your project in Solution Explorer , and choose Properties )
  • Change the value of the Output Type field from Console Application to Windows Application and safeguard the change.
  • Re-compile the project.

Now when you run your exe , you will see that it does not open a console, although it is running.

But beware that without a console, your Console.WriteLine sentences lose their meaning. What's more, some sentences, such as Console.Clear() will send you a IOException with the message:

  

The handle is invalid.

If you do not want a console, it's best not to use the Console class in your program.

    
answered by 10.11.2016 / 20:24
source
2

The following code serves to minimize a window, with which you can then minimize the CMD

private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr window, int nCmdShow);        

public static void minimizaWindow(IntPtr window)
{
    if (window.ToInt32() != 0)
       ShowWindow(window, SW_MINIMIZE);
}

Now, this uses a structure of type IntPtr , here is a method to get this open CMD processes.

public static IntPtr getWindow(string titleName)
{
   Process[] pros = Process.GetProcesses(".");
   foreach (Process p in pros)
      if (p.MainWindowTitle.ToUpper() == (titleName.ToUpper()))
         return p.MainWindowHandle; //obtiene la primera ventana que coincida el nombre
}
    
answered by 10.11.2016 в 18:59
1

Launch the program with a visual basic script:

1.- You create a text file and paste this:

set objshell = createobject("wscript.shell")
objshell.run "Nombre-del-fichero-de-tu-programa.exe",vbhide

2.- You save it with the extension .vbs

3.- You execute that vbs script without the console.

    
answered by 10.11.2016 в 19:03