Archive for the ‘Visual Studio Tips’ Category

Visual Studio Tip: Import a *.reg file

Wednesday, April 22nd, 2009

Screenshot of Visual Studio IDE showing the Registry Import menu item

Here is a handy feature of Visual Studio which took me a while to discover when I started using the tool.

As part of packaging up an application it is common to store configuration settings within the registry. While using a Smart Device CAB project you can use the convenient Registry view to set up these values graphically.

The feature that I didn’t notice for a while is the fact that you can right click on the window to get an Import… menu item (as shown in the screenshot above). This enables you to import a group of settings from a *.reg file, which saves you manually entering them all via the IDE.

So how do you create a *.reg file? One way is to manually write one in a text editor. Another is to generate it via the Windows CE Remote Registry Editor utility. Once you have configured your device you can use this tool to export a specific sub-tree of the device’s registry as a *.reg file that you can then import directly into Visual Studio.

Screenshot of Windows CE Remote Registry Editor utility showing the Export Registry File menu option

Visual Studio Tip: Selecting a Deployment Device

Saturday, April 5th, 2008

Screenshot of Target Device combo box located on Visual Studio 2008 toolbarHere is a small tip for C++ Smart Device developers who use Visual Studio. While developing native applications it is common to target an older version of the Device SDK in order to produce a single executable that will run on a wide range of devices. For example you may purposely target the Pocket PC 2003 Device SDK in order for your executable to be compatible with any Windows Mobile 2003, 5.0, 6 or 6.1 device.

One problem with this technique is that by default the target device drop down used to select the device used for deployment and debugging purposes is filtered to only show the devices that match the selected version of the Device SDK.

If your application makes use of functions such as LoadLibrary and GetProcAddress to conditionally take advantage of functionality present in newer versions of the Windows Mobile operating system (as discussed previously) it can be handy to debug your application on a more recent version of Windows Mobile while still compiling against the older version of the Device SDK.

Luckily this can easily be achieved, but the feature is slightly hidden. If you open the Project Properties dialog (right click on the project within Solution Explorer and select Properties) you will be able to find a “Deployment Device” setting within the “Deployment” section. You will notice that this combo box displays all currently installed emulators for all versions of Windows Mobile. By selecting an emulator from this list and clicking OK you will notice that the Target Device combo box updates to reflect your selection even if the selected emulator is for a different version of Windows Mobile.

Screenshot of project settings dialog showing deployment device settings

Visual Studio Tip: Open containing folder

Wednesday, March 26th, 2008

A handy feature while editing files within Visual Studio is to open up a Windows Explorer window for the folder that contains the selected file (perhaps to rename a set of files, or to change the attributes etc). This feature has been within the Visual Studio IDE for a few revisions. The easiest way to access it is to open a file in the editor, then right click on the file’s tab and select the “Open Containing Folder” menu option.

Extended Functionality

One problem with this feature is that it requires the file to be opened within the editor in order to be able to right click on its tab. For forms and other project files with complex editor behaviour it can be a timely exercise waiting for the file to load into the editor just to be able to right click on it.

A handy solution to this problem within Visual Studio 2008 is an add-on called the “PowerCommands for Visual Studio 2008“. This extension, written by Microsoft, adds a set of additional functionality to various context menus within the IDE. The two most relevant to this discussion are the “Open Command Prompt” and “Open Containing Folder” options available while right clicking files within the Solution Explorer window (as seen in the screenshot to the right). This saves needing to load the file within the editor in order to access this option and is a great productivity boost. For more details check out the Visual Studio Extensibility (VSX) team blog. The PowerCommands extension is worth installing for just this feature, but also introduces a range of additional ones you will soon not be able to live without.

Going even further

IDE productivity fanatics and keyboard power users may be interested in the following additional things which may further maximise the use of this feature:

  • You could follow the “How to: Work with Shortcut key Combinations” article on MSDN in order to bind the feature to a keyboard shortcut. The command is “File.OpenContainingFolder”.
  • You could add this feature as a menu item within the Tools menu by following the instructions recently given on the .NET Tip of The Day blog. I for one didn’t think about using Visual Studio environment variables such as $(ItemPath) within this dialog.

If you like these kinds of IDE productivity tips, you might like to subscribe to Sara Ford’s blog.

