【问题标题】:C++ dll on C# OpenCVC# OpenCV 上的 C++ dll
【发布时间】:2018-01-20 19:36:43
【问题描述】:

我正在使用 C++ 和 OpenCV 创建一个 dll。此 dll 将用于 C#。

我的 c++ (dll) 代码具有三个功能:

extern "C"
{
    bool x(Mat image, Mat gray, Point center)
    {...}
}

extern "C"
{
    bool y(Mat image. Mat gray, Point center)
    {...}
}


extern "C"
{
    __declspec(dllexport)

    int main (const char *cam1 ,int blur)
    {...}
}

main 函数从 C# 代码中接收一个字符串和一个 int。
在 C# 中,我有:

[DllImport("mydll.dll")]
    public static extern int main(string path, int blur);

我应该如何在 C# 上声明其他函数,知道它们具有 Mat 和 Point 等类型的参数?

【问题讨论】:

  • 您可以使用 structs e.t.c 将 mat 类型转换为 C# 类型。或者你可以在你的 C++ dll 中有另一个函数,你可以用它来初始化一个 Mat 类型,然后它会返回它指向 C# 代码的指针。
  • 托管堆上的对象可以随时被 gc 移动。如果让 dll 访问行指针,则需要在 dll 函数调用期间固定这些对象。如果对象包含对托管堆上其他对象的引用,这可能还不够。这取决于 Mat 类型的复杂程度。
  • 我忘了告诉你 Mat 是在 main 函数中初始化的。 bool 函数在 main 函数中调用。

标签: c# c++ opencv


【解决方案1】:

我按照 Owuor 的建议做了,效果很好。

我的 C++ bool 函数现在是

extern "C"
{
    bool x(Mat *image, Mat *gray, Point *center)
    {...}
}

extern "C"
{
    bool y(Mat *image, Mat *gray, Point *center)
    {...}
}

在 C# 上我有

[DllImport("mydll.dll")]
    public static extern int main(string path, int blur);

[DllImport("mydll.dll")]
    public static extern bool (IntPtr ptr1, IntPtr ptr1, IntPtr ptr1);

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多