我建了一个基于对话框的MFC程序,主要功能实现局域网内的信息和文件传送,
功能基本实现,
但是发现程序在退出时总要报错,跟踪调试了一下,直到进入汇编之前都没有
问题,就是到了汇编这句话才出错:
7C809783 lock xadd dword ptr [ecx],eax
错误信息是:
Unhandled exception in MyNetChat.exe (KERNEL32.DLL):0xC0000005:Access Violation.不知道是什么原因?
该怎么调啊?
功能基本实现,
但是发现程序在退出时总要报错,跟踪调试了一下,直到进入汇编之前都没有
问题,就是到了汇编这句话才出错:
7C809783 lock xadd dword ptr [ecx],eax
错误信息是:
Unhandled exception in MyNetChat.exe (KERNEL32.DLL):0xC0000005:Access Violation.不知道是什么原因?
该怎么调啊?
主要的代码太长了,大侠们如果不嫌麻烦就帮我看看了:这是主要的h文件
// MyNetChatDlg.h : header file
//#if !defined(AFX_MYNETCHATDLG_H__7E866954_A03F_4DE5_97A6_6A54EFB5D522__INCLUDED_)
#define AFX_MYNETCHATDLG_H__7E866954_A03F_4DE5_97A6_6A54EFB5D522__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Proto.h"
#include "Exception.h"
/*#include <list>
using namespace std;
#define SERVER_PORT 2280struct stUserListNode
{
//char userName[10];
unsigned int ip;
unsigned short port;
};
typedef list <stUserListNode *> UserList;*/
/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg dialogclass CMyNetChatDlg : public CDialog
{
// Construction
public:
int GetLocalHostInfo(CString &strHostName,CString &strIPAddress);
sockaddr_in ClientMesAddr;
void ExpandDialog(int nResourceID,BOOL bExpand);
CMyNetChatDlg(CWnd* pParent = NULL); // standard constructor
CString GetNameFromAddr(sockaddr_in sock);
sockaddr_in GetAddrFromName(CString Name); SOCKET AcceptFileSock;
sockaddr_in SaveFileTransferAddr;
sockaddr_in AcceptFileAddr;
DWORD m_dwTransferSize;
DWORD m_dwFileSize;
CString m_strSendPath;
CString m_strRecvPath;
// Dialog Data
//{{AFX_DATA(CMyNetChatDlg)
enum { IDD = IDD_MYNETCHAT_DIALOG };
CProgressCtrl m_ctrProgress;
CString m_user;
CString m_showstr;
CString m_strFileName;
CString m_strFileSize;
CString m_strTransferSize;
CString m_showName;
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyNetChatDlg)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CMyNetChatDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnCreathost();
afx_msg void OnHelp();
afx_msg BOOL OnSended();
afx_msg void OnFinduser();
afx_msg void OnDblclkUser();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnExitchat();
afx_msg void OnFileTransfer();
afx_msg void OnAccept();
afx_msg void OnRefuse();
afx_msg void OnStoptransfer();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:};
//CString CMyNetChatDlg::m_showstr="";
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYNETCHATDLG_H__7E866954_A03F_4DE5_97A6_6A54EFB5D522__INCLUDED_)
还有一个h文件
#pragma once
#include <list>#define COMMANDMAXC 256
#define MAXRETRY 5#define FILETRANSFERPORT 6001
#define BLOCKSIZE 2048
// 定义iMessageType的值
#define LOGIN 1
#define LOGOUT 2
#define P2PTRANS 3
#define GETALLUSER 4
#define REQUEST 5
#define ACCEPT 6
#define REFUSE 7
#define RCVFILE 8
#define STOPFILETRANSFER 9// 服务器端口
#define SERVER_PORT 2280
// Client登录时向服务器发送的消息
struct stLoginMessage
{
char userName[20];
char password[10];
};
// Client注销时发送的消息
struct stLogoutMessage
{
char userName[20];
};
// Client向服务器请求另外一个Client(userName)向自己方向发送UDP打洞消息
struct stP2PTranslate
{
char userName[20];
};
// Client向服务器发送的消息格式
struct stMessage
{
int iMessageType;
union _message
{
stLoginMessage loginmember;
stLogoutMessage logoutmember;
stP2PTranslate translatemessage;
}message;
};
// 客户节点信息
struct stUserListNode
{
char userName[20];
unsigned int ip;
unsigned short port;
};
// Server向Client发送的消息
struct stServerToClient
{
int iMessageType;
union _message
{
stUserListNode user;
}message;
};
//======================================
// 下面的协议用于客户端之间的通信
//======================================
#define P2PMESSAGE 100 // 发送消息
#define P2PMESSAGEACK 101 // 收到消息的应答
#define P2PSOMEONEWANTTOCALLYOU 102 // 服务器向客户端发送的消息
// 希望此客户端发送一个UDP打洞包
#define P2PTRASH 103 // 客户端发送的打洞包,接收端应该忽略此消息struct filemessage
{
char FileName[30];
DWORD FileSize;
};// 客户端之间发送消息格式
struct stP2PMessage
{
int iMessageType;
int iStringLen; // or IP address
unsigned short Port;
filemessage fileinfo;
sockaddr_in filetransfersockaddr;
};
using namespace std;
typedef list<stUserListNode *> UserList;
这是主要的CPP文件
// MyNetChatDlg.cpp : implementation file
//#include "stdafx.h"
#include "MyNetChat.h"
#include "MyNetChatDlg.h"
//#include <winbase.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
SOCKET PrimaryUDP;
sockaddr_in server;
stMessage sendbuf;char ServerIP[20];
bool RecvedACK;
UserList ClientList;
CListBox *output;
bool FILEACK=FALSE;
bool STOPTRANSFER=FALSE;/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg dialogCMyNetChatDlg::CMyNetChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyNetChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyNetChatDlg)
m_user = _T("");
m_showstr = _T("");
m_strFileName = _T("");
m_strFileSize = _T("");
m_strTransferSize = _T("");
m_showName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_dwTransferSize=0;
m_dwFileSize=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMyNetChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyNetChatDlg)
DDX_Control(pDX, IDC_PROGRESS, m_ctrProgress);
DDX_LBString(pDX, IDC_USER, m_user);
DDX_Text(pDX, IDC_SHOWSTR, m_showstr);
DDX_Text(pDX, IDC_FILENAME, m_strFileName);
DDX_Text(pDX, IDC_FILESIZE, m_strFileSize);
DDX_Text(pDX, IDC_TRANSFERSIZE, m_strTransferSize);
DDX_Text(pDX, IDC_SHOWNAME, m_showName);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMyNetChatDlg, CDialog)
//{{AFX_MSG_MAP(CMyNetChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATHOST, OnCreathost)
ON_COMMAND(ID_HELP, OnHelp)
ON_BN_CLICKED(IDC_SENDED, OnSended)
ON_BN_CLICKED(IDC_FINDUSER, OnFinduser)
ON_LBN_DBLCLK(IDC_USER, OnDblclkUser)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_EXITCHAT, OnExitchat)
ON_COMMAND(ID_FILE_TRANSFER, OnFileTransfer)
ON_BN_CLICKED(IDC_ACCEPT, OnAccept)
ON_BN_CLICKED(IDC_REFUSE, OnRefuse)
ON_BN_CLICKED(IDC_STOPTRANSFER, OnStoptransfer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg message handlersBOOL CMyNetChatDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
/*char localname[64];
GetName(localname);
changstr=localname;*/
PrimaryUDP=socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_family = AF_INET;
//sin.sin_port = htons(SERVER_PORT);
sin.sin_port =htons(6000);
bind(PrimaryUDP,(struct sockaddr*)&sin,sizeof(sin));
server.sin_addr.S_un.S_addr = inet_addr("192.168.1.22");
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
//output=(CListBox *)GetDlgItem(IDC_USER);
m_dwTransferSize=0;
m_dwFileSize=0; //不显示传输文件扩展框
ExpandDialog(IDC_SEPERATER,FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}void CMyNetChatDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CMyNetChatDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyNetChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}