Register DLL for demand load from a console application
This is my first application based only in VB.NET, and I'm trying to replicate what I did for previous VBA applications, for which I had a console application that placed the dvb and acaddoc.lsp files in a specific folder so that the commands would be available without any action need from the user other than running that console application.
First, as suggested in AutoCAD .NET Developer's Guide, I placed this procedure inside the DLL:
Imports Microsoft.Win32 Imports System.Reflection Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices <CommandMethod("RegisterMyApp")> Public Sub RegisterMyApp() '' Get the AutoCAD Applications key Dim sProdKey As String = HostApplicationServices.Current.UserRegistryProductRootKey Dim sAppName As String = "MyApp" Dim regAcadProdKey As Autodesk.AutoCAD.Runtime.RegistryKey = Autodesk.AutoCAD.Runtime.Registry.CurrentUser.OpenSubKey(sProdKey) Dim regAcadAppKey As Autodesk.AutoCAD.Runtime.RegistryKey = regAcadProdKey.OpenSubKey("Applications", True) '' Check to see if the "MyApp" key exists Dim subKeys() As String = regAcadAppKey.GetSubKeyNames() For Each sSubKey As String In subKeys '' If the application is already registered, exit If (sSubKey.Equals(sAppName)) Then regAcadAppKey.Close() Exit Sub End If Next '' Get the location of this module Dim sAssemblyPath As String = Assembly.GetExecutingAssembly().Location '' Register the application Dim regAppAddInKey As Autodesk.AutoCAD.Runtime.RegistryKey = regAcadAppKey.CreateSubKey(sAppName) regAppAddInKey.SetValue("DESCRIPTION", sAppName, RegistryValueKind.String) regAppAddInKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord) regAppAddInKey.SetValue("LOADER", sAssemblyPath, RegistryValueKind.String) regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord) regAcadAppKey.Close() End
This way I can load the DLL with NETLOAD and, after running the procedure, for the next time it loads automatically. So far the only way I found to do the same automatically from the console application is with the following procedure:
Private Sub RegisterMyApp() On Error Resume Next Dim acadApp As Object Dim i As Integer, version As String For i = 0 To 4 Select Case i Case 0 : version = "19.1" Case 1 : version = "20" Case 2 : version = "20.1" Case 3 : version = "21" Case Else : version = "22" End Select acadApp = CreateObject("AutoCAD.Application." & version) acadApp.ActiveDocument.SendCommand("(command ""NETLOAD""" & Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).Replace("\", "/") & "/<path>.dll""" & ")" & vbCr) acadApp.ActiveDocument.SendCommand("(command ""REGISTERMYAPP""" & ")" & vbCr) acadApp.Quit() Next End Sub
It works, but I know that using SendCommand in the middle of a running code is not a good approach, and in this case AutoCAD remains opened, even after acadApp.Quit(), which is not intended. I suppose that's because it is still running the SendCommand at that time.
Is it possible to have the first "RegisterMyApp" procedure inside the console application, inside the loop, registering the DLL for all AutoCAD versions available (without the need of loading the DLL at that time and suppressing both SendCommands)? I didn't find how to do it yet because the procedure seem to require AutoCAD's references and I suppose I cannot have then on the console application.
Thanks in advance.