簡體   English   中英

使用DLLImport將無符號short *映射到VB .net的數據類型

[英]Data type mapping for unsigned short* to VB .net using DLLImport

我可以使用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);

應該是UShort,此網站是檢查它們的好方法http://www.marvintest.com/knowledgebase/KBArticle.aspx?ID=210

如果正確使用IntPtr ,就足夠了。 大小無關緊要,因為IntPtr用作內存指針 ,這意味着它僅指向內存中的位置。 它不應該具有實際值,因此您不能僅為其分配一些類型的UShort並期望它起作用。

但是,如果我沒記錯的話,您也應該能夠將其聲明為ByRef UShortByRef代表By Reference ),這意味着該方法現在需要一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM