There is a library in c ++ and documentation for it. In general, I can work with it, but there is a problem with some functions.

In the documentation:

LONG CLSCRF_MifareStandard_AuthKey( IN LPVOID pReader, IN BYTE bKeyType, IN LPBYTE pbUID, IN DWORD dwSector, IN LPBYTE pbCodedKey ); 

I connect library:

 [DllImport(ClscrflDll)] public static extern uint CLSCRF_MifareStandard_AuthKey(IntPtr pReader, byte bKeyType, IntPtr pbUID, uint dwSector, IntPtr pbCodedKey); 

For example, a UID is an array of bytes, in IntPrt I translate it like this:

 var pbUID = Marshal.AllocHGlobal(UID.Length); Marshal.Copy(UID, 0, pbUID, UID.Length); 

With the other types, everything seems to be clear. As a result, when I call a function, I get an error:

Attempting to read or write protected memory. This often indicates that the other memory is damaged.

There are suspicions that this is something with pbCodedKey. This is an authorization key. I get it by another function:

 [DllImport(ClscrflDll)] public static extern uint CLSCRF_MifareStandard_HostCodeKey(IntPtr pReader, IntPtr pbUncoded, ref IntPtr pbCoded); ///---------- byte[] key = new byte[6] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; var pbKey = Marshal.AllocHGlobal(key.Length); Marshal.Copy(key, 0, pbKey, key.Length); var _key = Marshal.AllocHGlobal(12); var err = Clscrfl.CLSCRF_MifareStandard_HostCodeKey(pReader, pbKey, ref _key); 

Her description:

 LONG CLSCRF_MifareStandard_HostCodeKey( IN LPVOID pReader, IN LPBYTE pbUncoded, OUT LPBYTE pbCoded ); 

What could be the problem, at least approximately?

  • How did you turn the OUT LPBYTE pbCoded into a ref IntPtr pbCoded ? There was a pointer to the buffer - there was a pointer to the pointer. - Pavel Mayorov
  • @PavelMayorov This was my mistake, which did not allow me to log in. - Nikolay

1 answer 1

Try this:

 //Не забывайте System.Runtime.InteropServices; [DllImport("Clscrfl.dll")]//подозреваю муки с ней /* LONG CLSCRF_MifareStandard_AuthKey( IN LPVOID pReader, IN BYTE bKeyType, IN LPBYTE pbUID, IN DWORD dwSector, IN LPBYTE pbCodedKey ); */ public static extern int CLSCRF_MifareStandard_AuthKey( [In, MarshalAs(UnmanagedType.I4)] int pReader, byte bKeyType, [In, MarshalAs(UnmanagedType.LPArray, SizeConst = 4)] byte[] pbUID, //Mifare Classic only int dwSector, [In, MarshalAs(UnmanagedType.LPArray, SizeConst = 12)] byte[] pbCodedKey); 

In this case, the function call does not require complex marshalling.

  • Welcome to Stackoverflow in russian ! Sorry, but answers only in russian allowed. Please translate you answer to russian and format your code) - Yuriy SPb
  • And the size of the pReader is not confused here? Better to leave it as IntPtr ... - Pavel Mayorov
  • Yes, and DWORD correct as uint record. - Pavel Mayorov
  • Works fine - checked - Zorg Batist
  • The only project needed to build a 32bit platform is Zorg Batist