📄 bqq.cpp
字号:
// BQQ.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "BQQ.h"
#include "BQQDlg.h"
#include "mailslot.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////GLOBAL var
CStringList g_listGroup;
BOOL g_bExit;
BOOL g_bSending;
BOOL g_bSendOnly;
BOOL g_bAlwaysTop = FALSE;
BOOL g_bHide = TRUE;
BOOL g_bEnableTooltips = TRUE;
BOOL g_bAutoPopup = TRUE;
BOOL g_bAutoPopupMessBox = FALSE;
BOOL g_bCloseDelLast = TRUE;
BOOL g_bUseSmiley = TRUE;
BOOL g_bPlaySound = FALSE;
BOOL m_bEnter2Send = FALSE;
int g_iMaxLast;
UINT g_uCloseAfter; // = 0; Nerver close
CString g_sSignature;
BYTE g_buffer[MAX_BUFFER_SIZE+1];
/////////////////////////////////////////////////////////////////////////////
// CBQQApp
BEGIN_MESSAGE_MAP(CBQQApp, CWinApp)
//{{AFX_MSG_MAP(CBQQApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBQQApp construction
CBQQApp::CBQQApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CBQQApp object
CBQQApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CBQQApp initialization
BOOL CBQQApp::InitInstance()
{
// 防止应用程序两次加载
hMutex = CreateMutex(NULL, FALSE, "RunOnlyOneInstance");
if(hMutex==NULL|| ERROR_ALREADY_EXISTS==::GetLastError())
{
m_bFirstRun = FALSE;
CloseHandle(hMutex);
return FALSE;
}
SetRegistryKey(_T("Hury's NetSend"));
closed = TRUE;
senderName= _T("***");
hisMsg = _T("");
tmpMsg = _T("");
sendFileFullName= _T("");
sendFileName= _T("");
hasAttachment= FALSE;
hostIP =_T("");
//// *** END ***
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CBQQDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
/////////////////////// GLOBALS FUNCTION ///////////////////////////
///////////////////////////////////////////////////////////////////////////////////
UINT ServerMailslot(LPVOID lpParam)
{
g_bSendOnly = FALSE;
CBQQDlg *pNetSendDlg = (CBQQDlg*)lpParam;
DWORD dwNumberOfBytesRead;
DWORD dwNumberOfBytesReadOld = -1;
CMailSlot mslotServer = CMailSlot("", MESSNGR_MAILSLOT);
if ( !mslotServer.CreateServer() )
{
g_bSendOnly = TRUE;
//if(pNetSendDlg->m_hWnd!=NULL)
// ::PostMessage(pNetSendDlg->m_hWnd,WM_EXIT,NULL,NULL);
return 0;
}
memset(g_buffer, 0, MAX_BUFFER_SIZE+1); // set all by 0
SYSTEMTIME timeDest;
SYSTEMTIME timeDestOld = {-1, -1, -1, -1, -1, -1, -1, -1};
while ((mslotServer.Read(g_buffer, MAX_BUFFER_SIZE, dwNumberOfBytesRead) == TRUE ))
{
if (g_bExit)
break;
TRACE("in while\r\n");
::GetLocalTime( &timeDest );
if ( (timeDestOld.wMilliseconds != timeDest.wMilliseconds) || (timeDestOld.wSecond != timeDest.wSecond) || (timeDestOld.wMinute != timeDest.wMinute) || (dwNumberOfBytesReadOld != dwNumberOfBytesRead) )
{
timeDestOld = timeDest;
dwNumberOfBytesReadOld = dwNumberOfBytesRead;
if(pNetSendDlg->m_hWnd!=NULL)
::PostMessage(pNetSendDlg->m_hWnd,WM_RECEIVE_MESSAGE, (WPARAM)dwNumberOfBytesRead, NULL);
}
}
mslotServer.Close();
//if(pNetSendDlg->m_hWnd!=NULL)
// ::PostMessage(pNetSendDlg->m_hWnd,WM_EXIT,NULL,NULL);
return 0;
}
int SendToMailsot(LPCSTR serverName, LPCSTR sFrom, LPCSTR sTo , LPCSTR sMessage)
{
DWORD dwBytesToWrite = 0;
TCHAR buffer[MAX_BUFFER_SIZE+1];
memset(buffer, 0, MAX_BUFFER_SIZE+1);
dwBytesToWrite = 0; // byte to write
// structure of data for send
strcpy(buffer, sFrom); // FROM
dwBytesToWrite += strlen(sFrom) + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, sTo); // TO
dwBytesToWrite += strlen(sTo) + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, sMessage);// CONTENT
dwBytesToWrite += strlen(sMessage); // TOTAL byte to send
CMailSlot mslot = CMailSlot(serverName, MESSNGR_MAILSLOT);
if (mslot.Create())
{
BOOL isOK = FALSE;
CString status ;
isOK = mslot.Write(buffer, dwBytesToWrite);
CString sedMsg ="",tm="";
CTime t = CTime::GetCurrentTime();
tm = t.Format( "%m月%d日%H:%M:%S");
sedMsg.Format("\r\n您将UDP消息发往%s["+tm+"]\r\n%s\r\n",sTo,sMessage);
((CBQQApp*)AfxGetApp())->hisMsg+=sedMsg;
mslot.Close();
return (isOK == TRUE)? 1 : 0 ;
}
else
return -1;
}
UINT SendData(LPVOID lpParam)
{
CBQQDlg *pNetSendDlg = (CBQQDlg*)lpParam;
DWORD dwBytesToWrite = 0;
TCHAR buffer[MAX_BUFFER_SIZE+1];
memset(buffer, 0, MAX_BUFFER_SIZE+1);
CString from=pNetSendDlg->hostName;
if(((CButton*)pNetSendDlg->GetDlgItem(IDC_ANONYMOUS))->GetCheck()==1)
from ="***";
CString sMessage,tm="";
CTime t = CTime::GetCurrentTime();
tm = t.Format( "%m月%d日%H:%M:%S");
sMessage = pNetSendDlg->sendMessageStr.Left(4000)+"\0";
int pos =-1;
if ( (pos =pNetSendDlg->sendTarget.Find(';', 0)) == -1 ) // only one user
{
pNetSendDlg->sendTarget = pNetSendDlg->sendTarget.Left(MAX_NAME_SIZE);
dwBytesToWrite = 0; // byte to write
// structure of data for send
strcpy(buffer, (LPCTSTR)from); // FROM
dwBytesToWrite += from.GetLength() + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, (LPCTSTR)pNetSendDlg->sendTarget); // TO
dwBytesToWrite += pNetSendDlg->sendTarget.GetLength() + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, (LPCTSTR)sMessage);// CONTENT
dwBytesToWrite += sMessage.GetLength() ; // TOTAL byte to send
int number = 1;
CMailSlot mslot = CMailSlot((LPCTSTR)pNetSendDlg->sendTarget, MESSNGR_MAILSLOT);
if (mslot.Create())
{
BOOL isErr = FALSE;
CString status ;
isErr = !mslot.Write(buffer, dwBytesToWrite);
status = _T("1 message sent.");
CString sTitle;
CString sContent;
if ( !isErr )
{
sTitle = "已用UDP方式发向:" + pNetSendDlg->sendTarget;
sContent = "向 \"" + pNetSendDlg->sendTarget + "\" 发送成功";
((CEdit*)pNetSendDlg->GetDlgItem(IDC_INOUT))->SetWindowText("");
((CBQQApp*)AfxGetApp())->hisMsg+="\r\n您将UDP消息发往"+pNetSendDlg->sendTarget+"["+tm+"]\r\n"+sMessage+"\r\n";
MessageBox(NULL, sContent, sTitle, MB_OK | MB_ICONINFORMATION );
}
else
{
sTitle = "已用UDP方式发向:" + pNetSendDlg->sendTarget;
sContent.Format("向 \"%s\"发送失败", pNetSendDlg->sendTarget);
MessageBox(NULL, sContent, sTitle, MB_OK | MB_ICONERROR );
}
mslot.Close();
}
}
else
{
CString sUsers(pNetSendDlg->sendTarget);
if ( !sUsers.IsEmpty () )
{
CStringList psUserList;
int res = 0;
CString sUser="";
parsePhrases(TRUE,sUsers, ';', &psUserList);
for (int i = 0; i< psUserList.GetCount () ; i++ )
{
sUser = psUserList.GetAt (psUserList.FindIndex (i));
res = SendToMailsot((LPCTSTR)sUser , (LPCTSTR)from, (LPCTSTR)sUser , (LPCTSTR)sMessage);
}
if(res>0)
{
MessageBox(NULL, "发送成功(UDP)", "提示", MB_OK | MB_ICONINFORMATION );
//((CBQQApp*)AfxGetApp())->hisMsg+="消息发往"+sUser+"["+tm+"]\r\n"+sMessage+"\r\n";
((CEdit*)pNetSendDlg->GetDlgItem(IDC_INOUT))->SetWindowText("");
}
else
{
MessageBox(NULL, "发送失败", "提示", MB_OK | MB_ICONINFORMATION );
}
psUserList.RemoveAll ();
}
}
pNetSendDlg->GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
return 0;
}
void parsePhrases(BOOL flag,CString strInput, TCHAR sepChar, CStringList *psList)
{
int index = 0;
CString aWord="",name= "";
index = strInput.Find(sepChar);
while (index != -1)
{
aWord = strInput.Mid(0, index);
if ( !aWord.IsEmpty() )
{
name=aWord;
if(flag)
{
unsigned long ipAddr=inet_addr(name.GetBuffer(name.GetLength()));
name.ReleaseBuffer();
if(ipAddr != INADDR_NONE)
{
LPHOSTENT lphost;
in_addr inAdr;
inAdr.s_addr = ipAddr;
lphost = gethostbyaddr((char *)&inAdr,4,AF_INET );
if (lphost != NULL)
name.Format("%s",(LPIN_ADDR)lphost->h_name);
}
}
psList->AddTail(name);
}
strInput = strInput.Mid(index+1);
index = strInput.Find(sepChar);
}
name = strInput;
if ( !name.IsEmpty() )
{
///Too
psList->AddTail(name);
}
}
UINT SendDataToGroup(LPVOID lpParam)
{
CBQQDlg *pNetSendDlg = (CBQQDlg*)lpParam;
CString sMessage;
sMessage = pNetSendDlg->sendMessageStr;
CString groupName = "整个网络";
CString listMember ="整个网络";
if ( !listMember.IsEmpty() )
{
CStringList psList;
parsePhrases (TRUE,listMember, SEP_CHAR_USER, &psList);
DWORD dwBytesToWrite = 0;
TCHAR buffer[MAX_BUFFER_SIZE+1];
memset(buffer, 0, MAX_BUFFER_SIZE+1);
dwBytesToWrite = 0; // byte to write
// structure of data for send
strcpy(buffer, pNetSendDlg->hostName); // FROM
dwBytesToWrite += pNetSendDlg->hostName.GetLength() + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, groupName); // TO GROUP
dwBytesToWrite += groupName.GetLength() + 1; // value = byte 0
strcpy(buffer + dwBytesToWrite, sMessage);// CONTENT
dwBytesToWrite += sMessage.GetLength() ; // TOTAL byte to send
BOOL isErr = FALSE;
int size = psList.GetCount();
for ( int k = 0; k < size; k++ )
{
CMailSlot mslot = CMailSlot( psList.GetAt(psList.FindIndex(k)) , MESSNGR_MAILSLOT);
if (mslot.Create())
{
CString status ;
isErr = !mslot.Write(buffer, dwBytesToWrite);
status = _T("1 message sent.");
mslot.Close();
}
}
CString sTitle;
CString sContent;
if ( !isErr )
{
sTitle = "[BQQ] Send to GROUP [" + groupName + "]";
sContent = "Message for \"" + groupName + "\" successfully sent";
if (g_bAutoPopupMessBox)
MessageBox(pNetSendDlg->m_hWnd, sContent, sTitle, MB_OK | MB_ICONINFORMATION );
}
else
{
sTitle = "[BQQ] Send to GROUP [" + groupName + "]";
sContent.Format("Error while sent to \"%s\" at message ", groupName, k+1);
::MessageBox(pNetSendDlg->m_hWnd, sContent, sTitle, MB_OK | MB_ICONERROR );
}
}
pNetSendDlg->m_InOutCtrl.SetWindowText("");
return 0;
}
UINT SendMyMsg(LPVOID lpParam)
{
CBQQDlg *pNetSendDlg = (CBQQDlg*)lpParam;
CString tmpStr="";
BOOL nomsgReached= TRUE;
tmpStr = pNetSendDlg->sendMessageStr;
if(nomsgReached)
{
//use mailslot to send data
if(pNetSendDlg->hostName.CompareNoCase("*")==0)
SendDataToGroup(lpParam);
else
SendData(lpParam);
}
pNetSendDlg->GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
//MessageBox( NULL, "信息已发送成功","提示", MB_OK | MB_ICONINFORMATION );
return 0;
}
int CBQQApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
if(hMutex!=NULL)
CloseHandle(hMutex);
return CWinApp::ExitInstance();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -