Fernando Machado Píriz's Blog

Posts about enterprise architecture and related topics

.NET UY Conf, October 3rd & 4th

leave a comment »

Written by fernandomachadopiriz

June 11, 2014 at 4:58 pm

Posted in Announcements

Connecting an Hyper-V Virtual Machine to the Internet through a Wireless Connection

leave a comment »

In a previous post I wrote about how to configure your laptop to act alternatively as a virtualization server or as a nearly-standard desktop. Why would you do that? Well, if you need to use 64 bits virtual machines on top of Microsoft’s virtualization solutions, your only choice is to run Windows Server 2008 or Windows Server 2008 R2 with Hyper-V. For example, if you need to have different SharePoint 2010 developing environment configurations on the same machine, you will need to use 64 bits virtual machines.

In that post I shown how to configure an hypervisor server operating system –specifically Windows Server 2008 R2 with Hyper-V– to behave almost the same as desktop operating system –let us say Windows 7–, by enabling the features that ship disabled by default: wireless networking, audio, Aero, hibernation, and so on. Even better, since you probably want to switch between the hypervisor server scenario back and forth to the desktop scenario, I included some scripts files to enable required services and disable non-needed ones in each one case.

Even if you can successfully run both 32 bits and 64 bits virtual machines on your laptop when Hyper-V is properly configured, provided you have the appropriate hardware resources, you will realize sooner or later that Hyper-V is designed to run on a physical server, not on a laptop. Let me give you an example: Why do you use a laptop, after all? For mobile computing, don’t you? Well, if you do mobile computing with your laptop, you will be connected wirelessly, at least occasionally. Then you will realize you cannot connect your virtual machines to the Internet by using a wireless connection: Hyper-V only shows Ethernet network adapters when creating external virtual connections.

The two workarounds I found in the Internet –configuring Internet Connection Sharing or using Routing and Remote Access Service– were useless for me because ICS is disabled by my administrator and RRAS is too much complicated to setup.

I have two network interface cards in my laptop, an Intel® 82567LM-3 Gigabit Network Connection, and an Intel® Centrino® Ultimate-N 6300 AGN:

image

But only the first one is available in the list of network cards to be used when creating an external virtual connection:

image

There is an easy way to workaround this, and get an external virtual network connected with a wireless connection:

  1. Configure an external virtual connection using your wired local area connection as usual. Your virtual machines use this connection when you are connected by wire to access the Internet.
  2. When you are not connected by wire, bridge your wireless connection with the external virtual connection, by selecting both connections in the Network Connections window, right clicking, and choosing Bridge Connections from the context menu.

Your will see an additional network connection named Network Bridge and you will be able to access the Internet from your virtual machine guests through the wireless connection. Delete the bridged connection when you get connected by wire again.

image

Since you probably do not need to be permanently connected to the Internet, but want to access your host’s files and folders, I usually configure an internal virtual connection in addition to the external one, and all my virtual machines have two virtual network adapters connected to each of them.

If you are in the same scenario than me, I hope this tip simplifies your life. Cheers.

Written by fernandomachadopiriz

March 26, 2011 at 11:33 am

Posted in SharePoint 2010

Tagged with

New release of Visual Studio 2010 and .NET Framework 4 Training Kit

with one comment

imageMicrosoft yesterday published for download the March 2011 release of the Visual Studio 2010 and .NET Framework 4 Training Kit. You can download the kit from here. This training kit includes presentations, hands-on labs, and demos, covering the latest topics in the recently updated versions of Visual Studio 2010 and .NET Framework 4. Enjoy.

 

 

 

 

 

 

 

Written by fernandomachadopiriz

March 15, 2011 at 10:15 pm

Visual Studio 2010 Service Pack 1 Released

leave a comment »

The final version of the Visual Studio 2010 Server Pack 1 is available since yesterday for MSDN susbscribers. The list of new features and fixes is available here.

Among the most interesting new features for me are:

  • Silverlight 4 compatibility
  • Unit testing support for .NET Framework 3.5 targets
  • IntelliTrace for 64 bits and SharePoint applications
  • Web applications and web project hosting on Internet Information Services (IIS) 7.5 Express
  • Microsoft Web Platform Installer support

Written by fernandomachadopiriz

March 10, 2011 at 10:08 am

Developing SharePoint 2010 solutions in your laptop using Windows Server 2008 R2 with Hyper-V

with one comment

If you usually develop SharePoint Server 2010 solutions from your office desktop, it is very likely that you have a full sized server there, with all required servers and tools installed on it, or better yet if you use different server versions or configurations, you have multiple virtual machines, each one with all required software installed on it. In such case, this blog post is not for you.

If your scenario is different, as mine is, and you work from different places using a laptop, and need to have different SharePoint Server 2010 installations (different versions, different languages, etc.), then you need your servers and tools running on virtual machines.

With Microsoft Office SharePoint Server 2007, both your host and your guest operating systems could be 32 bits, since you were able to use the 32 bits version of SharePoint servers. In addition, it was possible to use a client (non-server) operating system as host, by using Microsoft Virtual PC 2007 for example. With SharePoint Server 2010 that is not true anymore and the guest operating system needs to be a 64 bits one, let us say Windows Server 2008 R2 64 bits. This is because SharePoint Server 2010 only runs on top of 64 bits operating system.

You cannot have any 64 bits operating system on a virtual machine by using Microsoft Virtual PC 2007 or the new Windows 7’s Windows Virtual PC; the only way you can virtualize a 64 bits operating system is by using a hypervisor: Windows Server 2008 with the Hyper-V role.

Does it look a bit odd to run a hypervisor on a laptop, does not? Well, probably you are right, but you will get accustomed to it, believe me.

What do you lose by using Windows Server 2008 with Hyper-V as the host operating system of your laptop? If you use the default installation, you do not have:

  • Audio and Windows Media Player. The Windows Audio service is disabled by default and the Windows Media Player is not installed in a server. Usually there is nobody listening music at the servers room, it is a very noisy environment.
  • Wi-Fi connectivity. The Wireless LAN Service feature is not installed. Usually servers a wired-connected to the network.
  • Aero. The Themes service is disabled by default to preserve server’s resources.
  • Search. The Windows Search service is disabled by default also to preserve server’s resources.
  • Sleep and hibernate. Usually servers are always on and never sleep or hibernate, even turn off.
  • Event Tracker. You cannot shut down the system without explaining the reason why first.
  • Internet Explorer’s Enhanced Security. Every time you try to visit a website you get a message saying why you should not browse the web from a server.

But what is reasonable in a server, is not reasonable in the laptop you use for your everyday work, right? You want to hear music, you need wireless connections, you need to search for files and email messages, you love the look and feel of Aero, and you do not carry your laptop with the hard disk drive spinning every time you move from one place to another.

Except for sleeping and hibernating, you can have audio, wireless connectivity, file and email search, and a desktop look and feel in Windows Server 2008 R2. Unfortunately, as soon as you add the Hyper‑V role, you lose the ability to sleep/hibernate your laptop. But news are no so bad, you will be able to sleep/hibernate your laptop by temporarily disabling the hypervisor without removing it.

In this blog post I will show you how to tweak a default Windows Server 2008 R2 installation so it looks and behaves like a Windows 7 desktop. This is how mine looks:

WS2008R2

Enabling audio

This one is easy. Simply click the Start button and type “services” in the search box and then open the Services console. Look for the Windows Audio service, right click and choose Properties, then in the Properties dialog box change the Start type from Disabled to Automatic.

Or open an administrative Command Prompt and type sc \\[machine] config audiosrv start= auto replacing [machine] for your computer’s name and then type net start audiosrv.

Enabling Wi-Fi, Aero and Search

Install the missing features. Click the Start button and type “server” in the search box and then open the Server Manager console. On the left panel click on Features, then click on the Add Features link on the right panel. In the Add Features Wizard select and install the features Desktop Experience and Wireless LAN Service.

Once these features are installed, open the Services console again and look for the Themes service, right click and choose Properties, then in the Properties dialog box change the Start type from Disabled to Automatic. Do the same with the Desktop Window Manager Session Manager service. Do the same also with the Windows Search service but set Start type to Automatic (Delayed start).

Or open an administrative Command Prompt and type for %%s in (uxsms themes) do (sc \\[machine] config %%s start= auto) and then sc \\[machine] config wsearch start= auto and finally for %%s in (uxsms themes wsearch) do (net start %%s).

Enabling sleep/hibernate

Let us assume you have installed Windows Server 2008 R2 and then you added the Hyper-V role. If you open an administrative Command Prompt and then you run the bcdedit /enum {current} you will get an output like this one:

clip_image003

bcdedit is not a “binary coded decimal” editor, as you might probably guess, but a “boot configuration datastore” editor. Note the hypervisorlaunchtype set to Auto in the last line. Also note the Windows Server 2008 R2 description; this is the text you see during boot when multiple boot configurations are available or during system recovery.

Copy this boot configuration to a new one by typing bcdedit /copy {current} /d “Microsoft Windows Server 2008 R2 (w/o Hyper-V)” in an administrative Command Prompt. The text between the quotes is the description of the new boot configuration. You can choose a description different than mine.

The command outputs a message including a GUID of the new boot configuration. Disable the hypervisor role in the new boot configuration by using bcdedit /set {GUID} hypervisorlaunchtype off where GUID is the GUID shown by the previous command.

You are done. Additionally, if you want to change your default boot configuration, or change the time to display the list of configurations during boot, you can use bcdedit or the Startup and Recovery dialog box in the System Properties.

This new boot configuration will disable Hyper-V and let you enable sleep/hibernation, but (obviously) you will need to reboot your laptop before that. This is by far more efficient than completely removing the Hyper-V role, which by the way, also requires a reboot.

To enable hibernation after reboot, you will need to open an administrative Command Prompt and run powercfg –H (uppercase haitch).

Disabling Event Tracking

Open the Local Group Policy Editor by clicking on Start and typing “edit policy” in the search box. Then expand Local Computer Policy in the left pane, then Computer Configuration, then Administrative Templates, finally System. In the right pane look for Display Shutdown Event Tracker, double click it to edit its configuration, and choose Disable.

Disabling Internet Explorer Enhanced Security

Click the Start button and then type “server” in the search box and then open the Server Manager console. Select the root node in the left tree and then click on the Configure IE ESC link in the right pane. Choose Off to disable the setting.

Final remarks

Now you can use your laptop in two scenarios: as a hypervisor or as a regular laptop. In the first scenario, is highly recommended that you disable back services like Windows Audio, Themes, Windows Search, etc. and turn Aero themes off, in order to preserve system resources. In addition, if you have other servers like SQL Server or even a SharePoint server installed on top of your host operating system, it is highly recommended to turn them off while using the hypervisor.

Since you need to shut down the system before moving your laptop while using the hypervisor, and since you need to reboot to switch from one scenario to the other, consider buying a solid state disk drive for your system partition to speed up shut down and start up times. By the way, with a disk like that, turning your laptop off and on could be even faster than hibernating and restoring from a regular hard disk drive, if your laptop has a lot of RAM, as it should have to use eventually multiple virtual machines simultaneously.

To facilitate switching from the hypervisor scenario to the regular laptop scenario, you can use the following batch files and scripts.

Use this batch file to enable or disable search by simply typing search on or search off in an administrative Command Prompt. This batch file not only stops or starts the search service; it also enables or disables it, so the current status is kept after rebooting.

@echo off

rem Enables and starts or stops and disables search services

rem Search services:
rem	Windows Search

set x=%1%#
if %x%==# goto usage
if %1%==on goto enable
if %1%==off goto disable

:usage

echo "Usage: search on|off"
goto end

:enable

rem Enables desktop services

for %%s in (wsearch) do (sc \\[machine] config %%s start= delayed-auto)
for %%s in (wsearch) do (net start %%s)

pause
goto end

:disable

rem Disables search services

for %%s in (wsearch) do (net stop %%s)
for %%s in (wsearch) do (sc \\[machine] config %%s start= disabled)

pause
goto end

:end

 

You can use this other batch file to enable or disable the audio related services, by typing audio on or audio off in an administrative Command Prompt.

@echo off

rem Enables and starts or stops and disables audio related services

rem Audio services:
rem	Windows Audio
rem	Windows Audio Endpoint Builder

set x=%1%#
if %x%==# goto usage
if %1%==on goto enable
if %1%==off goto disable

:usage

echo "Usage: audio on|off"
goto end

:enable

