📄 netchck.cpp
字号:
MSG msg;
char szClassName[16] = ""; /* Flawfinder: ignore */
HWND hActiveWindow = NULL;
// Get start time
ulStartTime = HX_GET_TICKCOUNT();
do
{
// Keep pumping messages
if(PeekMessage(&msg, NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
PostQuitMessage(0);
break;
}
else
{
// XXXJDL Since we are pumping messages through here while we block elsewhere in the calling code
// we need to get the free dialog navigation support from windows for modeless dialog boxes. Modal dialog
// boxes are fine since they will not go through this message loop. I have been using this method
// in the main player application and it seems to work ok. Let me know if this causes pain for anyone.
// Basically if the active window is a dialog box class then we are assuming it is a modeless dialog box
// and calling IsDialogMessage with this handle. If the active window is neither a dialog class or
// doesn't process the dialog message then we do the normal message dispatching
hActiveWindow = ::GetActiveWindow();
szClassName[0] = '\0';
::GetClassName(hActiveWindow,
OS_STRING2(szClassName,sizeof(szClassName)),
sizeof(szClassName));
if (strcmp(szClassName,DIALOG_CLASS) || !IsDialogMessage(hActiveWindow, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
// If we have waited ulInterval time then drop out
ulCurrentTime = HX_GET_TICKCOUNT();
} while (CALCULATE_ELAPSED_TICKS(ulStartTime, ulCurrentTime) < ulInterval);
}
//******************************************************************************
//
// Method: CHXNetCheck::GetDNSAddress
//
// Purpose: Determines the IP address of the user's primary DNS server.
//
// Notes: Returns IP address in numeric form, in a CHXString. An empty
// string is returned when the IP address cannot be determined.
//
//******************************************************************************
void CHXNetCheck::GetDNSAddress(CHXString& strDNS)
{
#ifndef _WIN16
strDNS.Empty();
HKEY hKey = 0;
// Win95/98 likes it here
RegOpenKey(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\VxD\\MSTCP\\Parameters",
&hKey);
if (hKey != NULL)
{
GetNameServerKey(hKey, "NameServer", strDNS);
if (strDNS.IsEmpty())
GetNameServerKey(hKey, "DhcpNameServer", strDNS);
RegCloseKey(hKey);
}
// Some Win 95/98 machines put it here..
if (strDNS.IsEmpty())
{
RegOpenKey(HKEY_LOCAL_MACHINE, OS_STRING("System\\CurrentControlSet\\Services\\VxD\\MSTCP"),
&hKey);
if (hKey != NULL)
{
GetNameServerKey(hKey, "NameServer", strDNS);
if (strDNS.IsEmpty())
GetNameServerKey(hKey, "DhcpNameServer", strDNS);
RegCloseKey(hKey);
}
}
// Try WinNT registry location.
if (strDNS.IsEmpty())
{
RegOpenKey(HKEY_LOCAL_MACHINE, OS_STRING("System\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
&hKey);
if (hKey != NULL)
{
GetNameServerKey(hKey, "NameServer", strDNS);
if (strDNS.IsEmpty())
GetNameServerKey(hKey, "DhcpNameServer", strDNS);
RegCloseKey(hKey);
}
}
// Another WinNT registry location.
if (strDNS.IsEmpty())
{
RegOpenKey(HKEY_LOCAL_MACHINE, OS_STRING("System\\CurrentControlSet\\Services\\Tcpip"),
&hKey);
if (hKey != NULL)
{
GetNameServerKey(hKey, "NameServer", strDNS);
if (strDNS.IsEmpty())
GetNameServerKey(hKey, "DhcpNameServer", strDNS);
RegCloseKey(hKey);
}
}
// I want to make sure this technique of getting DNS address works. Email bpitzel
// if it fails.
HX_ASSERT( !strDNS.IsEmpty() );
#endif
}
#ifndef _WIN16
void CHXNetCheck::GetNameServerKey(HKEY hKey, const char* szKeyName, CHXString& strDNS)
{
char szDNS[MAX_PATH]; /* Flawfinder: ignore */
DWORD regType = 0 ;
DWORD cbuf;
cbuf = sizeof(szDNS);
if (ERROR_SUCCESS == RegQueryValueEx(hKey, OS_STRING(szKeyName), NULL, ®Type, (BYTE *) szDNS, &cbuf))
{
strDNS = szDNS;
// Win95/98 separate multiple DNS addresses with ","
// WinNT separate multiple DNS addresses with ","
// We will grab the first IP address in the list
strDNS = strDNS.SpanExcluding(" ,");
strDNS.TrimLeft();
strDNS.TrimRight();
}
}
#endif /* _WIN16 */
BOOL
CHXNetCheck::SmartPing()
{
using namespace NetDetectLog;
WRITE_LOG0("SmartPing fc entered ...");
UINT16 nPort = DEF_HTTP_PORT;
// try to get DNS address to ping.
CHXString strPingAddr;
GetDNSAddress(strPingAddr);
nPort = DEF_DNS_PORT;
// No DNS address? Default to a known web server.
// XXXBJP pinging video.real.com, used in Beta 1, should be
// changed for Beta 2!!
if (strPingAddr.IsEmpty())
{
strPingAddr = "209.66.98.23"; // video.real.com .. UGHHHH!!
nPort = DEF_HTTP_PORT;
}
return (Ping(strPingAddr, nPort, FALSE));
}
BOOL
CHXNetCheck::FNetCardActive()
{
#if defined(_WIN16) || defined(WIN32_PLATFORM_PSPC)
return TRUE;
#else
char szEnum_Name[ENUM_MAX][20] = {REGSTR_KEY_ROOTENUM, REGSTR_KEY_BIOSENUM, /* Flawfinder: ignore */
REGSTR_KEY_PCIENUM, REGSTR_KEY_ISAENUM, REGSTR_KEY_EISAENUM,
REGSTR_KEY_PCMCIAENUM};
using namespace NetDetectLog;
WRITE_LOG0("FNetCardActive entered ...");
char szClass[64]; /* Flawfinder: ignore */
ULONG ulType;
ULONG32 cbData;
CHXString sCurrentKey;
CHXString sCurrentFiles;
CHXString sDeviceID;
char szCurrentDeviceNode[STRINGSIZE]; /* Flawfinder: ignore */
char szCurrentDevice[STRINGSIZE]; /* Flawfinder: ignore */
char szSoftwareKey[STRINGSIZE]; /* Flawfinder: ignore */
BOOL bFoundKey = FALSE;
int i;
OSVERSIONINFO osv;
memset(&osv, 0, sizeof(osv));
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx(&osv);
if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
return FALSE;
using namespace NetDetectLog;
WRITE_LOG0("Win95/98 : Looking for devices of the Net class...");
// we want to look through the ENUM\BIOS, ENUM\ISAPNP, and ENUM\PCI
// trees for devices of the Net class. For each display device
// we find we will ask the Device manager if it is active.
for (i = 0; i < ENUM_MAX && !bFoundKey; i++)
{
HKEY hBusKey = 0;
sCurrentKey = (CHXString)REGSTR_KEY_ENUM + BACKSLASH + szEnum_Name[i];
// Start reading the devices
if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, OS_STRING(sCurrentKey), 0, KEY_READ, &hBusKey))
{
cbData = sizeof(szCurrentDeviceNode);
ULONG32 dwDevNodeEnumIndex = 0;
HKEY hDeviceNodeKey;
// Enumerate all of the devices on the bus
while (!bFoundKey &&
!RegEnumKeyEx(hBusKey, dwDevNodeEnumIndex,
OS_STRING2(szCurrentDeviceNode, cbData),
&cbData, NULL, NULL, NULL, NULL))
{
// get the registry key for the current device node
hDeviceNodeKey = 0;
sCurrentKey = (CHXString)REGSTR_KEY_ENUM + BACKSLASH + szEnum_Name[i] + BACKSLASH + (CHXString)szCurrentDeviceNode;
if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, OS_STRING(sCurrentKey), 0, KEY_READ, &hDeviceNodeKey))
{
cbData = sizeof(szCurrentDevice);
ULONG32 dwHWDeviceEnumIndex = 0;
HKEY hHWDeviceKey;
// enumerate all of the hardware devices in this Device Node
while (!bFoundKey &&
!RegEnumKeyEx(hDeviceNodeKey, dwHWDeviceEnumIndex,
OS_STRING2(szCurrentDevice, cbData),
&cbData, NULL, NULL, NULL, NULL))
{
// get the registry key for the current hardware device
int nBaseKeyLength = sCurrentKey.GetLength();
hHWDeviceKey = 0;
sCurrentKey += BACKSLASH + (CHXString) szCurrentDevice;
if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, OS_STRING(sCurrentKey), 0, KEY_READ, &hHWDeviceKey))
{
// Ask if this device is a "Net" device by checking it's class
cbData = sizeof(szClass);
szClass[0] = 0;
if (!RegQueryValueEx(hHWDeviceKey, OS_STRING("Class"), 0, &ulType, (LPBYTE) szClass, &cbData)
&& !stricmp(szClass, "Net"))
{
// if it is a display device then we want to find out if it is
// the active device for this class.
cbData = sizeof(szSoftwareKey);
sDeviceID = (CHXString) szEnum_Name[i] + BACKSLASH + (CHXString)szCurrentDeviceNode
+ BACKSLASH + (CHXString) szCurrentDevice;
// get the registry key name for the software key for the device, if it is not
// null and this is the active device then we are done.
if (!RegQueryValueEx(hHWDeviceKey, OS_STRING("Driver"), 0, &ulType, (LPBYTE) szSoftwareKey, &cbData)
&& szSoftwareKey[0] != '0'
&& DevNodeIsActive((sDeviceID))) // Call Config Manager in 16-bit code
{
bFoundKey = TRUE;
WRITE_LOG1("Found active node %s...", (char const*) sDeviceID);
}
}
}
// reset the key to the base for the next time through the loop
sCurrentKey = sCurrentKey.Left(nBaseKeyLength);
if (hHWDeviceKey)
{
RegCloseKey(hHWDeviceKey);
hHWDeviceKey = 0;
}
// move to next device key
dwHWDeviceEnumIndex++;
// reset size to szCurrentDevice size
cbData = sizeof(szCurrentDevice);
}
}
if (hDeviceNodeKey)
{
RegCloseKey(hDeviceNodeKey);
hDeviceNodeKey = 0;
}
// move to the next device node
dwDevNodeEnumIndex++;
// reset the size to szCurrentDeviceNode size
cbData = sizeof(szCurrentDeviceNode);
}
}
if (hBusKey)
{
RegCloseKey(hBusKey);
hBusKey = 0;
}
}
return bFoundKey;
#endif
}
BOOL
CHXNetCheck::DevNodeIsActive(const char *szDeviceID)
{
#ifdef _WIN16
HX_ASSERT (FALSE); // should never get called in win16
return TRUE;
#else
HINSTANCE hDevNodeInst;
DWORD dwStatus;
DWORD dwProblemNumber;
DWORD cr;
DWORD (WINAPI * pGetDevNodeStatus32Call) (const char *, LPDWORD, LPDWORD);
// 10 is a magic number for the configuration manager api
// that eventually gets called in the 16 bit dll.
dwStatus = dwProblemNumber = cr = 10;
pGetDevNodeStatus32Call = 0;
hDevNodeInst = LoadLibrary(OS_STRING(_32BIT_DLLNAME));
// add this code if this ever gets into win16; win16 will return nonnull on error from load library #if _WIN16 if (m_handle < HINSTANCE_ERROR) m_handle = NULL; #endif
if (hDevNodeInst)
{
(FARPROC &) pGetDevNodeStatus32Call = GetProcAddress(hDevNodeInst, OS_STRING("GetDevNodeStatus32Call"));
if (pGetDevNodeStatus32Call)
{
cr = pGetDevNodeStatus32Call(szDeviceID, &dwStatus, &dwProblemNumber);
}
FreeLibrary(hDevNodeInst);
}
if (cr == 0
&& dwProblemNumber == 0)
return TRUE;
else
return FALSE;
#endif
}
HX_RESULT CHXNetCheck::WinInetGetConnected(BOOL& bConnected)
{
using namespace NetDetectLog;
WRITE_LOG0("WinInetGetConnected fc entered...");
HINSTANCE hWinInet;
BOOL (WINAPI * pInternetGetConnectedState) (LPDWORD lpdwFlags, DWORD dwReserved);
hWinInet= LoadLibrary(OS_STRING(WININET_DLL));
if (!hWinInet)
{
WRITE_LOG0("WinInet not present ...");
return HXR_FAIL;
}
(FARPROC &) pInternetGetConnectedState = GetProcAddress(hWinInet, OS_STRING("InternetGetConnectedState"));
if (!pInternetGetConnectedState )
{
WRITE_LOG0("InternetGetConnectedState fc cannot be loaded ...");
FreeLibrary(hWinInet);
return HXR_FAIL;
}
DWORD dwFlags;
bConnected = pInternetGetConnectedState(&dwFlags, 0);
FreeLibrary(hWinInet);
WRITE_LOG_WINET(bConnected, dwFlags);
return HXR_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -