Thursday, June 30, 2016

Visual Studio/Windows 10: Running Visual Studio (or any Application) as Administrator on Windows 10 every time the Application is invoked

On certain projects Visual Studio needs to be run with Administrative privileges. As of Windows 10 it is possible automatically run Visual Studio or any application for that matter with administrative privileges.

Pre-Windows 10 Running as Administrator

The old way (pre-Windows 10) of doing things mean explicitly invoking administrative privileges each time an application was. The legacy way for Visual Studio or any application to run in Administrative mode was by right clicking on the icon used to launch the application. For Visual Studio pinned to the task bar, right clicking on the Visual Studio icon appears as follows:



From the previous context menu click on Visual Studio 2015 and and a context submenu appears:


From the sub-context menu it is possible to run Visual Studio as an administrator by selecting "Run as administrator." This is a tedious process especially if an application like Visual Studio is launched multiple times a day.

Windows 10 Era Running as Administrator

To automatically run Visual Studio as an Administrator, right click on the icon used to launch Visual Studio:



Right click on Visual Studio 2015 from the menu which displays the following sub-context menu:



From the sub-context menu click on the Properties menu item. This menu item displays the properties for the Visual Studio shortcut:


Select the Shortcut tab from the Properties dialog and then click on the Advanced button. This displays the Advanced Properties dialog (see below):


From the Advanced Properties dialog check "Run as administrator" and the click on OK. From the Properties dialog click on OK or Apply to insure the application will always run with Administrative privileges in the future.

After this is configured, Visual Studio will run as follows each time its shortcut icon is used to launch the application:


Notice in the previous screenshot that the title of Visual Studio is "Start Page - Microsoft Visual Studio (Administrator)" indicating it is running with Administrative privileges.

Wednesday, June 29, 2016

SETX: Permanently setting a Windows System Environment Variable from the Command-Line

For two decades I have used SET from the Windows from the command-line to display environment variables and to temporarily assign environment variables meaning the environment variables are local. The environment variables create exist for the duration of the console window in which SET was used to assign the environment variable.

On a recent project, I was required to permanently assign an environment variable from command-line (part of a build automation process that was invoked via batch files). After a quick search I found SETX which unbeknownst to me was including in Windows NT 4.0 Resource kit (circa 1996). SETX is akin to SETENV on Linux/Unix.

The features of SETX are extensive and are well documented at Setx. By default the environment variables created by SETX are local just as are the environment variables created by SET. The /m command-line option causes SETX to create environment variables that are persisted as system environment variables.

The standard command-line that allows SETX to create system environment variables is as follows:
  SETX <Environmental Variable Name> <Value> /m

An example of SETX is action is as follows:


Above a system environment variable is created named SomeKeyEnvVariable and is assigned a value of C:\foo\bar. The /m is the option specifies a system environment variable is created. Notice in the head of the previous screen shot that that command prompt is being run with administrator privileges. If SETX is executed without administrator privileges then SETX will fail.

Before discovering SETX, I was obligated to use the dialogs provided with Windows in order to create system environment variables. To demonstrate let's varify the previous SETX command actually created SomeKeyEnvVariable:

To check the state of environment variables navigate as follows:
    Control Panel > System and Security > 
        System > Advanced system settings

The sequence of screens to read environment variables is as follows. First open Control Panel:


Select the folder System and Security from Control Panel:


From System and Security select System:


On the left side click on Advanced system settings:


Click on the Environment Variables button to see the environment variables:


Notice in the Environment Variables dialog above, the SomeKeyEnvVariable system environment variable was created courtesy of SETX running with administrative privileges.

Friday, June 24, 2016

CURL for Win64: Download, Installing and using CURL to test RESTful Web Services

This posting demonstrates how to acquire, set up and run CURL, a command-line utility that allows RESTful web services to be tested and exercised. The target platform for this demonstration will be CURL running on 64-bit Windows.

Background

Over the years I have developed RESTful web services that were consumed by different client platforms. As a .NET developer it is tempting to use C# or JavaScript to test and use as examples (an SDK) of how to consume a web service. To be candid it might not do a Python or Java developer a lot of good to show them a C# sample of how to consume a web service.

Instead of relying on languages supported by .NET, I have used third party utilities to demonstrate and test web services.. There are many excellent candidates to choose from with respect to these utilities: CURL, Postman, SoapUI, TestMaker, and WebInject. I typically use the HTTP client tool used by majority of the developers I work with. When given a choice or when I am providing examples to other developers, I use CURL. The CURL utility is command-line based so examples can be handed out as sample scripts (CURL command plus command-line options) to be run. Any developer can reverse engineer CURL into their own client development language (C#, JavaScript, Pytho, Java,, C++ etc.). A Windows-based CURL example can be run on a Linux system by simply changing the double quotes to single quotes.

FYI: CURL is more powerful and does far more than invoke RESTful services. The CURL utility is general purpose HTTP client but this is beyond the content of this post.

Prerequisites 

Before downloading CURL, make sure you know if you Windows 32-bit or 64-bit. If you are unsure how to do this please see, "Are you running 32-bit Windows or 64-bit Windows?"

Additionally, download and install 7-Zip. The compression utility,7-Zip, is superior to the zip format provided natively with Windows because it provides a tighter level of compression. 7-Zip can be downloaded from  "http://www.7-zip.org/download.html." The CURL download is typically compressed in 7-zip format hence the 7-zip compression utility is required.

Downloading Curl

The "https://curl.haxx.se/dlwiz/" link displays a wizard allowing a user to choose the correct version of CURL. Running the aforementioned link in a browser displays the following:


In the previous screen under the heading, "Select Type of Package," select the "curl executable" link. Clicking on the curl executable link displays the following:


Under "Select Operating System," click on the drop down containing  button which displays the following:


Select "Win64" from the dropdown and click on Select! button. This action display the following web page:


Under "Select for What Flavour" leave the setting as "Generic" and click on the Select! button:



Under "Select which Win64 Version" leave "Any" select click on the Select! button:


Under "Select for What CPU" select x86_64 from the drop down and click on Select!. This reveals the version of CURL that the wizard recomends for download:


The top link is located in U.K. and when clicked on leads to http://winampplugins.co.uk/curl/:


For a 64-bit version of Windows download the 64-bit version of CRUL(on the previous screen version 7.49.1).

Setting Up CURL

I a using the terminology "setting up CURL" and not "Installing CURL." The CURL utilities provide for download do not come with an installer. For this reason first create a location where to copy the CURL folder to. My location for third-party applications that do not come with an installer is C:\bin, hence this is the location to which CURL will be set up.

Since 7Zip is installed right clicking on the CURL download (extension .7z) will contain a 7-zip menu and an option to "Extra to" a folder with the same as the CURL download (see below where CURL is extracted to curl_7_49_1_openssl_nghttp2_x64):


On my machine I take the <downloads>\curl_7_49_1_openssl_nghttp2_x64 folder and copy it to my C:\bin folder. The contents of curl_7_49_1_openssl_nghttp2_x64 are as follows:

To make life simpler let's add the C:\Bin\curl_7_49_1_openssl_nghttp2_x64 to our system path. This is handled by navigating to Control Panel:


Click on System and Security which displays the following:


Click on the System folder within System and Security:


On the left side click on Advanced system settings:


Click on Environment Variables:


Under System variables, select the Path system variable and click on the Edit button:


Windows 10 elegantly shows each path associated with with the Path environment variable on a separate line (thank you Microsoft; it is about time). Click on the New button so the path to CURL can be added:


Paste the path where the CURL.exe is located into the area created by the New button (for my machine this is C:\Bin\curl_7_49_1_openssl_nghttp2_x64).


Once the path to CURL has been added, click on OK. After this click on OK two more times to close on the windows opened to add the path to CURL to the Path environment variable.

Testing CURL

The best way to insurece CURL is working is to test it against working web services. These are a set of free web services documented at:

http://www.groupkt.com/post/f2129b88/free-restful-web-services-to-consume-and-test.htm

One of the RESTful services provided is found at http://services.groupkt.com/country/get/all. This UIR was added to a Windows batch file which is follows:

REM REST web-service to get a list of all Countries
REM http://services.groupkt.com/country/get/all
REM 
REM This rest web service will return a list countries in JSON 
REM format, each country 
REM object has 
REM {
REM "name":"India",
REM "alpha2_code":"IN",
REM "alpha3_code":"IND"
REM }
REM etc.

CURL "http://services.groupkt.com/country/get/all"

The reason the URI is wrapped in double quotes is to make sure that all any characters in the URI that are invalid in a Windows command-line do not cause execution of CURL to generate an error. When the previous code is executed as a batch file it displays the following (demonstrating CURL works):


Friday, June 17, 2016

Are you running 32-bit Windows or 64-bit Windows?

Pre-Blog Posting Pontification and Soapbox Tirade

FYI: If you have Windows 10, 32-bit you can only run 32-bit Hyper-V which means you cannot run operating systems like Windows 2012 R2 under Hyper-V because Windows 2012 R2 is 64-bit only 

How to Determine if you are running 32-bit or 64-bit Windows

It is 2016 and most of us wear big boy pants and use 64-bit Windows. If you are unsure if your Windows is 32- or 64-bit bring up Control Panel:



Click on the System and Security folder:


Click on the System folder:


I addition to showing whether your windows is 32-bit or 64-bit, other useful tidbits are displayed such as Processor type and amount of Installed Memory (RAM).

Wednesday, June 15, 2016

C#: Singletons (Static classes) Old School versus Really Old School

I have been asked during interviews how to implement a singleton using C# and how to implement a static class using C#. I started coding in C# during Alpha 3 of .NET two years before .NET shipped in 2002.

Decorating a Class with the static Keyword (the correct way)

The static key work was introduced for constructors, events, fields, methods, operators, properties in C# 1.0 (2002).In C# 2.0 (2005) the static keyword was applied to decorating classes, hence creating a clear way to define a class a "static class" (a singleton).

The correct answer on how to code a static class (a.k.a. a Singleton) is as follows where the class is declared static "public static class ExampleStaticClass" using the C# 2.0 decorator:

public static class ExampleStaticClass
{
  public static void Method01()
  {
  }

  public static void Method02()
  {
  }
}

Notice that the class is decorated by the static keyword and each method is similarly decorated with the static keyword.  The static class decorator requires that events, fields, methods, operators, and properties of the static class must also be decorate with the static keyword.

It is possible to have one and only one static constructor in a static class then again it is only possible to have one static constructor associated with any class.

A class decorated with the static keyword cannot be used as a base class. This is the same behavior a sealed.

.NET Static Classes Circa .NET 2.0

.NET is rife with static classes including:
public static class Math
public static class Directory
public static class Folder
public static class Path

Notice that each of the above classes are decorated with the C# 2.0 static keyword.

Creating a Static Class Old School (C# 1.0 style circa 2002)

Visual Studio (circa 2002) and Visual Studio 2003 (obviously circa 2003) shipped with C# 1.0 and C# 1.2 respectively. The static keyword existed in these version of C# but it could not be applied to class. The mechanism used to create static class was as follows:

1) Declare the class as sealed which means the class cannot be used as a base class.
2) Declare each event, field, method, operator, and property within the class as static.

There is no requirement that each event, field, method, operator, and property be static. This is a voluntary pattern that developers must follow in order to create a static class in the era of C# 1.0 and 1.2.

.NET Static Classes Circa .NET 1.0/1.2

.NET 1.0/1.2 is also rife with static classes including:
public sealed class Math
public sealed class Directory
public sealed class Folder
public sealed class Path

Notice that each of the above classes are decorated with the sealed keyword which does not necessarily mean static but is used as part of the static/singleton design patter as implemented in C# 1.0/1.2.


Tuesday, June 14, 2016

Visual Studio Unit Tests: Assert.AreSame versus Assert.AreEqual

In the posting "Visual Studio: Developing Unit Tests" an overview was given on creating unit tests with Visual Studio. In that posting the Microsoft.VisualStudio.TestTools.UnitTesting namespace's Assert class was reviewed. This is the class that puts the third A in the AAA (Arrange Act Assert) design pattern.

Two methods stuck out within the Assert class because they appear to be equivalent methods: AreSame versus AreEqual. The AreSame method uses each objects Equals method/methods to determine if two objects are the same memory reference. The AreEqual method determines if the objects are equal in value but may not be the same memory reference.

Both AreEqual and AreSame have a method that behaves in an opposite fashion, namely AreNotEqual and AreNotSame. The AreNotSame method asserts if two objects are the same object reference.  The AreNotEqual method asserts if two objects are equal in value.

An example demonstrating AreEqual, AreSame, AreNotEqual  and AreNotSame is as follows:



The example test method above succeeds because each assertion is true.

Monday, June 13, 2016

VirtualBox: does not support 64-bit Guest Operating Systems on Windows 10

The problem I encountered was, on a Windows 10 64-Bit system where Oracle's VirtualBox would not support running 64-bit guest operating systems.

This blog is broken down into three parts:
1) Demonstrate how VirtualBox cannot create a new 64-bit virtual machine
2) Demonstrated how to disable Hyper-V in order to allow VirtualBox to support 64-bit client operating systems.
3) Demonstrate how VirtualBox cannot add and run an existing 64-bit client operating system

VirtualBox Cannot Create a New 64-Bit Virtual Machine

The problem manifests itself when creating a new virtual machine. The steps to create a new virtual machine (this demonstration will result in failure) is a follows:

1) From VirtualBox's Machine menu, select New:


2) From the Create Virtual Machine dialog select the 64-bit operating system desired:




The previous screenshot contains no 32-bit hosted operating systems. The reason why is that, Hyper-V is enabled on Windows. If Hyper-V is is an installed feature of the operating system the VirtualBox is only capable of supporting 32-bit operating systems.

Uninstalling Hyper-V from Windows

The Programs category of Control Panel allows Windows features to be added and removed. To display the Program category of Control Panel, display Control Panel:


The Programs category is found in the lower left of the Control Panel folder. Click on Programs to display the Program category:


From under Programs and Features, click on Turn Windows features on and off which displays the Windows Features dialog;


Hyper-V is a listed feature and to uninstall it, simply uncheck this feature. 

VirtualBox Cannot Add an Existing 64-Bit Virtual Machine

For a recent project I was giving a VHD in VMDK format (a Windows 2012 R2 virtual machine which is  64-bit operating system). VMDK is the native VHD format of VMWare's virtualization technology. I installed VMWare Professional and attempted to get the VHD running as part of VMWare Profession virtual machine. For some reason it would not run and I was informed most team members were using VirtualBox. The reason was because some developers has MacBooks and some Windows Machines. VirtualBox was a free application that ran on both platforms.


In VirtualBox select the Machine menu's Add option to add an existing virtual machine:



The Add menu item allows a virtual machine to be selected which would be be the file that includes all the settings and references a specific virtual hard drive. The VirtualBox virtual machine is selected using the "Select virtual machine file" dialog:



From the dialog navigate to the location of the VirtualBox settings file (extension VBOX) and select Open:



Even though the virtual machine is 64-bit and cannot be run on VirtualBox (due to Hyper-V being installed on the computer) the virtual machine is added successfully VirtualBox's Manager application:



Attempting to start the VirtualMachine via Machine | Start | Normal Start results int he following error:


The error message displayed is "Failed to open a session for the virtual machine". This is an innocuous message but is frankly not helpful is diagnosing the issue. The problem is that it is a 64-bit guest operation system and VirtualBox is installed on a Windows machine on which Hyper-V is installed.


Sunday, June 12, 2016

Enabling the Snipping Tool on Windows 2012 R2

I was working a new project looking to document the development environment for both my personal edification and the benefit of future developers. I had Word 2016 installed and was going to use the Snipping tool to create some images to more clearly explain things. The default install of Windows 2012 R2 does not include the snipping too.

To enable the Snipping tool on Windows 2012 R2:

1) Run Server Manager:



2) Click on the Manage menu (upper right)




3) Click on Manage | Add Roles and Features:



4) The quick way to describe what do actions to take in order to install the Snipping Tool is to click Next then Next the Next. So on the previous screen click on Next (1 of 3) which displays the following:



5) The radio button on the previous screen has by default the "Role-based or feature-based installation" option select. This is the desired setting so click on Next (clicking Next 2 of 3) which displays the following:


Note: the environment this blog posting was created on used a lone Windows 20012 R2 running on Oracle's VirtualBox. In the previous screen shot the lone Windows 2012 R2 host (the one running within Virtual Box) is host name, ENCOREV-4UPPD388.

6) Before clicking on next make sure the host selected is the host on which the Snipping Tool is going to be installed. Once the correct host is selected, click Next (Next 3 of 3). In this example, the initially selected server is correct because we are managing a lone host, run within a VirtaulBox virtual machine. Clicking on Next displays the following screen:


7) Click on Next from the previous screen as there are no new roles to install.


8) On the previous screen scroll down until User Interfaces and Infrastructure is displayed and expand this option (see below):



9) Under User Interfaces and Infrastructure check Desktop Experience which will enable the Next button.
10) After clicking on Next additional features may be required to be installed in addition to Desktop Experience. If additional features are required a dialog will be displayed prompting the user to install these additional features:



11) Click on Add Features to install these additional features.



12) Once the additional feature dialog has been closed, click on Install to install the Desktop Experience features



13) After Install is pressed an additional dialog is displayed prompting a user to restart the machine if a restarted is required to install the features. Simply select Yes so the Desktop Experience features will be installed.

Once the install is underway the following screen will be displayed:


Once the Desktop Experience is installed, the virtual machine will reboot. The rebooted server will contain the Snipping Tool. Remember that Snipping Tool requires a minimum resolution for the server's display.