rem Enables audio services

for %%s in (audiosrv audioendpointbuilder) do (sc \\[machine] config %%s start= auto)
for %%s in (audiosrv audioendpointbuilder) do (net start %%s)

pause
goto end

:disable

rem Disables audio services

for %%s in (audiosrv audioendpointbuilder) do (net stop %%s)
for %%s in (audiosrv audioendpointbuilder) do (sc \\[machine] config %%s start= disabled)

pause
goto end

:end

 

To stop all servers’ services, there is another batch file you can use to stop and disable or start and enable these services, by typing servermode off or server mode on on an administrative Command Prompt.

@echo off

rem Enables and starts or stops and disables server related services

rem Server services:
rem	Application Host Helper Service
rem	Distributed Transaction Coordinator
rem	Simple Mail Transfer Protocol (SMTP)
rem	IIS Admin Service
rem	Net.Msmq Listener Adapter (depends on Message Queuing)
rem	Message Queuing
rem	SharePoint 2010 Timer
rem	SharePoint 2010 Tracing
rem	SharePoint 2010 User Code Host
rem	SharePoint Foundation Search V4
rem	SharePoint Server Search 14
rem	SQL Full-text Filter Daemon Launcher (MSSQLSERVER)
rem	SQL Server Agent (MSSQLSERVER) (depends on SQL Server (MSSQLSERVER))
rem	SQL Server (MSSQLSERVER)
rem	SQL Server (SHAREPOINT)
rem	SQL Server (SQLEXPRESS)
rem	SQL Server Analysis Services (MSSQLSERVER)
rem	SQL Server Browser
rem	SQL Server Integration Services 10.0
rem	SQL Server Reporting Services (MSSQLSERVER)
rem	SQL Server VSS Writer
rem	Visual Studio Team Foundation Background Job Agent
rem	Visual Studio Team Foundation Build Service Host
rem	World Wide Web Publishing Service

set x=%1%#
if %x%==# goto usage
if %1%==off goto disable
if %1%==on goto enable

:usage

echo "Usage: servermode on|off"
goto end

:disable

rem Disables server services

for %%s in (apphostsvc SMTPSVC msdtc iisadmin NetMsmqActivator msmq sptimerv4 sptracev4 spusercodev4 spsearch4 osearch14 SQLSERVERAGENT MSSQLSERVER MSSQL$SHAREPOINT MSSQL$SQLEXPRESS MSSQLServerOLAPService SQLBrowser MsDtsServer100 ReportServer SQLWriter TFSJobAgent TFSBuildServiceHost W3SVC) do (net stop %%s)
for %%s in (apphostsvc msdtc SMTPSVC iisadmin NetMsmqActivator msmq sptimerv4 sptracev4 spusercodev4 spsearch4 osearch14 MSSQLFDLauncher SQLSERVERAGENT MSSQLSERVER MSSQL$SHAREPOINT MSSQL$SQLEXPRESS MSSQLServerOLAPService SQLBrowser MsDtsServer100 ReportServer SQLWriter TFSJobAgent TFSBuildServiceHost W3SVC) do (sc \\[machine] config %%s start= disabled)

pause
goto end

:enable

rem Enables server services

for %%s in (apphostsvc msdtc iisadmin NetMsmqActivator msmq sptimerv4 sptracev4 spusercodev4 MSSQLFDLauncher SQLSERVERAGENT MSSQLSERVER MSSQL$SHAREPOINT MSSQL$SQLEXPRESS MSSQLServerOLAPService SQLBrowser MsDtsServer100 ReportServer SQLWriter TFSJobAgent TFSBuildServiceHost W3SVC) do (sc \\[machine] config %%s start= auto)
for %%s in (msdtc) do (sc \\[machine] config %%s start= delayed-auto)
for %%s in (spsearch4 osearch14 MSSQLFDLauncher SMTPSVC) do (sc \\[machine] config %%s start= demand)
for %%s in (apphostsvc msdtc iisadmin NetMsmqActivator msmq sptimerv4 sptracev4 spusercodev4 spsearch4 osearch14 MSSQLFDLauncher SQLSERVERAGENT MSSQLSERVER MSSQL$SHAREPOINT MSSQL$SQLEXPRESS MSSQLServerOLAPService SQLBrowser MsDtsServer100 ReportServer SQLWriter TFSJobAgent TFSBuildServiceHost W3SVC SMTPSVC) do (net start %%s)

pause
goto end

:end

 

Since hypervisor’s services are needed only when you boot using the hypervisor configuration, these services are enabled or disabled on a separate batch file.

@echo off

rem Starts or stops Hyper-V related services

if %1%==off goto disable
if %1%==on goto enable
goto end

:disable

for %%s in (vhdsvc nvspwmi vmms hvboot) do (net stop %%s)
for %%s in (vhdsvc nvspwmi vmms hvboot) do (sc \\[machine] config %%s start= disabled)
pause
goto end

:enable

for %%s in (vhdsvc nvspwmi vmms) do (sc \\[machine] config %%s start= auto)
for %%s in (hvboot) do (sc \\[machine] config %%s start= demand)
for %%s in (vhdsvc nvspwmi vmms hvboot) do (net start %%s)
pause
goto end

:end

 

Since I do a lot of work with many different servers (SQL Server, SharePoint Server, Team Foundation Server, etc.), the list of services I have in this batch file is big. You can remove or add your own services from the batch file. If you plan to modify it, pay attention to the following:

1. Dependencies. You need to stop a services after all the services that depends on it, to avoid confirmation messages stopping your batch file processing. Make sure your start them in the reverse order.

2. Start type. Disabling services is easy: there is just one disabled status. But enabling them is a bit more complicated, because there is more than one start type: automatic, automatic but delayed, and manual. Make sure you return the start type to the original one.

The extra mile: if you want your Windows Server 2008 R2 installation to look exactly (well, very similar at least) than a Windows 7 desktop, with Aero and themes, you can use these additional batch files and scripts. You bet it, there are desktopmode on and desktopmode off commands to do this.

@echo off

rem Enables and starts or stops and disables desktop related services

rem Desktop services:
rem	Windows Audio
rem	Windows Audio Endpoint Builder
rem	Windows Search
rem	Desktop Window Manager Session Manager
rem	Disk Defragmenter (stop only)
rem	Themes

set x=%1%#
if %x%==# goto usage
if %1%==on goto enable
if %1%==off goto disable

:usage

echo "Usage: desktopmode on|off"
goto end

:enable

rem Enables desktop services

for %%s in (audiosrv audioendpointbuilder uxsms defragsvc themes) do (sc \\[machine] config %%s start= auto)
for %%s in (wsearch) do (sc \\[machine] config %%s start= delayed-auto)
for %%s in (defragsvc) do (sc \\[machine] config %%s start= demand)
for %%s in (audiosrv audioendpointbuilder wsearch uxsms themes) do (net start %%s)
DesktopTheme.vbs

pause
goto end

:disable

rem Disables desktop services

for %%s in (audiosrv audioendpointbuilder wsearch uxsms defragsvc themes) do (net stop %%s)
for %%s in (audiosrv audioendpointbuilder wsearch uxsms defragsvc themes) do (sc \\[machine] config %%s start= disabled)
ServerTheme.vbs

pause
goto end

:end

 

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Themes\aero.theme"""

Wscript.Sleep 15000
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"

 

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\classic.theme"""

Wscript.Sleep 1000
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"

 

One final recommendation: add the folder where this batch files and scripts are stored to the PATH environment variable.

Have fun.

PS: In order to be completely honest, there are other things you will lose if you choose to install Windows Server 2008 R2 in your laptop. First, the Bluetooth drivers are usually not available for server operating systems, so be aware if you depend on Bluetooth devices for your daily work. Second, depending on your system’s hardware, other device drivers might not be available for server operating systems. The most common ones are the fingerprint reader and the smart card reader drivers. You can try installing the drivers for Windows 7, but this may or may not work. Finally, some third party tools detect a non-desktop operating system and refuse to run or even to install. So far, I can live with all these limitations, but you cannot say I did not tell you.

Written by fernandomachadopiriz

February 12, 2011 at 12:13 pm

First look to the Microsoft Visual Studio LightSwitch beta

with 6 comments

A new member of the developers’ tools family has been announced earlier this month and the first beta will be available soon for download: Microsoft Visual Studio LightSwitch.

What it is and what it is for?

With LightSwitch we can easily and quickly create business applications from the scratch, including not only the application user interface forms, but also the application database; you can also create applications from existing databases.

In this article I will provide an overview of LightSwitch using the beta version soon to be available. The project I will create is a simple application to record the books I loan to my friends.

A new LightSwitch application starts by creating a new Visual Studio project. A LightSwitch project will contain application’s data and forms definitions. By using these definitions LightSwitch generates a SQL Server database and the source code in C# or Visual Basic.

In this article I will use a C# project template. Here you can see the familiar Visual Studio new project dialog box, with the two templates provided by LightSwitch.

image

Once the project is created we can connect to an existing data source or create a new data definition for the application. In this post I will show how to create data definitions from the scratch.

image

The first thing to do is to define how book entities are. I am interested in each book’s author and title. Then we create an entity Book, with Author and Title fields, both of type String and both required. These entities will be stored in a Books table.

image

Then a definition for loans is created. From each loan I am interested in knowing to whom I loaned the book, when the book was loaned, and when it was returned. Then we create a second entity called Loan, with fields ToFriend, DateLoaned and DateReturned; the first field is of type String and the two other are of type Date; the first two fields are required, but the latest is not. These entities will be stored in the Loans table.

image

Now a new relationship between books and loans is defined.

image

See below how the definition of the entity Loan and its relationship with Book looks:

image

Now we need to define the forms to edit these entities. Many different form templates are available -allowing one record to be edited at a time, multiple records in a grid, etc. I will use a template for a simple record insertion.

image

The form definition created by the wizard can be modified. I have changed only the name to display and the description of the form.

image

Once the form definition is finished, we can test the application. This LightSwitch version allows creating the following application types:

image

When the build is started LightSwitch:

  • Creates the database
  • Generates the source code
  • Compiles the application

A LightSwitch application is typically a three tier application, using certain specific technologies for each tier:

In this article you can find more information about the architecture of a LightSwitch application.

When a LightSwitch application gets executed:

  • A local web server is started
  • The application is deployed on this web server
  • The application is started

That is what we see:

image

Let us complete the fields to add the first book:

image

The application automatically assigns a new identifier to the first book.

image

Out of the box, the application already has some simple validations, like the one corresponding to the fields marked as required.

image

image

image

The second form is to insert loans. This is also a simple new record form. We can see how LightSwitch assigns a Date Picker control to the DateLoaned and DateReturned fields, of type Date. It also assigned a Modal Window Picker control to edit the association between a Loan and a Book.

image

When we run the application we can see how the controls assigned to Date fields looks:

image

The Modal Window Picker control allows searching and selecting a book to be associated with a loan. The control includes not only the search features, but also allows paging results.

image

Although this way to edit loans is not so bad, I would prefer to see the list of books, and from there to easily access each book’s loans, and eventually adding new books in the same place. The template for this kind of editing form is called List and Details Screen. Look how LightSwitch allows indicating if books’ and loans’ details should be included on the form.

image

Once the form is created, we can edit its definition. Like in previous cases, I only slightly change the name to display and add a description.

image

We can see how the new form looks when running the application. The list of books appears on the left. From there we can add, modify, and delete books. For the book selected in the list on the left, we can see the loans in the right. From there we can also add, modify, and delete loans.

image

Be aware that for adding and modifying books and loans, LightSwitch automatically generated a form with the same controls defined in their respective new record forms.

image

image

These forms have also the default behavior for validating the rules defined in the entities.

image

When more than one record is changed in a list, modifications are not sent to the database until the list is saved by using the Save button.

image

How much time takes this application to be created? Not too much, as you can see in this video:

 

You can download the video from here.

Okay, but what kind of applications is LightSwitch for? Perhaps we need to wait for a while to see who really takes advantage of this new technology. Simple applications like the one I created in this post can be created really quickly, but we all know that real world applications are not as easy to build as the ones I have demoed, are they? For non-professional or occasional developers –for whom other technologies like Microsoft Access or Visual FoxPro was created in the past-, LightSwitch can be their new preferred tool. Professional developer will probably not be able to build 100% of the complex business applications they create today by using LightSwitch, but at least they can use LightSwitch as a tool for rapid prototyping.

In certain places in the application definition we can write C# code to add functions not provided by LightSwitch -for business rules for example. But then we need to know the object model of the code generated by LightSwitch, to be able to consume from our own handwritten code the types automatically generated by LightSwitch.

Some of the templates seem to be extensible –this beta version includes templates lists with just an element, but probably it is because it is a beta. Furthermore, it seems that LightSwitch will be able to create applications for Windows Azure also. Since all the code and the database are generated from definitions, the same application could be migrated from the desktop to the cloud just by changing a setting in the project properties page.

With an object model easy to be consumed in extension points and a strong ecosystem augmenting LightSwitch capabilities, it looks like we will be able to use it to develop applications more complex than the one created here. It is too soon yet, time will tell.

Written by fernandomachadopiriz

August 20, 2010 at 12:50 am

Give me a handle and I will move the Earth

with one comment

image

This post’s title paraphrases a saying attributed to Archimedes, who is said to have remarked of lever “Give me a place to stand on, and I will move the Earth”.

For people like me who started developing Windows applications a long time ago, handles were the entry door to almost any resource through Win32 API. Do you want to close or minimize a window? You need that window handle. Do you want to write some text into a window client area? You need the font handle, the device context handle, and the window handle. And so on.

Fortunately, in actual .NET Framework times, almost no one cares about using handles, nor cares about knowing what they are. The .NET Framework hides the complexity of handling Windows resources through the Win32 API, and with that, it also hides the handles.

However, once in a while, there appear some problems for which the only solution is to get a handle. Here is an example.

One of the features of the Giving a Presentation demo application is the ability to hide desktop icons during a presentation, showing them back again when the presentation ends. While developing thsi feature, I unsuccessfully searched for a way to do that. As far as I could found, there is no public API function to do it; and most hacks available on the web -for example this one- use handles and Win32 API function calls, but does not work correctly: they disable the entire desktop, not only its icons; as a consequence, the desktop’s pop up menu disappears.

Speaking about desktop’s popup menu, what I needed was a feature like the one in the Show desktop icons command.

image

Then it was when the old knowledge about handles and the like became useful again. I knew that this command ended sooner or later being transformed into a WM_COMMAND sent to the desktop. What I needed to emulate this behavior was to know the arguments of the WM_COMMAND message and –as you can imagine- the desktop handle. Once I get this information, I could send the same WM_COMMAND to the desktop. The desktop would not distinguish between a message sent by the menu command or by my program, so it should handle it in the same way.

For both two things I used an old tool still included in Visual Studio installation: Spy++. This tool allow finding a particular window, and among other things, look at the messages it receives. The following picture show the Spy++ windows list node corresponding to the desktop. Please note that desktop is a window of the SHELLDLL_DefView class; we will see later how this information is used.

clip_image003

The following image shows the messages received by this window when command Show desktop icons is executed. Please not the command ID 29698 of the WM_COMMAND message; we will also see later how to use this information.

clip_image004

The function to hide or show desktop icons finally gets something like this:

/// <summary>
/// Toggles desktop icons visibility by sending a low level command to desktop window in the same way as the popup menu "Show desktop  
/// icons" does. Use <code>AreDesktopIconsVisible</code> to get current desktop icons visibility status.  
/// </summary>  
private static void ToggleDesktopIconsVisibility()  
{  
    IntPtr defaultViewHandle = FindShellWindow();
    UIntPtr resultArgument; 
    IntPtr returnValue = NativeMethods.SendMessageTimeout(  
        defaultViewHandle,  
        NativeMethods.WM_COMMAND,  
        new IntPtr(29698),  
        IntPtr.Zero,  
        NativeMethods.SendMessageTimeoutFlags.SMTO_ABORTIFHUNG,  
        1000,   
       out resultArgument);
} 

The NativeMethods class contains all the functions and constants imported from Win32 API; code is not shown here due to simplicity, but you can download it from CodePlex. The function SendMessageTimeout is used to send the WM_COMMAND with ID 29698 to the window whose handle was obtained with the FindShellWindow function.

On the other side, the FindShellWindow function finds and returns the desktop handle, strictlyspeaking, the handle to the window containing desktop icons, whose class is SHELLDLL_DefView. Finding this window was not easy. Due to unknown reasons, in certain systems the window is in some place in the windows’ hierarchy, but in other systems is in another place – both system being Window 7 RTM 32 bits. Because of this, the Giving a Presentation feature to hide and show desktop icons, did not work in every situations. I should thanks to my MVP coleague Elías Mereb for his help to test different candidate solutions until finding the good one.

/// <summary>
/// Called by EnumWindows. Sets <code>shellWindowHandle</code> if a window with class "SHELLDLL_DefView" is found during enumeration.
/// </summary>
/// <param name="handle">The handle of the window being enumerated.</param>
/// <param name="param">The argument passed to <code>EnumWindowsProc</code>; not used in this application.</param>
/// <returns>Allways returns 1.</returns>
private static int EnumWindowsProc(IntPtr handle, int param)
{
    try
    {
        IntPtr foundHandle = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "SHELLDLL_DefView", null);
        if (!foundHandle.Equals(IntPtr.Zero))
        {
            shellWindowHandle = foundHandle;
            return 0;
        }
    }
    catch
    {
        // Intentionally left blank
    }

    return 1;
}

/// <summary>
/// Finds the window containing desktop icons.
/// </summary>
/// <returns>The handle of the window.</returns>
private static IntPtr FindShellWindow()
{
    IntPtr progmanHandle;
    IntPtr defaultViewHandle = IntPtr.Zero;
    IntPtr workerWHandle;
    int errorCode = NativeMethods.ERROR_SUCCESS;

    // Try the easy way first. "SHELLDLL_DefView" is a child window of "Progman".
    progmanHandle = NativeMethods.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);

    if (!progmanHandle.Equals(IntPtr.Zero))
    {
        defaultViewHandle = NativeMethods.FindWindowEx(progmanHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
        errorCode = Marshal.GetLastWin32Error();
    }

    if (!defaultViewHandle.Equals(IntPtr.Zero))
    {
        return defaultViewHandle;
    }
    else if (errorCode != NativeMethods.ERROR_SUCCESS)
    {
        Marshal.ThrowExceptionForHR(errorCode);
    }

    // Try the not so easy way then. In some systems "SHELLDLL_DefView" is a child of "WorkerW".
    errorCode = NativeMethods.ERROR_SUCCESS;
    workerWHandle = NativeMethods.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "WorkerW", null);
    Debug.WriteLine("FindShellWindow.workerWHandle: {0}", workerWHandle);

    if (!workerWHandle.Equals(IntPtr.Zero))
    {
        defaultViewHandle = NativeMethods.FindWindowEx(workerWHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
        errorCode = Marshal.GetLastWin32Error();
    }

    if (!defaultViewHandle.Equals(IntPtr.Zero))
    {
        return defaultViewHandle;
    }
    else if (errorCode != NativeMethods.ERROR_SUCCESS)
    {
        Marshal.ThrowExceptionForHR(errorCode);
    }

    shellWindowHandle = IntPtr.Zero;

    // Try the hard way. In some systems "SHELLDLL_DefView" is a child or a child of "Progman".
    if (NativeMethods.EnumWindows(EnumWindowsProc, progmanHandle) == 0)
    {
        errorCode = Marshal.GetLastWin32Error();
        if (errorCode != NativeMethods.ERROR_SUCCESS)
        {
            Marshal.ThrowExceptionForHR(errorCode);
        }
    }

    // Try the even more harder way. Just in case "SHELLDLL_DefView" is in another desktop.
    if (shellWindowHandle.Equals(IntPtr.Zero))
    {
        if (NativeMethods.EnumDesktopWindows(IntPtr.Zero, EnumWindowsProc, progmanHandle))
        {
            errorCode = Marshal.GetLastWin32Error();
            if (errorCode != NativeMethods.ERROR_SUCCESS)
            {
                Marshal.ThrowExceptionForHR(errorCode);
            }
        }
    }

    return shellWindowHandle;
}

In certain situations, even in the .NET Framework era, it is still useful to get a handle: give me one, and I will move the Earth. Or at least, I will hide and show desktop icons. See you later.

Written by fernandomachadopiriz

August 9, 2010 at 2:46 pm

Posted in C# 4.0

Tagged with

Follow

Get every new post delivered to your Inbox.

Join 201 other followers