Second Stanza

January 13, 2008

Combination Console and GUI .NET App

Filed under: .NET Examples — Tags: , — dfbaskin @ 9:57 pm

Here’s a simple example of how to create a .NET application that can be run either as a console application or a GUI application.


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ConsoleGUIApp
{
    static class Program
    {
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool FreeConsole();

        [STAThread]
        static void Main( string[] argLst )
        {
            if(( argLst.Length > 0 ) && Regex.IsMatch( argLst[0], "^[-/]gui$", RegexOptions.IgnoreCase ))
            {
                FreeConsole();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault( false );
                Application.Run( new GUIMode() );
            }
            else
            {
                ConsoleMode app = new ConsoleMode();
                app.Run();
            }
        }
    }

    public class ConsoleMode
    {
        public void Run()
        {
            Console.WriteLine( "Running from console ..." );
        }
    }

    public class GUIMode : Form
    {
        public GUIMode()
        {
            Text = "My GUI Form";
        }
    }
}

Make sure you compile the source as a “Console” application. By default, the application will run in console mode.  Add the “/gui” option to the command to run in GUI mode.

Advertisement

January 11, 2008

ZLIB Net Wrapper

Filed under: Open Source — Tags: — dfbaskin @ 10:38 pm

A while back, I created a C++ .NET 1.1 wrapper around the zlib compression library (the SourceForge project is here). It hasn’t been updated in a while, but it worked well for what I used it for.

I haven’t updated it to .NET 2.0 (or later) yet, since VC2005 no longer supports compiling of C code into mixed-mode DLLs. I assume that I will need to just create a static library from the C source and link it into the mixed-mode DLL.

Download / View Source Code
(View requires Microsoft Silverlight 1.0 and Internet Explorer)

Create a free website or blog at WordPress.com.