4

A business partner has created a DLL written in C++.

Our partner describes that the C++ DLL methods can be accessed by using this code in C++ (we have tested and it works very well with this code written in C++)

create_func_ptr createInstance = (create_func_ptr)(GetProcAddress(hdl, "createReader"));
LudvigInterface* ludvigInterface = createInstance();
if (ludvigInterface)
{
    bool isRunning = ludvigInterface->isRunning();
    destroy_func_ptr closeInstance = (destroy_func_ptr)(GetProcAddress(hdl, "closeReader"));
    closeInstance(ludvigInterface);
}

But our challenge is that we want to do the same in C#.

We are able to open the DLL through DLLImport statements, and everything runs well until we try to access the isRunning method.

class Program
{
    [DllImport(@"\Libraries\LudvigImplementation.dll")]
    private static extern IntPtr createReader();

    [DllImport(@"\Libraries\LudvigImplementation.dll")]
    private static extern bool isRunning();

    static void Main(string[] args)
    {

        IntPtr LudvigInterfacePtr = createReader(); //this line runs well

        var a = isRunning(); //This line gives a System.EntryPointNotFoundException: 'Unable to find an entry point named 'isRunning' in DLL LudvigImplementation.dll'
    }
}

How can we solve this? How can we access the DLL isRunning method in C#?

Thanks a lot

UPDATED 2. MAY AT 13:31 CET:

I got some questions of how the LudvigInterface looks like Here it is (LudvigInterface.h)

class LudvigInterface
{
public:
    virtual bool isRunning() = 0;
    virtual char* getVersion() = 0;
};

Thanks again

svennk
  • 49
  • 2
  • This technique is called 'marshalling' if you want to google. What exception did you get? – Mighty Badaboom May 02 '17 at 10:01
  • 2
    There is no extern method isRuning, you should call it in LudvigInterface – gabba May 02 '17 at 10:10
  • 2
    The isRunning function of the interface is simply not exported. You'll have to show what LudvigInterface looks like to get help. Albeit that it is fairly likely that it is C++ and you're screwed because you can't call C++ instance functions with pinvoke. And of course always best to talk to the programmer that wrote it. – Hans Passant May 02 '17 at 10:11
  • How @HansPassant said, using p-invoke with C++ is really hard, if programmer can't help, you can create a proxy lib in C that exports needed functions... – Seididieci May 02 '17 at 10:35
  • Thanks for your reply. We got a System.EntryPointNotFoundException when calling the isRunning method. It seems like the method is not exported and so far it can only be accessed through the LudvigInterface in C++ – svennk May 02 '17 at 11:39
  • C++ DLLs are no joke... you should consider wrapping your relevant interface as some `extern "C"` functions where you only pass the class pointer around and then compile with the same C++ compiler version as the original DLL. Only use the extern C functions in C# and it will be less painful, since plain old C function binary interfaces are relatively well defined. – grek40 May 02 '17 at 11:47
  • @svennk The question I linked this to is very similar, so it should be easy to adapt your problem. If you need help to do it ask. – xanatos May 02 '17 at 11:56

0 Answers0