Sean O'Connor's
 Windows Games
 
Conquer the island
Slay
Board game
Conquest
Command a squadron of spaceships
Critical Mass
Board game of bluffing
The General
Conquer the world
Mother Of All Battles
Real time WWII battle
Firefight
Capture The Flag
Capture The Flag
A card game
Niggle
Lead your tribe to safety
End Of Atlantis
Play in the Jurassic League
Football-o-
saurus

Kill all of the rats
Rats!
Play Foosball
Foosball
Collect the crystals
UFOs

The Games
Slay
Conquest
Critical Mass
The General
Mother Of All Battles
Firefight
Capture The Flag
Niggle
End Of Atlantis
Football-o-saurus
Rats!
Foosball
UFOs

Info
arrowLatest news
arrowDownload free versions
arrowBuy full versions
arrowHistory of the games
arrowLink to this site
arrowBecome an affiliate
arrowOther sites
arrowUseful code
arrowThanks to...

Contact
arrowForums
arrowJoin mailing list
arrowEmail me

About Sean O'Connor
arrowCV
arrowOther Projects
arrowPhotos

Running a script on a webserver using WinInet Asynchronously

This code will run a script on a webserver (eg Perl, PHP etc...) asynchronously so your program will not freeze while waiting for a response from the webserver. It does not read back any output from the script and will ignore what you've asked it to run if it is still busy dealing with your last request.
			
HINTERNET	hInternetSession = NULL;   
HINTERNET	hURL = NULL;

typedef struct
{
    HWND        hWindow;     // window handle
    HINTERNET   hResource;   // HINTERNET handle created by InternetOpenUrl
} REQUEST_CONTEXT;

REQUEST_CONTEXT	request_context;

void __stdcall InternetCallbackFunction(HINTERNET hInternet,
                        DWORD dwContext,
                        DWORD dwInternetStatus,
                        LPVOID lpvStatusInformation,
                        DWORD dwStatusInformationLength)
{
	REQUEST_CONTEXT *cpContext;
	INTERNET_ASYNC_RESULT* res;

	cpContext = (REQUEST_CONTEXT*)dwContext;

	// what has this callback function been told has happened?
	switch (dwInternetStatus)
	{
		case INTERNET_STATUS_HANDLE_CREATED:
			// get the handle now that it has been created so it can be freed up later
			res = (INTERNET_ASYNC_RESULT*)lpvStatusInformation;
			hURL = (HINTERNET)(res->dwResult);

			break;

		case INTERNET_STATUS_REQUEST_COMPLETE:
			// script has been called so close handles now and cancel the callback function
			InternetCloseHandle(hURL);
			InternetSetStatusCallback(hInternetSession, NULL);
			InternetCloseHandle(hInternetSession);

			// flag as having been completed now
			hURL = NULL;
			hInternetSession = NULL;

			break;
	}
}


void RunScript(char* szURL)
{
	// only run this script if finished all others
	if (hInternetSession == NULL)
	{
		hInternetSession = InternetOpen("Microsoft Internet Explorer",
						INTERNET_OPEN_TYPE_PRECONFIG,
						NULL,
						NULL,
						INTERNET_FLAG_ASYNC);
		
		if (hInternetSession != NULL)
		{
			// set the callback function
			InternetSetStatusCallback(hInternetSession,
						(INTERNET_STATUS_CALLBACK)InternetCallbackFunction);

			// run the script
			hURL = InternetOpenUrl(hInternetSession,
						szURL,
						NULL,
						0,
						INTERNET_FLAG_RELOAD |
						INTERNET_FLAG_PRAGMA_NOCACHE |
						INTERNET_FLAG_NO_CACHE_WRITE,
						(unsigned long)(&request_context));
		}
	}
}