August 2004 - Posts

Determine if an executable is a Console Application in C#

Thanks to Scott's comment I went on the hunt to figure out how to tell if an executable is a console application or not. After much hunting I figured out how to do it, here is what I came up with. Enjoy!

public const int SHGFI_EXETYPE = 0x000002000;
public const int IMAGE_NT_SIGNATURE = 0x00004550;

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, int cbFileInfo, int uFlags);

public static bool IsConsoleApplication(string path)
{
    SHFILEINFO fi;
    int exeType = SHGetFileInfo(path, 0out fi, 0, SHGFI_EXETYPE);
    return (LoWord(exeType) == IMAGE_NT_SIGNATURE && HiWord(exeType) == 0); 
}
public static int LoWord(int dwValue)
{
    return dwValue & 0xFFFF;
}
public static int HiWord(int dwValue)
{
    return (dwValue >> 16) & 0xFFFF;
}

Windows Run Command Wish List

In order to get more familiar with the new features in version 2.0 of .NET and C# I have been working on a side project using C# Express Beta 1. The project that I have been working on is a replacement for the windows run command. I for one use the run command a lot and there has always been some features lacking. Currently the run command provides a drop down list of the last 26 typed commands and the auto complete functionality for the file system / browser history as described here. Some features that I have already implemented for my new run command are:
  • Auto complete commands - as you type you are given a suggestion list matching what you have typed and the command that matches the closest is filled in for you
  • Smart suggestion list - the commands you use the most often appear at the top of the suggestion list, and the commands are pre-populated with common commands that someone might run like:
    • Start Menu Shortcuts
    • Control Panel Applets
    • Favorites
    • Recently Opened Documents
    • Common Windows Commands - i.e. My Computer, My Pictures, Administrative Tools, etc.
    • All Executables in the Path
  • Easily create short commands - being able to define short strings to run the command of your choice, like defining "ie" to open up Internet Explorer.
  • Command hotkeys - being able to assign a hotkey for any of your custom commands.
  • List computer shares on network - when you type "\\" it provides a list of the computers on your LAN and then when you type "\" again it provides a list of the shares on that computer (including hidden shares)
Also one other side feature not necessarily related to the run command that I'm including is the ability to assign a global hotkey that will minimize the foreground application to the system tray.
 
What I would like to know is if there are other things people would like to see included in a replacement run command? Please leave your suggestions in the comments. Once I have a working version I will post it.
 
Update: For anyone interested I have released see this post for information about the release of my program Run++.

Turn AutoCompletion on for Run Command

Have you ever been typing a file path in the run command and had to hit the down arrow to select the file you want from the drop down list? Wouldn't it be better if as you typed it automatically filled in the text for you?
 
The run command uses the SHAutoComplete interface which is also how IE and most other auto completing text/combo boxes in windows do auto complete. By default the auto complete mode is set to suggest only, meaning just give a drop down of items that match what I have typed. There is also an auto complete mode to append, which automatically fills in the text with the closest match from the suggest list. The append mode is turned off by default but you can enable it by going to Internet Options > Advanced, and checking the box next to Use inline AutoComplete, or if you like you can change it in the registry Change the Auto Complete Mode [1]. Now when you type paths in the run command, open/save dialogs, IE address bar, and others that use the default auto complete registry settings you will have both the suggest and append mode.
 
On a side note the textbox and combobox in .Net 2.0 also have auto complete functionality using the same SHAutoComplete underneath. The one thing I don't like about the new auto complete functionality in .Net 2.0 is that the dropdown width is set to the width of the textbox and you can't change it in code, at least I couldn't figure out how, this forced me to implement my own auto complete functionality.
 
[1] http://www.regedit.com, if you are interested in the registry I highly recommend this site, it is one of my favorites.

Help prevent windows startup clutter

I have been planning on talking about cleaning up your windows startup for a while but it looks like Jonathan Hardwick bet me too it with his excellent post What is all this stuff doing on my computer?. I do however have a couple more things to add.

If you run msconfig you will see that there is a startup tab (see Jonathan's post for a screen shot) this is the primary place that small programs will install startup stuff. Things in the startup tab primarily come from the following locations:

From the Registry:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
(Note there is also a RunOnce under HKLM that allows for something to run only on the next boot of your machine)
 
From the Startup Folder:
%USERPROFILE%\Start Menu\Programs\Startup
%ALLUSERPROFILE%\Start Menu\Programs\Startup

Now since I really hate having my computer startup and loading about a billion icons in my system tray, I clean out all these files that I know I don't need. If you don't know what you are doing or you are unsure about something I would suggest that you use msconfig because it doesn't delete the items it just copies them to another location for safe keeping incase you want to re-enable them later.

Warning: This next part is meant for power users!

Once I have cleaned up those startup items I go a step farther and prevent anything from being put in those locations. I usually go into the registry and set the security permissions on those two Run keys mentioned above to read only for everyone. So I clear out all users and add the group "Everyone" and allow only "Read" permissions. If you don't have at least "Read" permissions then the items you have left to startup will not startup. You should be able to set permissions on registry keys in Windows NT, 2000, XP, and 2003, you can't on Windows 95, 98, or ME. Now you could do a similar thing for the Startup folders if you are running NTFS but in my experience the majority of programs use the registry Run keys as opposed to the Startup folder, so I usually don't worry about them.

Now for some words of caution. When installing some software that tries to write to those registry keys, you can get a variety of possible problems. Some programs will give you a warning that says something like "can't write to blah key" or "don't have permissions to blah key", and give you the standard "Abort", "Ignore", or "Retry" options. Most of the time I hit Ignore unless I know it is something I want in the startup and then I will change the registry permissions temporarily to "Full Control" and then hit "Retry". Other install programs just flat out fail, and usually if that happens and I suspect that it was because of the registry permissions I will temporarily grant "Full Control" and try to re-install. Once the program finishes its install and I know I don't want it in my startup then I will delete the key it created and the set the permissions back to "Read" only. I really hate it when and installer just simply fails, they should handle those conditions more gracefully. Another little tip is that I add those two Run keys to my registry favorites, yes that's right if you didn't know the registry also has a list of favorites.

One last thing if you apply these changes and you haven't installed XP SP2 yet wait because the SP2 install crashed and burned on my machine at the very end of the install, and then I had to enable the permissions and install again.;) Why oh why can't Microsoft handle these conditions more gracefully...

Disclaimer: Modifying the registry or file system security permissions can cause serious problems that may require you to reinstall your operating system. I cannot guarantee that problems resulting from modifications to the registry or file system security permissions can be solved. Use the information provided at your own risk.

Posted by puzzlehacker | with no comments

My blogging philosophy and my need to analyse my referral log

One of the primary reasons that I blog is to help the community, I don't really expect anyone to directly subscribe to by blog (just out of curiosity does anyone?). I depend on people finding my information through search engines. Most of my posts are technical things that for what ever reason I spent some time figuring out how to accomplish.

Like most people when I try to figure out how to do something I first do a google search and most of the time I can find what I'm looking for pretty quickly but there are times when I wish the information was easier to find. Those times are what most of my blog entries deal with. If I figure out a solution to that particular problem and it is easily bloggable then I will post a blog entry about it. The hope is that if anyone else has a similar problem I will save them the time that it took me to figure out the problem. Of course there is also a time issue, I obviously don't have time to post every solution that I figure out but I try to post or queue up the ones I believe will be the most helpful. For my blog queue I actually have a separate task items folder in Outlook so that if I have an idea for a post I will add a new task with a quick note about the idea.

So in order to determine if my blog is getting the correct google searches I want to analyze my referral log. I know I can manually check my referrals through the admin interface but has anyone written or know of any little utilities that will gather and parse all my referrals and give me some stats about the google searches that found my blog? If no one knows of any then I may try to write one when I get time.

Recusive GetFiles for DirectoryInfo via a C# Iterator

I have been working on a project using C# Express and so I have been playing around with some of the new C# 2.0 features. In my project I had a need to get all the files of a particular type in a given directory including all sub-directories. The DirectoryInfo class has a method GetFiles that takes a search pattern (ie "*.exe") but it only searches that directory it doesn't search sub-directories. So I figured this would be a good chance for me to play with these new things called iterators. At any rate I wrote a recursive version of GetFiles using an iterator so that I could do a simple foreach loop to get all the files recursively.

public static IEnumerable<FileInfo> GetFilesRecursive(DirectoryInfo dirInfo)
{
    return GetFilesRecursive(dirInfo, "*.*");
}
public static IEnumerable<FileInfo> GetFilesRecursive(DirectoryInfo dirInfo, string searchPattern)
{
    foreach (DirectoryInfo di in dirInfo.GetDirectories())
        foreach (FileInfo fi in GetFilesRecursive(di, searchPattern))
            yield return fi;
        
    foreach (FileInfo fi in dirInfo.GetFiles(searchPattern))
        yield return fi;
}

RoboForm - Bookmark/Password manager

Well I started using RoboForm a couple of days ago after reading the comments on Dan's post. So far I have to say I really like it. It fills in form data. It acts as a bookmark manager and if that bookmark needs login information then it will fill in that information and login so you don't have to login every time you visit that page. Another feature I like is that all the bookmarks (passcards as they call them) show up in both Firefox and IE. I'm almost thinking of using this as my standard bookmark manager as well as my password database. For security it has a master password so I only need to remember that one password. Also if I want more security I could store my data on a USB key drive.

We'll see how things go, I'm going to continue using it for a while to see if it meets my needs. Does anyone else use this or know of any problems with it?

Finished my internship

I have not been blogging much over the summer because I was busy with my internship but I'm back now so you will see a few more blog posts from me.

I spent the summer working on a cool project in Outlook. I don't know what I'm allowed to say about my project so I guess I better not say anything. You will just have to wait until for the next version of Office to find out.

The internship was a very nice experience for me. There were a ton of events from various Tech Talks to the Microsoft Company Picnic and lets not forget about dinner at Bill’s place. I got to experience the Microsoft Life for 12 weeks and I must say I really enjoyed it; I really liked the work atmosphere. The good news is that I will be able to enjoy the experience some more once I graduate because I have been extended an offer for full time employment.

So now I just need to finish up my Masters over this next year and then I will more than likely be heading back to Redmond for full time employment.

More Posts