📄 asyncdemo.cpp
字号:
/* Microsoft Corporation Copyright 1999 - 2000 */
/********************************************************************
ProjectName : AsyncDemo
Purpose : This sample demonstrates how to submit two
WinInet requests, using InternentOpenUrl,
asynchronously.
Notes : This sample does not handle any authentication.
To properly handle authentication, the
functions that handle specific protocols (like
HttpOpenRequest/HttpSendRequest) would need to
be used instead of InternetOpenUrl.
Last Updated: February 2, 1998
*********************************************************************/
#include <windows.h>
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <wininet.h>
#include "resource.h"
//*******************************************************************
// Global Variable Declarations
//*******************************************************************
LPSTR lpszAgent = "Asynchronous WinInet Demo Program";
//root HINTERNET handle
HINTERNET hOpen;
//instance of the callback function
INTERNET_STATUS_CALLBACK iscCallback;
//structure to be passed as the dwContext value
typedef struct {
HWND hWindow; //main window handle
int nURL; //ID of the Edit Box w/ the URL
int nHeader; //ID of the Edit Box for the header info
int nResource; //ID of the Edit Box for the resource
HINTERNET hOpen; //HINTERNET handle created by InternetOpen
HINTERNET hResource; //HINTERNET handle created by InternetOpenUrl
char szMemo[512];//string to store status memo
HANDLE hThread; //thread handle
DWORD dwThreadID; //thread ID
} REQUEST_CONTEXT;
//two instances of the structure
static REQUEST_CONTEXT rcContext1, rcContext2;
HWND hButton;
//*******************************************************************
// Function Declarations
//*******************************************************************
//dialog box functions
BOOL CALLBACK AsyncURL(HWND, UINT, WPARAM, LPARAM);
//callback function
void __stdcall Juggler(HINTERNET, DWORD , DWORD , LPVOID, DWORD);
//thread function
DWORD Threader(LPVOID);
//functions
int WINAPI Dump(HWND, int, HINTERNET);
int WINAPI Header(HWND,int, int, HINTERNET);
void AsyncDirect (REQUEST_CONTEXT*, HINTERNET);
//*******************************************************************
// Main Program
//*******************************************************************
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR lpszArgs, int nWinMode)
{
//create the root HINTERNET handle using the systems default
//settings.
hOpen = InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, INTERNET_FLAG_ASYNC);
//check if the root HINTERNET handle has been created
if (hOpen == NULL)
{
return FALSE;
}
else
{
//sets the callback function
iscCallback = InternetSetStatusCallback(hOpen, (INTERNET_STATUS_CALLBACK)Juggler);
//creates the dialog box
DialogBox(hThisInst,"DB_ASYNCDEMO", HWND_DESKTOP,(DLGPROC)AsyncURL);
//closes the root HINTERNET handle
return InternetCloseHandle(hOpen);
}
}
//*******************************************************************
// Dialog Functions
//*******************************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BOOL CALLBACK AsyncURL(HWND hX, UINT message, WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
//change the cursor to indicate something is happening
SetCursor(LoadCursor(NULL,IDC_WAIT));
//set the default web sites
SetDlgItemText(hX,IDC_URL1,
LPSTR("http://www.microsoft.com"));
SetDlgItemText(hX,IDC_URL2,
LPSTR("http://home.microsoft.com"));
//initialize the first context value
rcContext1.hWindow = hX;
rcContext1.nURL = IDC_URL1;
rcContext1.nHeader = IDC_Header1;
rcContext1.nResource = IDC_Resource1;
sprintf(rcContext1.szMemo, "AsyncURL(%d)",
rcContext2.nURL);
//initialize the second context value
rcContext2.hWindow = hX;
rcContext2.nURL = IDC_URL2;
rcContext2.nHeader = IDC_Header2;
rcContext2.nResource = IDC_Resource2;
sprintf(rcContext2.szMemo, "AsyncURL(%d)",
rcContext2.nURL);
//change the cursor back to normal
SetCursor(LoadCursor(NULL,IDC_ARROW));
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_EXIT:
//change the cursor
SetCursor(LoadCursor(NULL,IDC_WAIT));
//close the dialog box
EndDialog(hX,0);
//return the cursor to normal
SetCursor(LoadCursor(NULL,IDC_ARROW));
return TRUE;
case IDC_Download:
hButton = GetDlgItem(hX, IDC_Download);
EnableWindow(hButton,0);
//reset the edit boxes
SetDlgItemText(hX,IDC_Resource1,LPSTR(""));
SetDlgItemText(hX,IDC_Resource2,LPSTR(""));
SetDlgItemText(hX,IDC_Header1,LPSTR(""));
SetDlgItemText(hX,IDC_Header2,LPSTR(""));
//start the downloads
AsyncDirect(&rcContext1, hOpen);
AsyncDirect(&rcContext2, hOpen);
return TRUE;
}
return FALSE;
case WM_DESTROY:
//change the cursor
SetCursor(LoadCursor(NULL,IDC_WAIT));
//close the dialog box
EndDialog(hX,0);
//return the cursor to normal
SetCursor(LoadCursor(NULL,IDC_ARROW));
return TRUE;
default:
return FALSE;
}
}
//*******************************************************************
// Other Functions
//*******************************************************************
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AsyncDirect handles the initial download request using
InternetOpenUrl.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void AsyncDirect (REQUEST_CONTEXT *prcContext, HINTERNET hOpen)
{
//dim a buffer to hold the URL
char szURL[256];
//retrieve the URL from the designated edit box
GetDlgItemText(prcContext->hWindow,prcContext->nURL,szURL,256);
//update the memo in the REQUEST_CONTEXT structure
sprintf(prcContext->szMemo, "AsyncDirect(%s)(%d):", szURL, prcContext->nURL);
//call InternetOpenUrl
prcContext->hResource = InternetOpenUrl(hOpen, szURL, NULL, 0, 0, (DWORD)prcContext);
//check the HINTERNET handle for errors
if (prcContext->hResource == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
//reuse the URL buffer for the error information
sprintf(szURL,"Error %d encountered.",GetLastError());
//write error to resource edit box
SetDlgItemText(prcContext->hWindow, prcContext->nResource,
szURL);
}
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Juggler is the callback function that is registered using
InternetSetStatusCallback.
Juggler displays the current callback in a list box with the ID,
IDC_CallbackList. The information displayed uses szMemo (which
contains the last function that was called), the
dwStatusInformationLength (to monitor the size of the information
being returned),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void __stdcall Juggler (HINTERNET hInternet, DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength)
{
REQUEST_CONTEXT *cpContext;
char szBuffer[256];
cpContext= (REQUEST_CONTEXT*)dwContext;
switch (dwInternetStatus)
{
case INTERNET_STATUS_CLOSING_CONNECTION:
//write the callback information to the buffer
sprintf(szBuffer,"%s: CLOSING_CONNECTION (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_CONNECTED_TO_SERVER:
//write the callback information to the buffer
sprintf(szBuffer,"%s: CONNECTED_TO_SERVER (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_CONNECTING_TO_SERVER:
//write the callback information to the buffer
sprintf(szBuffer,"%s: CONNECTING_TO_SERVER (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_CONNECTION_CLOSED:
//write the callback information to the buffer
sprintf(szBuffer,"%s: CONNECTION_CLOSED (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_HANDLE_CLOSING:
//write the callback information to the buffer
sprintf(szBuffer,"%s: HANDLE_CLOSING (%d)",
cpContext->szMemo, dwStatusInformationLength);
sprintf(cpContext->szMemo, "Closed");
//check if the both resource handles are closing
//if so, enable the download button.
if ((strcmp(rcContext1.szMemo,"Closed")) ||
(strcmp(rcContext2.szMemo,"Closed")))
{
hButton = GetDlgItem(cpContext->hWindow, IDC_Download);
EnableWindow(hButton,1);
}
break;
case INTERNET_STATUS_HANDLE_CREATED:
//write the callback information to the buffer
sprintf(szBuffer,"%s: HANDLE_CREATED (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
//write the callback information to the buffer
sprintf(szBuffer,"%s: INTERMEDIATE_RESPONSE (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_NAME_RESOLVED:
//write the callback information to the buffer
sprintf(szBuffer,"%s: NAME_RESOLVED (%d)",
cpContext->szMemo, dwStatusInformationLength);
break;
case INTERNET_STATUS_RECEIVING_RESPONSE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -