1
  • 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 + "\"");
        }
    }
H.Ashrafi
  • 19
  • 4
  • Is `RegisterFunction´ called at all? any of the exceptions that you handle thrown? silent failure or message in setup event log? – Cee McSharpface May 03 '17 at 13:56
  • Did you actually mark your DLL as requiring registration in the installer project? It is not automagic. – Hans Passant May 03 '17 at 14:08
  • yes @dlatikay it gets called, but only when I run the visual basic or Regasm as administrator. the error message that I receive mentions that I don't have admin privileges. – H.Ashrafi May 04 '17 at 11:21
  • @HansPassant yes I put Register property as vsdraCOM, if that is what you meant. – H.Ashrafi May 04 '17 at 11:26
  • That's what I meant. Next possible quirk is that your COM server appears to only work for 64-bit client programs. Be sure to mark your installer to be a 64-bit installer, the default is 32-bit and that will write the wrong registry keys. And use a utility like SysInternals' Process Monitor so you can see *exactly* what the installer does. Compare with the trace you get from your .bat file and the difference ought to pop out. – Hans Passant May 04 '17 at 11:31
  • thanks @HansPassant . I was trying this method [link](http://stackoverflow.com/questions/2151003/why-wont-my-setup-project-perform-my-custom-registration-process) through which I managed to use regsrv32 to register the dll in CLSID. But it's not added to the app registry hive. – H.Ashrafi May 04 '17 at 14:29

0 Answers0