说明文档如下:GSM MODEM 动态链接库 (DLL) 二次开发接口适用范围:本短信二次开发接口适用于WAVECOM、西门子、诺基亚、摩托罗拉等支持标准AT指令的GSM短信终端使用方式:将sms.dll文件拷贝到系统安装目录中的system32文件夹中,然后再根据以下接口函数说明和提供的例程源码开发
  
接口函数:      1、Sms_Connection(Com_Port As Integer,Com_BaudRate As Integer, Mobile_Type As String) As Integer
         Sms_Connection函数说明如下:
         功能描述:用于初始化终端与串口的连接
         Com_Port:串口号(0为红外接口,1,2,3,...为串口)
         Com_BaudRate:波特率
         Mobile_Type:返回终端型号
         Sms_Connection:返回值(0:连接终端失败;1:连接终端成功)

我写程序调用这个dll,代码如下: [DllImport("sms.dll", EntryPoint="Sms_Connection")]
        public static extern uint Sms_Connection(uint Com_Port, uint Com_BaudRate, out string Mobile_Type);
           private void Sms_Connection_Button_Click(object sender, EventArgs e)
           {               string port = comboBox1.SelectedItem.ToString();//               String TypeStr = "";//接收短信猫的版本号               if (Sms_Connection(uint.Parse(port), 9600, out TypeStr) == 1)
               {                   MessageBox.Show("连接短信猫成功!");
                   // InsertPort(port);
               }
               else
               {                   MessageBox.Show("连接短信猫失败,请重新设置!");
               }
           }但是运行到if (Sms_Connection(uint.Parse(port), 9600, out TypeStr) == 1)就会报错,错误信息如下:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。未处理 System.AccessViolationException
  Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
  Source="SMS"
  StackTrace:
       在 SMS.FromLink.Sms_Connection(UInt32 Com_Port, UInt32 Com_BaudRate, String& Mobile_Type)
       在 SMS.FromLink.Sms_Connection_Button_Click(Object sender, EventArgs e) 位置 D:\数据库版本1016\数据库版本\SMS\FromLink.cs:行号 39
       在 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(Int32 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)
       在 SMS.Program.Main() 位置 D:\数据库版本1016\数据库版本\SMS\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(Assembly 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)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

请高手帮忙看看,哪里错误了

解决方案 »

  1.   

     public static extern uint Sms_Connection(uint Com_Port, uint Com_BaudRate, out string Mobile_Type); 
    去掉out
      

  2.   

    Sms_Connection(4,9600, "") == 1,我都写死了,这个方法还是报告内存错误
      

  3.   

    试试
    out=>ref
    不清楚vb是传啥的。
    另外参考http://msdn.microsoft.com/zh-cn/library/ms164911(VS.80).aspx