Asynchronous call win32 internet functions

zhaozj2021-02-16  39

Time to visit the webpage for some time, using the CinternetSession easy to achieve synchronous access, but with asynchronous access is always popped up, it is very uncomfortable, and my program wants to achieve asynchronous access, search for a long time online, but It is not enough detailed, and the exploitation of people's exploits, finally successfully implemented asynchronous access to the web. . . Close this article for sharing of needs, if you need to present code, you can send it to Silver0001@163.com, I am free of the source code.

The following is briefly narrative step:

1. Establish Internet Handle with Internetopen

Hinternet Hinternet = Internetopen ("MyProgram / 1.0", pre_config_internet_access, 0,0, internet_flag_async)

2, call InternetSetStatusCallback to establish a callback function

Internet_Status_Callback Pret = InternetSetStatusCallback (M_HINTERNET, ONSTATUSCALLBACK);

3, call the Internetopenurl function to open a URL

Internetopenurl (Hinternet, PSTRURL, NULL, 0, Internet_flag_transfer_ascii, (dword) hWnd); here's HWND handle is a window handle for the message sent by the callback function

4, process the callback function

void AFXAPI OnStatusCallback (HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) {// AFX_MANAGE_STATE (AfxGetAppModuleState ()); HWND hwnd = (HWND) dwContext switch (dwInternetStatus) {case INTERNET_STATUS_CONNECTION_CLOSED: // Successfully closed the connection . to the server The lpvStatusInformation parameter is NULL if (IsWindow (hwnd)) PostMessage (m_hWndParent, WM_CONNECT_CLOSED, 0,0); break; case INTERNET_STATUS_REQUEST_COMPLETE:. {// Used by the Win32 API function InternetConnect to indicate that it has created the . new handle This lets the application call the Win32 function InternetCloseHandle from another thread if the connect is taking too long See the ActiveX SDK for more information about these functions INTERNET_ASYNC_RESULT * res = (INTERNET_ASYNC_RESULT *) lpvStatusInformation;.. HINTERNET hInet = (HINTERNET) (res-> dwresult); if (iswindow) PostMessage (hwnd, wm_request_c OMPLETE, (WPARAM) RES-> DWERROR, (LPARAM) Hinet;} Break; default: // Message ("Unknown Status:% D", dwinternetStatus; Break;}}

5, handle the WindowProc function in the window class with the HWND handle, define a variable in the header file.

Hinternet M_HINTERNET

TCHAR M_BUF [1024];

Internet_buffers IP;

Handling WindowProc functions in the CPP file

LRESULT CMainFrame :: WindowProc (UINT message, WPARAM wParam, LPARAM lParam) {// TODO: Add your specialized code here and / or call the base class switch (message) {case WM_CONNECT_CLOSED: {if (ip.dwBufferLength> 0) { m_buf [ip.dwbufferLength] = 0; ib.dwbuffertotal = ip.dwbufferLength; // Message (m_buf); // Here Storage or show m_buf;} trace ("- Bufferlen:% D, Total:% d --- - / n ", ib.dwbufferlength, ib.dwbuffertotal;

// Here Close TWO INTERNET HANDLE} Break; Case WM_Request_complete: {IF (WPARAM! = 0) {// Error Char BUF [255]; Sprintf (buf, "/ r / n ------ Error:% D ------ / r / n ", wparam; message (buf);} else {hinternet hinet = (hinternet) LParam; if (Hinet> 2) m_hinternet = Hinet; Else {// = 1, meaning last point network; trace ("- buffer:% d - / n", ib.dwbufferLength); if (ib.dwbufferLength> 0) {m_buf [ib.dwbufferlength] = 0; Trace "--- Receive Length:% D / N", Ib.dwBufferLength; Ib.dwBuffertotal = Ib.dwBufferLength; // Show or Storage M_BUF}

// clear m_buf; m_buf [0] = 0;}}} = 1; BOOL BOK;

While (ib.dwbufferlength! = 0) {MEMSET (m_buf, 0, sizeof); ib.lpvbuffer = m_buf; ib.dwbufferLength = SizeOf (m_buf) -1; bok = InternetReadFileEx (Hinet, & Ib, IRF_ASYNC, LPARAM) THIS-> M_HWND); if (! Bok && getlasterror () == Error_io_pending) {trace ("--------- pending --------"); Break;} IF (Bok && Ib.dwBufferLength! = 0) {m_buf [ib.dwbufferlength] = 0; trace ("--- Receive length:% D / N", Ib.dwBufferLength; Ib.dwBuffertotal = Ib.dwbufferLength; Message (m_buf); m_buf [0] = 0;}}}} Break;}

Return CFrameWnd :: WindowProc (Message, WPARAM, LPARAM);} 5, if you don't understand the above steps, or you can't contact me, welcome everyone.

转载请注明原文地址:https://www.9cbs.com/read-26760.html

New Post(0)