Visual Studio Tip: Three ways to use C# within a VB.NET project

Monday, March 10th, 2008

The .NET framework supports a wide range of languages meaning it is not uncommon to find code examples in a language that you are not familiar or comfortable with. This blog entry discusses some approaches that can be used to include portions of code written in a foreign language within your own application. We will demonstrate the technique by utilising C# code within a VB.NET project.

Technique 1: Code Translation

If you only have a couple of lines of C# to include within your VB.NET application you may like to attempt to manually convert the code into VB.NET so that it can be incorporated directly. If you are having difficulty converting the code (or do not have enough time to learn the specifics of another language) you could try an online code translation service such as the Developer Fusion C# to VB.NET code conversion tool which will attempt to automatically translate the code for you.

For example, given the following C# code:

public event EventHandler Foo;
 
public void FireFoo()
{
  EventHandler eh = Foo;
  if (eh != null)
    eh(this, EventArgs.Empty);
}

Developer Fusions’ code translation tool will automatically provide the following VB.NET conversion:

Public Event Foo As EventHandler 
 
Public Sub FireFoo() 
    Dim eh As EventHandler = Foo 
    RaiseEvent eh(Me, EventArgs.Empty) 
End Sub

Notice how the code translator is reasonably intelligent, for example it has removed the if statement from the original C# code snippet in favour of the VB.NET specific RaiseEvent statement which internally performs the same check. As with any automated translation utility, your milage may vary with respect to how well the translator copes with your particular code snippet.

Although this technique is useful for small amounts of code it does not lend itself well to converting large amounts of source code, or source code that utilises specialised features of a given language. For this we may like to investigate an alternative technique.

Technique 2: Class Library

Screenshot showing Solution Explorer for a solution containing both C# and VB.NET projectsOne of the many specifications surrounding the .NET environment is one called the Common Language Specification (CLS). This is a subset of CLR features that any language targeting the .NET runtime should support in order to be fully interoperable with other CLS-compliant languages. In other words if you stick to using CLS compliant language features your code should be usable in projects written in a wide range of other languages that also support the Common Language Specification. C# and VB.NET both support the Common Language Specification making it easy for these two languages to interoperate.

If you have a large amount of C# code (such as an entire class) that you would like to include within a VB.NET project you can create a C# class library project and include the source files within it. This allows you to compile the C# source code unmodified and without needing to fully understand how it works. After compilation all you have to do is add a reference to the class library from your VB.NET application as shown in the screenshot to the right. You will then be able to access the C# classes using standard VB.NET syntax.

While writing code how do you know if you are using the subset of language features that are CLS compliant? The easiest way is to add the CLSCompliant attribute to your assembly. If set to true, this will cause the C# or VB.NET compilers to emit warnings whenever they detect that you have used a language feature (or datatype etc) that is non CLS compliant.

For example within a C# class library you might add the following line to the AssemblyInfo.cs file:

using System
[assembly: CLSCompliant(true)]

Technique 3: Netmodules and assembly linking

I find as a .NET developer it is important to understand the limitations and artificial restrictions imposed by the tools I utilise. You may be surprised to know that Visual Studio does not provide access to every feature of the .NET platform.

One feature not supported by Visual Studio is the ability to compile assemblies that consist of code written in more than one language. For example having an assembly where one class is written in C# while another is written in VB.NET (or even MSIL etc).

If you are willing to drop down to command line compilation it is possible to achieve this. For example given two source files foo.cs and bar.vb we can create an assembly called myassembly.dll by executing the following commands from a Visual Studio command prompt window.

SET NETCF_DIR="C:\progra~1\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE"
 
csc /noconfig /nostdlib /r:"%NETCF_DIR%\mscorlib.dll" /t:module foo.cs
vbc /netcf /noconfig /nostdlib /sdkpath:%NETCF_DIR% /t:module bar.vb
 
link /LTCG /verbose /dll /out:myassembly.dll foo.netmodule bar.netmodule /subsystem:windows

The calls to the C# (csc) and VB.NET (vbc) command line compilers specify the /t:module parameter which causes the compiler to produce a netmodule (foo.netmodule and bar.netmodule respectively). A netmodule can be thought of as being similar to an object (*.obj) file that a native C or C++ developer may be familiar with.

Using the linker (link.exe) enables one or more netmodules to be merged into an assembly, in this case a DLL named myassembly.dll. The linker is not concerned with what language the individual netmodules were compiled from.

Sample Application

[Download multilanguageprojects.zip - 16KB]

The first sample application demonstrates creating a C# class library that can be consumed by a VB.NET winforms application. It takes the textbox cursor location code snippet I wrote in C# and turns it into a C# class library called CSharpClassLibrary.dll. This DLL is then added as a reference to the VB.NET application which makes use of the functionality to highlight the current cursor location within a multiline textbox. Only a minimal amount of C# knowledge was required to make this happen, just enough to wrap the code snippet up into a public class.

This application also demonstrates accessing functionality from an assembly which has classes written in more than one language. Within the MultiLanguageClassLibrary subdirectory you will file a small batch file that demonstrates using the command line compilers to build an assembly that contains individual classes written in C# and VB.NET. As you will see from the VB.NET sample application’s menu item there is no difficulty accessing the functionality of these classes once a reference to the assembly has been added.

[Download gps.zip - 23KB]

This blog entry was spawned by a request for a GPS Intermediate Driver example in VB.NET. The Windows Mobile 5.0 SDK includes an example of how to use this API, but it is written in C#. I have re-coded the GUI part of the GPS Intermediate Driver example into VB.NET and hence produced another example of a C# class library being used from VB.NET. This sample can be downloaded from the link above.

Visual Studio Tip: Discovering the Remote Tools

Thursday, March 6th, 2008

Screenshot showing path to Visual Studio Remote Tools within the Windows Start Menu

“Old” Embedded Visual C (EVC) developers will be familiar with a set of Remote Tools that enable developers to perform various tasks on a connected Windows Mobile device from the comfort of their desktop PC.

The good news is that these tools have made their way into Visual Studio 2005 and 2008. However it appears that they are less discoverable in their new home, based upon the number of forum requests where people seem to be unaware of their existence.

Instead of being located within a Tools menu within the IDE itself, the Remote Tools are now available via your Windows Start Menu, in a “Visual Studio Remote Tools” submenu. Within this menu you should find shortcuts to the following 6 tools:

  • Remote File Viewer – View and manage the file system on a target device (including exporting and importing files).
  • Remote Heap Walker – Display information about heap identifiers and flags for each process running on a target device.
  • Remote Process Viewer – Display information about each process (application) running on a target device
  • Remote Registry Editor – Display and edit the registry for a target device. Including the ability to import and export *.reg files.
  • Remote Spy – Display messages received by windows associated with applications running on a target device
  • Remote Zoom-in – Capture a screen shot from a target device and copy to the clipboard or save to disk.

These tools can come in handy while developing and debugging your smart device applications. If you use these tools regularly and hanker for them to be in the Tools menu of the Visual Studio IDE you can manually configure this by following the instructions listed on the Visual Studio for Devices team blog.

Visual Studio Tip: Document Outline window

Sunday, January 13th, 2008

Screenshot showing Document Outline window within Visual Studio 2005While developing System.Windows.Forms based applications it is common to end up with controls which are deeply nested within other controls (such as panels), or that are hard to select with the mouse (hidden behind another control). The Document Outline window is one solution to this problem.

Accessing the Document Outline Window
To enable the Document Outline Window select Document Outline within the Other Windows submenu of the main View menu (this feature also has the keyboard shortcut Ctrl + W, U).

Uses of the Document Outline Window
The Document Outline window provides a treeview showing the hierarchy of controls within the current form. By selecting a control within the Document Outline window you also select it within the Forms Designer and Properties window. By dragging and dropping within the Document Outline Window you can reparent and re-order controls within the form.

The feature I most use this window for is to re-order controls which I have docked to the same side of a panel. If you dock two or more controls to the top of a panel for example it is not as simple to use the mouse within the Form Designer to change which control is positioned at the top. Using the Document Outline window allows you to click and drag the controls to re-order them within the panel, and hence change the way the Docking and Anchoring layout algorithm is applied to them.

Another reason I use this feature is that it can be helpful since it does not loose all the event handlers you have hooked up to the controls which can occur if you cut and paste controls for example.