- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 C++ SDK 将我的 C# 代码连接到一些闭路电视摄像机。我想使用 SDK 的方法设置一个回调,我可以从中检索帧信息。
.h 文件中的代码:
typedef struct _frame_info
{
unsigned long deviceID;
unsigned long channel;
unsigned long frameType;
unsigned long length;
unsigned long keyFrame;
unsigned long width;
unsigned long height;
unsigned long frameIndex;
unsigned long frameAttrib;
unsigned long streamID;
long long time;
long long relativeTime;
} FRAME_INFO;
typedef void (__stdcall *PLAY_DATA_CALLBACK)(long lPlayHandle,
FRAME_INFO frameInfo, unsigned char *pBuffer, void *pUser);
extern "C" __declspec(dllimport) BOOL __stdcall SetPlayDataCallBack(
long lPlayHandle, PLAY_DATA_CALLBACK fPlayDataCallBack, void *pUser);
PInvoke 代码:
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public class FrameInfo
{
public uint deviceID;
public uint channel;
public uint frameType;
public uint length;
public uint keyFrame;
public uint width;
public uint height;
public uint frameIndex;
public uint frameAttrib;
public uint streamID;
public long time;
public long relativeTime;
}
public delegate void PlayDataCallback(int playbackHandle,
FrameInfo frameInfo, IntPtr buffer, IntPtr userData);
[DllImport("SDK.dll")]
public static extern bool SetPlayDataCallBack(int playbackHandle,
PlayDataCallback playDataCallback, IntPtr userData);
使用PInvoke代码的代码:
// Class member to keep the callback alive
PlayDataCallback _callbackDelegate;
// Assigning the callback
_callbackDelegate = new PlayDataCallback(MyCallback);
// Setting the callback
SetPlayDataCallBack(_playbackId, _callbackDelegate, IntPtr.Zero);
// The callback
private void MyCallback(int playbackHandle, FrameInfo frameInfo,
IntPtr buffer, IntPtr userdata)
{
// wishful thinking
}
当我运行代码时出现错误:“System.NullReferenceException 未处理消息:对象引用未设置到对象的实例。”
我尝试将回调签名更改为以下内容(即使用 IntPtr 代替 FrameInfo)-
public delegate void PlayDataCallback(int playbackHandle, IntPtr frameInfo,
IntPtr buffer, IntPtr userData);
结果 - 实际上调用了回调 (!) 但我无法使用 IntPtr frameInfo 做任何事情,例如尝试从回调中调用:
Marshal.PtrToStructure(frameInfo, typeof(FrameInfo));
或
var a = new IntPtr[14];
Marshal.Copy(frameInfo, a, 0, 1); // '1' is just an example, any number except '0' results in the same error
导致错误:“System.AccessViolationException 未处理消息:尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”
我还尝试在回调委托(delegate)上方使用 UnmanagedFunctionPointer 属性,但得到了同样的错误。
如您所见,我正在迈出处理托管代码和非托管代码的第一步。我读了一堆文章,听了几个看似相关但没有人谈到上述问题的 pluralsight 话题。我认为我编码 FrameInfo 结构的方式有问题,但我已经成功编码了类似的结构,所以老实说,我不知道下一步该做什么。
我将不胜感激任何帮助。谢谢。
最佳答案
您将结构翻译为一个类。那是一个引用类型。这意味着它是通过引用传递的。但是,C++ 代码按值传递严格。
通过将 FrameInfo
声明为结构来解决此问题。
关于c# - C++ 到 C# : PInvoke\Marshaling a callback with a complex type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36732237/
我有一个使用 c++ 的大型代码库标题和许多std::complex对象。但现在我还想使用其他几个使用 fftw 的库( spinsfast 和 ) .不幸的是,混合这两种类型的复合体似乎与 gc
我是 Maxima 的新手,在文档中找不到如何对复数进行正式计算。当我使用未指定的变量时,Maxima 似乎假设它们是真实的: 例如,共轭(x)返回 x。 有没有办法解决这个问题? 提前致谢。 最佳答
我是学习大O表示法的新手,并想到了这个问题。复杂度 O(a * b) 的名称是什么?是线性复杂度吗?多项式?或者是其他东西。实现代码如下。 function twoInputsMult(a, b) {
我是学习大O表示法的新手,并想到了这个问题。复杂度 O(a * b) 的名称是什么?是线性复杂度吗?多项式?或者是其他东西。实现代码如下。 function twoInputsMult(a, b) {
这是我的 Complex 类,我重载了“+” class Complex(object): def __init__(self, real, imag): self.__ima
我正在使用 R5RS 标准的 Scheme 实现。 现在假设您必须找出一个元素 '(2 3 4) 是否在列表 '(1 2 3 4) 中。 至于示例,更严格地说,您希望: 1. (is-in? '(2
注意事项: 我正在使用 Apple LLVM 版本 6.0 (clang-600.0.56)(基于 LLVM 3.5svn)在 OSX 上编译 具体来说,我正在尝试从 LibIIR 编译一个整体源,这
这段fortran代码最初是用Fortran 77格式编写的(我稍后会展示它)。拿到后,我通过转换工具将其转换为f90免费格式。使用intel fortran编译器 ifort,编译和运行和以前一样好
我有一个实现用户定义的算术类型的MyType类。此类提供以下转换运算符 struct MyType { ... operator double() { return to_double
我目前正在使用 Cxx 来允许 Julia 代码与 C++ 库交互。我想做的一部分是在两个方向上有效地传递复杂数据的集合(通常是 vector )。也就是说,我想要以下内容: cv = [1 + 2i
假设我有一个名为“汽车”的实体。 我使用复杂类型来定义“引擎”部分。 [TableName("T_CAR")] public sealed class Car:IEngine { ... pu
我想使用 static_cast 将 complex 转换为 complex 。 Convert complex to complex 我正在尝试做与这篇文章相同的事情,但我需要使用 static_c
` ?
对于多项式方程求解器,最好将其模板化为任何可用类型: template class PolynomialEquation { public: private: array myEquatio
为了在 cython 中将实部与复部分开,我通常使用 complex.real 和 complex.imag 来完成这项工作。然而,这确实会在 html 输出中生成颜色为“python red”的代码
我的问题很简单: Are both "complex float" and "float complex" valid C? 两者似乎都被 gcc 接受而没有警告。 最佳答案 complex 是 co
以下声明有什么区别? 结构体 *ptr1=(结构体*)malloc(4*sizeof(结构体)); 结构体 (*ptr1)[4]=(结构体*)malloc(sizeof(结构体)); 哪个更好用? 最
我想创建一个 C++ 类的复数。这里是Complex.h(最基本的形式) #ifndef _COMPLEX #define _COMPLEX #include "TVector2.h" class C
我已经使用 Visual Studio 2012 和 NDepend 对我的解决方案进行了代码分析 对于方法 MethodA,Visual Studio 显示复杂度为 105,Ndepend 显示为
我的代码: #include using std::cin; using std::cout; using std::istream; using std::ostream; template cl
我在 swift3 中有以下代码,我正在使用 swift lint 对代码进行 linting。给出代码如下: func selectedMenuInLoggedOutState(sender
我是一名优秀的程序员,十分优秀!