简体   繁体   English

C#互操作与非托管DLL

[英]c# interop with unmanaged dll

What I am trying to make is a CUDA accelerated fractal generator. 我要制作的是CUDA加速分形生成器。 I want to use c# to create the ui, but the fractal images are being generated by unmanaged cuda c compiled in a dll. 我想使用c#创建ui,但是分形图像是由dll中编译的非托管cuda c生成的。 Following some tutorials, I have managed to make interop work with simple data, but I am having difficulty creating the bitmap image and passing it back to the c# application. 在完成一些教程之后,我设法使interop可以处理简单的数据,但是在创建位图图像并将其传递回c#应用程序时遇到了困难。

I can put together a buffer in the dll with the pixel data succesfully using CUDA, but how can I use the pointer to that buffer to make a bitmap in the c sharp application? 我可以使用CUDA成功地将dll中的缓冲区与像素数据放在一起,但是如何使用指向该缓冲区的指针在c Sharp应用程序中创建位图?

One out of some ways to do that: 某些方法之一:

If your native function looks like: 如果您的本机函数如下所示:

void foo(void *bitmap);

You can use P/Invoke to declare: 您可以使用P / Invoke声明:

[DllImport(...)]
public static unsafe void foo(void *bitmap);

and then use it as follows: 然后按如下方式使用它:

unsafe
{
    byte[] buffer = new byte[...];
    fixed (void *pBuffer = buffer)
    {
        foo(pBuffer);
    }
}

Notice that you should allow unsafe code (in the project settings, Build tab). 请注意,您应该允许使用不安全的代码(在项目设置的“构建”选项卡中)。

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

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