client.cpp

来自「网吧管理系统VC源码」· C++ 代码 · 共 170 行

CPP
170
字号
// Client.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientApp

BEGIN_MESSAGE_MAP(CClientApp, CWinApp)
	//{{AFX_MSG_MAP(CClientApp)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CClientApp construction

CClientApp::CClientApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CClientApp object

CClientApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CClientApp initialization

BOOL CClientApp::InitInstance()
{
	//判断程序是否已运行
	if(AlreadyRunning())
	{
		AfxMessageBox(IDS_ALREADY_RUNNING,MB_ICONWARNING);
		return(FALSE);
	}

	//初始化CSocket类
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();

	//初始化COM组件
	CoInitialize(NULL);
    // 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

	//Register();

	CClientDlg dlg; //(CWnd::GetDesktopWindow()); //pFrame);
	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
	}
	return FALSE;
}


//将客户端程序写入注册表中,以便开机启动程序
BOOL CClientApp::Register()
{
	HKEY  hKEY;
	char  CurrentPath[MAX_PATH];
	long  ret;
	LPCTSTR RegisterPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run" ;
 
	//Get the file path
	GetModuleFileName(NULL, CurrentPath, MAX_PATH );

	//Open key
	ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegisterPath, 0, KEY_WRITE, &hKEY);
	
	if(ret!=ERROR_SUCCESS)
	{ 
		RegCloseKey(hKEY);
		return FALSE;
	}

	//Set Key
	ret=RegSetValueEx(hKEY, m_pszAppName, NULL, REG_SZ, (const unsigned char*)CurrentPath, MAX_PATH);
	
	if(ret!=ERROR_SUCCESS)
	{ 
		RegCloseKey(hKEY);
		return FALSE;
	}

	RegCloseKey(hKEY);
	return TRUE;
}


//隐藏进程
BOOL CClientApp::HideProcess()
{
	HINSTANCE hInst = LoadLibrary("KERNEL32.DLL"); 
	if(hInst) 
	{
		typedef DWORD (WINAPI *MYFUNC)(DWORD,DWORD);
		MYFUNC RegisterServiceProcessFun = NULL;
		RegisterServiceProcessFun = (MYFUNC)GetProcAddress(hInst, "RegisterServiceProcess");
		if(!RegisterServiceProcessFun)
		{
			FreeLibrary(hInst);
			AfxMessageBox("UnGetProcess");
			return FALSE;
		}
		RegisterServiceProcessFun(GetCurrentProcessId(),0);
		FreeLibrary(hInst);
		return TRUE;
	}
	return FALSE;
}


//检测客户端程序是否已运行
BOOL CClientApp::AlreadyRunning()
{
	BOOL bFound = FALSE;

	// Try to create a mutex with the app's name
	HANDLE hMutexOneInstance = ::CreateMutex(NULL,TRUE,_T(AfxGetAppName()));

	// Already there...means that we are already running an instance
	if(::GetLastError() == ERROR_ALREADY_EXISTS)
		bFound = TRUE;

	// Release the mutex
	if(hMutexOneInstance)
		::ReleaseMutex(hMutexOneInstance);

	return(bFound);
}

⌨️ 快捷键说明

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