This project is read-only.

CSExeComServer - referencing from other C# Projects

Sep 30, 2010 at 5:46 PM


First, thanks for the excellent example. It was exactly what we needed to get started. I have created my CSharp EXE COM server and now I am trying to test it from within a C# forms application.

I was able to get access to the object using the following late-binding code, where m_PNConvObject is a private object in the forms class:

private void button1_Click(object sender, EventArgs e)
    Type convProgID;

        if (m_PNConvObject == null)
            //Get the word object
            convProgID = Type.GetTypeFromProgID("Company.Toolkit.PNConvSrv");

            //Create word object instance
            m_PNConvObject = Activator.CreateInstance(convProgID);
    catch (Exception ex)
        MessageBox.Show( "Error creating object: " + ex.ToString(), Application.ProductName + ": Exception");
Is there anyway to access this COM object by adding it to the References (via Project - Add Reference)? 
Oct 1, 2010 at 10:24 AM

Tlb exported from .NET assembly cannot be added as a reference by a .NET project, so you have to use late binding. I'm not aware of any other solutions. If you find any, please let me know.

Oct 1, 2010 at 2:45 PM

Thanks for the response. That is what I though but as we are relatively new to COM in C# I wanted to make sure I wasn't missing anything simple.

I am now trying to hook, (in my C# test program) to the event that my CSComExeServer throws,  FloatPropertyChanging, using reflection. Do you know of any samples that can demonstrate how to do this?

My event is declared as follows:

[Guid(PNConvSrv.EventsId), ComVisible(true)]
public interface IPNConvSrvEvents

    void MessageAdded(string Message);


// ... declared in my class as 

public delegate void MessageAddedEventHandler(string strMessage);
public event MessageAddedEventHandler MessageAdded;

I've been working with the following code to try and add an event handler:

// Add event handler

// Get info on event that exists in that instance:
// Tried _MessageAdded, MessageAddedEventHandler, _MessageAddedEventHandler,
//      IPNConvSrvEvents_MessageAddedEventHandler, _IPNConvSrvEvents_MessageAddedEventHandler
// This always returns NULL????
EventInfo eventInfo = this.m_PNConvSrvType.GetEvent("MessageAdded", 
     System.Reflection.BindingFlags.Instance |
     System.Reflection.BindingFlags.Public |

// Get my application's event handler
MethodInfo methodInfoOfEventHandler =
    BindingFlags.Static |

// Create delegate
// Getting NullArgumentException here ...
System.Delegate delegateObj =

//Attach to COM Server Event with the delegate of the the EventHandler within this static clas:
eventInfo.GetAddMethod().Invoke(m_PNConvObject, new object[] { delegateObj });


Any thoughts or pointers to other references would be greatly appreciated.