0

This snippet is a helloworld-like example illustrating the framework goal I am trying to achieve. The goal of this project is to control the GUI from a seperate piece of code in C# or VBA, e.g. an automation routine that talks to equipment and other pieces of software. The GUI is there for the benefit of the user not using automation, but I also want to automate pieces of the GUI as if someone were there

I am trying to link a GUI and a COM dll together as stated above without much success.

Both projects work separately.

  1. I have verified I can call the COM dll functions.

  2. I have also verified the simple GUI works.

  3. Now I would like to have functions inside the GUI that are callable from the COM dll.

I thought it was as easy as adding the other project and adding a reference, but I am doing something wrong. It's funny because the code completion makes it seem as I am very close, as you can see from the screenshots.

enter image description here

GUI Project:

namespace GUI_HELLOWORLD
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void Button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello World.");
        }
        public void Button1_Click()
        {
            //For the sake of clicking through code - figure out correct method later
            MessageBox.Show("Hello World.");
        }
    }
 }

DLL project:

namespace GUI_COM_LIB
{
    [System.Runtime.InteropServices.ComVisible(true)] //- may not be necessary in some cases?
    public class ZZ_GUI_HELLOWORLD_API_DEMO
    {
        public void SelHello()
        {
            GUI_HELLOWORLD.MainWindow.Button1_Click(); // ERROR!
        }

When adding a reference to the GUI into the COM_LIB dll project, I can see the GUI in the pop up menu, so it's seeing it's there! However when I type GUI_HELLOWORLD.MainWindow., no public functions come up where I would expect to see Button1_Click()

enter image description here

SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • 1
    I took a closer look at your screenshots, and are you sure you've got your references right? Your Library is referencing your GUI project. *Assuming* that you have already generated an assembly for your COM Component (it looks like you already have) you would add a reference to your GUI project and point that reference to the COM Assembly. – K. Alan Bates Oct 24 '14 at 01:12
  • 1
    Regarding your final screenshot above, what Wes said seems to be correct. You have not constructed a "MainWindow" in order to call a button click. "Button_Click" exists as a click handler inside your MainWindow class, but why are you trying to reference this handler from within your class library? Perhaps if I understood your goal a little better, I'd be able to see a clear path to a solution. I wouldn't personally architect a solution in this manner, so I can't really help more without a bit more context. It looks a bit foreign to me. – K. Alan Bates Oct 24 '14 at 01:17
  • I am absolutely sure I have my references right. I want to call elements of the GUI from another program. So I must reference the GUI project in that other program. What Wes said is getting at the root problem. His solution works for making a new window, but I want to access the window that is already open, not a newly created GUI. Have any ideas on a better architecture? – SwimBikeRun Oct 24 '14 at 22:58

3 Answers3

0

It's never quite that simple with COM components. Have you looked through this ancient tome?

Calling COM Components from .NET Clients

Specifically, have you run TLBIMP on your COM component?

Edit

Check out this Stack Overflow question This Blog Post

Is your Client built as an Excel Add-In? (using Visual Studio Tools for Office)

Community
  • 1
  • 1
K. Alan Bates
  • 3,104
  • 5
  • 30
  • 54
0

You need an instance of your window object:

public void SelHello()
{
    var myWindow = new GUI_HELLOWORLD.MainWindow();
    myWindow.Button1_Click();
}
Wes Lybarger
  • 36
  • 1
  • 2
  • 1
    Perhaps I misunderstood. It appeared to be two .NET projects communicating between each other. – Wes Lybarger Oct 24 '14 at 01:03
  • It looks like he's already generated the assembly from the COM Component, but after taking a second look at the screenshot, his reference is backwards – K. Alan Bates Oct 24 '14 at 01:10
  • This works. It generates the popup that would happen if I clicked on the button1. However, this isn't quite what I want. I want to click the button in the GUI that is already open. – SwimBikeRun Oct 24 '14 at 23:43
0

WCF named pipe minimal example

I found the solution in this thread. By using a channel system, an interface can be created and functions from the GUI can be piped over to a dll. The dll is then callable from C#, Excel VBA, or other .NET.

Community
  • 1
  • 1
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85