locked
Exception of type 'System.ExecutionEngineException' Was thrown RRS feed

  • Question

  • I am using dll for cardreader.

    the Prototype of the function is 

    int M_MC_Read ( BYTE pm, BYTE *pTrack1, BYTE *pTrack2, BYTE *pTrack3, BYTE *pStatus )

    Parameter

    pm[in]: Specifies the track to read. See “MS Read – pm value”.

    pTrack1[out]: If there is data on Track 1, it returns the data.

    pTrack2[out]: If there is data on Track 2, it returns the data.

    pTrack3[out]: If there is data on Track 3, it returns the data.

    pStatus[out]: If there is more extra data, it returns the extra data.

    If there is no data, it returns NULL.

    and using in my project through dll import

           

    public class MSRW
        {

            [DllImport("MSRW0001.DLL")]
            public static extern int M_MC_Read(byte  pm, ref byte[] pTrack1, ref byte[] pTrack2, ref byte[] pTrack3, ref byte[] pStatus);
        }

    and in my code i am using it as 

    int ret = 0;

                byte[] pTrack1 = new byte[256];
                byte[] pTrack2 = new byte[256];
                byte[] pTrack3 = new byte[256];
                byte[] pStatus = new byte[256];

                try
                {
                    ret = MSRW.M_MC_Read(0x35, ref pTrack1, ref pTrack2, ref pTrack3, ref pStatus);

                }
                catch (Exception)
                {
                }

    while executing the code i am getting Exception of type 'System.ExecutionEngineException'  Was thrown.

    Can anyone help me out to solve this problem?

    Wednesday, November 28, 2018 6:57 AM

Answers

  • If this DLL is made by you, then maybe start with a simpler function, such as:

    // C++

    int M_MC_Read1( BYTE pm )

    {

       return 1234;

    }

    // C#

    [DllImport("MSRW0001.DLL")]

    public static extern int M_MC_Read1( byte  pm );

     

    Check if it works and returns the right value. Maybe you also need to specify the calling convention: [DllImport("MSRW0001.DLL", CallingConvention=CallingConvention.Cdecl)]. Use a value that corresponds to your C++ function. See the documentation for other values.

    Then add the array parameter and use MarshalAs:

    // C++

    int M_MC_Read2( BYTE pm, BYTE * pTrack1 )

    {

       pTrack1[0] = 11;

       pTrack1[1] = 22;

       return 1234;

    }

    // C#

    [DllImport("MSRW0001.DLL", CallingConvention=CallingConvention.Cdecl)]

    public static extern int M_MC_Read2( byte  pm, [MarshalAs(UnmanagedType.LPArray, SizeConst=256)] byte [] track1 );

     

    If this works too, then add other parameters.

    Note that you can also make a C++ DLL that include /clr option, where you can work with datatypes that are available in C# too. The interaction with such modules is easier.




    Wednesday, November 28, 2018 12:37 PM

All replies

  • Have you tried without the ref?


    Wednesday, November 28, 2018 7:55 AM
  • I replaced part of above code to  ret = MSRW.M_MC_Read(0x35, out byte[] pTrack1, out byte[] pTrack2, out byte[] pTrack3, out byte[] pStatus);

    but saem exception i am getting

    Wednesday, November 28, 2018 8:58 AM
  • Have you tried without the out?


    Wednesday, November 28, 2018 10:35 AM
  • You can try with IntPtr (like Win32 APIs that use out LPBYTE such GetPrinter

    where the pointer is allocated with Marshal.AllocHGlobal)

    Wednesday, November 28, 2018 11:09 AM
  • I tried without OUT and REF. i didnt get Error. But where i will get my function Values likr TRack1 dat etc?
    Wednesday, November 28, 2018 11:27 AM
  • now i used try catch block where the exception thrown was " Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
    Wednesday, November 28, 2018 11:30 AM
  • If this DLL is made by you, then maybe start with a simpler function, such as:

    // C++

    int M_MC_Read1( BYTE pm )

    {

       return 1234;

    }

    // C#

    [DllImport("MSRW0001.DLL")]

    public static extern int M_MC_Read1( byte  pm );

     

    Check if it works and returns the right value. Maybe you also need to specify the calling convention: [DllImport("MSRW0001.DLL", CallingConvention=CallingConvention.Cdecl)]. Use a value that corresponds to your C++ function. See the documentation for other values.

    Then add the array parameter and use MarshalAs:

    // C++

    int M_MC_Read2( BYTE pm, BYTE * pTrack1 )

    {

       pTrack1[0] = 11;

       pTrack1[1] = 22;

       return 1234;

    }

    // C#

    [DllImport("MSRW0001.DLL", CallingConvention=CallingConvention.Cdecl)]

    public static extern int M_MC_Read2( byte  pm, [MarshalAs(UnmanagedType.LPArray, SizeConst=256)] byte [] track1 );

     

    If this works too, then add other parameters.

    Note that you can also make a C++ DLL that include /clr option, where you can work with datatypes that are available in C# too. The interaction with such modules is easier.




    Wednesday, November 28, 2018 12:37 PM
  • Thank you sir. It is Working Perfectly
    • Marked as answer by nayakSudhakar Thursday, November 29, 2018 8:48 AM
    • Unmarked as answer by nayakSudhakar Thursday, November 29, 2018 8:49 AM
    Thursday, November 29, 2018 7:17 AM
  • Hi nayakSudhakar,

    Thank you for posting here.

    If your question has been solved, please mark the useful reply as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members.

    Best regards,

    Jack J Jun.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 29, 2018 7:56 AM