簡體   English   中英

如何將字節數組從 C# 傳遞到外部 DLL

[英]How to pass byte array from C# to external DLL

在 DLL 中給出具有此簽名的方法

int32_t __stdcall ndUDSReadDataByIdentifier(
    TD1 *diagRef, 
    uint16_t ID,  
    uint8_t dataOut[], 
    int32_t *len,
    int8_t *success);

C# 外部調用是什么樣子的?

我試過了:

[DllImport("nidiagcs.dll")]
public static extern Int32 ndUDSReadDataByIdentifier(
    ref TD1 diagRef, 
    [MarshalAs(UnmanagedType.U2)] UInt16 ID,
    byte[] dataOut,
    [MarshalAs(UnmanagedType.U4)] ref Int32 len,
    [MarshalAs(UnmanagedType.U1)] ref byte success);

調用已執行,但 dataOut 未填充。

好的,我找到了解決方案。

[DllImport("nidiagcs.dll", CallingConvention = CallingConvention.StdCall)]
private static extern Int32 ndUDSReadDataByIdentifier(
    ref TD1 diagRef,
    UInt16 ID,
    Byte[] dataOut,
    ref Int32 len, 
    out byte success);

這是調用 function 的正確方法。 這是我這邊的一個錯誤。 需要在 dataOut 中提供一個緩沖區數組,並在 len 中提供緩沖區的相應大小。 我總是將 len 設置為 0,這使庫認為 dataOut 數組的大小為零,因此不返回任何內容。

感謝大家的幫助!

暫無
暫無

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

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