cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle C# events in ABAP

Former Member
0 Kudos

I have an ActiveX user control what is displayed in the sapgui. This works fine.

Now I want to handle an event from the control to the ABAP code what was instantiating the control.

I know the handling with VB6 and the event id but how do it work with C#?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

OK, I found the solution by my self with hints from codeproject.com and stackoverflow.com. The main thing is the interface of type dispatch and the connection of it to the class.

Here is the code:

using System;

using System.Text;

using System.Runtime.InteropServices;

namespace SceSapGui

{

    public delegate void SceEventHandler(object text, EventArgs e);     // Definition of the event handler body (parameters)

    // Interface 1 (Dispatch)

    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] // This interface is for the event handler (dispatch)

    [GuidAttribute("00000000-0000-0000-0000-000000000001")] // Anyone needs a different guid

    [ComVisible(true)] // Make it visible

    public interface ISceSapGuiDispatch

    {

        [DispId(1)] // Define the eventid

        void SceClicked(object text, EventArgs e); // Event definition

    }

    // Interface 2 (Methode and properties)

    [GuidAttribute("00000000-0000-0000-0000-000000000002")] // Anyone needs a different guid

    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)] // This interface is for data in/out

    [ComVisible(true)] // Make it visible

    public interface ISceSapGui

    {

        void Init(string @URI); // definition of a methode

        string R3ReturnCode { get; } // definition of a readonly property

    }

    [GuidAttribute("00000000-0000-0000-0000-000000000003")] // Anyone needs a different guid

    [ClassInterface(ClassInterfaceType.None)] // Prevent compiler to build automaticaly a default interface

    [ComSourceInterfaces(typeof(ISceSapGuiDispatch))] // Attach the dispatch interface (don't know why in this way and not by inheritance like interface2)

    [ComVisible(true)] // Make it visible

    public partial class SceSapGui : ISceSapGui // Main class derived by interface 2

    {

#region Interface member ISceSapGuiDispatch

        public event SceEventHandler SceClicked; // Event handler variable

        protected virtual void OnSceClicked(EventArgs e, string text)   // Call this if you want to raise the event

        {

if (this.SceClicked != null) // but make it only if someone attached to it

this.SceClicked((object)text, new EventArgs()); // Raise the event

        }

#endregion

#region Interface member ISceSapGui

        [ComVisible(true)]

        public void Init(string @URI)

        {

// ...

        }

        [ComVisible(true)]

        public string R3ReturnCode

        {

get { return "0"; }

}

#endregion

    }

}

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks Hans It's work now with the abap stack tanks for your help!!!

Former Member
0 Kudos

Hi Hans,

I have to do the same for a specific user control, is it possible if you can provide sample example from c# code and abap code?

Thanks and regards

Jonathan

Former Member
0 Kudos

Hi Jonathan,

here in the discussion you see the c# code of the frame of the activeX control. If you have any questions about this, you can ask me. The ABAP code wrote another one. I must ask if he can provide an example what he done but this can take a few days.

Former Member
0 Kudos

Hi Hans,

Thanks for your answer, is it possible to send me the complete c# code ? I can give you my mail if you want.

I have copy your code and try it with my own ABAP code but nothing append, I'm new in C# ... I can give you it if you prefer...

Thanks and regards.

Former Member
0 Kudos

Sorry Jonathan,

I cannot provide the whole code. This is not open source code and the project became giant with imaging and SAPGUI scripting.

But, if you not firm in C# and ActiveX, did you switch on the "Register for COM interop" in the C# project properties of the VisualStudio ( or a similar switch for other compilers) and do you register the resulting Dll with "c:\windows\Microsoft.NET\Framework\<.NET-version>\regasm.exe /tlb myactivx.dll"?

Former Member
0 Kudos

No problem Hans, I will test your recommandation...

I have created a setup.exe using my dll and in the properties I have set the Register properties as vsdraCOM is it the same than doing regasme.exe ?

Thanks for your help

Former Member
0 Kudos

Yes, it is the same. But this is only if you install your project. If you want to debug your application without install you need to run regasm.

Former Member
0 Kudos

I found two other entries in the forum about this theme but no one was answered.

Is this the wrong forum? I thought this is a main point in SAP intoroperability - .NET!

Or is the knowledge in vacation and i am impatient?