新聞速報

        

2019年3月5日 星期二

C# DLL 常見宣告

DLL 原型   extern ULONG WINAPI CAS_MifareLoadKey( IN HANDLE hHandle, IN BYTE *baKey );

C# 宣告  [DllImport("EZPROXAPI.dll", EntryPoint = "CAS_MifareLoadKey", SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern UInt32 CAS_MifareLoadKey(IntPtr hHandle,
                                                        Byte[] baKey
                                                        );


DLL 原型     extern ULONG WINAPI CAS_TypeA_ActiveIdle( IN HANDLE hHandle, IN BYTE bBaudRate, OUT BYTE *baATQA, OUT BYTE *bSAK, OUT BYTE *bCSN_Length, OUT BYTE *baCSN );

C# 宣告        
[DllImport("EZPROXAPI.dll", EntryPoint = "CAS_TypeA_ActiveIdle", SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern UInt32 CAS_TypeA_ActiveIdle(IntPtr hHandle,
                                                            Byte bBaudRate,
                                                            Byte[] baATQA,
                                                            Byte[] bSAK,
                                                            Byte[] bCSN_Length,
                                                            Byte[] baCSN
                                                            );



DLL 原型     extern ULONG WINAPI CAS_GenerateKeys( IN HANDLE hHandle, IN AUTHKEY_DATA *stKeyData );

C# 宣告
[DllImport("EZPROXAPI.dll", EntryPoint = "CAS_GenerateKeys", SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern UInt32 CAS_GenerateKeys(IntPtr hHandle,
                                                        AUTHKEY_DATA stKeyData
                                                        );

C#  結構  
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct AUTHKEY_DATA
        {
            [MarshalAs( UnmanagedType.U1,SizeConst = 1)]
            public Byte    bKeyType;        // Key Type

            [MarshalAs(UnmanagedType.U1, SizeConst = 1)]
            public Byte    bKeyIndex;        // Key Index

            [MarshalAs( UnmanagedType.ByValArray,SizeConst = 8)]
            public Byte[] baRND_R;        // Random Number R

            [MarshalAs( UnmanagedType.ByValArray,SizeConst = 16)]
            public Byte[] baEnRND;    // Encrypted Random Number B & R
        }

----------------------------------------------------------------------------------------------------------------------------
 DLL 原型     extern void WINAPI GP_DES( IN int iE_D, IN BYTE bKeyLen, IN BYTE *baKey, IN BYTE *baData, OUT BYTE *baOut);

C# 宣告
[DllImport("EZPROXAPI.dll", EntryPoint = "GP_DES", SetLastError = true, CharSet = CharSet.Ansi)]
        public unsafe static extern void GP_DES(UInt32 iE_D,
                                                Byte bKeyLen,
                                                Byte* baKey,
                                                Byte* baData,
                                                Byte* baOut

                                              );

C# 內部程式碼使用
byte[] AAA = new byte[16];
byte[] BBB = new byte[16];

unsafe
{
    fixed (byte* ptr_d_AEK0_VALUE = &d_AEK0_VALUE[0])
    {
        fixed (byte* ptr_AAA = &AAA[0])
        {
            fixed (byte* ptr_BBB = &BBB[0])
            {
                EZPROXAPI.GP_DES(EZPROXAPI.d_ENCRYPTION, 16, ptr_d_AEK0_VALUE, ptr_AAA, ptr_BBB);      //GP_DES(d_ENCRYPTION,16,d_AEK0_VALUE,baTmpRN,baIMEAAuthSK);
            }
        }
    }

}

ShowHex("BBB ", BBB); 



沒有留言:

張貼留言