CSExeComServer - referencing from other C# Projects

Sep 30, 2010 at 4:46 PM

Hello,

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;

    try
    {
        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)? 
Thanks, 
Geekgrrl.
 
Coordinator
Oct 1, 2010 at 9: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 1: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)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IPNConvSrvEvents
{

    [DispId(1)]
    void MessageAdded(string Message);

}

// ... declared in my class as 

[ComVisible(true)]
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 |
     System.Reflection.BindingFlags.IgnoreCase
     )
 );

// Get my application's event handler
MethodInfo methodInfoOfEventHandler =
    typeof(Program).GetMethod("PNConvSrv_MessageAddedHandler",
    (
    BindingFlags.Static |
    BindingFlags.Public)
);

// Create delegate
// Getting NullArgumentException here ...
System.Delegate delegateObj =
    Delegate.CreateDelegate(
        eventInfo.EventHandlerType,
        null,
        methodInfoOfEventHandler);

//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.

Thanks,

Geekgrrl.