Posts Tagged ‘Windows’

Annoy Your Friends and Colleagues with Global Flags

Step 1

Open gflags.exe. It comes with the Debugging Tools for Windows.

Step 2

On the Image File tab, enter the name of a popular executable. Suggestions:

  • devenv.exe [developers]
  • outlook.exe [human resources]
  • excel.exe [bankers and finance dweebs]
  • iexplore.exe / firefox.exe [catch-all]
  • Step 3

    Press tab to refresh the form. Check “Debugger,” and enter sol.exe in the textbox. Press ok.

    Step 4

    Bask in the hilarity of your sophisticated prank.

    gflags.exe

Console.WriteLine with Wordwrap

Here’s a class I wrote that will write with wordwrap to standard out in a .NET console app. There are a few other nifty options.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace McKinley
{
    public sealed class Out
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct COORD
        {
            public short X;
            public short Y;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct CONSOLE_SCREEN_BUFFER_INFO
        {
            public COORD dwSize;
            public COORD dwCursorPosition;
            public short  wAttributes;
            public SMALL_RECT srWindow;
            public COORD dwMaximumWindowSize;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SMALL_RECT
        {
            public short Left;
            public short Top;
            public short Right;
            public short Bottom;
        }

        private const int STD_OUTPUT_HANDLE = -11;

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetConsoleWindow();

        [DllImport("kernel32.dll")]
        private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput,
            out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(int nStdHandle);

        private static CONSOLE_SCREEN_BUFFER_INFO StdOutInfo()
        {
            IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if(hOut != IntPtr.Zero)
            {
                 CONSOLE_SCREEN_BUFFER_INFO info;
                 if(GetConsoleScreenBufferInfo(hOut, out info))
                 {
                     return info;
                 }
            }
            return new CONSOLE_SCREEN_BUFFER_INFO();
        }

        private static Point GetCursorPosition()
        {
            CONSOLE_SCREEN_BUFFER_INFO info = StdOutInfo();
            return new Point(info.dwCursorPosition.X, info.dwCursorPosition.Y);
        }

        private static Point GetConsoleWindowSize()
        {
            CONSOLE_SCREEN_BUFFER_INFO info = StdOutInfo();
            return new Point(info.dwSize.X, info.dwSize.Y);
        }

        /// <summary>
        /// Writes <paramref name="val" /> to standard output with word wrap.
        /// Each line is indented by <paramref name="indent" /> characters,
        /// and is prefixed by the string specified by <paramref name="prefix"/>.
        /// </summary>
        public static void WordWrap(string val, int indent, string prefix)
        {
            int max = (GetConsoleWindowSize()).X;
            string pad = new string(' ', indent)+prefix;

            Regex r = new Regex(@"([\w\.]*(\s)?)");
            Match words = r.Match(val);

            int count = (GetCursorPosition()).X;
            if(count == 0)
            {
                 Console.Write(pad);
            }
            else
            {
                 Console.Write(prefix);
            }
            count = (GetCursorPosition()).X;
            while(words.Success)
            {
                 string word = words.Value;
                 count += word.Length;
                 if(count >= max-1)
                 {
                     Console.WriteLine();
                     Console.Write(pad);
                     count = word.Length + pad.Length;
                 }
                 Console.Write(word);
                 words = words.NextMatch();
            }
        }

        /// <summary>
        /// Writes <paramref name="val"/> to the standard output
        /// with word wrap. Each line is indented by
        /// <paramref name="indent"/> characters.
        /// </summary>
        public static void WordWrap(string val, int indent)
        {
            WordWrap(val, indent, string.Empty);
        }

        /// <summary>
        /// Writes <paramref name="val"/> to the standard output
        /// with word wrap.
        /// </summary>
        public static void WordWrap(string val)
        {
            WordWrap(val,0);
        }

        private Out() { }
    }
}

The Danger of The Code Project

I’d say it’s the sheer number of hacks available with minimal discussion or explanation, coupled with the tendency of these kinds of sites to outrank MSDN on google for certain things. This isn’t necessarily a critique of The Code Project site specifically, but rather internet code snippets in general.

I was writing a Windows service today, and I wanted it to have a tray icon to provide access to some options. This isn’t as straightforward as adding a NotifyIcon component, because by default services aren’t allowed to create windows and so forth.

After some googling I came across this.

Now, bitter experience has already taught me never to trust internet code samples. I won’t go into that. But what was interesting was that this article goes so far as to mention:

I am assuming that Microsoft left this out for a reason…

Well, as it turns out, they did. There are at least a few security concerns in this situation, and one should be more educated on the subject than I am to attempt it. At that point I backed up and went with a client/server control model. It was more robust anyway, but it would take longer to write.

I would be interested to know how many authors of production code grabbed and used this code snippet without a second thought. It was the top search result for my terms, so I’m guessing it would be a high number.

The tendency of people to believe everything they read is of course not new.