Hi,
I have looked into the #ifdef _DEBUG directives and the RUNTIME_CLASS macros too...But nothing is find to be suspecious, may be I am dull about it.
I attach my code as follows(It may be too long , but I will attach both .h and .cpp file here... as it might help to find what is wrong), It is the CView derived class which cannot be created at the run-time. Anyone got idea what has happened that my .exe file fail to create the view, please let me know. I have been working on it for 3 days and no progress...
For the MPVIEWERView.h,
<br />
<br />
<br />
#pragma once<br />
#include "RegularExpression.h"<br />
#include "afxcoll.h"<br />
#include "MPVIEWERDoc.h"<br />
#include ".\InboundRecordArray.h"<br />
#include ".\STATVIEW.h"<br />
<br />
class CMPVIEWERView : public CView<br />
{<br />
protected:
CMPVIEWERView();<br />
DECLARE_DYNCREATE(CMPVIEWERView)<br />
<br />
public:<br />
CMPVIEWERDoc * GetDocument() const;<br />
<br />
public:<br />
<br />
public:<br />
virtual void OnDraw(CDC* pDC);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);<br />
protected:<br />
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);<br />
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);<br />
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);<br />
<br />
public:<br />
virtual ~CMPVIEWERView();<br />
#ifdef _DEBUG<br />
virtual void AssertValid() const;<br />
virtual void Dump(CDumpContext& dc) const;<br />
#endif<br />
<br />
protected:<br />
<br />
protected:<br />
DECLARE_MESSAGE_MAP()<br />
public:<br />
afx_msg void OnFileStartlogging();<br />
<br />
virtual void OnInitialUpdate(void);<br />
<br />
static UINT run(LPVOID p);<br />
void run();<br />
volatile BOOL running;<br />
<br />
void plot(void);<br />
<br />
void CallbackGrepBlock(RegularExpression * cre, const REMatchInfo & Matches, unsigned LineIndex, unsigned StartOfLine, bool & bcontinue);<br />
void CallbackGrepInbound(RegularExpression * cre, const REMatchInfo & Matches, unsigned LineIndex, unsigned StartOfLine, bool & bcontinue);<br />
<br />
<br />
CStringArray ipColorArray;
CPen CMPVIEWERView::GetColorPen(CString colorCode);<br />
afx_msg void OnEditOptions();<br />
int m_MaxLineShown;<br />
int m_LineSpace;<br />
<br />
afx_msg void OnUpdateFileStartlogging(CCmdUI *pCmdUI);<br />
<br />
CString ReadConfig(CString option);<br />
void WriteConfig(CString option, CString value);<br />
<br />
void UpdateStatusBarText(CString newStatusText);<br />
<br />
int blockNum; <br />
int inboundNum;<br />
<br />
CInboundRecordArray inboundRecordArray;<br />
CSTATVIEW * m_pStatView;<br />
void SendStartLoggingMsg(void);<br />
CString systemStr;<br />
void dumpcall(void);<br />
void RemoveALine(void);<br />
<br />
void DrawBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, int xDest, int yDest );<br />
<br />
};<br />
<br />
#ifndef _DEBUG // debug version in MPVIEWERView.cpp<br />
inline CMPVIEWERDoc* CMPVIEWERView::GetDocument() const<br />
{ return reinterpret_cast<CMPVIEWERDoc*>(m_pDocument); }<br />
#endif
For the MPVIEWERView.cpp,
<br />
#include "stdafx.h"<br />
#include "MPVIEWER.h"<br />
#include "MPVIEWERDoc.h"<br />
#include "MPVIEWERView.h"<br />
#include ".\mpviewerview.h"<br />
#include ".\MainFrm.h"<br />
#include ".\STATVIEW.h"<br />
#include <WinSock.h><br />
#include <stdio.h><br />
#include <iostream><br />
#define SERVER_PORT htons(514) /* Syslogd UDP Port */<br />
<br />
using namespace std;<br />
<br />
#include "RegularExpression.h"<br />
#include <vector><br />
#include <string><br />
#include <sstream><br />
<br />
#include ".\OptionsDlg.h"<br />
#include ".\TokenEx.h"<br />
#include <vector><br />
#include <string><br />
#include <sstream><br />
<br />
#ifdef _DEBUG<br />
#define new DEBUG_NEW<br />
#endif<br />
<br />
<br />
<br />
IMPLEMENT_DYNCREATE(CMPVIEWERView, CView)<br />
<br />
BEGIN_MESSAGE_MAP(CMPVIEWERView, CView)<br />
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)<br />
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)<br />
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)<br />
ON_COMMAND(ID_FILE_STARTLOGGING, OnFileStartlogging)<br />
ON_COMMAND(ID_EDIT_OPTIONS, OnEditOptions)<br />
ON_UPDATE_COMMAND_UI(ID_FILE_STARTLOGGING, OnUpdateFileStartlogging)<br />
END_MESSAGE_MAP()<br />
<br />
<br />
CMPVIEWERView::CMPVIEWERView()<br />
: m_MaxLineShown(0)<br />
, m_LineSpace(0)<br />
, blockNum(0)<br />
, inboundNum(0)<br />
, m_pStatView(NULL)<br />
, systemStr(_T(""))<br />
{<br />
m_LineSpace = _ttoi(ReadConfig("linespace"));<br />
m_MaxLineShown = _ttoi(ReadConfig("maxlines"));<br />
running = FALSE;<br />
m_pStatView = NULL;<br />
systemStr.Format("MP Viewer is not running."); <br />
}<br />
<br />
CMPVIEWERView::~CMPVIEWERView()<br />
{<br />
}<br />
<br />
BOOL CMPVIEWERView::PreCreateWindow(CREATESTRUCT& cs)<br />
{<br />
return CView::PreCreateWindow(cs);<br />
}<br />
<br />
<br />
void CMPVIEWERView::OnDraw(CDC* pDC)<br />
{<br />
CMPVIEWERDoc* pDoc = GetDocument();<br />
ASSERT_VALID(pDoc);<br />
if (!pDoc)<br />
return;<br />
plot();<br />
}<br />
<br />
<br />
<br />
BOOL CMPVIEWERView::OnPreparePrinting(CPrintInfo* pInfo)<br />
{<br />
return DoPreparePrinting(pInfo);<br />
}<br />
<br />
void CMPVIEWERView::OnBeginPrinting(CDC* , CPrintInfo* )<br />
{<br />
}<br />
<br />
void CMPVIEWERView::OnEndPrinting(CDC* , CPrintInfo* )<br />
{<br />
}<br />
<br />
<br />
#ifdef _DEBUG<br />
void CMPVIEWERView::AssertValid() const<br />
{<br />
CView::AssertValid();<br />
}<br />
<br />
void CMPVIEWERView::Dump(CDumpContext& dc) const<br />
{<br />
CView::Dump(dc);<br />
}<br />
<br />
CMPVIEWERDoc* CMPVIEWERView::GetDocument() const
{<br />
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMPVIEWERDoc)));<br />
return (CMPVIEWERDoc*)m_pDocument;<br />
}<br />
#endif //_DEBUG<br />
<br />
<br />
void CMPVIEWERView::OnInitialUpdate(void)<br />
{<br />
CView::OnInitialUpdate();<br />
<br />
<br />
}<br />
<br />
UINT CMPVIEWERView::run(LPVOID p) <br />
{<br />
CMPVIEWERView * me = (CMPVIEWERView *) p;<br />
me->run();<br />
return 0;<br />
}<br />
<br />
void CMPVIEWERView::run()<br />
{<br />
SOCKET sockfd;<br />
int n;<br />
SOCKADDR_IN servaddr, cliaddr;<br />
char mesg[1024];<br />
int sin_size = sizeof(struct sockaddr_in);<br />
<br />
sockfd = socket(AF_INET, SOCK_DGRAM, 0);<br />
<br />
servaddr.sin_family = AF_INET;<br />
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />
servaddr.sin_port = htons(SERVER_PORT);<br />
memset(&(servaddr.sin_zero), 0, 8);<br />
if(bind(sockfd, (LPSOCKADDR) &servaddr, sizeof(servaddr))==0) {<br />
SendStartLoggingMsg();<br />
systemStr.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("MP Viewer started running since %I: %M: %S %p, %m/%d/%Y")));<br />
<br />
m_pStatView->lblStart = CTime::GetCurrentTime();<br />
m_pStatView->running = TRUE;<br />
<br />
<br />
plot();
<br />
}<br />
else {<br />
MessageBox("Bind operation failed");<br />
}<br />
<br />
CString block_str = "<[0-9]+>([a-zA-Z]+\\s+\\d+ \\d+:\\d+:\\d+) .* ruleset=check_relay, arg1=(.*), arg2=(.*), relay=.*, reject=550 5\\.7\\.1 Rejected: This system is protected by Mail Prove Service, \\d+\\.\\d+\\.\\d+\\.\\d+ is blacklisted\\. Please go to www\\.mailprove\\.com to unblock\\.";<br />
<br />
CString inbound_str = "<[0-9]+>([a-zA-Z]+\\s+\\d+ \\d+:\\d+:\\d+) .*: .*: from=<(.*)>, size=(\\d+), class=.*, nrcpts=.*, msgid=<.*>, proto=.*, daemon=.*, relay=(.*) \\[(.*)\\]";<br />
while (running)<br />
{<br />
n = recvfrom(sockfd, mesg, 1024, 0,(LPSOCKADDR) &cliaddr, &sin_size);<br />
mesg[n] = '\0'; <br />
CFile file("messagelog.txt", CFile::modeWrite);
file.SeekToEnd();<br />
file.Write(mesg, n+1);<br />
file.Write("\r\n", 2);<br />
file.Close();<br />
<br />
RegularExpression reo_block;<br />
__hook(&RegularExpression::OnGrepping, &reo_block, &CMPVIEWERView::CallbackGrepBlock);<br />
reo_block.Expression = block_str;<br />
reo_block.StringToMatch = mesg;<br />
reo_block.Grep();<br />
<br />
RegularExpression reo_inbound;<br />
__hook(&RegularExpression::OnGrepping, &reo_inbound, &CMPVIEWERView::CallbackGrepInbound);<br />
reo_inbound.Expression = inbound_str;<br />
reo_inbound.StringToMatch = mesg;<br />
reo_inbound.Grep();<br />
}<br />
shutdown(sockfd, SD_BOTH);<br />
if (closesocket(sockfd) == 0) {<br />
AfxMessageBox("Closed socket successfully",MB_OK);<br />
systemStr.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("MP Viewer stopped at %I: %M: %S %p, %m/%d/%Y")));<br />
<br />
m_pStatView->lblStart = CTime::GetCurrentTime();<br />
m_pStatView->running = FALSE;<br />
<br />
plot();
} else {<br />
AfxMessageBox("Failed to close socket", MB_OK);<br />
}<br />
<br />
}<br />
void CMPVIEWERView::plot(void)<br />
{<br />
<br />
CDC * pDC;<br />
pDC = GetDC();<br />
<br />
CRect rect;<br />
GetClientRect(&rect);<br />
<br />
CBrush NewBrush(RGB(255,255,255)); CBrush * pBrush = pDC->SelectObject(&NewBrush);<br />
<br />
CRect urect=rect;
pDC->Rectangle(urect);<br />
pDC->SelectObject(pBrush);<br />
<br />
CPen pen( PS_SOLID, 0, RGB(255,0,255));<br />
CPen* pPenOld = pDC->SelectObject(&pen);<br />
int nMap = pDC->SetMapMode(MM_HIENGLISH);
<br />
int x1 = 500;<br />
int x2 = 3000;<br />
int y1 = -4000;<br />
int y2 = -1000;<br />
<br />
int red,green,blue = 0;<br />
<br />
<br />
CString colorCode;<br />
CString ip;<br />
for (int i=0; i<ipColorArray.GetSize(); i++) <br />
{<br />
CString ipColor = ipColorArray[i];<br />
ip = ipColorArray[i];<br />
ip.Delete(ipColor.GetLength()-1, 1);<br />
<br />
colorCode = ipColor.Right(1);
y2-=m_LineSpace;<br />
<br />
if (colorCode.Compare("g") == 0) {<br />
red = 0; green = 255; blue = 0;<br />
} else if (colorCode.Compare("r") == 0) {<br />
red = 255; green = 0; blue = 0;<br />
} else {<br />
red = 255; green = 255; blue = 255;<br />
}<br />
<br />
CPen pen( PS_SOLID, 0, RGB(red, green, blue));<br />
pPenOld = pDC->SelectObject(&pen);<br />
pDC->SetTextColor(RGB(red, green, blue));<br />
pDC->MoveTo(x1, y1);<br />
pDC->LineTo(x2, y2);<br />
pDC->TextOut(x2, y2, ip);<br />
pDC->TextOut(x2, y2, ip); <br />
}<br />
<br />
<br />
<br />
}<br />
<br />
void CMPVIEWERView::CallbackGrepBlock(RegularExpression* cre, const REMatchInfo & Matches, unsigned LineIndex, unsigned StartOfLine, bool & bcontinue)<br />
{<br />
std::string whatmatched1(Matches[1].string);<br />
std::ostringstream sout1;<br />
sout1 << whatmatched1;<br />
std::string ws1(sout1.str());<br />
CString dateTime(ws1.c_str());<br />
<br />
std::string whatmatched2(Matches[2].string);<br />
std::ostringstream sout2;<br />
sout2 << whatmatched2;<br />
std::string ws2(sout2.str());<br />
CString relay(ws2.c_str());<br />
<br />
std::string whatmatched3(Matches[3].string);<br />
std::ostringstream sout3;<br />
sout3 << whatmatched3;<br />
std::string ws3(sout3.str());<br />
CString ip(ws3.c_str());<br />
<br />
m_pStatView->AddBlockRecord(dateTime, relay, ip);<br />
m_pStatView->AddBlockListRecord(dateTime, relay, ip); <br />
<br />
while (ipColorArray.GetSize() >= m_MaxLineShown) {
ipColorArray.RemoveAt(0, 1);<br />
}<br />
ipColorArray.Add(ip+"r");
<br />
blockNum++;
<br />
plot();<br />
<br />
CString log_msg = "Blocked " + ip + "\r\n";<br />
int length = log_msg.GetLength();<br />
LPCTSTR str = log_msg;<br />
<br />
CFile file("simplelog.txt", CFile::modeWrite); <br />
file.SeekToEnd();<br />
file.Write(str, length);<br />
file.Close();<br />
<br />
}<br />
<br />
void CMPVIEWERView::CallbackGrepInbound(RegularExpression* cre, const REMatchInfo & Matches, unsigned LineIndex, unsigned StartOfLine, bool & bcontinue)<br />
{<br />
std::string whatmatched1(Matches[1].string);<br />
std::ostringstream sout1;<br />
sout1 << whatmatched1;<br />
std::string ws1(sout1.str());<br />
CString dateTime(ws1.c_str());<br />
<br />
std::string whatmatched2(Matches[2].string);<br />
std::ostringstream sout2;<br />
sout2 << whatmatched2;<br />
std::string ws2(sout2.str());<br />
CString email(ws2.c_str());<br />
<br />
std::string whatmatched3(Matches[3].string);<br />
std::ostringstream sout3;<br />
sout3 << whatmatched3;<br />
std::string ws3(sout3.str());<br />
CString size(ws3.c_str());<br />
<br />
std::string whatmatched4(Matches[4].string);<br />
std::ostringstream sout4;<br />
sout4 << whatmatched4;<br />
std::string ws4(sout4.str());<br />
CString relay(ws4.c_str());<br />
<br />
std::string whatmatched5(Matches[5].string);<br />
std::ostringstream sout5;<br />
sout5 << whatmatched5;<br />
std::string ws5(sout5.str());<br />
CString ip(ws5.c_str());<br />
m_pStatView->AddInboundRecord(dateTime, email, size, relay, ip);<br />
m_pStatView->AddInboundListRecord(dateTime, email, size, relay, ip); <br />
<br />
while (ipColorArray.GetSize() >= m_MaxLineShown) {
ipColorArray.RemoveAt(0, 1);<br />
}<br />
ipColorArray.Add(ip+"g");
inboundNum++;
<br />
plot();<br />
<br />
CString log_msg = "Inbound " + ip + "\r\n";<br />
int length = log_msg.GetLength();<br />
<br />
LPCTSTR str = log_msg;<br />
<br />
CFile file("simplelog.txt", CFile::modeWrite);
file.SeekToEnd();<br />
file.Write(str, length);<br />
<br />
file.Close();<br />
}<br />
<br />
<br />
void CMPVIEWERView::OnEditOptions()<br />
{<br />
COptionsDlg dlg;<br />
if (dlg.DoModal() == IDOK) {<br />
m_MaxLineShown = dlg.maxLineShown;<br />
m_LineSpace = dlg.lineSpace;<br />
}<br />
CString value;<br />
value.Format("%i", m_MaxLineShown); <br />
WriteConfig(_T("maxlines"), _T(value));<br />
value.Format("%i", m_LineSpace); <br />
WriteConfig(_T("linespace"), _T(value));<br />
}<br />
<br />
<br />
void CMPVIEWERView::OnFileStartlogging()<br />
{<br />
<br />
running = !running;<br />
if (running) {<br />
AfxBeginThread(run, this);<br />
} else {
MessageBox("Running is FALSE, Send End Connection Message to Server");<br />
<br />
SOCKET sockfd; <br />
SOCKADDR_IN servaddr;<br />
int sin_size = sizeof(struct sockaddr_in);<br />
<br />
CString endStr;<br />
endStr.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("Stop logging at %I: %M: %S %p, %m/%d/%Y")));<br />
<br />
LPTSTR sendstr = new TCHAR[endStr.GetLength()+1];<br />
_tcscpy(sendstr, endStr);<br />
<br />
<br />
<br />
servaddr.sin_family = AF_INET;<br />
servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");<br />
servaddr.sin_port = htons(514);<br />
<br />
sockfd = socket(AF_INET, SOCK_DGRAM, 0);<br />
<br />
sendto(sockfd, sendstr, strlen(sendstr), 0, (LPSOCKADDR) &servaddr, sin_size);<br />
<br />
}<br />
}<br />
<br />
<br />
void CMPVIEWERView::OnUpdateFileStartlogging(CCmdUI *pCmdUI)<br />
{<br />
if (running == TRUE) {<br />
pCmdUI->SetText("Stop Logging");<br />
} else {<br />
pCmdUI->SetText("Start Logging");<br />
}<br />
}<br />
<br />
CString CMPVIEWERView::ReadConfig(CString option)<br />
{<br />
CFile file("config.txt", CFile::modeRead);<br />
<br />
ULONGLONG nLen = file.GetLength();<br />
char* pBuffer = new char[ nLen + 1 ];<br />
file.Read( pBuffer, nLen );<br />
pBuffer[ nLen ] = 0;<br />
CString strTemp = pBuffer;<br />
delete [] pBuffer;<br />
file.Close();<br />
<br />
CTokenEx tok;<br />
CStringArray lines;<br />
tok.Split(strTemp, "\n", lines);<br />
<br />
<br />
for (int i=0; i<lines.GetSize(); i++) {<br />
lines[i].Trim();<br />
if (lines[i].Find(option, 0) == 0) {
lines[i].Delete(0, option.GetLength());<br />
return lines[i].Trim();<br />
}<br />
}<br />
<br />
return "";<br />
}<br />
<br />
void CMPVIEWERView::WriteConfig(CString option, CString value)<br />
{<br />
<br />
CFile file_r("config.txt", CFile::modeRead);<br />
<br />
ULONGLONG nLen = file_r.GetLength();<br />
char* pBuffer = new char[ nLen + 1 ];<br />
file_r.Read( pBuffer, nLen );<br />
pBuffer[ nLen ] = 0;<br />
CString strTemp = pBuffer;<br />
delete [] pBuffer;<br />
file_r.Close();<br />
<br />
CTokenEx tok;<br />
CStringArray lines;<br />
tok.Split(strTemp, "\n", lines);<br />
<br />
CFile file_w("config.txt", CFile::modeWrite);<br />
<br />
for (int i=0; i<lines.GetSize(); i++) {<br />
<br />
lines[i].Trim();<br />
lines[i] += "\r\n";<br />
if (lines[i].Find(option, 0) != 0) {
int nLen = lines[i].GetLength(); <br />
file_w.Write( lines[i], nLen );<br />
<br />
} else {<br />
CString strToWrite;<br />
strToWrite = option + " " + value + "\r\n";<br />
int strToWrite_length = strToWrite.GetLength(); <br />
file_w.Write( strToWrite, strToWrite_length );<br />
<br />
}<br />
}<br />
<br />
file_w.Close();<br />
<br />
}<br />
void CMPVIEWERView::SendStartLoggingMsg(void)<br />
{<br />
SOCKET sockfd; <br />
SOCKADDR_IN servaddr;<br />
int sin_size = sizeof(struct sockaddr_in);<br />
<br />
CString startStr;<br />
startStr.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("Start logging at %I: %M: %S %p, %m/%d/%Y")));<br />
<br />
LPTSTR sendstr = new TCHAR[startStr.GetLength()+1];<br />
_tcscpy(sendstr, startStr);<br />
<br />
<br />
<br />
servaddr.sin_family = AF_INET;<br />
servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");<br />
servaddr.sin_port = htons(514);<br />
<br />
sockfd = socket(AF_INET, SOCK_DGRAM, 0);<br />
<br />
sendto(sockfd, sendstr, strlen(sendstr), 0, (LPSOCKADDR) &servaddr, sin_size);<br />
}<br />
<br />
void CMPVIEWERView::dumpcall(void)<br />
{<br />
MessageBox("CMPVIEWERView's dumpcall"); <br />
}<br />
<br />
void CMPVIEWERView::RemoveALine(void)<br />
{<br />
if (ipColorArray.GetSize() > 0) {<br />
ipColorArray.RemoveAt(0, 1);
plot();<br />
}<br />
}<br />
<br />
void CMPVIEWERView::DrawBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, int xDest, int yDest )<br />
{<br />
BITMAP bm;<br />
::GetObject( hBitmap, sizeof( bm ), &bm );<br />
CPoint size( bm.bmWidth, bm.bmHeight );<br />
pDC->DPtoLP(&size);<br />
<br />
CPoint org(0,0);<br />
pDC->DPtoLP(&org);<br />
<br />
CDC memDC;<br />
memDC.CreateCompatibleDC( pDC );<br />
memDC.SetMapMode( pDC->GetMapMode() );<br />
<br />
HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );<br />
<br />
<br />
if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)<br />
{<br />
SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );<br />
pDC->RealizePalette();<br />
}<br />
pDC->BitBlt(xDest, yDest, size.x, size.y, &memDC, org.x, org.y, SRCCOPY);<br />
<br />
::SelectObject( memDC.m_hDC, hBmOld );<br />
}
Help! I need somebody help!
Would you please help me?
|