Purpose
This document explains how to create a Visual Basic dll.
Create Visual Basic project with Visual Studio
- Open Visual Studio
- File -> New -> Project
- Select Visual CBasic -> Class Library
Click OK.
In the solution tree -> Go to project properties.
Verify that solution platform is x64
Otherwise create it.
In Build tab, select platform x64 and check "Register for COM interop"
Rename Class1.vb file with an appropriate name for the add-in.
Modify code to make the Add-in
Open AssemblyInfo.vb in My Project folder, cut selected lines below:
- Paste it in the MyVBAddin.vb
- Verify that you have ComVisible(true) and not ComVisible(false).
- Add: Imports System.Runtime.InteropServices;
Like in screen shot below:
Include reference to EwAPIX.dll
Right click on Reference and select Add reference...
Browse to the location of EwAPIX.dll
Add: Imports EwAPI;
if Erreur BC31541 Change EwAPI (in references on the right) to incorporate... to false
Inherit your main class from EwAddIn
Implement those two methods:
Public Class MyVBAddin
'Inherit your main class from EwAddIn
Inherits EwAddInClass
'Implement those two methods:
Private Shared mEwApplicationX As IEwApplicationX
Private Shared mEwInteropFactory As IEwInteropFactoryX
Public Overrides Function connectToEwAPI(ByVal ewInteropFactory As Object) As Boolean
mEwInteropFactory = CType(ewInteropFactory, IEwInteropFactoryX)
If mEwInteropFactory Is Nothing Then
Return False
End If
Dim ewErrorCode As EwErrorCode = EwErrorCode.EW_UNDEFINED_ERROR
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", ewErrorCode)
If (ewErrorCode <> EwErrorCode.EW_NO_ERROR) OrElse (mEwApplicationX Is Nothing) Then
Return False
End If
hello()
Return True
End Function
Public Overrides Function disconnectFromEwApi() As Boolean
goodBye()
Return True
End Function
Finally add those two methods to your main class to register this add-in:
<ComRegisterFunctionAttribute>
Public Shared Sub RegisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
Dim addinkey As Microsoft.Win32.RegistryKey = hklm.CreateSubKey(keyname)
addinkey.SetValue(Nothing, 0)
addinkey.SetValue("Description", "Your add in description")
addinkey.SetValue("Title", "Your Add-in Name")
addinkey.SetValue("Path", System.Reflection.Assembly.GetExecutingAssembly().Location)
addinkey = hkcu.CreateSubKey(keyname)
addinkey.SetValue("StartUp", Convert.ToInt32(1), Microsoft.Win32.RegistryValueKind.DWord)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem registering this dll. " & vbLf & """" & nl.Message & """")
Catch e As System.Exception
Console.WriteLine(e.Message)
End Try
End Sub
<ComUnregisterFunctionAttribute>
Public Shared Sub UnregisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
hklm.DeleteSubKey(keyname)
hkcu.DeleteSubKey(keyname)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem unregistering this dll: " & nl.Message)
Catch e As System.Exception
Console.WriteLine("There was a problem unregistering this dll: " & e.Message)
End Try
End Sub
Here is an example of implementation:
Imports System.Runtime.InteropServices
Imports EwAPI 'add reference in MyProject
Imports Microsoft.VisualBasic.Strings
Imports System.IO
'Cut from AssemblyInfo.vb in My Project folder
<Assembly: ComVisible(True)>
<Assembly: Guid("9A110FC6-F7D5-4EE0-ADF1-E2E170BA8F52")>
'! [VB_Get_connectToEwAPI]
Public Class MyVBAddin
'Inherit your main class from EwAddIn
Inherits EwAddInClass
'Implement those two methods:
Private Shared mEwApplicationX As IEwApplicationX
Private Shared mEwInteropFactory As IEwInteropFactoryX
Public Overrides Function connectToEwAPI(ByVal ewInteropFactory As Object) As Boolean
mEwInteropFactory = CType(ewInteropFactory, IEwInteropFactoryX)
If mEwInteropFactory Is Nothing Then
Return False
End If
Dim ewErrorCode As EwErrorCode = EwErrorCode.EW_UNDEFINED_ERROR
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", ewErrorCode)
If (ewErrorCode <> EwErrorCode.EW_NO_ERROR) OrElse (mEwApplicationX Is Nothing) Then
Return False
End If
hello()
Return True
End Function
Public Overrides Function disconnectFromEwApi() As Boolean
goodBye()
Return True
End Function
'! [VB_Get_connectToEwAPI]
Public Function hello() As Boolean
Return True
End Function
Public Function goodBye() As Boolean
Return True
End Function
'Finally add those two methods to your main class to register this Add-in:
'! [VB_Get_Registry]
<ComRegisterFunctionAttribute>
Public Shared Sub RegisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
Dim addinkey As Microsoft.Win32.RegistryKey = hklm.CreateSubKey(keyname)
addinkey.SetValue(Nothing, 0)
addinkey.SetValue("Description", "Your add in description")
addinkey.SetValue("Title", "Your Add-in Name")
addinkey.SetValue("Path", System.Reflection.Assembly.GetExecutingAssembly().Location)
addinkey = hkcu.CreateSubKey(keyname)
addinkey.SetValue("StartUp", Convert.ToInt32(1), Microsoft.Win32.RegistryValueKind.DWord)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem registering this dll. " & vbLf & """" & nl.Message & """")
Catch e As System.Exception
Console.WriteLine(e.Message)
End Try
End Sub
<ComUnregisterFunctionAttribute>
Public Shared Sub UnregisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
hklm.DeleteSubKey(keyname)
hkcu.DeleteSubKey(keyname)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem unregistering this dll: " & nl.Message)
Catch e As System.Exception
Console.WriteLine("There was a problem unregistering this dll: " & e.Message)
End Try
End Sub
'! [VB_Get_Registry]
End Class
- Note
- License Code(*): Contact your reseller to get your license code