尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
   [STAThread]        [DllImport("sms.dll", EntryPoint = "Sms_Connection")]
        public static extern uint Sms_Connection(uint Com_Port, uint Com_BaudRate, out string Mobile_Type);        [DllImport("sms.dll", EntryPoint = "Sms_Disconnection")]
        public static extern uint Sms_Disconnection();        [DllImport("sms.dll", EntryPoint = "Sms_Send")]
        public static extern uint Sms_Send(string Sms_TelNum, string Sms_Text);        [DllImport("sms.dll", EntryPoint = "Sms_Receive")]
        public static extern uint Sms_Receive(string Sms_Type, out string Sms_Text);        [DllImport("sms.dll", EntryPoint = "Sms_Delete")]
        public static extern uint Sms_Delete(string Sms_Index);        [DllImport("sms.dll", EntryPoint = "Sms_AutoFlag")]
        public static extern uint Sms_AutoFlag();        [DllImport("sms.dll", EntryPoint = "Sms_NewFlag")]
        public static extern uint Sms_NewFlag();
        //如果连接成功返回true 否则返回false
        public bool SmsConn()
        {
            try
            {
                string TypeStr = "";
                if (Sms_Connection(uint.Parse(AddResource.SMSPORT), 9600, out TypeStr) == 1) ///5为串口号,0为红外接口,1,2,3,...为串口
                {
                    return true;
                }else {
                    return false;
                }
            }catch (Exception) {
                return false;
            }
        }

解决方案 »

  1.   

    这个东西楼主别写了.csdn上有源码而且是可以用的。
    资源那里搜索一下。sms就可以搜到。
      

  2.   

    调用的dll有问题,内存泄漏了。
      

  3.   

    //如果连接成功返回true 否则返回false
      public bool SmsConn()
      {
      try
      {
      string TypeStr = "";
      if (Sms_Connection(uint.Parse(AddResource.SMSPORT), 9600, out TypeStr) == 1) ///5为串口号,0为红外接口,1,2,3,...为串口
      {
      return true;
      }else {
      return false;
      }
      }catch (Exception) {
      return false;
      }
      }抛出的异常是 AcessViolationException   
    尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
      

  4.   

    你把Sms_Connection这个函数C++的定义贴出来
      

  5.   

    C++函数的原型是什么?
    有可能是out string这里有问题吧,改成StringBuilder试试。。
    定义个StringBuilder sb = new StringBuilder(500)试试咯
      

  6.   

    搞定了 原来是没有调用关闭的 disconntion() 关闭连接的方法