Home Contact

Igor Milovanović

.NET, cats and more...

VS2012 - How to manually convert .NET Class Library to a Portable Class Library

The portable libraries are the  response to the growing profile fragmentation in .NET frameworks. With help of portable libraries you can share code between different runtimes without dreadful

#ifdef PLATFORM

statements or even worse “Add as Link” source file sharing practices.

If you have an existing .net class library which you would like to reference from a different runtime (e.g. you have a .NET Framework 4.5 library which you would like to reference from a Windows Store project), you can either create a new portable class library and move the classes there or edit the existing .csproj file and change the XML directly. The following example shows how to convert a .NET Framework 4.5 library to a Portable Class Library.

image

First Unload the Project and change the following settings in the .csproj file:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

to:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable
\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

and add the following keys to the first property group in order to get visual studio to show the framework picker dialog:

<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

 

After that you can select the frameworks in the Library Tab of the Portable Library:

image

 

As last step, delete any framework references from the library as you have them already referenced via the .NET Portable Subset.

 

 

[1] Cross-Platform Development with the .NET Framework - http://msdn.microsoft.com/en-us/library/gg597391.aspx

[2] Framework Profiles in .NET: http://nitoprograms.blogspot.de/2012/05/framework-profiles-in-net.html



Visual Studio 2012 - Setting the target framework in C++ Projects

 

The Visual Studio 2012 doesn’t have a UI to set the Target Framework in C++ Projects.  

 v40

Target Framework : 4.0

 

The online documentation does say to edit the .vcxproj project and change the TargetFrameworkVersion Tag.

However, The C++ projects don’t have that tag by default. They just assume that the target framework is v4.0.

 

Instead, you have to add the TargetFrameworkVersion-Tag to the PropertyGroup Globals.

 

   1:  <PropertyGroup Label="Globals">
   2:      ...
   3:      <RootNamespace>...</RootNamespace>
   4:      <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
   5:  </PropertyGroup>

 

When you reload the project, the target framework version in your project will be changed.

v45

Target Framework : 4.5

 

[1] How to: Modify the Target Framework and Platform Toolset http://msdn.microsoft.com/en-us/library/ff770576.aspx



Mouse Button Swapper – A simple windows 7 Silverlight gadget

 

I am using the mouse with my left hand but I am not swapping the mouse buttons. (Old habit  from the times I was using public workstations at the university. I was too lazy to play with the system setup every time, so that I just moved the mouse from the right to the left side.)

I am also using multiple pointing devices with my notebook. (a gaming mouse with multiple buttons at home, a simpler one at work, and also from time to time the touchpad ). Normally I would setup the mouse button layout in the driver once  for every device . A few months ago however I bought a Roccat Kova gaming mouse. Cool, fast, precise but unfortunately also driverless. Swapping the mouse button layout for Kova and for all other pointing devices started being rather complicated: Click on Control-Panel, Mouse, Swap-Mouse Buttons etc.

After few times I had to do go through the Control Panel, I decided that I had enough of clicking (and I was also in the mood to play around a bit with the technology) so I wrote a small windows 7 gadget in Silverlight which can be used to quickly swap mouse buttons. This way I can just quickly double-click the gadget on the desktop to swap layout, without the need to go to control panel.( I am planning to add some automated device recognition to the gadget in the future, but for now I am pretty much ok with this.)

 

Swapping the mouse buttons under windows is actually a very simple API call:

 

    public static class Win32ApiUtilities
    {
        private static readonly int SM_SWAPBUTTON = 23;
 
        public static bool IsMouseSwapped()
        {
            return GetSystemMetrics(SM_SWAPBUTTON) > 0;
        }
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int GetSystemMetrics(int nIndex);
 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool 
            SwapMouseButton([param: MarshalAs(UnmanagedType.Bool)] bool fSwap);
    }

 

The problem with API-calls is that they can’t be done directly from Silverlight (silverlight runs in a sandbox), however there is a workaround :

 

A windows 7 gadget is actually a simple html page with some XML settings zipped and renamed to .gadget file. As Silverlight can communicate with the JavaScript engine of the host page, and the security settings of a gadget allow the browser to instantiate and invoke COM objects, the solution for the invocation of an API call from Silverlight can be done like this: Silverlight –> JavaScript –> COM Interop –> API Call. Not nice but doable. Here are the necessary steps:

 

Create a class and register the assembly for COM Interop:

 
[ComVisible(true)]
[Guid("4C5DE161-3D74-401A-88BF-85F05E2D77AC")]
[ProgId("MouseSwapper.MouseSwapper")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MouseSwapper : IMouseSwapper
{
    public void LeftySetting()
    {
        Win32ApiUtilities.SwapMouseButton(true);
    }
}

 

Write a script function  in the gadget HTML page to instantiate the ActiveXObject and invoke the method:

 

<script type="text/jscript">
        function LeftySetting() {
            var mouseSwapper = new ActiveXObject("MouseSwapper.MouseSwapper");
            mouseSwapper.LeftySetting();
        }
</script>
 
And call the script from Silverlight:
 
 
HtmlPage.Window.Invoke("LeftySetting");

 

This is how the gadget looks like:

 

image

image

 

 

 

 

 

If you are left handed and happen to have the same problem like me with your Kova (or any other driverless device) the working version of the gadget is below (You will need .NET Framework 4.0 and Silverlight 4 installed on your machine):

 



Working with Visual Studio Web Development Server and IE6 in XP Mode on Windows 7

 

(Brian Reiter from  thoughtful computing has described this setup in this StackOverflow thread. The credit for the idea is entirely his, I have just extended it with some step by step descriptions and added some links and screenhots.)

 

If you are forced  to still support Internet Explorer 6, you can setup following combination on your machine to make the development for it less painful. A common problem when developing on Windows 7 is that you can’t install IE6 on your machine. (Not that you want that anyway). You will probably end up working locally with IE8 and FF, and test your IE6 compatibility on a separate machine. This can get quite annoying, because you will have to maintain two different development environments where you might not have all needed tools available etc. If you have Windows 7, you can help yourself by installing IE6 in a Windows 7 XP Mode, which is basically just a Windows XP running in a virtual machine.

 

[1] Windows XP Mode installation

 

After you have installed and configured the XP mode (remember the security settings like Windows Update and antivirus software)  you should add the shortcut to the IE6 in the virtual machine to the “all users” start menu. This shortcut will be replicated to your windows 7 XP mode start menu, and you will be able to seamlessly start your IE 6 as a normal window on your Windows 7 desktop.

 

[2] Configure IE6 for the Windows 7 installation

 

If you configure your XP – Mode to use Shared Networking (NAT), you can now use IE6 to browse the sites on the internet. (add proxy settings to IE6 if necessary).

 

image

 

 

 

 

 

 

 

 

  

The next problem you will confront now is that you can’t connect to the webdev server which is running on your local machine. This is because web development server is crippled to allow only local connections for security reasons. In order to trick webdev in believing that the requests are coming from local machine itself you can use a light weight proxy like privoxy on your host (windows 7) machine and configure the IE6 running in the virtual host.

 
The first step is to make the host machine (running windows 7) reachable from the virtual machine (running XP). In order to do that install the loopback adapter and configure it to use an IP which is routable from the virtual machine. 
 

[3] How to install loopback adapter in Windows 7

 

After installation, assign a static IP which is routable from the virtual machine (in example 192.168.1.66)

image

 

 

 

 

 

 

 

 

The next step is to configure privoxy to listen on that IP address (using some not used port) .Change following line in config.txt:

 

#
#      Suppose you are running Privoxy on an IPv6-capable machine and
#      you want it to listen on the IPv6 address of the loopback device:
#
#        listen-address [::1]:8118
#
#
listen-address  192.168.1.66:8118

 

The last step is to configure the IE6 to use Privoxy which is running on your Windows 7 host machine as proxy for all addresses (including localhost)

 

image

 

 

 

 

 

 

 

 

 

 

 

 

And now you can use your Windows7 XP Mode IE6 to connect to your Visual Studio’s webdev web server.

 

image

 

 

 

 

 

 

 

 

 

[4] http://stackoverflow.com/questions/683151/connect-remotely-to-webdev-webserver-exe



.NET Day Franken Community Conference

 

The call for papers for the first franconian .NET community conference is open.

[1] http://www.dotnet-day-franken.de/Sprecher.html