Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:09:58+00:00 2026-06-18T15:09:58+00:00

I have an class in a Windows Runtime Component (written in C#) that raises

  • 0

I have an class in a Windows Runtime Component (written in C#) that raises events.
I cannot work out how to subscribe to these events in a C++/CX app that references the component.

The C# code (in the Windows Runtime Component):

public sealed class Messenger {

    private EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>> messageReceivedTokenTable;


public event EventHandler<MessageReceivedEventArgs> MessageReceived
{
    add
    {
        return EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
            .GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
            .AddEventHandler(value);
    }

    remove
    {
        EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
            .GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
            .RemoveEventHandler(value);
    }
}

internal void OnMessageReceived(string message, string location)
{
    EventHandler<MessageReceivedEventArgs> temp =
        EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
        .GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
        .InvocationList;

    temp(this, new MessageReceivedEventArgs(message, location));
}

}

MessageReceivedEventArgs is:

public sealed class MessageReceivedEventArgs : object
{
    public MessageReceivedEventArgs(string message, string location)
    {
        this.Message = message;
        this.SenderLocation = location;
    }

    public string Message { get; set; }


    public string SenderLocation { get; set; }
}

Note that as per MSDN this descends from object and not EventArgs.

Then in C++:

msngr = ref new Messenger();

msngr->MessageReceived += ?????????

What should go after the += and in the relevant method (and anywhere else – in C# and/or C++) so that I can receive the messages in the C++ app?

I’ve tried various things and the various compiler warnings I’ve encountered have been unable to point me to a solution.

All examples I’ve found of using a Windows Runtime Component written in C# but consumed in a C++ app have been trivial and only show using properties and calling methods. Both of which I can do without problem. I’m after an example of subscribing to an event in C++ that is raised in C#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:09:59+00:00Added an answer on June 18, 2026 at 3:09 pm

    it’s necessary create a proxy to use such types.

    That’s indeed your problem, a COM proxy/stub is required to marshal your MessageReceivedEventArgs class from C# to C++/CX. And yes, very poorly documented. I’ll take a stab at explaining the process. Get started with this WinRT sample, it demonstrates the way to setup a solution to get the proxy you need and does exactly what you want to do.

    The starting point is the ProxyStubForWinRTComponents_server project, a C# project that declares the shared classes. The important part of the project is the Post-Build event, it looks like this:

    call "$(DevEnvDir)..\..\VC\vcvarsall.bat" x86
    winmdidl /outdir:"$(ProjectDir)\" "$(TargetPath)"
    

    The first statement sets up the environment to run SDK tools. The second step runs winmdidl.exe, a completely undocumented build tool that decompiles the .winmd file generated by the project into an IDL file and then compiles that. Output of this build step are:

    • Microsoft.SDKSamples.Kitchen.idl – the decompiled .winmd file in IDL format and used to generate the rest of the files
    • Microsoft.SDKSamples.Kitchen.h – contains the declarations of the C# types in C++ format, suitable to be #included in your C++/CX project
    • Microsoft.SDKSamples.Kitchen_i.c – contains the GUIDs of the C# types, used to build the proxy
    • Microsoft.SDKSamples.Kitchen_p.c – contains the goo from which the proxy and stub are generated
    • dlldata.c – used to build the proxy.

    Next look at the ProxyStubsForWinRTComponentsPS project, that’s the one that builds the proxy/stub DLL. It consumes the files generated by winmdidl.exe, the only added one is the .def file that declares the exports from the DLL. COM calls those to use the proxy, you can use the file as-is. It is best to use this project as-is, only changing the names of the files, so you’ve got all the compiler and linker settings correct.

    Unpleasant and lots of ways to get this wrong, no doubt. Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Application class in System.Windows.Forms have a some properties that can be quite useful.
I have a windows form app, and a class which is called Game. Let's
I have a basic Window class that I need to have events such as
I have a .NET C# windows app that I need to read some excel
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to
Lets say we have a derivided class SerializableLabel from the base class System.Windows.Controls. [XmlRoot(SerializableLabel)]
I have this class which I want to pass around Windows as LPARAM parameter.
I have a Windows service built upon ATL 7's CAtlServiceModuleT class. This service serves
I have a C++ runtime component in a WP8 application, and if I change
This has been stumping me for a bit. I have a Class written in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.