{"id":52,"date":"2016-10-12T23:07:56","date_gmt":"2016-10-12T23:07:56","guid":{"rendered":"http:\/\/www.maxwellmckinnon.com\/blog\/?p=52"},"modified":"2022-03-05T20:34:33","modified_gmt":"2022-03-05T20:34:33","slug":"how-to-tie-a-c-net-gui-to-a-callable-c-net-class-library-dll-from-excel-vba-guided-minimum-framework-example","status":"publish","type":"post","link":"http:\/\/maxwellmckinnon.com\/blog\/2016\/10\/12\/how-to-tie-a-c-net-gui-to-a-callable-c-net-class-library-dll-from-excel-vba-guided-minimum-framework-example\/","title":{"rendered":"How to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA: Guided Minimum Framework Example"},"content":{"rendered":"<h1>Summary:<\/h1>\n<p>A minimum working example of how to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA or another C# application.<\/p>\n<p>&nbsp;<\/p>\n<p>This is a follow up to the non GUI example posted earlier<\/p>\n<p>&nbsp;<\/p>\n<p>Project:\u00a0<a href=\"http:\/\/www.maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/AutomationComExample.zip\">AutomationComExample.zip<\/a><\/p>\n<p>&nbsp;<\/p>\n<div class=\"toc-macro rbtoc1476313562439\">\n<ul class=\"toc-indentation\">\n<li><span class=\"TOCOutline\">1<\/span> Summary:<\/li>\n<li><span class=\"TOCOutline\">2<\/span> Framework Structure\n<ul class=\"toc-indentation\">\n<li><span class=\"TOCOutline\">2.1<\/span> ExampleTargetApp (GUI Application)\n<ul class=\"toc-indentation\">\n<li><span class=\"TOCOutline\">2.1.1<\/span> MainWindow.xaml.cs<\/li>\n<li><span class=\"TOCOutline\">2.1.2<\/span> HelloWorldService.cs<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"TOCOutline\">2.2<\/span> ServiceDefinition\n<ul class=\"toc-indentation\">\n<li><span class=\"TOCOutline\">2.2.1<\/span> IHelloWorldService.cs<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"TOCOutline\">2.3<\/span> CallingDll\n<ul class=\"toc-indentation\">\n<li><span class=\"TOCOutline\">2.3.1<\/span> ServiceProxy.cs<\/li>\n<li><span class=\"TOCOutline\">2.3.2<\/span> ICallTargetApp.cs<\/li>\n<li><span class=\"TOCOutline\">2.3.3<\/span> CallTargetApp.cs<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"TOCOutline\">3<\/span> Example access from VBA<\/li>\n<\/ul>\n<h1 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-FrameworkStructure\">Framework Structure<\/h1>\n<p>The goal is to connect the piping features of WCF named piping class with the GUI features of the MainWindow class. C# does not support multiple inheritance, but that is fine. We only need an interface and C# supports that of course. We can create an interface from the GUI that the DLL will utilize to allow changes to the GUI from another application.<\/p>\n<p>The framework consists of three projects within visual studio: ExampleTargetApp, ServiceDefinition, and CallingDll.<\/p>\n<ul>\n<li>ExampleTargetApp contains the information for the GUI and handles what happens when elements of the GUI interact with the user, presumably via mouse and keyboard but could also be via function piping. This project also contains a service to make use of the pipes interface.<\/li>\n<li>ServiceDefinition contains an interface for the functions to go through WCF pipes.<\/li>\n<li>CallingDll uses the service interface to access functionality contained and defined in the ExampleTargetApp project<\/li>\n<\/ul>\n<h2 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-ExampleTargetApp(GUIApplication)\">ExampleTargetApp (GUI Application)<\/h2>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-MainWindow.xaml.cs\">MainWindow.xaml.cs<\/h3>\n<p>Contains the elements of the GUI such as Button1_Click() and SelectRadio1()<\/p>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-HelloWorldService.cs\">HelloWorldService.cs<\/h3>\n<p>Contains the service class using the service interface<\/p>\n<h2 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-ServiceDefinition\">ServiceDefinition<\/h2>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-IHelloWorldService.cs\">IHelloWorldService.cs<\/h3>\n<p>Contains the service interface definition<\/p>\n<h2 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-CallingDll\">CallingDll<\/h2>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-ServiceProxy.cs\">ServiceProxy.cs<\/h3>\n<p>Set up the service to work using the service contract defined in the ServiceDefinition project<\/p>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-ICallTargetApp.cs\">ICallTargetApp.cs<\/h3>\n<p>Interface for calling DLL<\/p>\n<h3 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-CallTargetApp.cs\">CallTargetApp.cs<\/h3>\n<p>Functions for calling DLL through the proxy<\/p>\n<p>&nbsp;<\/p>\n<h1 id=\"HowtotieaC#.NETGUItoacallableC#.NETclasslibrarydllfromExcelVBA:GuidedMinimumFrameworkExample-ExampleaccessfromVBA\">Example access from VBA<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-60\" src=\"http:\/\/www.maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll.png\" alt=\"vbacallingdll\" width=\"1392\" height=\"787\" srcset=\"http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll.png 1392w, http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll-300x170.png 300w, http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll-768x434.png 768w, http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll-1024x579.png 1024w, http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll-800x452.png 800w, http:\/\/maxwellmckinnon.com\/blog\/wp-content\/uploads\/2016\/10\/vbaCallingDll-318x180.png 318w\" sizes=\"auto, (max-width: 1392px) 100vw, 1392px\" \/><\/p>\n<p>Load the dll &#8220;CallingDll&#8221; by selecting browse then going to the project build directory: C:\\dev\\AutomationComExample\\CallingDll\\bin\\x86\\Debug\\CallingDll.tlb<\/p>\n<div class=\"codeHeader panelHeader pdl hide-border-bottom\"><b class=\" code-title\">Excel VBA Code<\/b><span class=\"collapse-source expand-control\"><span class=\"expand-control-icon icon expanded\">\u00a0<\/span><\/span><\/div>\n<div class=\"codeContent panelContent pdl hide-toolbar show-border-top\">\n<div>\n<div id=\"highlighter_327433\" class=\"syntaxhighlighter vb expanded\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\" title=\"Hint: double-click to select code\">\n<div class=\"line number1 index0 alt2\"><code class=\"vb keyword\">Sub<\/code> <code class=\"vb plain\">SelectHello()<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Dim<\/code> <code class=\"vb plain\">targetApp <\/code><code class=\"vb keyword\">As<\/code> <code class=\"vb keyword\">Object<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Set<\/code> <code class=\"vb plain\">targetApp = CreateObject(<\/code><code class=\"vb string\">\"CallingDll.CallTargetApp\"<\/code><code class=\"vb plain\">)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb plain\">targetApp.SelectHello<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"vb keyword\">End<\/code> <code class=\"vb keyword\">Sub<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"vb keyword\">Sub<\/code> <code class=\"vb plain\">SelectGoodbye()<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Dim<\/code> <code class=\"vb plain\">targetApp <\/code><code class=\"vb keyword\">As<\/code> <code class=\"vb keyword\">Object<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Set<\/code> <code class=\"vb plain\">targetApp = CreateObject(<\/code><code class=\"vb string\">\"CallingDll.CallTargetApp\"<\/code><code class=\"vb plain\">)<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb plain\">targetApp.SelectGoodbye<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"vb keyword\">End<\/code> <code class=\"vb keyword\">Sub<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"vb keyword\">Sub<\/code> <code class=\"vb plain\">SelectButton()<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Dim<\/code> <code class=\"vb plain\">targetApp <\/code><code class=\"vb keyword\">As<\/code> <code class=\"vb keyword\">Object<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb keyword\">Set<\/code> <code class=\"vb plain\">targetApp = CreateObject(<\/code><code class=\"vb string\">\"CallingDll.CallTargetApp\"<\/code><code class=\"vb plain\">)<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"vb spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"vb plain\">targetApp.SelectButton<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"vb keyword\">End<\/code> <code class=\"vb keyword\">Sub<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: A minimum working example of how to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA or another C# application. &nbsp; This is a follow up to the non GUI example posted earlier &nbsp; Project:\u00a0AutomationComExample.zip &nbsp; 1 Summary: 2 Framework Structure 2.1 ExampleTargetApp (GUI Application) 2.1.1 MainWindow.xaml.cs&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[8,7],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-dev","tag-net","tag-c"],"_links":{"self":[{"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":7,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":64,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/64"}],"wp:attachment":[{"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/maxwellmckinnon.com\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}