PInvoke的 - 如何元帅“SOMETYPE * []'? [英] PInvoke - How to marshal for 'SomeType* []'?

查看:79
本文介绍了PInvoke的 - 如何元帅“SOMETYPE * []'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它的一些本土 n-型本机库,并想在它的P / Invoke某些功能。

I have a native library with some native ntype in it and would like to p/invoke some functions in it.

我能够元帅:

foo1(ntype** p) ==> foo1(IntPtr[] p)



但不知道如何做到这一点的:

But don't know how to do it for:

foo1(ntype*[] p) ==> foo1(<???> p)

至少 IntPtr的[ ] 并没有奏效。

修改

我试图与当元帅的非托管函数为:

The unmanaged function I'm trying to marshal with is:

extern mxArray* mclCreateSimpleFunctionHandle(mxFunctionPtr fcn);



其中, mxFunctionPtr 是:

typedef void(*mxFunctionPtr)(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

这是对下面的MATLAB函数签名的呼叫:

This represent a call to the following matlab function signature:

function [varargout] = callback(varargins)
%[
    %% Do callback code %%
%]

显然,从我的期望,这个函数指针应为我提供了 mxArray 2列出*

Obviously, from my expectations, this function pointer should provide me with 2 lists of mxArray*:


  • 的输入参数列表(即prhs,在Matlab的一面初始化)

  • 的输出参数列表(即plhs,全部初始化为0,但我应该写入)

目前从我所做的测试中,其仅在 plhs返回首创 mxArray * prhs 列表

Currently from the tests I've made, it only returns for firsts mxArray* in plhs and prhs lists

推荐答案

明白了

有关正确编组有时* [] 中:

extern mxArray* mclCreateSimpleFunctionHandle(mxFunctionPtr fcn);
typedef void(*mxFunctionPtr)(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[]);



是:

is:

// For function pointer
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void MCRInteropDelegate(int nlhs,
                                        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.SysInt, SizeParamIndex = 0)][Out] IntPtr[] plhs, 
                                        int nrhs,
                                        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.SysInt, SizeParamIndex = 2)][In] IntPtr[] prhs);

// For API function
[DllImport(DLLNAME, EntryPoint = "mclCreateSimpleFunctionHandle", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern IntPtr _mclCreateSimpleFunctionHandle(MCRInteropDelegate fctn);



说明

的MarshalAs 属性表明元帅有时* [] LPArray 的IntPtr ,其中数组的大小由函数的参数从零开始的索引处包含 SizeParamIndex

MarshalAs attribute indicates to marshal SomeTime*[] as a LPArray of IntPtr, where the size of the array is contained by function's parameter at the zero-based index SizeParamIndex

这篇关于PInvoke的 - 如何元帅“SOMETYPE * []'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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