未处理 System.AccessViolationException Message:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

南宫萧尘 2012-07-25 03:32:31
今天把公司一个winform的程序移植到web上面,因为有些数据,通过软件麻烦,直接web展示方便,没想到出现这种情况,各位帮小弟看看到底是哪里出错了:
先贴上winform的代码:
这个是类文件:RDCAPI.cs
代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using YD_EMS;
using System.Net.Sockets;
using System.Net;
using System.Text;

// 实时数据回传 回调函数
public delegate int RDC_VarOk(
IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData //
);

namespace YD_EMS
{
public partial class testApi : System.Web.UI.Page
{
IntPtr m_handle;//句柄

string strIP = "127.0.0.1"; //访问IP

int nPort = 8109; //端口



protected void Page_Load(object sender, EventArgs e)
{


}

private int GetData(IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData)

{
if (lpszDbVarName == "d.test1")
test1.Text = lpszVal;

if (lpszDbVarName == "d.test2")
test2.Text = lpszVal;

if (lpszDbVarName == "d.test3")
test3.Text = lpszVal;


return 0;
}

protected void Timer1_Tick(object sender, EventArgs e)
{
//此段拿来测试页面刷新的,可以忽略。
string str = DateTime.Now.ToString("tt") == "上午" ? "AM" : "PM";
lblTime.Text = "当前时间为:" + DateTime.Now.ToString("yyyy年MM月dd日") + "   " +
CultureInfo.GetCultureInfo("zh-cn").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + "   " +
DateTime.Now.ToLongTimeString() + "   " + str;

}

protected void Button1_Click(object sender, EventArgs e)
{
RDC_VarOk myCallBack = new RDC_VarOk(GetData);
m_handle = App_Code.RDCAPI.RDC_Open(strIP, nPort, myCallBack, 0);// 打开数据连接
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test1", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test2", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test3", 2);//增加标签
int n = App_Code.RDCAPI.RDC_StartRun(m_handle);
}

//**********************
// 连接
//**********************
private static Socket ConnectSocket(string server, int port)
{
Socket socket = null;

IPHostEntry iphostentry = null;

foreach (IPAddress address in iphostentry.AddressList)
{

IPEndPoint IPEPoint = new IPEndPoint(address, port);

Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

newSocket.Connect(IPEPoint);

if (newSocket.Connected)
{
socket = newSocket;
break;
}
else
{
continue;
}

}
return socket;
}

//**********************
// 发送与接收
//**********************

private static string SocketSendReceive(string server, int port)
{
string request = "get var name";

Byte[] btSend = Encoding.ASCII.GetBytes(request);

Byte[] btReceived = new Byte[256];

Socket socket = ConnectSocket(server, port);

if (socket == null)
return ("连接失败");

socket.Send(btSend, btSend.Length, 0);

int intContent = 0;

string strContent = "";

do
{
intContent = socket.Receive(btReceived, btReceived.Length, 0);

strContent += Encoding.ASCII.GetString(btReceived, 0, intContent);

}
while (intContent > 0);

return strContent;

}

}
}

winform窗口文件:Form1.cs
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
using System.IO;

// 实时数据回传 回调函数
public delegate int RDC_VarOk(
IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData //
);

namespace WindowsFormsApplication6
{

public partial class Form1 : Form
{



IntPtr m_handle;

string strIP = "127.0.0.1";

int nPort = 8109;

public Form1()
{
InitializeComponent();

}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
WindowsFormsApplication6.dll.RDCAPI.RDC_StopRun(m_handle);
WindowsFormsApplication6.dll.RDCAPI.RDC_Close(m_handle);
}

private void button6_Click(object sender, EventArgs e)//点击开始自动更新数据
{


RDC_VarOk myCallBack = new RDC_VarOk(GetData);

m_handle = WindowsFormsApplication6.dll.RDCAPI.RDC_Open(strIP, nPort, myCallBack, 0);


WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test1", 2);
WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test2", 2);
WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test3", 2);

int n = WindowsFormsApplication6.dll.RDCAPI.RDC_StartRun(m_handle);

}
private int GetData(IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData)
{
if (lpszDbVarName == "d.test1")
textBox1.Text = lpszVal;

if (lpszDbVarName == "d.test2")
textBox2.Text = lpszVal;

if (lpszDbVarName == "d.test3")
textBox3.Text = lpszVal;


return 0;

}

}

}

//**********************
// 连接
//**********************
private static Socket ConnectSocket(string server, int port)
{
Socket socket = null;

IPHostEntry iphostentry = null;

foreach (IPAddress address in iphostentry.AddressList)
{

IPEndPoint IPEPoint = new IPEndPoint(address,port);

Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

newSocket.Connect(IPEPoint);

if (newSocket.Connected)
{
socket = newSocket;
break;
}
else
{
continue;
}




}
return socket;
}

//**********************
// 发送与接收
//**********************

private static string SocketSendReceive(string server,int port)
{
string request="get var name";

Byte[] btSend=Encoding.ASCII.GetBytes(request);

Byte[] btReceived =new Byte[256];

Socket socket=ConnectSocket(server,port);

if(socket==null)
return ("连接失败");

socket.Send(btSend,btSend.Length,0);

int intContent=0;

string strContent="";

do
{
intContent =socket.Receive(btReceived,btReceived.Length,0);

strContent+=Encoding.ASCII.GetString(btReceived,0,intContent);

}
while(intContent>0);

return strContent;

}


}
}
以上的在winform的项目里面运行至正常的

下面是我移植到web上面的时候的代码,其中类文件RDCAPI.cs不变,代码也一样;

这个是web页面名称:testApi.aspx
页面代码(采用页面无刷新效果,因为采集的数据是实时更新的,需要定时刷新):
由于字数限制,我把代码贴在一楼

DeBug项目的时候,报错如下:
未处理 System.AccessViolationException
Message: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
...全文
1083 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cl2636026900 2014-02-25
  • 打赏
  • 举报
回复
你牛,写这么多谁会逐句看
南宫萧尘 2012-07-25
  • 打赏
  • 举报
回复
都没有人理我的?
南宫萧尘 2012-07-25
  • 打赏
  • 举报
回复
这里是CS文件的代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using YD_EMS;
using System.Net.Sockets;
using System.Net;
using System.Text;




// 实时数据回传 回调函数
public delegate int RDC_VarOk(
IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData //
);


namespace YD_EMS
{
public partial class testApi : System.Web.UI.Page
{
IntPtr m_handle;//句柄

string strIP = "127.0.0.1"; //访问IP

int nPort = 8109; //端口



protected void Page_Load(object sender, EventArgs e)
{


}



private int GetData(IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData)

{


if (lpszDbVarName == "d.test1")
test1.Text = lpszVal;

if (lpszDbVarName == "d.test2")
test2.Text = lpszVal;

if (lpszDbVarName == "d.test3")
test3.Text = lpszVal;


return 0;
}

protected void Timer1_Tick(object sender, EventArgs e)
{
//此段拿来测试页面刷新的,可以忽略。
string str = DateTime.Now.ToString("tt") == "上午" ? "AM" : "PM";
lblTime.Text = "当前时间为:" + DateTime.Now.ToString("yyyy年MM月dd日") + "   " +
CultureInfo.GetCultureInfo("zh-cn").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + "   " +
DateTime.Now.ToLongTimeString() + "   " + str;

}

protected void Button1_Click(object sender, EventArgs e)
{
RDC_VarOk myCallBack = new RDC_VarOk(GetData);
m_handle = App_Code.RDCAPI.RDC_Open(strIP, nPort, myCallBack, 0);// 打开数据连接
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test1", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test2", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test3", 2);//增加标签
int n = App_Code.RDCAPI.RDC_StartRun(m_handle);
}

//**********************
// 连接
//**********************
private static Socket ConnectSocket(string server, int port)
{
Socket socket = null;

IPHostEntry iphostentry = null;

foreach (IPAddress address in iphostentry.AddressList)
{

IPEndPoint IPEPoint = new IPEndPoint(address, port);

Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

newSocket.Connect(IPEPoint);

if (newSocket.Connected)
{
socket = newSocket;
break;
}
else
{
continue;
}

}
return socket;
}

//**********************
// 发送与接收
//**********************

private static string SocketSendReceive(string server, int port)
{
string request = "get var name";

Byte[] btSend = Encoding.ASCII.GetBytes(request);

Byte[] btReceived = new Byte[256];

Socket socket = ConnectSocket(server, port);

if (socket == null)
return ("连接失败");

socket.Send(btSend, btSend.Length, 0);

int intContent = 0;

string strContent = "";

do
{
intContent = socket.Receive(btReceived, btReceived.Length, 0);

strContent += Encoding.ASCII.GetString(btReceived, 0, intContent);

}
while (intContent > 0);

return strContent;

}

}
}
南宫萧尘 2012-07-25
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testApi.aspx.cs" Inherits="YD_EMS.testApi" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">

function pageLoad() {
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="test1"></asp:Label>
<asp:Label ID="test1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="test2"></asp:Label>
<asp:Label ID="test2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label5" runat="server" Text="test3"></asp:Label>
<asp:Label ID="test3" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="更新数据" />
<br />
<asp:Label ID="Label6" runat="server" Text="测试时间"></asp:Label>
<asp:Label ID="lblTime" runat="server" Text="testTime"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