Click here to Skip to main content
Licence CPOL
First Posted 24 Aug 2007
Views 23,669
Bookmarked 16 times

Using WinInet to Call a Server Script Asynchronously

By | 24 Aug 2007 | Article
Using WinInet to call a server script asynchronously

Introduction

I wanted to find a simple way of running a script (e.g. Perl, PHP, etc.) on a webserver but it needed to be asynchronous as I didn't want the application to hang while waiting for the webserver's response. I didn't need to read any output from the script so all the solutions I could find were overly complicated for my needs.

Theory

  1. Use INTERNET_FLAG_ASYNC to open the session
  2. Set a status callback using InternetSetStatusCallback
  3. Open the connection using InternetOpenUrl
  4. Read the handle when the callback function receives INTERNET_STATUS_HANDLE_CREATED
  5. Free up everything when the call has finished and the callback function receives INTERNET_STATUS_REQUEST_COMPLETE

Using the Code

The complete code is:

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));
        }
    }
}   

History

  • 24th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sean OConnor

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmemberbadwei19:16 22 Aug '10  
General997 error Pinmemberdelinhabit2:05 6 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar21:15 18 May '08  
GeneralRe: 997 error Pinmemberdelinhabit23:49 18 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar23:59 18 May '08  
GeneralRe: 997 error Pinmemberdelinhabit0:18 19 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar0:42 19 May '08  
GeneralRe: 997 error Pinmemberdelinhabit0:55 19 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar1:36 19 May '08  
GeneralRe: 997 error PinmemberAnandtv2:31 28 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar2:57 28 May '08  
AnswerRe: 997 error PinmemberAnandtv20:04 28 May '08  
GeneralRe: 997 error PinmemberShilpi Boosar20:44 28 May '08  
GeneralExactly what I was looking for! PinmemberGilad Novik7:24 27 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Aug 2007
Article Copyright 2007 by Sean OConnor
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid