将参数C#编组到C ++ [英] marshalling parameters C# to C++

查看:87
本文介绍了将参数C#编组到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎只是简单的步骤,但我无法绕开它.
在我的C#应用​​程序中,我有一个第三方API函数,将缓冲区作为byte []返回.我必须将此缓冲区传递给C ++ DLL(我使用DLLImport进行互操作)函数,该函数将指向此缓冲区的指针作为unsigned short *.
如何在C#中将byte []编组为unsigned short *,以便C ++函数可以处理此缓冲区的内容?

在此先感谢.

Seems like simple step, but can''t my head around it.
In my C# app, I have a third party API function returning buffer as byte[]. I have to pass this buffer to C++ DLL (which I interop using DLLImport) function which takes pointer to this buffer as unsigned short*.
How do I marshal byte[] to unsigned short* in C#, so that the C++ function can process the contents of this buffer?

Thanks much in advance.

推荐答案

此示例的问题在于,非托管端(C ++)上的数组大小未知.我会说,您的问题并未完全定义您的问题.您需要此信息.将此传递给C ++函数(因此可以使用任何长度),这不是问题.

此代码示例将为您提供一个想法:
http://stackoverflow.com/questions/289076/how-can-i-pass-a-pointer-to-an-array-using-p-invoke-in-c [
The problem with this example is that the size of the array on unmanaged side (C++) is unknown. I would say, your Question does not fully define your problem. You need this information. It this is passed to the C++ function (so it could work with any length) it is not a problem.

This code sample will give you an idea:
http://stackoverflow.com/questions/289076/how-can-i-pass-a-pointer-to-an-array-using-p-invoke-in-c[^]. In this sample the assumed size of the array is some immediate constant. If this value is known before the call to the unmanaged function, it can be variable. It this is not the case, please explain how this size is defined and passed.

—SA


这篇关于将参数C#编组到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