Hi all.
I wrote some code that will check if a message has been posted in the forum. It will check the data.txt file every 30 minutes to see if the number has changed. The code is based on this cp article [^]
Note that this code is a very quick hack so expect bugs! It works for me though...
<br />
<br />
#include <windows.h><br />
#include <wininet.h><br />
#include <stdio.h><br />
#include <stdlib.h><br />
<br />
typedef BOOL (__stdcall * LPFN_INETAUTODIAL)(DWORD, HWND);<br />
typedef BOOL (__stdcall * LPFN_INETREADFILE)(LPVOID, LPVOID, DWORD, LPDWORD);<br />
typedef BOOL (__stdcall * LPFN_INETAUTOHANGUP)(DWORD);<br />
typedef BOOL (__stdcall * LPFN_INETCLOSEHANDLE)(LPVOID);<br />
typedef BOOL (__stdcall * LPFN_INETGETCONNSTATE)(LPDWORD, DWORD);<br />
typedef LPVOID (__stdcall * LPFN_INETOPEN)(LPCSTR, DWORD, LPCSTR, LPCSTR, DWORD);<br />
typedef LPVOID (__stdcall * LPFN_INETOPENURL)(LPVOID, LPCSTR, LPCSTR, DWORD, DWORD, DWORD);<br />
<br />
HINSTANCE hWinInet;<br />
bool bDLLFailure;<br />
HINTERNET hInternetSession;<br />
HINTERNET hURL;<br />
char szTempName[MAX_PATH];<br />
DWORD m_LastKnownAmount;<br />
<br />
LPFN_INETGETCONNSTATE Inet_GetConnectedState;<br />
LPFN_INETAUTODIAL Inet_AutoDial;<br />
LPFN_INETOPEN Inet_Open;<br />
LPFN_INETCLOSEHANDLE Inet_CloseHandle;<br />
LPFN_INETOPENURL Inet_OpenUrl;<br />
LPFN_INETREADFILE Inet_ReadFile;<br />
LPFN_INETAUTOHANGUP Inet_AutoHangup;<br />
<br />
int RegReadDWord( HKEY hkey, LPTSTR name, DWORD *pval);<br />
DWORD RegReadDWord(LPCTSTR key, LPCTSTR name, DWORD defaultVal, HKEY hParent = HKEY_LOCAL_MACHINE);<br />
int RegWriteDWord(HKEY hkey, LPTSTR name, DWORD val);<br />
BOOL RegWriteDWord(LPCTSTR hKey, LPCTSTR name, DWORD value, HKEY hKeyParent = HKEY_LOCAL_MACHINE);<br />
<br />
void ErrorHandler (char * szMessage)<br />
{<br />
::MessageBox (NULL, szMessage, "Checking Forum", MB_OK | MB_ICONINFORMATION);<br />
}<br />
<br />
bool ConnectToWeb (char * szAgent)<br />
{<br />
DWORD dwFlags;<br />
<br />
if (!Inet_GetConnectedState (&dwFlags, 0)) {<br />
if (!Inet_AutoDial (0, NULL)) {<br />
ErrorHandler ("This application must connect to the Internet to check the forum.");<br />
return false;<br />
}<br />
}<br />
<br />
hInternetSession = Inet_Open (szAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);<br />
if (hInternetSession == NULL) {<br />
ErrorHandler ("Cannot connect to the Internet."); <br />
return false;<br />
}<br />
<br />
return true;<br />
}<br />
<br />
bool DisconnectFromWeb ()<br />
{<br />
Inet_CloseHandle (hInternetSession);<br />
<br />
Inet_AutoHangup (0);<br />
<br />
return true;<br />
}<br />
<br />
bool RetrieveFileFromWeb (char * szUrl, char * szFileName)<br />
{<br />
DWORD dwBytesRead, dwBytesWritten;<br />
char szFileBuffer[4096];<br />
HANDLE hTempFile;<br />
char * szBuffer;<br />
<br />
szBuffer = new char [strlen (szUrl) + strlen (szFileName) + 2];<br />
if (szBuffer == NULL) {<br />
ErrorHandler ("Cannot allocate memory to buffer!"); <br />
return false;<br />
}<br />
<br />
sprintf (szBuffer, "%s/%s", szUrl, szFileName);<br />
<br />
hURL = Inet_OpenUrl (hInternetSession, szBuffer, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0, 0);<br />
if (hURL == NULL) {<br />
ErrorHandler ("Cannot connect to forum URL.");<br />
delete[] szBuffer;<br />
return false;<br />
}<br />
<br />
char* pBuf = new char[MAX_PATH];<br />
GetTempPath(MAX_PATH, pBuf);<br />
GetTempFileName (pBuf, "WU_", 0, szTempName);<br />
delete[] pBuf;<br />
hTempFile = CreateFile ((LPTSTR) szTempName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);<br />
<br />
if (hTempFile == INVALID_HANDLE_VALUE) {<br />
ErrorHandler ("Cannot create a temporary forum check file.");<br />
delete[] szBuffer;<br />
return false;<br />
}<br />
<br />
do {<br />
Inet_ReadFile (hURL, (LPSTR) szFileBuffer, (DWORD) 1024, &dwBytesRead);<br />
<br />
if (dwBytesRead > 0) WriteFile (hTempFile, (PVOID) szFileBuffer, dwBytesRead, &dwBytesWritten, NULL);<br />
} while (dwBytesRead > 0);<br />
<br />
CloseHandle (hTempFile);<br />
<br />
Inet_CloseHandle (hURL);<br />
<br />
return true;<br />
}<br />
<br />
bool ParseVersionFile (char * szVersionFile)<br />
{<br />
DWORD dwBytesRead, dwSize;<br />
int nCounter = 0;<br />
char * szBuffer;<br />
HANDLE hVersionFile;<br />
<br />
hVersionFile = CreateFile (szVersionFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);<br />
if (hVersionFile == INVALID_HANDLE_VALUE) {<br />
ErrorHandler ("Cannot open downloaded version file."); <br />
return false;<br />
}<br />
<br />
dwSize = GetFileSize (hVersionFile, NULL);<br />
<br />
szBuffer = new char [dwSize];<br />
if (szBuffer == NULL) {<br />
ErrorHandler ("Cannot allocate memory for buffer.");<br />
return false;<br />
}<br />
<br />
memset(szBuffer, 0, dwSize);<br />
if (!ReadFile (hVersionFile, szBuffer, dwSize, &dwBytesRead, NULL)) {<br />
ErrorHandler ("Cannot read from downloaded version file."); <br />
return false;<br />
}<br />
<br />
CloseHandle (hVersionFile);<br />
<br />
if (atol(szBuffer) != m_LastKnownAmount)<br />
{<br />
m_LastKnownAmount = atol(szBuffer);<br />
char msg[MAX_PATH];<br />
memset(&msg, 0, MAX_PATH);<br />
sprintf((char *)&msg, "New message! Total messages: %d.", m_LastKnownAmount);<br />
ErrorHandler(msg);<br />
HKEY hKey;<br />
DWORD disp = REG_OPENED_EXISTING_KEY;<br />
if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE,<br />
"SOFTWARE\\YOURKEY\\FCheck",<br />
0,<br />
"",<br />
REG_OPTION_NON_VOLATILE,<br />
KEY_ALL_ACCESS,<br />
NULL,<br />
&hKey,<br />
&disp) != ERROR_SUCCESS)<br />
{<br />
return FALSE;<br />
}<br />
::RegWriteDWord(hKey, "Forum Check", m_LastKnownAmount);<br />
}<br />
<br />
delete [] szBuffer;<br />
<br />
return true;<br />
}<br />
<br />
int APIENTRY WinMain(HINSTANCE hInstance,<br />
HINSTANCE hPrevInstance,<br />
LPSTR lpCmdLine,<br />
int nCmdShow)<br />
{<br />
HKEY hKey;<br />
RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\YOURKEY\\FCheck", &hKey);<br />
if (RegReadDWord(hKey, "Forum Check", &m_LastKnownAmount)<0)<br />
{<br />
m_LastKnownAmount = 60000;<br />
}<br />
RegCloseKey(hKey);<br />
<br />
bDLLFailure = false;<br />
<br />
hWinInet = LoadLibrary ("WinInet.Dll");<br />
if (hWinInet == NULL) {<br />
ErrorHandler ("Cannot load WinInet.Dll. Please make sure IE 4.0 or greater is installed!");<br />
return TRUE;<br />
}<br />
<br />
Inet_GetConnectedState = (LPFN_INETGETCONNSTATE) GetProcAddress (hWinInet, "InternetGetConnectedState");<br />
Inet_AutoDial = (LPFN_INETAUTODIAL) GetProcAddress (hWinInet, "InternetAutodial");<br />
Inet_Open = (LPFN_INETOPEN) GetProcAddress (hWinInet, "InternetOpenA");<br />
Inet_CloseHandle = (LPFN_INETCLOSEHANDLE) GetProcAddress (hWinInet, "InternetCloseHandle");<br />
Inet_OpenUrl = (LPFN_INETOPENURL) GetProcAddress (hWinInet, "InternetOpenUrlA");<br />
Inet_ReadFile = (LPFN_INETREADFILE) GetProcAddress (hWinInet, "InternetReadFile");<br />
Inet_AutoHangup = (LPFN_INETAUTOHANGUP) GetProcAddress (hWinInet, "InternetAutodialHangup");<br />
<br />
if (Inet_GetConnectedState == NULL) bDLLFailure = true;<br />
if (Inet_AutoDial == NULL) bDLLFailure = true;<br />
if (Inet_Open == NULL) bDLLFailure = true;<br />
if (Inet_CloseHandle == NULL) bDLLFailure = true;<br />
if (Inet_OpenUrl == NULL) bDLLFailure = true;<br />
if (Inet_ReadFile == NULL) bDLLFailure = true;<br />
if (Inet_AutoHangup == NULL) bDLLFailure = true;<br />
<br />
if (bDLLFailure == true) {<br />
ErrorHandler ("Cannot load WinInet.Dll. Please make sure IE 4.0 or greater is installed!");<br />
return TRUE;<br />
}<br />
<br />
if (bDLLFailure == true) {<br />
ErrorHandler ("IE 4.0 or greater has to be installed to use forum check!");<br />
return -1;<br />
}<br />
<br />
while (1)<br />
{<br />
if (!ConnectToWeb (forum checker"))<br />
{<br />
Sleep(5 * 60 * 1000);<br />
continue;<br />
}<br />
if (!RetrieveFileFromWeb ("http:
{<br />
DisconnectFromWeb();<br />
Sleep(5 * 60 * 1000);<br />
continue;<br />
}<br />
<br />
if (!ParseVersionFile (szTempName))<br />
{<br />
DisconnectFromWeb();<br />
Sleep(5 * 60 * 1000);<br />
continue;<br />
}<br />
<br />
if (!DisconnectFromWeb ())<br />
{<br />
Sleep(5 * 60 * 1000);<br />
continue;<br />
}<br />
<br />
DeleteFile(szTempName);<br />
Sleep(30 * 60 * 1000);<br />
}<br />
<br />
return 0;<br />
}<br />
<br />
<br />
<br />
<br />
int RegReadDWord( HKEY hkey, LPTSTR name, DWORD *pval)<br />
{<br />
DWORD var = 0;<br />
DWORD type = REG_DWORD;<br />
DWORD size = sizeof( DWORD);<br />
<br />
LONG retv = RegQueryValueEx( hkey,<br />
name,<br />
NULL,<br />
&type,<br />
(BYTE*)&var,<br />
&size);<br />
if ( retv == ERROR_SUCCESS && type == REG_DWORD)<br />
{<br />
*pval = var;<br />
return 0;<br />
}<br />
<br />
return -1;<br />
}<br />
<br />
<br />
<br />
DWORD RegReadDWord(LPCTSTR key, LPCTSTR name, DWORD defaultVal, HKEY hKeyParent)<br />
{<br />
HKEY hKey = NULL;<br />
DWORD result = defaultVal;<br />
if (RegOpenKeyEx(hKeyParent,<br />
(LPCTSTR)key,<br />
0,<br />
KEY_QUERY_VALUE,<br />
&hKey) == ERROR_SUCCESS) <br />
{<br />
<br />
DWORD type = REG_SZ;<br />
DWORD size = sizeof(DWORD);<br />
DWORD valueRead;<br />
if (RegQueryValueEx(hKey,<br />
name,<br />
NULL,<br />
&type,<br />
(BYTE *)&valueRead,<br />
&size) == ERROR_SUCCESS)<br />
{<br />
result = valueRead;<br />
}<br />
RegCloseKey(hKey);<br />
}<br />
return result;<br />
}<br />
<br />
<br />
int RegWriteDWord( HKEY hkey, LPTSTR name, DWORD val)<br />
{<br />
if ( RegSetValueEx( hkey, name, 0, REG_DWORD,<br />
(BYTE*)&val, sizeof( DWORD)) != ERROR_SUCCESS)<br />
return -1;<br />
<br />
return 0;<br />
}<br />
<br />
<br />
<br />
BOOL RegWriteDWord(LPCTSTR key, LPCTSTR name, DWORD value, HKEY hKeyParent)<br />
{<br />
HKEY hKey;<br />
DWORD disposition;<br />
if ( RegCreateKeyEx(hKeyParent,<br />
key,<br />
0,<br />
"",<br />
REG_OPTION_NON_VOLATILE,<br />
KEY_SET_VALUE,<br />
NULL,<br />
&hKey,<br />
&disposition) != ERROR_SUCCESS) <br />
{<br />
return FALSE;<br />
}<br />
if (RegSetValueEx(hKey,<br />
name, <br />
0,<br />
REG_DWORD,<br />
(const BYTE *)&value,<br />
sizeof(DWORD)) != ERROR_SUCCESS)<br />
{<br />
RegCloseKey(hKey);<br />
return FALSE;<br />
}<br />
<br />
RegCloseKey(hKey);<br />
return TRUE;<br />
}<br />
-----------------------
New and improved: kwakkelflap.com
My second CP article: MAP files[^]
while (!:bob:.IsDrunk())<br />
{<br />
:bob:.Drink( :beer: );<br />
}
|