首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 呼叫 Prodave DLL 實戰(三):P/Invoke 簽名轉換、結構體與枚舉定義

     編輯:關於C語言

(4)補充說明

若為物件型參考(如 unsigned char* 轉 byte[]),不需加 ref;但 C++ int 轉 C# int 則需加 ref 關鍵字。

以下為轉換代碼,請見教:

public class Prodave6
{
    #region 常值定義
    public const int MAX_CONNECTIONS = 64;
    public const int MAX_DEVNAME_LEN = 128;
    public const int MAX_BUFFERS = 64;
    public const int MAX_BUFFER = 65536;
    #endregion

    #region 結構體定義
    public struct CON_TABLE_TYPE
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public byte[] Adr; // connection address
        public byte AdrType; // 1=MPI/PB, 2=IP, 3=Mac
        public byte SlotNr; public byte RackNr;
    }
    public enum DatType : byte { BYTE = 0x02, Word = 0x04, DWord = 0x06 }
    public enum FieldType : byte
    {
        d = 100, D = 68, e = 101, E = 69, a = 97, A = 65,
        m = 109, M = 77, t = 116, T = 84
    }
    #endregion

    #region PLC基本函數
    [DllImport("Prodave6.dll")]
    public extern static int LoadConnection_ex6(int ConNr, string pAccessPoint, int ConTableLen, ref CON_TABLE_TYPE pConTable);

    [DllImport("Prodave6.dll")]
    public extern static int UnloadConnection_ex6(UInt16 ConNr);

    [DllImport("Prodave6.dll")]
    public extern static int SetActiveConnection_ex6(UInt16 ConNr);

    [DllImport("Prodave6.dll")]
    public extern static int db_read_ex6(UInt16 BlkNr, DatType DType, UInt16 StartNr, ref UInt32 pAmount, UInt32 BufLen, byte[] pBuffer, ref UInt32 pDatLen);

    [DllImport("Prodave6.dll")]
    public extern static int db_write_ex6(UInt16 BlkNr, DatType Type, UInt16 StartNr, ref UInt32 pAmount, UInt32 BufLen, byte[] pBuffer);

    [DllImport("Prodave6.dll")]
    public extern static int field_read_ex6(FieldType FType, UInt16 BlkNr, UInt16 StartNr, UInt32 pAmount, UInt32 BufLen, byte[] pBuffer, ref UInt32 pDatLen);

    [DllImport("Prodave6.dll")]
    public extern static int field_write_ex6(FieldType FType, UInt16 BlkNr, UInt16 StartNr, UInt32 pAmount, UInt32 BufLen, byte[] pBuffer);

    [DllImport("Prodave6.dll")]
    public extern static int mb_bittest_ex6(UInt16 MbNr, UInt16 BitNr, ref int pValue);

    [DllImport("Prodave6.dll")]
    public extern static int mb_setbit_ex6(UInt16 MbNr, UInt16 BitNr, byte Value);
    #endregion

    #region PLC200用資料傳輸函數
    [DllImport("Prodave6.dll")]
    public extern static int as200_field_read_ex6(FieldType FType, UInt16 BlkNr, UInt16 StartNr, UInt32 pAmount, UInt32 BufLen, byte[] pBuffer, ref UInt32 pDatLen);
    [DllImport("Prodave6.dll")]
    public extern static int as200_field_write_ex6(FieldType FType, UInt16 BlkNr, UInt16 StartNr, UInt32 pAmount, UInt32 BufLen, byte[] pBuffer);
    [DllImport("Prodave6.dll")]
    public extern static int as200_mb_bittest_ex6(UInt16 MbNr, UInt16 BitNr, ref int pValue);
    [DllImport("Prodave6.dll")]
    public extern static int as200_mb_setbit_ex6(UInt16 MbNr, UInt16 BitNr, byte Value);
    #endregion

    #region PLC資料轉換函數
    [DllImport("Prodave6.dll")]
    public extern static int GetErrorMessage_ex6(int ErrorNr, UInt32 BufLen, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pBuffer);
    [DllImport("Prodave6.dll")]
    public extern static int gp_2_float_ex6(UInt32 gp, ref float pIEee);
    [DllImport("Prodave6.dll")]
    public extern static int float_2_gp_ex6(float IEee, ref UInt32 pgp);
    [DllImport("Prodave6.dll")]
    public extern static int testbit_ex6(byte Value, int BitNr);
    [DllImport("Prodave6.dll")]
    public extern static void byte_2_bool_ex6(byte Value, int[] pBuffer);
    [DllImport("Prodave6.dll")]
    public extern static byte bool_2_byte_ex6(int[] pBuffer);
    [DllImport("Prodave6.dll")]
    public extern static UInt16 kf_2_integer_ex6(UInt16 wValue);
    [DllImport("Prodave6.dll")]
    public extern static UInt32 kf_2_long_ex6(UInt32 dwValue);
    [DllImport("Prodave6.dll")]
    public extern static void swab_buffer_ex6(byte[] pBuffer, UInt32 Amount);
    [DllImport("Prodave6.dll")]
    public extern static void copy_buffer_ex6(byte[] pTargetBuffer, byte[] pSourceBuffer, UInt32 Amount);
    [DllImport("Prodave6.dll")]
    public extern static void ushort_2_bcd_ex6(UInt16[] pwValues, UInt32 Amount, int InBytechange, int OutBytechange);
    [DllImport("Prodave6.dll")]
    public extern static void ulong_2_bcd_ex6(UInt32[] pdwValues, UInt32 Amount, int InBytechange, int OutBytechange);
    [DllImport("Prodave6.dll")]
    public extern static void bcd_2_ushort_ex6(UInt16[] pwValues, UInt32 Amount, int InBytechange, int OutBytechange);
    [DllImport("Prodave6.dll")]
    public extern static void bcd_2_ulong_ex6(UInt32[] pdwValues, UInt32 Amount, int InBytechange, int OutBytechange);
    [DllImport("Prodave6.dll")]
    public extern static void GetLoadedConnections_ex6(UInt32 BufLen, int[] pBuffer);
    #endregion

    #region 自訂輔助函數
    public static UInt16 bytes_2_word(byte dbb0, byte dbb1) { return (UInt16)(dbb0 * 256 + dbb1); }
    public static UInt32 bytes_2_dword(byte dbb0, byte dbb1, byte dbb2, byte dbb3) { return (UInt32)(dbb0 * 16777216 + dbb1 * 65536 + dbb2 * 256 + dbb3); }
    public static UInt32 words_2_dword(UInt16 dbw0, UInt16 dbw2) { return (UInt32)(dbw0 * 65536 + dbw2); }
    public static byte[] word_2_bytes(UInt16 dbw0) { return new byte[] { (byte)(dbw0 / 256), (byte)(dbw0 % 256) }; }
    public static byte[] dword_2_bytes(UInt32 dbd0) { return new byte[] { (byte)(dbd0 / 16777216), (byte)((dbd0 % 16777216) / 65536), (byte)((dbd0 % 65536) / 256), (byte)(dbd0 % 256) }; }
    public static UInt16[] dword_2_words(UInt32 dbd0) { return new UInt16[] { (UInt16)(dbd0 / 65536), (UInt16)(dbd0 % 65536) }; }
    #endregion
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved