未处理 System.AccessViolationException
  Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
  Source=4222卡
  StackTrace:
       在 _4222卡.ReadWrite.GetCardName(TApplyArea& ApplyArea, String p_Name, Int32& i_Error)
       在 _4222卡.ReadWrite.TReadWriteSystemCard.TGetCardName(String str_Name) 位置 C:\Users\FUCK\Desktop\4222卡\4222卡\ReadWrite.cs:行号 224
       在 _4222卡.ReadWrite.TReadWriteSystemCard.TJudgeCardType() 位置 C:\Users\FUCK\Desktop\4222卡\4222卡\ReadWrite.cs:行号 237
       在 _4222卡.Form1.button2_Click(Object sender, EventArgs e) 位置 C:\Users\FUCK\Desktop\4222卡\4222卡\Form1.cs:行号 147
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 _4222卡.Program.Main() 位置 C:\Users\FUCK\Desktop\4222卡\4222卡\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

解决方案 »

  1.   

    c++ 函数如何定义的?
    你的c#是如何封装这个dll的函数的?
    还有你清楚这个c++内部如何处理输入参数的?
    如果不清楚,你知道c++如何调用这个dll函数的?
      

  2.   

    这是我的代码  我也不知道dll里都是些什么函数以及函数的参数  dll是经理给我的  没有任何的说明  我也不知道怎么查看里面的内容   我这样写是根据公司以前用delphi写的项目改编的
    namespace _4222卡
    {
        public class ReadWrite
        {        [StructLayout(LayoutKind.Sequential)]
            public class TApplyArea
            {
                public int i_Comm;                        //串口号
                public int i_BaudRate;                    //波特率
                public int i_ReaderType;                  //读写卡器类型
                public int i_AppID;                       //应用程序ID
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_RunKey;
                //public char[] str_RunKey = new char[17];       //运行密钥
                public int i_AreaCode;                    //分区号
                public int i_CardType;                    //卡类型
                public int i_SetupControl;                //参数控制字
                public int i_Decimal;                     //计量小数位            public int i_StarMark;                    //初始化标志
                public int i_Position;                    //水表定位号
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_UserNo;     //用户号
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_MeterNo;    //水表表号
                public int i_Times;                       //购水次数
                public double e_Buy;                         //购水量
                public double e_PreValue;                    //预置水量
                public double e_AlarmValue;                  //报警水量
                public double e_InputValue;                  //充值下限
                public double e_OverLimit;                   //透支限额
                public double e_AddWater;                    //增加水量
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_Date ;        //当前日历
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_Time;        //当前时钟
                public double e_MSumBuy;                     //表累计购气量(回收转移卡用)
                public double e_MLeft;                       //表内剩余气量(回收转移卡用)
                public int i_Ladder1;                     //第一阶截止量
                public double e_Price1;                      //第一阶价格
                public int i_Ladder2;                     //第二阶截止量
                public double e_Price2;                      //第二阶价格
                public int i_Ladder3;                     //第三阶截止量
                public double e_Price3;                      //第三阶价格
                public int i_Ladder4;                     //第四阶截止量
                public double e_Price4;                      //第四阶价格
                public int i_Ladder5;                     //第五阶截止量
                public double e_Price5;                      //第五阶价格
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_NewDate;     //新水价启动日期
            }        public class TReturnArea
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_Mark;        //起始码
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_UserNo;     //用户号
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_MeterNo;    //水表表号
                public double e_AlarmValue;                  //报警水量
                public double e_InputValue;                  //充值下限
                public double e_OverLimit;                   //透支限额
                public int i_Decimal;                     //表计量小数位
                public int i_Position;                    //水表定位号
                public double e_Buy;                         //本次购水量
                public double e_SumBuy;                      //表累计购水量
                public int i_Times;                       //购水次数
                public double e_Left;                        //表内剩余水量
                public double e_SumUsed;                     //表累计用水量
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_State;       //水表工作状态字
                public int i_Magnet;                      //磁干扰次数
                public int i_Low;                         //低电压次数
                public double e_Month12;                     //最近第12月用水量
                public double e_Month11;                     //最近第11月用水量
                public double e_Month10;                     //最近第10月用水量
                public double e_Month9;                      //最近第9月用水量
                public double e_Month8;                      //最近第8月用水量
                public double e_Month7;                      //最近第7月用水量
                public double e_Month6;                      //最近第6月用水量
                public double e_Month5;                      //最近第5月用水量
                public double e_Month4;                      //最近第4月用水量
                public double e_Month3;                      //最近第3月用水量
                public double e_Month2;                      //最近第2月用水量
                public double e_Month1;                      //最近第1月用水量
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_Date;        //当前日期
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_Clock ;       //当前日期
                public int i_SetupControl;                //参数控制字
                public int i_Ladder1;                        //第一阶截止量
                public double e_Price1;                      //第一阶价格
                public int i_Ladder2;                        //第二阶截止量
                public double e_Price2;                      //第二阶价格
                public int i_Ladder3;                        //第三阶截止量
                public double e_Price3;                      //第三阶价格
                public int i_Ladder4;                        //第四阶截止量
                public double e_Price4;                      //第四阶价格
                public int i_Ladder5;                        //第五阶截止量
                public double e_Price5;                      //第五阶价格
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public char[] str_NewDate;     //新水价启动日期
            }
      

  3.   

    [DllImport("RWCard.DLL", EntryPoint = "MadeSystemCard", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool MadeSystemCard(ref TApplyArea ApplyArea, ref int i_Error);        [DllImport("RWCard.DLL", EntryPoint = "ReadSystemCard", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool ReadSystemCard(ref TApplyArea ApplyArea, ref TReturnArea ReturnArea, ref int i_Error);        [DllImport("RWCard.DLL", EntryPoint = "GetCardName", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool GetCardName(ref TApplyArea ApplyArea, string p_Name, ref int i_Error);        [DllImport("RWCard.DLL", EntryPoint = "GetErrorInfo", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool GetErrorInfo(ref int i_Error, string p_Error);
            [DllImport("RWCard.DLL", EntryPoint = "RecyclingSystemCard", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool RecyclingSystemCard(ref TApplyArea ApplyArea, ref int i_Error);        [DllImport("RWCard.DLL", EntryPoint = "RecyclingNotSystemCard", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern bool RecyclingNotSystemCard(ref TApplyArea ApplyArea, string str_Pin, ref int i_Error);        int CommHandle = 0;        public  static int    i_Con_App01ID           = 1;              //4442卡小麦          
            public  int    i_Con_User01ID          = 1;              //用户卡
            public  int    i_Con_RUser01ID         = -1;             //带返写用户卡
            public  int    i_Con_Check01ID         = 2;              //检查卡
            public  int    i_Con_Init01ID          = 3;              //初始化卡
            public  int    i_Con_MeterNo01ID       = 4;              //表号设置卡
            public  int    i_Con_Setup01ID         = 5;              //参数设置卡
            public  int    i_Con_Move01ID          = 6;              //回收转移卡
            public  int    i_Con_RMove01ID         = -6;             //回收转移卡(带返写)
            public  int    i_Con_AddWater01ID      = 7;              //加水卡
            public  int    i_Con_Position01ID      = 8;              //定位卡
            public  int    i_Con_KeyModify01ID     = 9;              //密钥修改卡
            public  int    i_Con_Time01ID          = 10;             //校时卡        public  string str_Con_User01Name      = "用户卡";
            public  string str_Con_Check01Name     = "检查卡";
            public  string str_Con_Init01Name      = "初始化卡";
            public  string str_Con_MeterNo01Name   = "表号设置卡";
            public  string str_Con_Setup01Name     = "参数设置卡";
            public  string str_Con_Move01Name      = "回收转移卡";
            public  string str_Con_AddWater01Name  = "加水卡";
            public  string str_Con_Position01Name  = "定位卡";
            public  string str_Con_KeyModify01Name = "密钥修改卡";
            public  string str_Con_Time01Name      = "校时卡";
            public  static string str_Con_NotSysName      = "非本系统卡";        public class TReadWriteSystemCard
            {
                public void TGetErrorInfo(ref int i_Error, string str_Error)
                {
                    string p_Error = null;
                    GetErrorInfo(ref i_Error, p_Error);
                    str_Error = Convert.ToString(p_Error);
                }            public void TShowErrorInfo(ref int i_Error)
                {
                    string str_Error = null;
                    TGetErrorInfo(ref i_Error, str_Error);
                    MessageBox.Show(str_Error, "错误信息");
                }            public int GetCommID(string str_Comm)
                {
                    int Result = 0;
                    if (str_Comm != null &&!str_Comm.Equals(""))    //null是长度为0   并不是“”
                    {
                        try
                        {
                            Result = Convert.ToInt32(str_Comm.Substring(str_Comm.Length - 1,1));
                        }
                        catch (IOException E)
                        {
                            Result = 1;
                            throw E;
                        }
                    }
                    else
                    {
                        Result = 1;
                    }
                    return Result;
                }            public int Create { set; get; }            TApplyArea  ApplyArea    = new TApplyArea();
                TReturnArea ReturnArea   = new TReturnArea();
                Form2       readwritefm2 = new Form2();
                int         i_Con_WD     = 0;            public bool TGetCardName(string str_Name)
                {                string p_Name = null;
                    int i_Error = 0;                ApplyArea.i_Comm       = GetCommID(readwritefm2.Cbo_PortName.Text.ToString());
                    ApplyArea.i_BaudRate = Convert.ToInt32(readwritefm2.Cbo_BaudRate.Text);
                    ApplyArea.i_ReaderType = i_Con_WD;
                    ApplyArea.i_AppID      = i_Con_App01ID;                if (!GetCardName(ref ApplyArea, p_Name,ref  i_Error))
                    {
                        TShowErrorInfo(ref i_Error);
                        return false;
                    }
                    str_Name = Convert.ToString(p_Name);
                    return true;
                }            public bool TJudgeCardType()
                {
                    string str_Name = null;
          
                    if(!TGetCardName(str_Name))
                    {
                        return false;                                 //bool类型  需在return后面加false或true
                    }
                    if (str_Name != str_Con_NotSysName)
                    {
                        DialogResult s = MessageBox.Show(
                            ("此卡已是"+str_Name+",继续写卡吗?"),
                            "系统信息",
                            MessageBoxButtons.YesNo,                   //yes 和 no按钮
                            MessageBoxIcon.Question                    //消息框显示一个?号图标
                            );                    if (s == DialogResult.No)
                        {
                            return false;
                        }
                     }
                    return  true;
                }
                public bool TMadeSystemCard(ref TApplyArea ApplyArea)
                {
                    int i_Error            = 0;                ApplyArea.i_Comm       = GetCommID(readwritefm2.Cbo_PortName.Text.ToString());
                    ApplyArea.i_BaudRate   = Convert.ToInt32(readwritefm2.Cbo_BaudRate.Text);
                    ApplyArea.i_ReaderType = i_Con_WD;
                    ApplyArea.i_AppID      = i_Con_App01ID;                ApplyArea.str_RunKey   = readwritefm2.Edt_RunKey.Text.ToString().ToCharArray();
                    ApplyArea.e_PreValue   = Convert.ToDouble(readwritefm2.Edt_PreValue.Text);
                    ApplyArea.e_AlarmValue = Convert.ToDouble(readwritefm2.Edt_AlarmValue.Text);
                    ApplyArea.e_InputValue = Convert.ToDouble(readwritefm2.Edt_InputValue.Text);
                    ApplyArea.e_OverLimit  = Convert.ToDouble(readwritefm2.Edt_OverLimit.Text);                if (!MadeSystemCard(ref ApplyArea, ref i_Error))
                    {
                        TShowErrorInfo(ref i_Error);
                        return false;
                    }
                    return true;
                }
      

  4.   

    public bool TReadSystemCard(ref TApplyArea ApplyArea, ref TReturnArea ReturnArea)
                {
                    int i_Error = 0;                ApplyArea.i_Comm       = GetCommID(readwritefm2.Cbo_PortName.Text.ToString());
                    ApplyArea.i_BaudRate   = Convert.ToInt32(readwritefm2.Cbo_BaudRate.Text);
                    ApplyArea.i_ReaderType = i_Con_WD;
                    ApplyArea.i_AppID      = i_Con_App01ID;
                    ApplyArea.str_RunKey   = readwritefm2.Edt_RunKey.Text.ToString().ToCharArray();                if(!ReadSystemCard(ref ApplyArea,ref ReturnArea,ref i_Error))
                    {
                        TShowErrorInfo(ref i_Error);
                        return false;
                    }
                    return true;
                }
                public bool TRecylingSystemCard()
                {
                    string str_Name = null;
                    int i_Error = 0;                if(!TGetCardName(str_Name))
                    {
                        return false;
                    }
                    if(str_Name!=str_Con_NotSysName)
                    {
                        DialogResult s = MessageBox.Show(
                            ("此卡已是"+str_Name+",确定要回收吗?"),
                            "系统信息",
                            MessageBoxButtons.YesNo,                   //yes 和 no按钮
                            MessageBoxIcon.Question                    //消息框显示一个?号图标
                            );                    if (s == DialogResult.No)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        MessageBox.Show(
                            "非本系统卡",
                            "错误信息",
                            MessageBoxButtons.OK
                            );
                        return false;
                    }                ApplyArea.i_Comm       = GetCommID(readwritefm2.Cbo_PortName.Text.ToString());
                    ApplyArea.i_BaudRate   = Convert.ToInt32(readwritefm2.Cbo_BaudRate.Text);
                    ApplyArea.i_ReaderType = i_Con_WD;
                    ApplyArea.i_AppID      = i_Con_App01ID;
                    ApplyArea.str_RunKey   = readwritefm2.Edt_RunKey.Text.ToString().ToCharArray();                if(!RecyclingSystemCard(ref ApplyArea,ref i_Error))
                    {
                        TShowErrorInfo(ref i_Error);
                        return false;
                    }
                    return true;
                }
                public bool TRecylingNotSystemCard(string str_Pin)
                {
                    int i_Error = 0;
                    ApplyArea.i_Comm       = GetCommID(readwritefm2.Cbo_PortName.Text.ToString());
                    ApplyArea.i_BaudRate   = Convert.ToInt32(readwritefm2.Cbo_BaudRate.Text);
                    ApplyArea.i_ReaderType = i_Con_WD;
                    ApplyArea.i_AppID      = i_Con_App01ID;                if (!RecyclingNotSystemCard(ref ApplyArea, str_Pin,ref i_Error))
                    {
                        TShowErrorInfo(ref i_Error);
                        return false;
                    }
                    return true;
                }
            }
            
        }
    }
      

  5.   

    从报错的message来看,该对象已经被别的进程使用了。给两个解决办法,一是给对象加lock,一是clone一个同样的对象。
      

  6.   

    你不知道dll里都是些什么函数以及函数的参数?代码里不是都有写吗?
    比如
    [DllImport("RWCard.DLL", EntryPoint = "MadeSystemCard", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern bool MadeSystemCard(ref TApplyArea ApplyArea, ref int i_Error);
      

  7.   


    这个是我根据公司以前用delphi写的项目改编来的  
      

  8.   

    TApplyArea 用 struct ,里面所有的 char[] 都改成 byte[]
      

  9.   


    要是我改为byte[]的话   那我这句 ApplyArea.str_RunKey = readwritefm2.Edt_RunKey.Text.ToString().ToCharArray();  怎么写啊   
      

  10.   

    readwritefm2.Edt_RunKey.Text 里是什么内容,如果都是英文字母就 
    ApplyArea.str_RunKey = Encoding.ASCII.GetBytes(readwritefm2.Edt_RunKey.Text + "\0");
      

  11.   

    readwritefm2.Edt_RunKey.Text 这个里面是一串数字,怎么写
      

  12.   

    readwritefm2.Edt_RunKey.Text 这个里面是一串数字,怎么写