Kasper Hansen
Kasper Hansen

Reputation: 6557

I cannot inherit methods imported via DllImport. What to do?

I have a parent class with some imported methods like this:

class Parent
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}

Then I have another class that inherits from this Parent class

class Child : Parent
{
    GetForegroundWindow(); // intellisense cannot find it
}

Does that mean I have to create a wrapper method around the imported GetForegroundWindow() method in the parent class in order to inherit and use it in the child class?

Upvotes: 0

Views: 148

Answers (1)

Alex F
Alex F

Reputation: 43311

Place GetForegroundWindow call inside of some class method, and not directly in the class.

class Child : Parent 
{
    void Foo()
    {
        GetForegroundWindow();
    }
}

Upvotes: 2

Related Questions