简体   繁体   English

如何将DllImport与C ++类一起使用?

[英]How do I use DllImport with a C++ class?

I'm trying to use a DLL written in C++. 我正在尝试使用用C ++编写的DLL。 In the example of the DLL, there is a header (.h) with the following code: 在DLL的示例中,有一个标题(.h),代码如下:

#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? 如何访问dll并使用他们的方法? I know I'll have to use the [DllImports("codegen.dll")] but as I use the constructor of the same form given the example? 我知道我将不得不使用[DllImports("codegen.dll")]但是因为我使用相同形式的构造函数给出了示例?

P/Invoke is intended to work with a C API, not with a C++ class. P / Invoke旨在使用C API,而不是C ++类。

You'll need to wrap up the C++ class in a C API, and export that. 您需要在C API中包装C ++类并导出它。 You could then P/Invoke (using [DllImport(...)] ) the individual methods in your C API. 然后,您可以P / Invoke(使用[DllImport(...)] )C API中的各个方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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