Tahir Hassan's Blog

My Technical Notes

Friday, 29 May 2015

Getting Help in PowerShell

Getting help in Powershell is quite easy. To get all the loaded modules, we use:

To get all the commands in a newly installed module, for example, PowerShell Community Extensions, you type in:

In this case:

To get help on a specific command, we use `Get-Help <command name>`, for example:

To add help to your own commands, see

Friday, 22 May 2015

F# COM Component code

This is a simple COM component written in F#.

The F# library project template does not come with an `AssemblyInfo.fs`, therefore you have to add one yourself. Stick it at the top of your solution.

Google-ing for `AssemblyInfo.fs` gives many examples. There is even an extension, F# Assembly Info File, for it.

You will also need an assembly key file which can be generated using the below command, which should be created in the project root directory.

Here is a simple example of a COM class, `Class1` with an interface `IClass1`, with a string property `XYZ`:

Tuesday, 19 May 2015

Creating a COM Component in C#

Here are the steps to expose a library written in C# to COM:

Setting COM-Visibility

Set the `ComVisible` attribute to `true` in `AssemblyInfo.cs`: This will make all public classes visible to COM. If you want to selectively hide a class, then put on top of it.

Registering the Library

You can register your .NET library with COM in two different ways:

1. Using Visual Studio, go to the project properties, go to the `Build` tab, and check `Register for COM interop`. COM interop registration will happen on each build.

2. For more control, use the `regasm.exe` command line utility.

There are two versions of `regasm.exe`, the 64-bit version resides in `Framework64`. Full path:

The 32-bit version resides in the `Framework` folder (remove "64" from the above path): Use the appropriate version, depending on whether you want to register it for a 64-bit consumer or a 32-bit consumer.

Execute the below command:

With the `/codebase` parameter, it is recommended to strong-name the assembly.

To unregister the interface, we do:

C# class

Note that all attributes mentioned below are found in `System.Runtime.InteropServices` namespace.

Because all types in the DLL will be visible to COM, it suffices to add only one attribute on top of a class we wish to expose:

For example:

This will generate an interface based on the class.

If, instead we wish to follow the guidelines and create a separate interface for each COM class, as like [1] explains and [2] recomments, we must generate GUIDs for each class (using Tools → Create GUID) and set other COM specific attributes:

I had to do this to expose it to an AutoHotKey script, otherwise it would not find the method.

Testing the COM component in PowerShell

Assuming that the library name is `ClassLibrary1`, the following PowerShell code should successfully instantiate an `Arithmetic` object and use it to add two numbers together.

Angular: Populating the model from an input

TODO:Polish this up a bit

DataRow to JSON

Following `DataRowToJson` method converts a `DataRow` object into its JSON representation (`string`):

Monday, 11 May 2015

C# - Running Asynchronous synchronously within a UI thread

Following `RunSync` method runs a method synchronously on the ThreadPool and returns the result:

Call it like:

Friday, 8 May 2015

PowerShell: using the Visual Studio Command Prompt bat file

Install PowerShell Community Extensions and Import the PSCX module:

You may have to restart the computer after installing it.

You can use either `Invoke-BatchFile` with the full path to the bat file:

or you can use the `Import-VisualStudioVars` command:

Note that calling it twice will still re-append to the `path` variable. To prevent this from happening, we can use `Get-Command`: