简体   繁体   中英

How do I use DllImport with a C++ class?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM