使用DLLImport将unsigned short *的数据类型映射到VB .net

时间:2018-01-14 01:14:36

标签: .net vb.net dll dllimport function-calls

我可以使用DLLImport

调用我的64位C风格的dll

标头文件

typedef long long PHI_INT;
PHI_STATUS PHI_EXP_CONV PHI_LibraryInitialize(PHI_64 init_settings = PHI_NULL);

在VB .net

<DllImport("PhiOpticsSDK.dll")>
Private Shared Function PHI_LibraryInitialize(ByVal init_settings As IntPtr) As IntPtr
End Function

有人知道unsigned short*映射到的数据类型是什么?正在考虑IntPtr,但那个尺寸错了..

PHI_STATUS PHI_EXP_CONV PHI_ComputePush(const unsigned short* ptr_in, PHI_64 rows, PHI_64 cols, PHI_64 pattern, PHI_64 timeout_ms, float background_offset = 0.0f);

2 个答案:

答案 0 :(得分:0)

应该是UShort,这个网站是检查它们的好方法as in the currently accepted answer

答案 1 :(得分:0)

如果使用正确,IntPtr就足够了。大小无关紧要,因为IntPtr用作 内存指针 ,这意味着它仅指向内存中的位置。它不应该保留实际值,因此您不能只为它分配一些类型UShort并期望它可以工作。

但是,如果我没有记错,您还应该能够将其声明为ByRef UShortByRef代表按参考),意味着该方法现在需要UShort 引用指针 (即指向其内存位置的指针),就像C函数一样。

宣言看起来像这样:

Private Shared Function PHI_ComputePush(ByRef ptr_in As UShort, ...

修改

使用IntPtr的快速而肮脏的解决方案,仅供参考:

'Replace "Number" with your actual variable.
Dim Number As UShort = 35846
Dim Handle As GCHandle

Try
    Handle = GCHandle.Alloc(Number, GCHandleType.Pinned)

    'Gets the memory address of the "Number" variable.
    Dim Ptr As IntPtr = Handle.AddrOfPinnedObject()

    PHI_ComputePush(Ptr, ...your parameters here...)
Finally
    'Releases the GCHandle so that our "Number" variable can be collected by the GC (Garbage Collector) when the time comes.
    If Handle.IsAllocated = True Then Handle.Free()
End Try