You can use PowerShell to register a typelib. The script uses a small piece of C# code, that in its turn calls WinApi function LoadTypeLibEx
. The script has a single argument, a path of the typelib. Create RegisterTypeLib.ps1
:
param($path)
Write-Host $path
$signature = @'
[DllImport("oleaut32.dll", CharSet=CharSet.Unicode, ExactSpelling=true, SetLastError=true)]
public static extern int LoadTypeLibEx(string fileName, uint regkind, out System.Runtime.InteropServices.ComTypes.ITypeLib typeLib);
'@
$type = Add-Type -MemberDefinition $signature `
-Name Win32Utils -Namespace LoadTypeLibEx `
-PassThru
$typeLib = $null
$hr = $type::LoadTypeLibEx($path, 1, ([ref]$typeLib))
if ($hr -ne 0)
{
throw "LoadTypeLibEx failed, hr = $hr"
}
It's handy to use a batch file to launch the script; it just passes the argument to the script:
powershell -ExecutionPolicy ByPass -NoProfile -Command "& '%~dp0\RegisterTypeLib.ps1'" %*
Additionally, the following script can be used to unregister a typelib. It's a bit tricky but works well:
param($path)
Add-Type -Language CSharp @'
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace Helpers
{
public static class TypeLibRegistration
{
[DllImport("oleaut32.dll", CharSet=CharSet.Unicode, ExactSpelling=true, SetLastError=true)]
public static extern int LoadTypeLibEx(string fileName, uint regkind, out ITypeLib typeLib);
[DllImport("oleaut32.dll", CharSet=CharSet.Unicode, ExactSpelling=true, SetLastError=true)]
public static extern int UnRegisterTypeLib(ref Guid libID, ushort wVerMajor, ushort wVerMinor, uint lcid, uint syskind);
public struct TLIBATTR
{
public Guid guid;
public uint lcid;
public uint syskind;
public ushort wMajorVerNum;
public ushort wMinorVerNum;
public ushort wLibFlags;
}
public static void Unregister(string fileName)
{
ITypeLib typeLib;
int hr = LoadTypeLibEx(fileName, 0, out typeLib);
if (0 != hr)
throw new Exception(string.Format("LoadTypeLibEx() failed, hr = {0}", hr));
IntPtr libAttrPtr;
typeLib.GetLibAttr(out libAttrPtr);
TLIBATTR libAttr = (TLIBATTR)Marshal.PtrToStructure(libAttrPtr, typeof(TLIBATTR));
hr = UnRegisterTypeLib(ref libAttr.guid, libAttr.wMajorVerNum, libAttr.wMinorVerNum, libAttr.lcid, libAttr.syskind);
if (0 != hr)
throw new Exception(string.Format("UnRegisterTypeLib() failed, hr = {0}", hr));
}
}
}
'@
[Helpers.TypeLibRegistration]::Unregister($path)