Reply

C# connecting to events

[ Edited ]

Hi,

I'm trying to connect to events in Solid Edge.

Tools: Microsoft Visual C# 2010 and .Net framework 4.

 

For first go I've tried the example in the documentation.

When running it I get exception with:

"event invocation for COM objects requires event to be attributed with DispIdAttribute"

 

The line that causes the exception marked with "

 

How to solve this??

 

Please help me since I've read some about decoration the code so it would get access to the COM but filed so I hoped you'll have ideas before I paste the decorated version..

 

code:

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

using System.Runtime.InteropServices;

using SolidEdgeFramework;

 

namespace beforeSaveEvent

{

    public partial class Form1 : Form

    {

        private SolidEdgeFramework.Application application;

        private SolidEdgeFramework.ISEApplicationEvents_Event appEvents;

        public Form1()

        {

            InitializeComponent();

        }

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

            try

            {

                // Connect to a running instance of Solid Edge

                application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

                // Get a reference to the application events

                appEvents = (SolidEdgeFramework.ISEApplicationEvents_Event)application.ApplicationEvents;

                // Example sink of AfterCommandRun

                appEvents.AfterCommandRun += new SolidEdgeFramework.ISEApplicationEvents_AfterCommandRunEventHandler(appEvents_AfterCommandRun); //

            }

            catch (System.Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (appEvents != null)

            {

                Marshal.ReleaseComObject(appEvents);

                appEvents = null;

            }

            if (application != null)

            {

                Marshal.ReleaseComObject(application);

                application = null;

            }

        }

        void appEvents_AfterCommandRun(int theCommandID)

        {

        }

    }

}

 

 

 

Posted by: joseph polk
Post date: 1/17/2012 12:51:18 AM

3 REPLIES

RE: C# connecting to events

[ Edited ]

Hi polk,

 

try using the type DISEApplicationEvents_Event instead of ISEApplicationEvents_Event.

Seems to work only that way.

 

 

Posted by: Wolfgang Kunert
Post date: 1/17/2012 2:45:49 AM

RE: C# connecting to events

[ Edited ]

Hi,

Thanks! that did the trick

could you direct me to some reading matterial on the matter?

I failed to googling it... and would like to understand what I'm doing rather than crying here over wrong documentation.

 

 

Posted by: joseph polk
Post date: 1/17/2012 6:03:10 AM

Re: C# connecting to events

[ Edited ]

Go to the reference and check the properties on it. If you see that embed COM interop types is set, uncheck it.

 

I have noticed a number of runtime failures when embedded interop types is checked. Checking it means you don't have to deliver the interop assemblies created when you added a reference to the .tlb files. But you can get these hard to track down issues that don't show up until runtime. Besides events I have seen runtime exceptions when debugging. The message box the framework brings up can be a bit cryptic but if you see "interop" or "embedded" in the box, turn off embedding of the interop types.

 

The projects always compile and some of the exceptions don't show up until you actually execute a line of code! That means if you use embedded interop types in .NET 4, you better make sure you have executed every line of code in testing before deployment. But of course everyone does that!

 

Posted by: R.D. Holland
Post date: 2/27/2012 11:10:44 AM