how to call a C# function, foo(out byte [])? -- help passing a byte pointer

8 ビュー (過去 30 日間)
daniel rasnow
daniel rasnow 2014 年 6 月 5 日
コメント済み: daniel rasnow 2014 年 7 月 28 日
Been struggling to call the following function in a C# dll:
public bool GetLatestBuffer(out byte[] buffer)
I've allocated the buffer in Matlab and tried various castings with libpointer, but keep getting the result:
No method 'GetLatestBuffer' with matching signature found for class ...
I could write a wrapper in C# that allocates the buffer ... but would rather do the memory management in Matlab.
My pseudocode is basically:
NET.addAssembly(dllLoc)
ls = luma_ns.Luma(initParams);
ls.function1(params) % --> it works passing parameters by value
buf = zeros(10000,'uint8'); % tried uint16, int16, etc.
pbuf = libpointer('uint8Ptr',buf);
ls.GetLatestBuffer(pbuf) % doesn't match the prototype of out byte []
No method 'GetLatestBuffer' with matching signature found for class 'Luma_ns.Luma'.
Thanks!!

採用された回答

Friedrich
Friedrich 2014 年 7 月 21 日
編集済み: Friedrich 2014 年 7 月 22 日
Hi,
have you tried calling it like this
[bool_val, byte_array] = obj.GetLatestBuffer();
This works fine for me in 14a with the following example
C#
public bool GetLatestBuffer(out byte[] Buffer)
{
Buffer = new byte[10];
for (int i = 0; i < 10;i++)
{
Buffer[i] = (byte)i;
}
return true;
}
ML:
>> a = ClassLibrary1.Class1
a =
Class1 with no properties.
>> [bool,array] = a.GetLatestBuffer()
bool =
1
array =
Byte[] with properties:
Length: 10
LongLength: 10
Rank: 1
SyncRoot: [1x1 System.Byte[]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0
>>
SideNote: The out keyword forces the routine GetLatestBuffer to assign Buffer to some value. So you do NOT need to pass down a preallocated array and since you dont need to do this it appears as output in MATLAB directly.
Also in C# when using OUT you never pass down a preallocated array. So what MATLAB does is correct here and matches the .NET programming.
  1 件のコメント
daniel rasnow
daniel rasnow 2014 年 7 月 28 日
AWESOME!! That did it!! Followed by
img = shiftdim(reshape(uint8(array.SyncRoot),3,numH,numV),1);
imagesc(img) % rbg image, which would have taken pages of C#

サインインしてコメントする。

その他の回答 (2 件)

James Tursa
James Tursa 2014 年 6 月 5 日
Have you tried changing the header signature to something more friendly to MATLAB, but have the same basic I/O? E.g.,
bool GetLatestBuffer(char *buffer);
  3 件のコメント
James Tursa
James Tursa 2014 年 6 月 6 日
Don't recompile. Just create a new header file off to the side for MATLAB to use. I.e., copy the actual header h file to a new file, modify that new file by changing the signature of the function in question, then use that new header h file for calllib in MATLAB.
Friedrich
Friedrich 2014 年 7 月 22 日
This wont work because that is the .NET interface he is using and not loadlibrary!!!!

サインインしてコメントする。


Anthony
Anthony 2014 年 7 月 17 日
Hi,
You can create a byte[] like this:
A = NET.createArray('System.Byte',4).
Then you just have to pass it to your function.
Hope it helps,
Anthony
  1 件のコメント
daniel rasnow
daniel rasnow 2014 年 7 月 20 日
Thanks Anthony! That looks so logical, but it still doesn't work, generating the error in Matlab: No method 'GetLatestBuffer' with matching signature found for class ..." I have the source (but would rather not change it). GetLatestBuffer is defined as
public bool GetLatestBuffer(out byte[] buffer)
I'm wondering if C# tries to be so smart to check the sizes of the buffers -- but I'm pretty sure I'm passing the exact size needed. Also tried >> buf = NET.createArray('System.Byte',1200,1199,3) vs. a single argument equal to the product, but all permutations give the same error msg. I'm very stumped!! |

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall C from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by