Purpose
This document explains how to create a c# dll.
Create C# project with Visual Studio
- Open Visual Studio
- File -> New -> Project
- Select Visual C# -> 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"
In the Build Events tab, In the Post-build events command line Add this comand line: C:\WINDOWS\Microsoft.NET\Framework64\$(frameworkversion)\regasm.exe "$(TargetPath)" /codebase
Rename Class1.cs file with an appropriate name for the Add-in.
Modify code to make the Add-in
Open Assemblyinfo.cs, cut selected lines below:
In your Class interface (HelloWorld.cs):
- Paste previously copied information
- Verify that you have ComVisible(true) and not ComVisible(false).
- Add: using 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: using EwAPI;
Inherit your main class from EwAddIn
Implement those two methods:
public class HelloWorld : EwAPI.EwAddIn
{
public bool connectToEwAPI(object ewInteropFactory)
{
if (mEwInteropFactory == null)
return false;
mEwApplicationX = mEwInteropFactory.
getEwApplication(
"License Code(*)", out ewErrorCode);
if ((ewErrorCode !=
EwErrorCode.EW_NO_ERROR) ||(mEwApplicationX ==
null))
return false;
hello();
return true;
}
public bool disconnectFromEwApi()
{
goodBye();
return true;
}
EwErrorCode
All errors codes for this API.
Definition: EnumDefinition.idl:24
Use this interface to manage the application.
Definition: EwAPI.idl:694
Use this interface to connect to the application.
Definition: EwAPI.idl:1500
IEwApplicationX getEwApplication(BSTR strKey, EwErrorCode *errorCode)
Return an IEwApplicationX object connect to the application.
Finally add those two methods to your main class to register this Add-in:
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 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 (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll. \n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "Software\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
hkcu.DeleteSubKey(keyname);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem unregistering this dll: " + nl.Message);
}
catch (System.Exception e)
{
Console.WriteLine("There was a problem unregistering this dll: " + e.Message);
}
}
Here is an example of implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using EwAPI;
namespace MyCSharpAddin
{
[ComVisible(true)]
[Guid("edb6bfd9-22a6-486a-98b0-07be0d9b34d0")]
public class HelloWorld : EwAPI.EwAddIn
{
public bool connectToEwAPI(object ewInteropFactory)
{
if (mEwInteropFactory == null)
return false;
mEwApplicationX = mEwInteropFactory.
getEwApplication(
"License Code(*)", out ewErrorCode);
if ((ewErrorCode !=
EwErrorCode.EW_NO_ERROR) ||(mEwApplicationX ==
null))
return false;
hello();
return true;
}
public bool disconnectFromEwApi()
{
goodBye();
return true;
}
public void hello()
{
if (mEwApplicationX == null)
return;
EwDialogTaskX ewDialogTask = mEwApplicationX.
getEwDialogTask(out ewErrorCode);
if ((ewErrorCode !=
EwErrorCode.EW_NO_ERROR) || (ewDialogTask ==
null))
return;
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
}
public void goodBye()
{
if (mEwApplicationX == null)
return;
EwDialogTaskX ewDialogTask = mEwApplicationX.
getEwDialogTask(out ewErrorCode);
if ((ewErrorCode !=
EwErrorCode.EW_NO_ERROR) || (ewDialogTask ==
null))
return;
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 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 (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll. \n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "Software\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
hkcu.DeleteSubKey(keyname);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem unregistering this dll: " + nl.Message);
}
catch (System.Exception e)
{
Console.WriteLine("There was a problem unregistering this dll: " + e.Message);
}
}
}
}
IEwDialogTaskX getEwDialogTask(EwErrorCode *errorCode)
To get the task dialog wrapper.
void setContent(BSTR strContent)
Set the content.
- Note
- License Code(*): Contact your reseller to get your license code