Dean Seo
Dean Seo

Reputation: 5693

How to invoke Application.AddMessageFilter in a Dll

I'd like to create a dll file in C# that has a static method calling Application.AddMessageFilter() on a Win form loading this dll.

For example,

public Form1()
{
    // initializing
    MyDll.InvokeFilter(SomeClass); // Perfect!
}

And I don't want it to look like as below, (I don't even think that this code would correctly work though)

public Form1()
{
     // initializing

     // Send also the delegate to Application.AddMessageFilter as a parameter
     // to let MyDll know it, which is not as good.
     MyDll.InvokeFilterWithDelegate(SomeClass, Application.AddMessageFilter);
}

The thing is I don't know how to invoke Application.AddMessageFilter from my Dll file because Application class belongs to Form1, not to my Dll library.

What am I missing?

Thanks in advance.

Upvotes: 1

Views: 694

Answers (1)

Tergiver
Tergiver

Reputation: 14517

The Application class, or more specifically System.Windows.Forms.Application does not "belong to Form1", it can be found in System.Windows.Forms.dll. Add a reference to it in your DLL project.

There is a potential issue with ApplicationContexts, but it's unlikely the caller would be making the call from anything other than the primary UI thread.

Having said that, if "SomeClass" is just an implementation of IMessageFilter, what's the point of your library function? They can just call Application.AddMessageFilter themselves.

Upvotes: 2

Related Questions