未处理 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: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
...全文
1132 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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>
访问真八字排盘系统介绍: 1、八字排盘 八字排盘是将年月日时按照天干地支的形式进行排列,一个时间单位代表了一个柱。八字由年柱、月柱、日柱和时柱共四个柱组成,也被称为四柱八字。八字学中基于中国阴阳五行、天干地支与刑冲克害、以及民间盲派的神煞论等方式,进一步预测爱情顺遂、工作高低、姻缘好坏、财富高低、学业成就、身体健康等事的学问。 八字排盘由以下元素组合:年月日时四柱、大运干支、胎元 、流年干支、十神、地势、神煞等。生辰八字不只是把干支历计算出来,而还要遵守月令、节令的强弱,时辰的阴阳变化进行校正。排盘分析,就是根据出生者的性别、天干地支的阴阳五行关系、进一步推算出来的一套方法论,给预测者做人生吉凶的参考数据,在未来事业、财运、婚姻、家庭等问题时,能做出风险较低的决策。 八字排盘怎么看 八字排盘由年、月、日、时四柱组成,每柱包含一个天干和一个地支,共八个字。年柱代表出生的年份,月柱代表出生的月份,日柱代表出生的日期,时柱则代表出生的时辰。每个柱的干支组合都会对个人的命运产生影响。天干地支旁边标注的正财、偏财、偏印、正印、比肩、劫财、食神、伤官、正官、七杀等,称为十神。 2、八字排盘软件介绍 我们是腾讯云市场金牌合作伙伴,广州正规软件开发公司,开发的八字排盘系统数据最全面精准,我们八字排盘采用最精确的排盘程序,而且运用“真太阳时”,进行更精确的时间划分。大家都知道我们使用的北京时间,是统一规定的标准时间。而八字排盘需要相对于太阳方位的天文时间,即平太阳时。我们国家地大物博,北京时间19时,哈尔滨已经夜幕降临,而新疆却还是太阳高挂,这时哈尔滨的天文时间可能在20:00以后,而新疆的天文时间可能在16时以前。北京时间是东经120度经线的平太阳时,如果您出生地的经度与北京时间所处的经度差异较大,或者处于单数时间点的前后,比如6点差一刻,8点,10点15分等

111,097

社区成员

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

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

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