Skip to content

Commit f5a9e7a

Browse files
committed
Create DnaComServer sample with step-by-step instructions
1 parent 1f57b18 commit f5a9e7a

File tree

8 files changed

+387
-0
lines changed

8 files changed

+387
-0
lines changed

DnaComServer/AddIn.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Runtime.InteropServices;
2+
using ExcelDna.ComInterop;
3+
using ExcelDna.Integration;
4+
5+
namespace DnaComServer
6+
{
7+
[ComVisible(true)]
8+
[ClassInterface(ClassInterfaceType.AutoDual)]
9+
public class ComLibrary
10+
{
11+
public string ComLibraryHello()
12+
{
13+
return "Hello from DnaComServer.ComLibrary";
14+
}
15+
16+
public double Add(double x, double y)
17+
{
18+
return x + y;
19+
}
20+
}
21+
22+
[ComVisible(false)]
23+
public class ExcelAddin : IExcelAddIn
24+
{
25+
public void AutoOpen()
26+
{
27+
ComServer.DllRegisterServer();
28+
}
29+
public void AutoClose()
30+
{
31+
ComServer.DllUnregisterServer();
32+
}
33+
}
34+
35+
public static class Functions
36+
{
37+
[ExcelFunction]
38+
public static object DnaComServerHello()
39+
{
40+
return "Hello from DnaComServer!";
41+
}
42+
}
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<DnaLibrary Name="DnaComServer Add-In" RuntimeVersion="v4.0">
2+
<ExternalLibrary Path="DnaComServer.dll" ExplicitExports="false" ComServer="true" LoadFromBytes="true" Pack="true" />
3+
</DnaLibrary>

DnaComServer/DnaComServer.csproj

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DnaComServer</RootNamespace>
11+
<AssemblyName>DnaComServer</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="ExcelDna.Integration, Version=0.34.6373.42344, Culture=neutral, processorArchitecture=MSIL">
34+
<HintPath>packages\ExcelDna.Integration.0.34.6\lib\ExcelDna.Integration.dll</HintPath>
35+
<Private>False</Private>
36+
</Reference>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="AddIn.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="packages.config" />
51+
<None Include="Properties\ExcelDna.Build.props" />
52+
<None Include="DnaComServer-AddIn.dna" />
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55+
<PropertyGroup>
56+
<PostBuildEvent> REM Setting up environment vairables
57+
call "$(DevEnvDir)..\..\VC\Auxiliary\Build\vcvarsall.bat" x86
58+
59+
REM Temporarily copy ExcelDna.Integration.dll into output
60+
REM Note: Might need to change depending on where packages directory is
61+
copy "$(ProjectDir)\packages\ExcelDna.Integration.0.34.6\lib\ExcelDna.Integration.dll" "$(TargetDir)"
62+
63+
REM Create .tlb file
64+
tlbexp.exe "$(ProjectDir)$(OutDir)$(TargetName)$(TargetExt)" /out:"$(ProjectDir)$(OutDir)$(TargetName).tlb"
65+
66+
REM Delete extra copy of ExcelDna.Integration.dll from output
67+
del "$(TargetDir)ExcelDna.Integration.dll"
68+
69+
REM Re-run the packing to include the .tlb inside the packed files for distribution
70+
REM Note: Might need to change depending on where packages directory is
71+
"$(ProjectDir)\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDnaPack.exe" "$(ProjectDir)$(OutDir)$(TargetName)-AddIn.dna" /Y /O "$(ProjectDir)$(OutDir)$(TargetName)-AddIn-packed.xll"
72+
"$(ProjectDir)\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDnaPack.exe" "$(ProjectDir)$(OutDir)$(TargetName)-AddIn64.dna" /Y /O "$(ProjectDir)$(OutDir)$(TargetName)-AddIn64-packed.xll"
73+
74+
REM Register COM servers in add-in on this machine for testing
75+
REM Note: Change this to -AddIn64.xll if the 64-bit version of Excel is installed
76+
regsvr32.exe /s "$(ProjectDir)$(OutDir)$(TargetName)-AddIn.xll"</PostBuildEvent>
77+
</PropertyGroup>
78+
<Import Project="packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets" Condition="Exists('packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets')" />
79+
<Target Name="EnsureExcelDnaTargetsImported" BeforeTargets="BeforeBuild" Condition="'$(ExcelDnaTargetsImported)' == ''">
80+
<Error Condition="!Exists('packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets') And ('$(RunExcelDnaBuild)' != '' And $(RunExcelDnaBuild))" Text="You are trying to build with ExcelDna, but the NuGet targets file that ExcelDna depends on is not available on this computer. This is probably because the ExcelDna package has not been committed to source control, or NuGet Package Restore is not enabled. Please enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
81+
<Error Condition="Exists('packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets') And ('$(RunExcelDnaBuild)' != '' And $(RunExcelDnaBuild))" Text="ExcelDna cannot be run because NuGet packages were restored prior to the build running, and the targets file was unavailable when the build started. Please build the project again to include these packages in the build. You may also need to make sure that your build server does not delete packages prior to each build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
82+
</Target>
83+
</Project>

DnaComServer/DnaComServer.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2020
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DnaComServer", "DnaComServer.csproj", "{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5DB7CDE5-2379-4F53-9A6E-474419E9D97A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CAB7F5C1-D7FD-4A38-B48E-DAA656ECC722}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DnaComServer")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DnaComServer")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5db7cde5-2379-4f53-9a6e-474419e9d97a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="ExcelDnaProps">
3+
<!--
4+
If you change properties in this file, they may not come into effect until you:
5+
* Rebuild the solution/project
6+
7+
or
8+
9+
* Close Visual Studio
10+
* Delete .vs folder, if exists
11+
* Delete ProjectName.csproj.user (or equivalent for VB, F#, etc.), if exists
12+
* Delete SolutionName.suo, if exists
13+
* Open your solution/project again in Visual Studio
14+
-->
15+
16+
<!--
17+
Configuration properties for building .dna files
18+
-->
19+
<PropertyGroup>
20+
<!--
21+
Enable/Disable automatic generation of platform-specific versions of .dna files
22+
-->
23+
<ExcelDnaCreate32BitAddIn Condition="'$(ExcelDnaCreate32BitAddIn)' == ''">true</ExcelDnaCreate32BitAddIn>
24+
<ExcelDnaCreate64BitAddIn Condition="'$(ExcelDnaCreate64BitAddIn)' == ''">true</ExcelDnaCreate64BitAddIn>
25+
26+
<!--
27+
Define the suffix used for each platform-specific file e.g. MyAddIn64.dna
28+
-->
29+
<ExcelDna32BitAddInSuffix Condition="'$(ExcelDna32BitAddInSuffix)' == ''"></ExcelDna32BitAddInSuffix>
30+
<ExcelDna64BitAddInSuffix Condition="'$(ExcelDna64BitAddInSuffix)' == ''">64</ExcelDna64BitAddInSuffix>
31+
</PropertyGroup>
32+
33+
<!--
34+
Configuration properties for packing .dna files
35+
-->
36+
<PropertyGroup>
37+
<!--
38+
Enable/Disable packing of .dna files
39+
-->
40+
<RunExcelDnaPack Condition="'$(RunExcelDnaPack)' == ''">true</RunExcelDnaPack>
41+
42+
<!--
43+
Suffix used for packed .xll files e.g. MyAddIn-packed.xll
44+
-->
45+
<ExcelDnaPackXllSuffix Condition="'$(ExcelDnaPackXllSuffix)' == ''">-packed</ExcelDnaPackXllSuffix>
46+
</PropertyGroup>
47+
</Project>
48+

DnaComServer/Readme.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
* Create new C# project of type "Class Library (.NET Framework)" called DnaComServer
2+
3+
* Package Manager Console:
4+
`PM> Install-Package ExcelDna.AddIn`
5+
6+
* Rename Class1.cs to AddIn.cs with this code:
7+
8+
```c#
9+
using System.Runtime.InteropServices;
10+
using ExcelDna.ComInterop;
11+
using ExcelDna.Integration;
12+
13+
namespace DnaComServer
14+
{
15+
[ComVisible(true)]
16+
[ClassInterface(ClassInterfaceType.AutoDual)]
17+
public class ComLibrary
18+
{
19+
public string ComLibraryHello()
20+
{
21+
return "Hello from DnaComServer.ComLibrary";
22+
}
23+
24+
public double Add(double x, double y)
25+
{
26+
return x + y;
27+
}
28+
}
29+
30+
[ComVisible(false)]
31+
public class ExcelAddin : IExcelAddIn
32+
{
33+
public void AutoOpen()
34+
{
35+
ComServer.DllRegisterServer();
36+
}
37+
public void AutoClose()
38+
{
39+
ComServer.DllUnregisterServer();
40+
}
41+
}
42+
43+
public static class Functions
44+
{
45+
[ExcelFunction]
46+
public static object DnaComServerHello()
47+
{
48+
return "Hello from DnaComServer!";
49+
}
50+
}
51+
}
52+
```
53+
54+
* DnaComServer-AddIn.dna file:
55+
56+
```xml
57+
<DnaLibrary Name="DnaComServer Add-In" RuntimeVersion="v4.0">
58+
<ExternalLibrary Path="DnaComServer.dll" ExplicitExports="false" ComServer="true" LoadFromBytes="true" Pack="true" />
59+
</DnaLibrary>
60+
```
61+
* Fix Project -> Debug settings to remove %1 in "Start external program" path if present
62+
63+
* Press F5 to build and load in Excel
64+
65+
* Check formula =DnaComServerHello() in a sheet
66+
67+
* Open VBA IDE (Alt+F11)
68+
69+
* Insert a new Module
70+
71+
* Put this code in to test late-bound COM Server
72+
73+
```vb
74+
Sub TestLateBound()
75+
Dim dnaComServer As Object
76+
Dim hello As String
77+
Dim result As Double
78+
79+
Set dnaComServer = CreateObject("DnaComServer.ComLibrary")
80+
hello = dnaComServer.ComLibraryHello()
81+
result = dnaComServer.Add(1, 2)
82+
83+
Debug.Print hello, result
84+
End Sub
85+
```
86+
87+
* Run by clicking inside Sub and pressing F5
88+
89+
* Check Immediate window for output:
90+
Hello from DnaComServer.ComLibrary 3
91+
92+
* Set up the tlbexp run in the post-build step
93+
94+
```
95+
REM Setting up environment vairables
96+
call "$(DevEnvDir)..\..\VC\Auxiliary\Build\vcvarsall.bat" x86
97+
98+
REM Temporarily copy ExcelDna.Integration.dll into output
99+
REM Note: Might need to change depending on where packages directory is
100+
copy "$(ProjectDir)\packages\ExcelDna.Integration.0.34.6\lib\ExcelDna.Integration.dll" "$(TargetDir)"
101+
102+
REM Create .tlb file
103+
tlbexp.exe "$(ProjectDir)$(OutDir)$(TargetName)$(TargetExt)" /out:"$(ProjectDir)$(OutDir)$(TargetName).tlb"
104+
105+
REM Delete extra copy of ExcelDna.Integration.dll from output
106+
del "$(TargetDir)ExcelDna.Integration.dll"
107+
108+
REM Re-run the packing to include the .tlb inside the packed files for distribution
109+
REM Note: Might need to change depending on where packages directory is
110+
"$(ProjectDir)\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDnaPack.exe" "$(ProjectDir)$(OutDir)$(TargetName)-AddIn.dna" /Y /O "$(ProjectDir)$(OutDir)$(TargetName)-AddIn-packed.xll"
111+
"$(ProjectDir)\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDnaPack.exe" "$(ProjectDir)$(OutDir)$(TargetName)-AddIn64.dna" /Y /O "$(ProjectDir)$(OutDir)$(TargetName)-AddIn64-packed.xll"
112+
113+
REM Register COM servers in add-in on this machine for testing
114+
REM Note: Change this to -AddIn64.xll if the 64-bit version of Excel is installed
115+
regsvr32.exe /s "$(ProjectDir)$(OutDir)$(TargetName)-AddIn.xll"
116+
```
117+
118+
* F5 to build and run Excel
119+
120+
* Open VBA IDE (Alt+F11)
121+
122+
* Insert a new Module
123+
124+
* Add a reference to the .tlb:
125+
* Tools -> References
126+
* Find and tick DnaComServer in the list
127+
128+
* Put this code in to test late-bound COM Server
129+
130+
```vb
131+
Sub TestEarlyBound()
132+
Dim dnaComServer As DnaComServer.ComLibrary
133+
Dim hello As String
134+
Dim result As Double
135+
136+
Set dnaComServer = New DnaComServer.ComLibrary
137+
hello = dnaComServer.ComLibraryHello()
138+
result = dnaComServer.Add(1, 2)
139+
140+
Debug.Print hello, result
141+
End Sub
142+
```
143+
* Put cursor inside the Sub and press F5 to test - check Immediate window for output
144+

DnaComServer/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="ExcelDna.AddIn" version="0.34.6" targetFramework="net40" />
4+
<package id="ExcelDna.Integration" version="0.34.6" targetFramework="net40" />
5+
</packages>

0 commit comments

Comments
 (0)