在C#中使用非托管DLL

干ries

我一直在尝试为我创建的C ++类创建ac#包装器。我环顾了如何执行此操作,但是这些示例似乎都没有使用类和对象。我在c ++中有以下代码:

#ifndef PORTAUDIOMANAGER_H
#define PORTAUDIOMANAGER_H

#include "portaudio.h"
#include "pa_asio.h"

class PortAudioManager
{
public:
    PortAudioManager();
    virtual ~PortAudioManager();

    static PortAudioManager* createObject();
    void openStream();

    void dispose(PortAudioManager* obj);

    void stopStream();

    typedef struct
    {
        float left_phase;
        float right_phase;
    }
    paTestData;

private:
    void* stream;

    paTestData data;
    static PortAudioManager* audioManager;
};

#endif

createObject方法创建PortAudioManager的新对象,并将其注册到audioManager指针。dispose方法充当析构函数(因为我认为您不能在C#中使用构造函数和析构函数)。

因此,应该如何使用它就像这样:

PortAudioManager manager = PortAudioManager.createObject();
manager.openStream();

我将如何创建一个可以在C#中使用的系统?如果您需要更多信息,请告诉我。

癸二酸脂

创建一个新的类库项目,并使用/ clr标志进行编译。给定上面显示的本机C ++类,添加以下C ++ / CLI类以包装本机类:

public ref class PortAudioManagerManaged
{
private:
    PortAudioManagerManaged(PortAudioManager* native)
        : m_native(native) { }

public:
    PortAudioManagerManaged()
        : m_native(new PortAudioManager) { }

    // = IDisposable.Dispose
    virtual ~PortAudioManagerManaged() {
        this->!PortAudioManagerManaged();
    }

    // = Object.Finalize
    !PortAudioManagerManaged() {
        delete m_native;
        m_native = nullptr;
    }

    static PortAudioManagerManaged^ CreateObject()
    {
        return gcnew PortAudioManagerManaged(PortAudioManager::createObject());
    }

    void OpenStream()
    {
        if (!m_native)
            throw gcnew System::ObjectDisposedException(GetType()->FullName);
        m_native->openStream(); 
    }

    void StopStream()
    {
        if (!m_native)
            throw gcnew System::ObjectDisposedException(GetType()->FullName);
        m_native->stopStream(); 
    }

private:
    PortAudioManager* m_native;
};

在您的C#项目中,添加对C ++ / CLI类库的引用。

using (PortAudioManagerManaged manager = PortAudioManagerManaged.CreateObject())
{
    manager.OpenStream();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C#项目中使用托管dll(使用非托管dll)

在C ++ / FORTRAN中使用非托管C#-DLL

NUnit C#使用DllImport的非托管dll引发BadImageFormatException

无法使用C#和Robert Giesecke的非托管导出工具创建非托管dll

我可以在非托管Delphi EXE中使用托管C#DLL吗?

如何在 C++ 项目中使用非托管的 .dll、.lib、.exp

使用Mupen64Plus非托管C dll API命令填充C#结构

P /调用-从C#调用非托管DLL

使用非托管代码的C#应用程序加载msvcr90.dll + msvcr80.dll + msvcr100.dll。这有关系吗?

将本机非托管 C++ DLL 加载到托管 C# 应用程序会导致 DLL 输出垃圾

用于C#使用的非托管C ++包装器

在C#中使用C ++ dll对象

在C#中使用的C ++ COM DLL

从 c# 调用非托管代码。使用 IntPtr 获取数据

使用exe编译非托管DLL

C#到非托管C ++ DLL访问冲突/“框架不在模块中”

C#运行时中的C ++非托管DLL导入失败

从非托管C ++ DLL返回字符串到C#

将字节数组从 C++ 非托管 dll 传递到 C# unity

将C#结构传递给非托管C ++ DLL时出现SafeArrayTypeMismatchException

当传递给非托管C DLL函数时,C#结构变量保持为空

C#将指向结构(包含不可复制类型)的指针传递给非托管C ++ DLL

从C#调用C ++ dll。“无法封送'返回值':无效的托管/非托管类型组合。”

在C#代码中使用cpp dll

始终从 C# 中的同一线程调用非托管 dll

如何在C#中定义非托管dll依赖项

C#中非托管DLL的调用约定

从C#调用非托管C ++

围绕非托管DLL的C#包装器库要求在构建过程中非托管DLL位于同一目录中