Skip to content

Instantly share code, notes, and snippets.

@dfch
Created August 21, 2019 12:33
Show Gist options
  • Save dfch/6a27bb1b9320c93456cee6d5b2b9d551 to your computer and use it in GitHub Desktop.
Save dfch/6a27bb1b9320c93456cee6d5b2b9d551 to your computer and use it in GitHub Desktop.
Sparx Enterprise Architect Registration of ActiveX Controls under HKEY_CURRENT_USER
public class ComRegistration
{
private const string DEFAULT_VALUE = "";
private const char BACKSLASH = '\\';
private const string HKEY_SOFTWARE_CLASSES = @"SOFTWARE\Classes\";
private const string HKEY_SOFTWARE_WOW_CLSID = @"Software\Classes\WOW6432Node\CLSID\";
private const string IMPLEMENTED_CATEGORY_DOTNET = @"Implemented Categories\\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}";
private const string THREADING_MODEL = "ThreadingModel";
private const string CLASS = "Class";
private const string ASSEMBLY = "Assembly";
private const string RUNTIME_VERSION = "RuntimeVersion";
private const string CODEBASE = "CodeBase";
public string ProgName { get; }
public Guid ProgGuid { get; }
public Type Type { get; }
private readonly string typeFullName;
public ComRegistration(Type type)
{
Contract.Requires(null != type);
Contract.Requires(typeof(IContainerControl).IsAssignableFrom(type));
this.Type = type;
var nameAttribute = Type.GetCustomAttribute<ProgIdAttribute>();
Contract.Assert(null != nameAttribute);
Contract.Assert(!string.IsNullOrWhiteSpace(nameAttribute.Value));
ProgName = nameAttribute.Value;
var guidAttribute = Type.GetCustomAttribute<GuidAttribute>();
Contract.Assert(null != guidAttribute);
Contract.Assert(default != guidAttribute.Value);
ProgGuid = new Guid(guidAttribute.Value);
typeFullName = Type.FullName;
Contract.Assert(!string.IsNullOrWhiteSpace(typeFullName));
}
public bool Register()
{
var keyClasses = $"{HKEY_SOFTWARE_CLASSES}{ProgName}{BACKSLASH}CLSID";
var regKeyClasses = Registry.CurrentUser.CreateSubKey(keyClasses);
Contract.Assert(null != regKeyClasses);
regKeyClasses.SetValue(DEFAULT_VALUE, ProgGuid.ToString("B"));
var keyClsid = $"{HKEY_SOFTWARE_WOW_CLSID}{ProgGuid:B}";
var regKeyClsid = Registry.CurrentUser.CreateSubKey(keyClsid);
Contract.Assert(null != regKeyClsid);
regKeyClsid.SetValue(DEFAULT_VALUE, typeFullName);
var implementedCategories = regKeyClsid.CreateSubKey(IMPLEMENTED_CATEGORY_DOTNET);
Contract.Assert(null != implementedCategories);
var assembly = Type.Assembly;
Contract.Assert(null != assembly);
var regKeyInprocServer32 = regKeyClsid.CreateSubKey("InprocServer32");
Contract.Assert(null != regKeyInprocServer32);
regKeyInprocServer32.SetValue(DEFAULT_VALUE, "mscoree.dll");
regKeyInprocServer32.SetValue(THREADING_MODEL, "Both");
regKeyInprocServer32.SetValue(CLASS, typeFullName);
regKeyInprocServer32.SetValue(ASSEMBLY, assembly.ToString());
regKeyInprocServer32.SetValue(RUNTIME_VERSION, assembly.ImageRuntimeVersion);
regKeyInprocServer32.SetValue(CODEBASE, assembly.CodeBase);
var regKeyVersion = regKeyInprocServer32.CreateSubKey("1.0.0.0");
Contract.Assert(null != regKeyVersion);
regKeyVersion.SetValue(CLASS, typeFullName);
regKeyVersion.SetValue(ASSEMBLY, assembly.ToString());
regKeyVersion.SetValue(RUNTIME_VERSION, assembly.ImageRuntimeVersion);
regKeyVersion.SetValue(CODEBASE, assembly.CodeBase);
var regKeyProgId = regKeyClsid.CreateSubKey("ProgId");
Contract.Assert(null != regKeyProgId);
regKeyProgId.SetValue(DEFAULT_VALUE, typeFullName);
return true;
}
public bool UnRegister()
{
try
{
var keyClasses = $"{HKEY_SOFTWARE_CLASSES}{ProgName}";
Registry.CurrentUser.DeleteSubKeyTree(keyClasses);
}
catch (Exception) { /* */ }
try
{
var keyClsid = $"{HKEY_SOFTWARE_WOW_CLSID}{ProgGuid:B}";
Registry.CurrentUser.DeleteSubKeyTree(keyClsid);
}
catch (Exception) { /* */ }
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment