www.pudn.com > CtrlIPClt.rar > CtrlIPClt.cpp


// CtrlIPClt.cpp : Defines the initialization routines for the DLL. 
// 
 
#include "stdafx.h" 
#include "CtrlIPClt.h" 
#include "CtrlIPHead.h" 
#include "RunThread.h" 
#include "ConsoleInfoDlg.h" 
 
#ifdef _DEBUG 
	#define		WAIT_RESPONSE_TIMEOUT		4 * 1000 
#else 
	#define		WAIT_RESPONSE_TIMEOUT		10 * 1000 
#endif 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// 
//	Note! 
// 
//		If this DLL is dynamically linked against the MFC 
//		DLLs, any functions exported from this DLL which 
//		call into MFC must have the AFX_MANAGE_STATE macro 
//		added at the very beginning of the function. 
// 
//		For example: 
// 
//		extern "C" BOOL PASCAL EXPORT ExportedFunction() 
//		{ 
//			AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
//			// normal function body here 
//		} 
// 
//		It is very important that this macro appear in each 
//		function, prior to any calls into MFC.  This means that 
//		it must appear as the first statement within the  
//		function, even before any object variable declarations 
//		as their constructors may generate calls into the MFC 
//		DLL. 
// 
//		Please see MFC Technical Notes 33 and 58 for additional 
//		details. 
// 
 
///////////////////////////////////////////////////////////////////////////// 
// CCtrlIPCltApp 
 
BEGIN_MESSAGE_MAP(CCtrlIPCltApp, CWinApp) 
	//{{AFX_MSG_MAP(CCtrlIPCltApp) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCtrlIPCltApp construction 
 
CCtrlIPCltApp::CCtrlIPCltApp() 
{ 
	// TODO: add construction code here, 
	// Place all significant initialization in InitInstance 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only CCtrlIPCltApp object 
 
CCtrlIPCltApp theApp; 
 
///////////////////////////////////////////////////////////////////////////// 
// CCtrlIPCltApp initialization 
 
BOOL CCtrlIPCltApp::InitInstance() 
{ 
	if (!AfxSocketInit()) 
	{ 
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED); 
		return FALSE; 
	} 
 
	char szFilename[MAX_PATH] = {0}; 
	//::GetModuleFileName(m_hInstance, szFilename, sizeof(szFilename)); 
	::GetTempPath(sizeof(szFilename), szFilename); 
	strcat(szFilename, "CtrlIPClt.dll.log"); 
	m_cRunLog.SetLogFilename(szFilename); 
	m_cRunLog.EnableWriteLog(TRUE); 
	m_cRunLog.DeleteLogFile(); 
	 
	return TRUE; 
} 
 
BOOL CCtrlIPCltApp::GetConsoleInfoEx(LPCTSTR lpszSn, CString &strConsoleIP, UINT &uConsolePort, UINT &uUpdatePort, CString strIP) 
{ 
	if(lpszSn==NULL) 
	{ 
		m_cRunLog.WriteDebugLog("序列号为空!"); 
		return FALSE; 
	} 
	m_strConsoleSN = lpszSn; 
	if(m_strConsoleSN.GetLength() > 128) 
	{ 
		m_cRunLog.WriteDebugLog("序列号过长!"); 
		return FALSE; 
	} 
 
	BOOL bRtn = TRUE; 
 
	CRunThread *pThread = (CRunThread *)AfxBeginThread(RUNTIME_CLASS(CRunThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); 
	pThread->m_bAutoDelete = FALSE; 
	pThread->SetIPAddr(strIP); 
	pThread->ResumeThread(); 
 
	if(WAIT_OBJECT_0 != ::WaitForSingleObject(pThread->m_hThread, WAIT_RESPONSE_TIMEOUT)) 
	{ 
		pThread->PostThreadMessage(WM_QUIT, 0, 0); 
	} 
	 
	// 获取控制台信息 
	int nCount = pThread->m_cUDPSocket.m_ConsoleInfoArray.GetSize(); 
	LPCONSOLEINFO lpItem = NULL; 
	 
	if(nCount != 0) 
	{ 
#ifdef _DEBUG 
		if(nCount > 1)	// 不只一个控制台,则需要用户选择 
		{ 
			CConsoleInfoDlg ciDlg; 
			ciDlg.m_nCurSel = 0; 
			ciDlg.m_pConsoleInfoArray = &pThread->m_cUDPSocket.m_ConsoleInfoArray; 
			 
			// 显示用户选择对话框 
			HINSTANCE hOldRes = AfxGetResourceHandle(); 
			AfxSetResourceHandle(theApp.m_hInstance); 
			 
			ciDlg.DoModal(); 
			AfxSetResourceHandle(hOldRes); 
			 
			lpItem = pThread->m_cUDPSocket.m_ConsoleInfoArray.GetAt(ciDlg.m_nCurSel); 
		} 
		else 
		{ 
			lpItem = pThread->m_cUDPSocket.m_ConsoleInfoArray.GetAt(0); 
		} 
#else	// Release 版本,直接使用第一个控制台信息,其他舍弃 
		lpItem = pThread->m_cUDPSocket.m_ConsoleInfoArray.GetAt(0); 
#endif 
		 
		// 置返回值 
		strConsoleIP = lpItem->szConsoleIP; 
		uUpdatePort = lpItem->stCtrlIPInfo.uUpdateServerPort; 
		uConsolePort = lpItem->stCtrlIPInfo.uConsolePort; 
		 
		m_cRunLog.WriteDebugLog("获取控制台信息成功:strConsoleIP=%s, uConsolePort=%d, uUpdatePort=%d", \ 
			strConsoleIP, uConsolePort, uUpdatePort); 
 
		bRtn = TRUE; 
	} 
	else 
	{ 
		bRtn = FALSE; 
		m_cRunLog.WriteDebugLog("获取控制台信息失败!"); 
	} 
 
	if(WAIT_OBJECT_0 != ::WaitForSingleObject(pThread->m_hThread, 1 * 1000)) 
	{ 
		::TerminateThread(pThread->m_hThread, 0); 
		m_cRunLog.WriteWarningLog("线程没有退出,强杀。TerminateThread!"); 
	} 
	else 
	{ 
		m_cRunLog.WriteDebugLog("线程正常退出,pThread->Delete()!"); 
		pThread->m_bAutoDelete = TRUE; 
		pThread->Delete(); 
	} 
 
	return bRtn; 
} 
 
 
/////////////////////////////////////////////////////////////////////////// 
// EXPORT Function 
/////////////////////////////////////////////////////////////////////////// 
BOOL GetConsoleInfo(LPCTSTR lpszSn, CString &strConsoleIP, UINT &uConsolePort, UINT &uUpdatePort, int nRetryTimes=0) 
{ 
	AFX_MODULE_STATE* pModuleState = AfxGetModuleState(); 
	pModuleState->m_hCurrentInstanceHandle = ::GetModuleHandle(NULL); 
	pModuleState->m_hCurrentResourceHandle = ::GetModuleHandle(NULL); 
	 
	theApp.m_cRunLog.WriteDebugLog("into GetConsoleInfo(...), nRetryTimes=%d。", nRetryTimes); 
 
	// 获取本机的ip地址列表 
	char szName[256] = {0}; 
	gethostname(szName, sizeof(szName)); 
	 
	struct hostent *pHost = gethostbyname(szName); 
	CStringArray strIPArray; 
	int i = 0, j = 0; 
	struct   sockaddr_in sAddr; 
	CString strIP; 
 
	while(pHost->h_addr_list[i] != NULL) 
	{ 
		memcpy(&sAddr.sin_addr.s_addr, pHost->h_addr_list[i], pHost->h_length); 
		strIP = inet_ntoa(sAddr.sin_addr); 
		strIPArray.Add(strIP); 
		theApp.m_cRunLog.WriteDebugLog("获取的本机的IP地址为:%s", strIP); 
		i++; 
	} 
	 
	int nCount = strIPArray.GetSize(); 
 
	i = 0; 
	for(;;) 
	{ 
		for(j=0; j nRetryTimes) 
			{ 
				strIPArray.RemoveAll(); 
				return FALSE;	// 超过重试次数 
			} 
			else 
			{ 
				Sleep(10 * 1000);	// 10 秒后重试 
				continue ;			// 重试  
			} 
		} 
	} 
	 
	strIPArray.RemoveAll(); 
	return FALSE; 
}