我正在调用C#中的一个方法:
[DllImport(@"pHash.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ph_dct_videohash(string file, ref int length);
这是我从库中调用的方法
ulong64* ph_dct_videohash(const char *filename, int &Length){
CImgList<uint8_t> *keyframes = ph_getKeyFramesFromVideo(filename);
if (keyframes == NULL)
return NULL;
Length = keyframes->size();
ulong64 *hash = (ulong64*)malloc(sizeof(ulong64)*Length);
//some code to fill the hash array
return hash;
}
如何阅读ulong
中的IntPtr
数组?
答案 0 :(得分:2)
虽然Marshal
class没有提供直接处理ulong
的任何方法,但 为您提供Marshal.Copy(IntPtr, long[], int, int)
,您可以使用获取long
数组,然后将值转换为ulong
s。
以下适用于我:
[DllImport("F:/CPP_DLL.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static extern IntPtr uint64method(string file, ref int length);
static ulong[] GetUlongArray(IntPtr ptr, int length)
{
var buffer = new long[length];
Marshal.Copy(ptr, buffer, 0, length);
// If you're not a fan of LINQ, this can be
// replaced with a for loop or
// return Array.ConvertAll<long, ulong>(buffer, l => (ulong)l);
return buffer.Select(l => (ulong)l).ToArray();
}
void Main()
{
int length = 4;
IntPtr arrayPointer = uint64method("dummy", ref length);
ulong[] values = GetUlongArray(arrayPointer, length);
}
答案 1 :(得分:2)
考虑使用不安全的代码:
IntPtr pfoo = ph_dct_videohash(/* args */);
unsafe {
ulong* foo = (ulong*)pfoo;
ulong value = *foo;
Console.WriteLine(value);
}