- Currently:
I can register the dll to COM if I run the visual studio as administrator. Because there is a script in post-build event command line and that of Start Options that runs the regasm and registers it. I can register it via regasm manually but only if I run command prompt as admin.
- I want to:
use Visual Studio Installer to do it so that the dll gets registered on end user machine after installation. the weird thing is that the VS installer creates the bootstrapper (.msi file) which does install the app but the dll is not registered afterwards. I have an admin account on windows 10.
IF EXIST "$(TargetDir)$(TargetName).bmp" (GOTO REGISTRATION)
XCOPY "$(ProjectDir)AddinIcon.bmp" "$(TargetDir)" /F
REN "$(TargetDir)AddinIcon.bmp" "$(TargetName).bmp"
:REGISTRATION
IF "$(TargetFrameworkVersion)"=="v4.0" GOTO NET40
IF "$(TargetFrameworkVersion)"=="v3.5" GOTO NET20
IF "$(TargetFrameworkVersion)"=="v3.0" GOTO NET20
IF "$(TargetFrameworkVersion)"=="v2.0" GOTO NET20
GOTO END
:NET40
set FMWK="v4.0.30319"
GOTO REG
:NET20
set FMWK="v2.0.50727"
GOTO REG
:REG
IF "$(PlatformName)" == "AnyCPU" GOTO ANYCPU
IF "$(PlatformName)" == "x64" GOTO X64
GOTO END
:ANYCPU
IF EXIST "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm.exe"
"%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm" /codebase "$(TargetPath)"
GOTO END
:X64
IF EXIST "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm.exe"
"%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm" /codebase "$(TargetPath)"
GOTO END
:END
and this is the ComRegisterFunctionAttribute
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
#region Get Custom Attribute: SwAddinAttribute
SwAddinAttribute SWattr = null;
Type type = typeof(SwAddin);
foreach (System.Attribute attr in type.GetCustomAttributes(false))
{
if (attr is SwAddinAttribute)
{
SWattr = attr as SwAddinAttribute;
break;
}
}
#endregion
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\Addins\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
addinkey.SetValue("Description", SWattr.Description);
addinkey.SetValue("Title", SWattr.Title);
keyname = "Software\\SolidWorks\\AddInsStartup\\{" + t.GUID.ToString() + "}";
addinkey = hkcu.CreateSubKey(keyname);
addinkey.SetValue(null, Convert.ToInt32(SWattr.LoadAtStartup), Microsoft.Win32.RegistryValueKind.DWord);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll: SWattr is null. \n\"" + nl.Message + "\"");
System.Windows.Forms.MessageBox.Show("There was a problem registering this dll: SWattr is null.\n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
System.Windows.Forms.MessageBox.Show("There was a problem registering the function: \n\"" + e.Message + "\"");
}
}