📄 tcpiptrcdlg.cpp
字号:
}
m_TraceTail = lin;
if (m_TraceListSize > m_NoEvents) {
// remove the first
int mark = m_TraceHead->Mark;
while ((m_TraceHead!=NULL) && (mark == m_TraceHead->Mark)) {
// remove the line
lin = m_TraceHead;
// free up the memory
m_TraceHead = m_TraceHead->Next;
if (lin->Data) free(lin->Data);
free(lin);
if (m_TraceHead == NULL) m_TraceTail = NULL;
}
InterlockedDecrement(&m_TraceListSize);
}
}
LeaveCriticalSection(&m_Critical);
}
if (!m_Paused) {
int ndx = m_EventListCtrl.AddString(m_StrBuffer);
if (ndx>=0) {
m_EventListCtrl.SetItemData(ndx,m_TrcIndex);
m_LastNdx = ndx;
}
while (m_NoEntries>m_NoEvents) {
// remove oldest item
DWORD mark;
mark = m_EventListCtrl.GetItemData(0);
while (mark == m_EventListCtrl.GetItemData(0)) {
m_EventListCtrl.DeleteString(0);
m_LastNdx--;
}
m_NoEntries--;
}
}
}
/******************************************************************************/
/* GetHostNameFromRegistry */
/******************************************************************************/
/* Get the TCP/IP host name from the Registry */
static BOOL GetHostNameFromRegistry(LPSTR HostName,DWORD dwSizeHostName)
{
HKEY hkTcpip;
DWORD dwError;
DWORD dwSize;
char *p;
dwError = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
0,
KEY_READ,
&hkTcpip);
if (dwError) {
SetLastError(dwError);
return FALSE;
}
/* Get the host name */
dwSize = dwSizeHostName;
dwError = RegQueryValueEx(
hkTcpip,
"Hostname",
NULL,
NULL,
(unsigned char *)HostName,
&dwSize);
if (dwError) {
RegCloseKey(hkTcpip);
SetLastError(dwError);
return FALSE;
}
/* Append a dot */
strncat(HostName,".",dwSizeHostName);
/* Append the domain name */
dwSize = dwSizeHostName - strlen(HostName);
p = HostName + strlen(HostName);
dwError = RegQueryValueEx(
hkTcpip,
"Domain",
NULL,
NULL,
(unsigned char *)p,
&dwSize);
if (dwError || strlen(p)==0) {
//RegCloseKey(hkTcpip);
//SetLastError(dwError);
//return FALSE;
// remove trailing .
HostName[strlen(HostName)-1] = 0;
}
/* Terminate successfully */
RegCloseKey(hkTcpip);
return TRUE;
}
/******************************************************************************/
/* GetLocalHostIPInfo */
/******************************************************************************/
/* Get the local host IP addresses and names into the LocalHostIPInfo array.
Return the number of addresses in the nAddresses variable. */
#define MAX_INET_ADDRESSES 5
typedef struct _IPInfo {
IN_ADDR IPAddr;
char Name[256];
char Address[64];
} tIPInfo;
static tIPInfo LocalHostIPInfo[MAX_INET_ADDRESSES];
static void GetLocalHostIPInfo(char *localip)
{
char szBuff[256];
DWORD dwSize;
PHOSTENT phe;
int i;
int nAddresses;
/* Error condition is indicated if this is 0 on exit */
nAddresses = 0;
/* Get host name */
dwSize = sizeof(szBuff);
if (gethostname(szBuff,dwSize)) {
return;
}
/* Get host addresses */
phe = gethostbyname(szBuff);
if (phe == NULL) {
return;
}
if (strchr(szBuff,'.')==NULL) {
/* gethostname() did not return a FQDN. Go get the FQDN from the Registry */
if (!GetHostNameFromRegistry(szBuff,dwSize)) {
return;
}
}
/* Copy the IP addresses */
strncpy(LocalHostIPInfo[0].Name,"",sizeof(LocalHostIPInfo[i].Name));
i = 0;
while (i<MAX_INET_ADDRESSES) {
if (phe->h_addr_list[i]==0) break;
memcpy(&LocalHostIPInfo[i].IPAddr,phe->h_addr_list[i],sizeof(struct in_addr));
strncpy(LocalHostIPInfo[i].Address,
inet_ntoa(LocalHostIPInfo[i].IPAddr),
sizeof(LocalHostIPInfo[i].Address));
strncpy(LocalHostIPInfo[i].Name,szBuff,sizeof(LocalHostIPInfo[i].Name));
i++;
}
nAddresses = i;
if (nAddresses>0) {
strcpy(localip,LocalHostIPInfo[0].Address);
} else {
strcpy(localip,"?");
}
#if 0
/* Now look up the names corresponding to the addresses */
for (i=0;i<nAddresses;i++) {
time_t tStartedTime, tFinishedTime;
tStartedTime = time(&tStartedTime);
phe = gethostbyaddr((const char *)&LocalHostIPInfo[i].IPAddr,sizeof(IN_ADDR),PF_INET);
tFinishedTime = time(&tFinishedTime);
//Did it take more than 20 seconds? If so, DNS does not active.
if (tFinishedTime - tStartedTime > 20) {
MessageBox(NULL,
"DNS(Domain Name Service) not operational.\nVerify DNS and your network.",
"TCPIPTrc",
MB_OK);
return;
}
if (phe!=NULL) {
/* We got something which claims to be a host name */
if (strchr(phe->h_name,'.')!=0) {
/* It has a dot, so it's probably a FQDN - replace the host name we got earlier. */
strncpy(LocalHostIPInfo[i].Name,phe->h_name,sizeof(LocalHostIPInfo[i].Name));
}
}
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
// CTCPIPTrcDlg message handlers
BOOL CTCPIPTrcDlg::OnInitDialog()
{
char filename[512];
DWORD regvalue;
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
m_Trc = NULL;
m_NoHostNames = 0;
m_TrcIndex = 0;
m_NoEntries = 0;
m_TraceHead = m_TraceTail = NULL;
m_TraceListSize = 0;
GetLocalHostIPInfo(filename);
m_LocalIPCtrl.SetWindowText(filename);
InitializeCriticalSection(&m_Critical);
GetTraceFileFromRegistry(filename);
m_FileNameCtrl.SetWindowText(filename);
regvalue = 256;
GetDWORDFromRegistry("NoEvents",®value);
ltoa(regvalue,filename,10);
m_NoEventsCtrl.SetWindowText(filename);
regvalue = 0;
GetDWORDFromRegistry("ChannelNo",®value);
ltoa(regvalue,filename,10);
m_ChannelCtrl.SetWindowText(filename);
int tabstop = 12;
m_EventListCtrl.SetTabStops(tabstop);
m_PauseCtrl.EnableWindow(false);
m_StartAcqCtrl.EnableWindow(true);
m_StopAcqCtrl.EnableWindow(false);
m_Paused = false;
m_SaveAll = false;
OnChangeFilename();
OnChangeChannel();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTCPIPTrcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CTCPIPTrcDlg::OnDestroy()
{
WinHelp(0L, HELP_QUIT);
CDialog::OnDestroy();
}
// 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 CTCPIPTrcDlg::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 CTCPIPTrcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// Automation servers should not exit when a user closes the UI
// if a controller still holds on to one of its objects. These
// message handlers make sure that if the proxy is still in use,
// then the UI is hidden but the dialog remains around if it
// is dismissed.
void CTCPIPTrcDlg::OnClose()
{
if (CanExit()) {
if (!m_Stopped) {
Stop();
while (!m_Stopped) Sleep(100);
}
TidyUp();
CDialog::OnClose();
}
}
void CTCPIPTrcDlg::OnOK()
{
if (CanExit()) {
if(!m_StopAcquiring)
OnStopacq();
/*if (!m_Stopped) {
Stop();
while (!m_Stopped) Sleep(100);
}*/
TidyUp();
CDialog::OnOK();
}
}
void CTCPIPTrcDlg::OnCancel()
{
if (CanExit()) {
if (!m_Stopped) {
Stop();
while (!m_Stopped) Sleep(100);
}
TidyUp();
CDialog::OnCancel();
}
}
BOOL CTCPIPTrcDlg::CanExit()
{
// If the proxy object is still around, then the automation
// controller is still holding on to this application. Leave
// the dialog around, but hide its UI.
if (m_pAutoProxy != NULL)
{
ShowWindow(SW_HIDE);
return FALSE;
}
return TRUE;
}
void CTCPIPTrcDlg::OnHelp()
{
}
void CTCPIPTrcDlg::OnChangeFilename()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
CString fname;
m_FileNameCtrl.GetWindowText(fname);
m_SaveAllCtrl.EnableWindow(fname.GetLength()>0);
m_SaveCurCtrl.EnableWindow(fname.GetLength()>0);
}
void CTCPIPTrcDlg::OnChangeNoevents()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CTCPIPTrcDlg::OnPause()
{
if (m_Paused) {
m_Paused = false;
m_PauseCtrl.SetWindowText("Pause display");
} else {
m_Paused = true;
m_PauseCtrl.SetWindowText("Start display");
}
}
void CTCPIPTrcDlg::OnSavecur()
{
OPENFILENAME fn;
char filename[512];
m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
memset(&fn,0,sizeof(fn));
fn.lStructSize = sizeof(fn);
fn.lpstrFilter = "Trace dump files\x00*.trc\x00";
fn.lpstrFile = filename;
fn.nMaxFile = sizeof(filename);
fn.lpstrTitle = "File the trace information is to be saved to";
fn.Flags = OFN_CREATEPROMPT|OFN_LONGNAMES|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
if (GetSaveFileName(&fn)) {
FILE *trc = fopen(filename,"w");
if (trc == NULL) {
char err[1024];
sprintf(err,"Unable to open %s for writing",filename);
MessageBox(err,"TCP/IP Tracing");
} else {
TraceLine *lin;
int nevents=0;
int mark = -1;
char emsg[512];
SaveTraceFileIntoRegistry(filename);
EnterCriticalSection(&m_Critical);
lin = m_TraceHead;
while (lin!=NULL) {
fprintf(trc,"%s\n",lin->Data);
if (lin->Mark != mark) {
nevents++;
mark = lin->Mark;
}
lin = lin->Next;
}
LeaveCriticalSection(&m_Critical);
fclose(trc);
sprintf(emsg,"%d events saved to %s",nevents,filename);
MessageBox(emsg,"TCP/IP Tracing");
}
}
}
void CTCPIPTrcDlg::OnSavefile()
{
// TODO: Add your control notification handler code here
}
void CTCPIPTrcDlg::OnStartacq()
{
int ok=1;
HANDLE hRead;
DWORD dwThreadId;
CString chn,noevs;
char filename[512];
TidyUp();
m_NoEventsCtrl.GetWindowText(noevs);
m_NoEvents = atoi(noevs);
SaveDWORDIntoRegistry("NoEvents",m_NoEvents);
m_ChannelCtrl.GetWindowText(chn);
m_Channel = atoi(chn);
SaveDWORDIntoRegistry("ChannelNo",m_Channel);
m_PauseCtrl.SetWindowText("Pause display");
m_Paused = false;
m_PauseCtrl.EnableWindow(true);
m_StopAcquiring = false;
m_StopAcqCtrl.EnableWindow(true);
m_StartAcqCtrl.EnableWindow(false);
m_SaveAllCtrl.EnableWindow(false);
if (m_SaveAll) {
m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
if (filename[0]==0) {
OPENFILENAME fn;
memset(&fn,0,sizeof(fn));
fn.lStructSize = sizeof(fn);
fn.lpstrFilter = "Trace dump files\x00*.trc\x00";
fn.lpstrFile = filename;
fn.nMaxFile = sizeof(filename);
fn.lpstrTitle = "File the trace information is to be saved to";
fn.Flags = OFN_CREATEPROMPT|OFN_LONGNAMES|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&fn)) {
m_FileNameCtrl.SetWindowText(filename);
}
}
m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
if (filename[0]!=0) {
strcpy(m_SaveAlFileName,filename);
m_Trc = fopen(filename,"w");
if (m_Trc == NULL) {
char err[1024];
sprintf(err,"Unable to open %s for writing",filename);
MessageBox(err,"TCP/IP Tracing");
} else {
SaveTraceFileIntoRegistry(filename);
}
}
}
// create the threads
hRead = CreateThread(
NULL, // no security attribute
0, // default stack size
(LPTHREAD_START_ROUTINE) ReaderThread,
(LPVOID) this, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hRead == NULL) {
// failed to create a thread to service the request
MessageBox("Failed to create thread to acquire event data from DSP","TCP/IP Tracing");
ok = 0;
} else {
CloseHandle(hRead);
}
}
void CTCPIPTrcDlg::Stop(void)
{
m_StopAcquiring = true;
if (m_Client != INVALID_SOCKET) {
closesocket(m_Client);
m_Client = INVALID_SOCKET;
}
if (m_Listen != INVALID_SOCKET) {
closesocket(m_Listen);
m_Listen = INVALID_SOCKET;
}
if (m_Trc!=NULL) {
char em[512];
fclose(m_Trc);
m_Trc = NULL;
sprintf(em,"Traced events have been saved in %s",m_SaveAlFileName);
MessageBox(em,"TCP/IP Tracing");
}
}
void CTCPIPTrcDlg::OnStopacq()
{
m_PauseCtrl.EnableWindow(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -