日期:2014-05-20  浏览次数:20872 次

发短信功能的程序总报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。


   [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;
            }
        }


------解决方案--------------------
检查端口号是否开启或是被占用。

既然你跟踪程序了,你可以看到抛出的具体异常信息。