AllDayPiano
AllDayPiano

Reputation: 422

Class from Interface.cll

I'm trying to implement a class instance of an interface class. Exploring the interface (.NET DLL) with the project explorer, it says:

bool CreateInstance(SharedLibrary::MemoryArbiter^ pntMemory,
                    SharedLibrary::clsMessageQueue^ pntMessageQueue, 
                    SharedLibrary::clsGPIO^ pntGPIO, 
                    SharedLibrary::Types^ pntProgramSettings, 
                    SharedLibrary::DisplayDriver^ pntDisplayDriver)

Member from Plugin_Interface::IPlugin

But if I write in my MyClass.h:

using namespace System;
using namespace System::ComponentModel;
using namespace SharedLibrary;

namespace MyCppPlugin {
    [AttributeUsageAttribute(AttributeTargets::Class | AttributeTargets::Method | 
                             AttributeTargets::Property | AttributeTargets::Field, 
                             AllowMultiple = true, Inherited = false)]
    ref class MyPlugin abstract : public Plugin_Interface::IPlugin
    {
    bool CreateInstance(SharedLibrary::MemoryArbiter^ pntMemory, 
                 SharedLibrary::clsMessageQueue^ pntMessageQueue, 
                 SharedLibrary::clsGPIO^ pntGPIO, SharedLibrary::Types^ 
                 pntProgramSettings, SharedLibrary::DisplayDriver^ pntDisplayDriver);
    };
};

It says: "error C3766: Missing implementation of Plugin_Interface::IPlugin::CreateInstace(...)

What the heck do I do wrong?

EDIT:

Forgot the abstract statement.

And: Why is it saying "IntelliSense: Class can not implement interface member function "Plugin_Interface::IPlugin::CreateInstance" (declared in "Plugin_Interface.dll")"

???

Upvotes: 0

Views: 61

Answers (2)

AllDayPiano
AllDayPiano

Reputation: 422

I got it. Thanks to Hans Passant who gave me so many hints :)

To export the function it has to implement the Interface 1:1. The export statement has to be added over the class header:

[Export(IPlugin::typeid)]
public ref class MyPlugin : public Plugin_Interface::IPlugin

And: While VB.NET will compile to "Any CPU" and C++/CLI will compile to Win64/Win32 it will missfit. Both Projects have to have the same target - either 64bit OR 32bit.

Now it works.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941277

You got a lot more diagnostic messages from this snippet, you are making several mistakes:

  • [AttributeUsage] is only valid on a class that derives from System::Attribute. You no doubt need to use some kind of attribute so that the plugin host can recognize your class as a valid plugin candidate, I can't guess what that attribute might be.
  • A method that implements an interface method should be public.
  • A method that implements an interface method must be virtual.
  • The method signature must be an exact match with the interface method declaration.
  • Just in case: you must actually implement the method, not just declare it.

The third and forth bullets are the chief reasons for the "must provide an implementation of the interface method" compile error. So proper code ought to resemble something like this:

[NoIdeaWhatAttribute]
public ref class MyPlugin : public Plugin_Interface::IPlugin {
public:
    virtual bool CreateInstance(SharedLibrary::MemoryArbiter^% pntMemory,
                                SharedLibrary::clsMessageQueue^% pntMessageQueue,
                                SharedLibrary::clsGPIO^% pntGPIO, 
                                SharedLibrary::Types^% pntProgramSettings,
                                SharedLibrary::DisplayDriver^% pntDisplayDriver)
    {
        // Todo...
        return false;
    }
};

Upvotes: 1

Related Questions