Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all
i have been on this one for a long time now, i have a c# winform app that calls an unmanaged 32-bit c++ dll, but anytime i try to call it, the app either crashes or brings an exceptional error

"Attemted to read or write protected memory. This is often an indication that other memory is currupt"

below is my code sample, am i doing something wrong!

Notes:
i have tried changing my platform target to X86
i even tried unchecking "Suppress JIT optimization module load"

but none solved the problem

any help will be greatly appreciated.

What I have tried:

C#
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Unit1
{
    public partial class TForm1: Form
    {       
        public TForm1()
        {
            InitializeComponent();
        }

 private bool rdCard()
        {
            bool result = false;
             int st;
            //result = false;
            // Units.Unit1.bufCard = "";
           Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            st = Units.Unit1.ReadCard(Units.Unit1.flagUSB, Units.Unit1.bufCard);
          
            if (st != 0)
            {
                MessageBox.Show(("Read Card Failure" + '\n' + (st).ToString() + "the buf: " + Units.Unit1.bufCard), "Note", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error);
                return result;
            }
            if (Units.Unit1.bufCard.Substring(0, 6) != "551501")
            {
                MessageBox.Show(("No Valid Card On Reader" + '\n' + Units.Unit1.bufCard), "Note", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
               return result;
            }
            else
            {
                result = true;
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                return result;
            }
        }

 // Read Card Data
        public void BitBtn3Click(System.Object Sender, System.EventArgs _e1)
        {
            if (!rdCard())
            {
                return;
            }
            // Read Card
            edt_CardData.Text = Units.Unit1.bufCard;
            //@ Unsupported function or procedure: 'copy'
            MessageBox.Show((("Card ID£º" + Units.Unit1.bufCard.Substring(25, 8))), "Note", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
        }
    }
}


namespace Unit1.Units
{
    public class Unit1
    {       
        public static TForm1 Form1 = null;
        public static byte flagUSB = 0;
        // Reader Type,0--USB,1--proUSB
        public static int st = 0;       
        public static string bufCard;

        [DllImport("proRFL.DLL")]
        public static extern int ReadCard(byte fUSB, string Buffer); 
        
        [DllImport("proRFL.DLL")]
        public static extern int CardErase(byte fUSB, int dlsCoID, string cardHexStr);
    } // end Unit1
}
Posted
Updated 10-Dec-17 4:31am
v3
Comments
FranzBe 9-Dec-17 17:48pm    
You are passing an empty string to parameter 'Buffer' of the ReadCard() function. Are you sure who is allocation the memory for the string? The function of the unmanaged Dll? I would try to assign some space to the 'bufCard' variable before passing to the ReadCard() function.

1 solution

Franz B is correct: you must provide some memory for the function. The implementation problem is that your Unit1 class isnt read to work. I would change or add this details.

C#
public class Unit1
   {
       //public static TForm1 Form1 = null; // remove
       private byte flagUSB = 0;
       // Reader Type,0--USB,1--proUSB
       //public static int st = 0; // remove
       private byte[] bufCard;
       //constructor
       public Unit1(int buffSize, int flag)
       {
          bufCard = new byte[buffSize];//allocate the needed buffer
          flagUSB = flag;
       }
      public GetBuffer()
       {
          return bufCard;// buffer access
       }
       public int ReadBuffer()
       {
          return ReadCard(flagUSB, bufCard);//read the needed buffer
       }
It some clearer code. I hope it works ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900