Category Archives: News

General news

Creating Addins – ‘An error occurred, and the wizard could not generate the project.’

When doing a little bit of work on a solution that contains a Visual Studio Addin the other day, I noticed that there’s a little bit of an issue with Visual Studio. If you create an addin project and you get the message:

An error occurred, and the wizard could not generate the project. Verify that the programming language is properly installed.

Then double check where you are creating your addin. If it is in a child folder of the solution, then this error can occur. The solution – add the addin project to the solution root. Then if you need to, you can move it afterwards.

This issue occurs in Visual Studio 2012, but a bit of googling suggests that it may also be an issue in 2010.

Getting Paths for Files in NUnit Tests

When using NUnit, sometimes you will want to access files in the test project. These might be xml files with data, assembly references or whatever. Now typically, NUnit will actually copy the files it thinks it needs into a temporary location. This causes the problem that you can then do things like use a relative path to get files in the project. You can use manifest resource streams but sometimes this just isn’t suitable.

To get the path of the root of your test project, you can use the snippet below. Make sure you call it in a unit test fixture that’s actually in your test project, not from a class referenced in another project!

This class, ‘TestHelper’ can be included in a Unit Test project to let you quickly get the path to the test project.

public static class TestHelper
{
    public static string GetTestsPath()
    {
        return Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", string.Empty);
    }
}

Introducing FireKeys

I don’t know when I learnt that Windows + E opened up Windows Explorer. It must have been a while ago. But it’s imprinted in my muscle memory, the number of times I hit that combo every day is probably quite high. But how many other hotkeys do I use? Asides from a few other functional ones, like Win + D, I don’t use hotkeys so much. And I got to thinking, I’d love to open Google Chrome with a hotkey just like I do with explorer.

So I wrote FireKeys – a lightweight application that lets you assign hotkeys to actions. These actions could be opening program, a folder or a URL, but the underlying model is designed to be extensible.

FireKeysMain

You can get the tool from the FireKeys page. There’s an article on how it was developed on the CodeProject, FireKeys – Open Programs, Folders and URLs with Hot Keys.

Spider Solitaire and Augmented Reality

A while ago, I made an implementation of Solitaire and Spider Solitaire using WPF and my Apex MVVM library. I wrote about it on the CodeProject, in an article called Solitaire and Spider Solitaire for WPF (imaginative title indeed).

Anyway, just recently I got a very interesting message from rupam rupam, who has made an augmented reality version of the project! In his application, you use your webcam to play the game physically by picking up cards with gestures. Other gestures, like thumbs up and thumbs down are bound to commands in the game – here’s a screenshot:

SpiderAugmented

The project is called GesCard and as far as I know there isn’t a page showing the code – but there are more links on the YouTube video for the page. Check out the YouTube video with the link here https://www.youtube.com/watch?v=wCOjuPdBooI. Thanks to rupam for getting in touch and sharing this very cool code!

 

The Visual Studio Experimental Instance

Working on some addins lately has taught me a few really useful tricks about debugging in Visual Studio. I’ll update this post over time.

The Experimental Instance

Very useful to know – the experimental instance loads its extensions from a special folder, and debugging extensions drops them there. The location is:

%UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\

Creating Info Tip Handlers with .NET

I have just added an article to the CodeProject that discusses how to create Info Tip shell extensions in .NET. These extensions are used by the shell to customise the tooltips shown over shell items.

ShellInfoTipHandler

The article shows how you can use SharpShell to very quickly create these extensions, you can find it at: http://www.codeproject.com/Articles/527058/NET-Shell-Extensions-Shell-Info-Tip-Handlers.

So just how easy does SharpShell make creating Shell Info Tip Handlers? The answer is pretty easy indeed. The code below shows the full implementation of a Shell Info Tip Handler that changes the tooltips for folders to show the name of the folder and the number of items it contains:

/// <summary>
/// The FolderInfoTip handler is an example SharpInfoTipHandler that provides an info tip
/// for folders that shows the number of items in the folder.
/// </summary>
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
public class FolderInfoTipHandler : SharpInfoTipHandler
{
    /// <summary>
    /// Gets info for the selected item (SelectedItemPath).
    /// </summary>
    /// <param name="infoType">Type of info to return.</param>
    /// <param name="singleLine">if set to <c>true</c>, put the info in a single line.</param>
    /// <returns>
    /// Specified info for the selected file.
    /// </returns>
    protected override string GetInfo(RequestedInfoType infoType, bool singleLine)
    {
        //  Switch on the tip of info we need to provide.
        switch (infoType)
        {
            case RequestedInfoType.InfoTip:
 
                //  Format the formatted info tip.
                return string.Format(singleLine
                                       ? "{0} - {1} Items"
                                       : "{0}" + Environment.NewLine + "Contains {1} Items",
                                       Path.GetFileName(SelectedItemPath), Directory.GetFiles(SelectedItemPath).Length);
 
            case RequestedInfoType.Name:
                
                //  Return the name of the folder.
                return string.Format("Folder '{0}'", Path.GetFileName(SelectedItemPath));
                
            default:
 
                //  We won't be asked for anything else, like shortcut paths, for folders, so we 
                //  can return an empty string in the default case.
                return string.Empty;
        }
    }
} 

As you can see, all of the COM interfaces are hidden away and handled for you, there is no ugly pinvoke code and no use of strange structures imported from Win32. SharpShell handles all of the plumbing for you.

SharpShell

SharpShell is a project that I have recently uploaded to CodePlex. This class library, and set of tools and samples, is designed to be a framework to enable rapid development of Shell Extensions using the .NET Framework. In time it may grow to contain some functionality for using Shell entities within managed applications (for example, allowing an Explorer context menu to be built dynamically for a given path).

Anyway, the code is all at sharpshell.codeplex.com. You can also see a nice article on the CodeProject that show’s how to create a Shell Context Menu Extension using C#, the article is at: .NET Shell Extensions – Shell Context Menus.

Screenshot1_ExampleIconHandler

Above: An example of a Managed Shell Extension. This sample colours the icons for dlls differently, depending on whether they are native dlls or assemblies.

So far, in the repo on CodePlex there are also samples for Shell Icon Handlers (which customise icons in Explorer) and Shell Info Tip Handlers (which customise tooltips). Both of these extension types are fully supported in the current dev version and will be released in the next few days. There’s also a partially functioning Shell Property Sheet implementation which will be delivered in the subsequent version. The Shell Property Sheet introduces some particularly strange code – 32 and 64 bit C++ dlls are embedded as manifest resource streams and extracted as needed to provide access to C++ function pointers – ouch.

More to follow – check out the project and the article.

SharpGL 2.1

For those who are interested, I’m now starting development of SharpGL 2.1. SharpGL 2.1 will primarily be a release to implement features and fix bugs that users have added to the Codeplex site. The actual features and bugs that’ll be sorted are on the CodePlex site – just search for release ‘SharpGL 2.1′.

This will also be the first release of SharpGL that will be published on Nuget.

Visual Studio Extensions and Menu Subitems

Recently I was working on a Visual Studio Extension for VS2010, instead of a single item in the the tools menu, what I wanted was a single item with a set of child items.

Strangely enough, documentation on this is quite lacking. So if you need to know how to do it, here’s the gist. First, create a standard Visual Studio 2010 extension with the wizard, we’ll have some code like the below to start off with, in the OnConnection function:

object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";

//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

//Find the Tools command bar on the MenuBar command bar:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
//  just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
	//Add a command to the Commands collection:
    Command command = commands.AddNamedCommand2(_addInInstance, "MyCommand", "MyCommand",
        "Executes the command for MyCommand", true, 59, ref contextGUIDS,
        (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
        (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

   	//Add a control for the command to the tools menu:
	if((command != null) && (toolsPopup != null))
	{
		command.AddControl(toolsPopup.CommandBar, 1);
	}
}
catch(System.ArgumentException)
{
	//If we are here, then the exception is probably because a command with that name
	//  already exists. If so there is no need to recreate the command and we can
    //  safely ignore the exception.
}

Now what we’re going to do first, is change the code so that we don’t actually add a Command named MyCommand, but instead a popup:

//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
//  just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
	//  Have we got the tools popup?
	if(toolsPopup != null)
	{
        //  Create 'MyCommand' as a popup.
        var popup = (CommandBarPopup)toolsPopup.Controls.Add(MsoControlType.msoControlPopup);
        popup.Caption = "MyCommand";
	}
}
catch(System.ArgumentException)
{
	//If we are here, then the exception is probably because a command with that name<br />
	//  already exists. If so there is no need to recreate the command and we can<br />
    //  safely ignore the exception.
}

Now that we have the popup object, we can create commands and add them to the popup instead:

//  Have we got the tools popup?
if(toolsPopup != null)
{
    //  Create 'MyCommand' as a popup.
    var popup = (CommandBarPopup)toolsPopup.Controls.Add(MsoControlType.msoControlPopup);
    popup.Caption = "MyCommand";
    //  Create sub item 1.
    var subItem1Command = commands.AddNamedCommand2(_addInInstance, "MyCommand1", "My Command Subitem 1", "My Command Subitem 1",
                                                       true, 59, ref contextGUIDS);
    //  Add it.
    subItem1Command.AddControl(popup.CommandBar, 1);
    //  Create sub item 2.
    var subItem2Command = commands.AddNamedCommand2(_addInInstance, "MyCommand2", "My Command Subitem 2", "My Command Subitem 2",
                                                       true, 59, ref contextGUIDS);
    //  Add it.
    subItem2Command.AddControl(popup.CommandBar, 2);
}

Now that we have made these changes, if we run the addin, we get a menu structure like this: