0

I'm trying to use a DLL written in C++. In the example of the DLL, there is a header (.h) with the following code:

#ifndef CODEGEN_H
#define CODEGEN_H

// Entry point for generating codes from PCM data.
#define VERSION 3.15

#include <memory>
#include <string>

#ifdef _MSC_VER
    #ifdef CODEGEN_EXPORTS
        #define CODEGEN_API __declspec(dllexport)
        #pragma message("Exporting codegen.dll")
    #else
        #define CODEGEN_API __declspec(dllimport)
        #pragma message("Importing codegen.dll")
    #endif
#else
    #define CODEGEN_API
#endif

class Fingerprint;
class CODEGEN_API Codegen
{
public:
    Codegen(const float* pcm, uint numSamples, int start_offset);

    string getCodeString(){return _CodeString;}
    int getNumCodes(){return _NumCodes;}
    float getVersion() { return VERSION; }
private:
    string _CodeString;
    int _NumCodes;
};

#endif

How can I access the dll and use their methods? I know I'll have to use the [DllImports("codegen.dll")] but as I use the constructor of the same form given the example?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • The P/Invoke mechanism is not intended for C++ classes, it works only with C APIs. You'll need to either write wrapper functions, or use C++/CLI instead of C#. – Cody Gray - on strike Jun 11 '13 at 19:44
  • possible duplicate of [call unmanaged C++ code from C# using pinvoke](http://stackoverflow.com/questions/6332126/call-unmanaged-c-code-from-c-sharp-using-pinvoke), [Using DLLImport to import an Object](http://stackoverflow.com/q/2071993), [How to use a DLL written in Visual C++ in a C# program?](http://stackoverflow.com/q/13222076) – Cody Gray - on strike Jun 11 '13 at 19:46

1 Answers1

2

P/Invoke is intended to work with a C API, not with a C++ class.

You'll need to wrap up the C++ class in a C API, and export that. You could then P/Invoke (using [DllImport(...)]) the individual methods in your C API.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373