CLocalMachine.cpp
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:2k
- // CLocalMachine.cpp: implementation of the CLocalMachine class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CLocalMachine.h"
- //#include <Winsock2.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //#pragma comment(lib, "Ws2_32.lib")
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLocalMachine::CLocalMachine()
- {
- m_bInit = false;
- InitSystem();
- }
- CLocalMachine::~CLocalMachine()
- {
- Uninit();
- }
- void CLocalMachine::InitSystem(void)
- {
- if (m_bInit)
- return;
- // Init socket
- WORD wVersionRequested = MAKEWORD(2, 0);
- WSADATA wsaData;
- if (WSAStartup(wVersionRequested, &wsaData) == 0)
- {
- m_bInit = true;
- }
- // Other init...
- if (m_bInit)
- {
- //m_bInit =
- }
- }
- void CLocalMachine::Uninit(void)
- {
- // Clean up socket
- if (m_bInit)
- {
- WSACleanup();
- }
- }
- bool CLocalMachine::GetIPAddress(char * outIP, char * outHost)
- {
- if (m_bInit)
- {
- char name[255];
- if (gethostname(name, sizeof(name)) == 0)
- {
- // Output the host name
- if (outHost != NULL)
- strcpy(outHost, name);
- PHOSTENT hostinfo;
- if((hostinfo = gethostbyname(name)) != NULL)
- {
- // Output local machine's IP address string
- LPCSTR pIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
- if (outIP != NULL)
- strcpy(outIP, pIP);
- return true;
- }
- }
- }
- return false;
- }