Click here to Skip to main content
12,697,076 members (25,740 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked

Using WinInet to Call a Server Script Asynchronously

, 24 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Using WinInet to call a server script asynchronously


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.


  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;   

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

REQUEST_CONTEXT    request_context;

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

    cpContext = (REQUEST_CONTEXT*)dwContext;

    // what has this callback function been told has happened?
    switch (dwInternetStatus)
            // 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);


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

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


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

            // run the script
            hURL = InternetOpenUrl(hInternetSession,
                        INTERNET_FLAG_RELOAD |
                        INTERNET_FLAG_PRAGMA_NOCACHE |
                        (unsigned long)(&request_context));


  • 24th August, 2007: Initial post


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
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
badwei22-Aug-10 20:16
memberbadwei22-Aug-10 20:16 
General997 error Pin
delinhabit6-May-08 3:05
memberdelinhabit6-May-08 3:05 
GeneralRe: 997 error Pin
Shilpi Boosar18-May-08 22:15
memberShilpi Boosar18-May-08 22:15 
GeneralRe: 997 error Pin
delinhabit19-May-08 0:49
memberdelinhabit19-May-08 0:49 
GeneralRe: 997 error Pin
Shilpi Boosar19-May-08 0:59
memberShilpi Boosar19-May-08 0:59 
GeneralRe: 997 error Pin
delinhabit19-May-08 1:18
memberdelinhabit19-May-08 1:18 
GeneralRe: 997 error Pin
Shilpi Boosar19-May-08 1:42
memberShilpi Boosar19-May-08 1:42 
GeneralRe: 997 error Pin
delinhabit19-May-08 1:55
memberdelinhabit19-May-08 1:55 
GeneralRe: 997 error Pin
Shilpi Boosar19-May-08 2:36
memberShilpi Boosar19-May-08 2:36 
Actually i am working on generating a suggestion list . When i write in english the same is phonetically converted into hindi or other indian langugaes and give the appropriate suggestion to me.
and i do not want user to wait for response so i use thread to send request and use syncronous connecion to get response and i use one condition if current buffer is same as response buffer than only the suggestion is display.It is done and working properly, but when i type very fast than it takes time for me to display suggestion list when i stop typing.
and this is because it wait for response. that's the reason i switch to asyncronous connection. Smile | :)
I think now i am clear.

Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

GeneralRe: 997 error Pin
Anandtv28-May-08 3:31
memberAnandtv28-May-08 3:31 
GeneralRe: 997 error Pin
Shilpi Boosar28-May-08 3:57
memberShilpi Boosar28-May-08 3:57 
AnswerRe: 997 error Pin
Anandtv28-May-08 21:04
memberAnandtv28-May-08 21:04 
GeneralRe: 997 error Pin
Shilpi Boosar28-May-08 21:44
memberShilpi Boosar28-May-08 21:44 
GeneralExactly what I was looking for! Pin
Gilad Novik27-Aug-07 8:24
memberGilad Novik27-Aug-07 8:24 

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

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

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