Programming

11 Articles

Coding Tips from Writing Code that isn’t Needed YouTube Video

The above video,Three Flaws in Software Design – Part 1: Writing Code that isn’t Needed, was a bit long considering the content presented but there were two ideas worth sharing:

  1. Let the version control system worry about tracking dead code. I’ve worked in plenty of programs with code that I’ve been tempted to bypass because it was suspected dead code. Removing the suspected dead code might not cause the customers testing to fail but customers don’t always test their own requirements properly. Without the old code it becomes difficult to track how the program once functions unless you use a version control system. Whats worse than trying to avoid making dead code? Working around mysterious code from a long departed coder, unsure if it’s still relevant, while trying to make my own logic clear among rotting bits.
  2. Establish when functions can assume variables are well formatted and note them in the function documentation. It’s so easy to start coding away, checking for possible conditions that might have been checked closer to the user input. It’s painfully agonizing trying to return a high level validation failure from a very low level function.

If the addition videos in the series contain any pearls of wisdom they’ll get posted here.

International Spectrum Article

The company I work with contributes to International Spectrum. The publication serves the multi-value database community with news and new technologies. It was my pleasure to contribute an article. The second part of the two-part article was recently featured in the publication.

The article demonstrates how BASIC-like languages can be extended using the object oriented programming (OOP) concept of encapsulation to protect private variables. The article covers benefits and provides examples of how these modern concepts can further benefit non-OOP languages.

First part
http://www.intl-spectrum.com/mag/MARAPR.2010/default.aspx

Second part
http://www.intl-spectrum.com/mag/JANFEB.2010/default.aspx

The entire magazine is available for PDF download. No registration required.

Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL – Visual Basic Example Code

By default the latest version of an assembly is loaded from the GAC (Global Assembly Cache) when the assembly is instantiated. While working on a side project the need arose to load older versions of the assembly without hard coding the assembly versions. The answer, load the existing assembly library from the assembly DLL and interrogate it to find the assembly information. Then ask the GAC to provide a handle to the specific object. The result allows multiple versions of the same assembly to be loaded loaded at the same time.

The Visual Basic .NET example code below loads SampleAppV1\DotNetLibrary.DLL and AnotherSampleAppV2\DotNetLibrary.DLL which are different versions of the same assembly.

'Load assembly using reflection from the file specified so we can get information about it
Dim DLLAssemblyInfo As System.Reflection.Assembly
DLLAssemblyInfo = System.Reflection.Assembly.LoadFrom("\\\\appserver.company.int\\SampleAppV1\\DotNetLibrary.DLL")

'Now load the assembly from the GAC using the information in the file. 
Dim GACDLL As System.Reflection.Assembly
GACDLL = System.Reflection.Assembly.Load(AssemblyInfo.FullName, DLLAssemblyInfo.Evidence)
'Load the second version of the same assembly from the newer application
Dim DLLAssemblyInfoV2 As System.Reflection.Assembly
DLLAssemblyInfoV2 = System.Reflection.Assembly.LoadFrom("\\\\appserver.company.int\\AnotherSampleAppV2\\DotNetLibrary.DLL")
'Again, use the DLL information from the assembly file to load the assembly from the GAC
Dim GACDLLv2 As System.Reflection.Assembly
GACDLLv2 = System.Reflection.Assembly.Load(AssemblyInfoV2.FullName, DLLAssemblyInfoV2.Evidence)
'At this point in the code we have references to both assemblies loaded from the GAC. The next step is to instantiate an object from each of the references.
'Get an MyAssembly.Library from the old version and run the version method
Dim GACDLLType As Type = GACDLL.GetType("MyAssembly.Library")
Dim MyLibV1 As Object = Activator.CreateInstance(GACDLLType)
Dim v1 As String
v1 = MyLibV1.Version
'Finally, do the same and get the new MyAssembly.Library
Dim GACDLLTypeV2 As Type = GACDLLV2.GetType("MyAssembly.Library")
Dim MyLibV2 As Object = Activator.CreateInstance(GACDLLTypeV2)
Dim v2 As String
v2 = MyLibV2.Version

The variables v1 and v2 were both retrieved from the MyAssembly.Library.Version() method but the variables will not contain the same value because different versions of the assembly were loaded from the GAC during the System.Reflection.Assembly.Load() call.

Why not just load the assembly from each of the DLL files instead of the GAC? Security restrictions prevented untrusted assemblies from being loaded over the network. A possible work around would be to trust the network location or sign the assemblies and trust the signer but that remains for another posting.

How to Launch a WSH VBScript as Administrator in Windows 7 and Vista

The example below launches a VB script as administrator on Windows 7 and Vista regardless if the context menu contains the “Run as administrator” option. Registry tweaks exist to add the run as administrator option to the context menu for .vbs files but this creates complications if the script is distributed to other people.

When the script below is executed it checks to see if it was passed a command line argument named “elevated”. If it is not found the script recursively calls itself passing the elevated command line argument and requests to be run as administrator. The user is prompted to confirm the action and the restricted script exits leaving the escalated administrator script running. When the user grants permission the elevated argument is found and the script changes back from the %System32% working directory to the script where the script is located.

If WScript.Arguments.Named.Exists(“elevated”) = False Then
‘Launch the script again as administrator
CreateObject(“Shell.Application”).ShellExecute “wscript.exe”, “””” & WScript.ScriptFullName & “”” /elevated”, “”, “runas”, 1
WScript.Quit
Else
‘Change the working directory from the system32 folder back to the script’s folder.
Set oShell = CreateObject(“WScript.Shell”)
oShell.CurrentDirectory = CreateObject(“Scripting.FileSystemObject”).GetParentFolderName(WScript.ScriptFullName)
MsgBox “Now running with elevated permissions”
End If
If WScript.Arguments.Named.Exists("elevated") = False Then
	'Launch the script again as administrator
	CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
	WScript.Quit
Else
	'Change the working directory from the system32 folder back to the script's folder.
	Set oShell = CreateObject("WScript.Shell")
	oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
	MsgBox "Now running with elevated permissions"
End If

Give Google An Answer To Find the Question!?

This is a anecdote of how Google gave the question to the answer I already had.

Normally, people input questions and keywords into Google to find an answer. In my case the answer was known. Use caspol.exe with the -pub -file arguments to trust all the assemblies signed with my certificate. The MSDN caspol.exe documentation was helpful but the exact syntax for implementing the command was a little fuzzy.

When in doubt Google it. Behold, the question to my answer stated in a question:

“What does this statement do? caspol -machine -addgroup All_Code -pub -file MyApp.exe FullTrust”

Thanks to Matthew Podwysocki his question fit my answer and provided the syntax needed to complete my work with caspol.