none
Getting Access violation exception while calling unmanaged DLL RRS feed

  • Question

  • Method declaration

      [DllImport(NBSDLLPATH, CharSet = CharSet.Unicode, EntryPoint = "IC_PowerOn")]
            public static extern int IC_PowerOn(int Cno, out uint nRx, out byte[] ATR);

    Method calling

                            byte[] pbATR = new byte[512];
                            uint typedef = 40;
                            int ATRRes = IC_PowerOn(0, out typedef, out pbATR);

    I am getting below exception while returning byte array.

    System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Thursday, April 2, 2020 2:10 PM

Answers

  • Try using ref instead of out for the nRx parameter and

    [MarshalAs(UnmanagedType.LPArray, SizeConst = 512)] ref byte[] ATR

    for the byte[] array.


    • Edited by RLWA32 Friday, April 3, 2020 3:43 PM added ref
    • Marked as answer by Mayuresh Prachand Friday, April 3, 2020 6:34 PM
    Friday, April 3, 2020 3:39 PM

All replies

  • Method declaration

      [DllImport(NBSDLLPATH, CharSet = CharSet.Unicode, EntryPoint = "IC_PowerOn")]
            public static extern int IC_PowerOn(int Cno, out uint nRx, out byte[] ATR);

    Method calling

                            byte[] pbATR = new byte[512];
                            uint typedef = 40;
                            int ATRRes = IC_PowerOn(0, out typedef, out pbATR);

    I am getting below exception while returning byte array.

    System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Show us the declaration for the function in the unmanaged DLL.  For example, what does the C/C++ function prototype declare as parameters and return value?

    Since the p/invoke method declaration has the byte array as an out parameter that means that the array is created by the unmanaged Dll and marshaled to the calling C# function.  How has the the unmanaged Dll allocated the memory for the array that it creates? It can be a problem if the COM allocator (CoTaskMemAlloc) has not been used.

    Thursday, April 2, 2020 2:25 PM
  • Below is declaration of function in unmanaged c++ DLL.

    I also tried using ref instead of out variable but it is giving same exception.

    IC_PowerOn(Int32 Cno, UInt32& nRx, Byte[]& ATR)


    Friday, April 3, 2020 3:08 PM
  • Below is declaration of function in unmanaged c++ DLL

    IC_PowerOn(Int32 Cno, UInt32& nRx, Byte[]& ATR)

    This doesn't look like native C++ (unmanaged C++) to me.  The data types look strange.

    Is this code from third-party software?





    • Edited by RLWA32 Friday, April 3, 2020 3:34 PM
    Friday, April 3, 2020 3:24 PM
  • Yes, it is third party DLL and it is unmanaged c++ dll
    Friday, April 3, 2020 3:26 PM
  • Try using ref instead of out for the nRx parameter and

    [MarshalAs(UnmanagedType.LPArray, SizeConst = 512)] ref byte[] ATR

    for the byte[] array.


    • Edited by RLWA32 Friday, April 3, 2020 3:43 PM added ref
    • Marked as answer by Mayuresh Prachand Friday, April 3, 2020 6:34 PM
    Friday, April 3, 2020 3:39 PM
  • Yes, it is third party DLL and it is unmanaged c++ dll
    When declaring an array in native C++ the [] brackets follow the variable, not the data type.  for example BYTE ATR[], not BYTE[] ATR.
    Friday, April 3, 2020 3:48 PM
  • Thanks for answer .. Issue is resolved..above solution is working..

    Friday, April 3, 2020 6:35 PM