Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFile

Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFileInternetOpen:Initializesanapplication'suseoftheWinINetfunctions.HINTERNETInternetOpen(_

大家好,又见面了,我是你们的朋友全栈君。

InternetOpen:

Initializes an application’s use of the WinINet functions.

HINTERNET InternetOpen(
  _In_ LPCTSTR lpszAgent,
  _In_ DWORD   dwAccessType,
  _In_ LPCTSTR lpszProxyName,
  _In_ LPCTSTR lpszProxyBypass,
  _In_ DWORD   dwFlags
);

Remarks

InternetOpen is the first WinINet function called by an application. It tells the Internet DLL to initialize internal data structures and prepare for future calls from the application. When the application finishes using the Internet functions, it should call InternetCloseHandle to free the handle and any associated resources.

 

InternetOpenUrl:

Opens a resource specified by a complete FTP or HTTP URL.

HINTERNET InternetOpenUrl(
  _In_ HINTERNET hInternet,
  _In_ LPCTSTR   lpszUrl,
  _In_ LPCTSTR   lpszHeaders,
  _In_ DWORD     dwHeadersLength,
  _In_ DWORD     dwFlags,
  _In_ DWORD_PTR dwContext
);

InternetReadFile:

Reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, or HttpOpenRequest function.

BOOL InternetReadFile(
  _In_  HINTERNET hFile,
  _Out_ LPVOID    lpBuffer,
  _In_  DWORD     dwNumberOfBytesToRead,
  _Out_ LPDWORD   lpdwNumberOfBytesRead
);

 

#include <windows.h>
#include <WinInet.h>
#include <iostream>
#pragma comment(lib,"wininet.lib")

using namespace std;

int DownLoader()
{
    DWORD dwByteRead = 0;
    char szDownBuffer[4096];
    memset(szDownBuffer, 0, 4096);
    BOOL bIntNetReadFile = TRUE;
    BOOL bWriteFile = TRUE;
    DWORD dwWritten = 0;

    HINTERNET hintInternetOpen = InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (!hintInternetOpen)
    {
        InternetCloseHandle(hintInternetOpen);
        return 1;
    }
    HINTERNET hintInternetOpenUrl = InternetOpenUrl(hintInternetOpen, "http://w.x.baidu.com/go/mini/2/10100", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    if (!hintInternetOpenUrl)
    {
        InternetCloseHandle(hintInternetOpen);
        InternetCloseHandle(hintInternetOpenUrl);
        return 1;
    }

    HANDLE hCreateFile = CreateFile("D:\\BaiDu2.exe", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (hCreateFile == INVALID_HANDLE_VALUE)
    {
        InternetCloseHandle(hintInternetOpen);
        InternetCloseHandle(hintInternetOpenUrl);
        return 1;
    }
    
    while (bIntNetReadFile && bWriteFile)
    {
        bIntNetReadFile = InternetReadFile(hintInternetOpenUrl, szDownBuffer, sizeof(szDownBuffer), &dwByteRead);
        if (!dwByteRead)
        {
            break;
        }

        bWriteFile = WriteFile(hCreateFile, szDownBuffer, sizeof(szDownBuffer), &dwWritten, NULL);
        if (!dwWritten)
        {
            break;
        }
    }
    
    InternetCloseHandle(hintInternetOpen);
    InternetCloseHandle(hintInternetOpenUrl);
    return 0;
}

int main()
{
    DownLoader();
    system("pause");
    return 0;
}

D盘下生成了下载的文件:

<span role="heading" aria-level="2">Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFile

 如果改为动态调用

#include <windows.h> #include <iostream> #include "dynamicfreetokill.h" using namespace std; int main() {  cout << hmWinINetDLL << endl; if (!hmWinINetDLL) { cout << "HMDOULE" << endl; } pfINTERNETCRACKURL pfInternetCrackUrl = (pfINTERNETCRACKURL)GetProcAddress(hmWinINetDLL, "InternetCrackUrlW"); cout << pfInternetCrackUrl << endl; if (!pfInternetCrackUrl) { cout << "InternetCrackUrlW" << endl; } pfINTERNETOPEN pfInternetOpen = (pfINTERNETOPEN)GetProcAddress(hmWinINetDLL, "InternetOpenW"); cout << pfInternetOpen << endl; if (!pfInternetOpen) { cout << "InternetOpenW" << endl; } pfINTERNETCONNECT pfInternetConnect = (pfINTERNETCONNECT)GetProcAddress(hmWinINetDLL, "InternetConnectW"); cout << pfInternetConnect << endl; if (!pfInternetOpen) { cout << "InternetConnectW" << endl; } pfHTTPOPENREQUEST pfHttpOpenRequest = (pfHTTPOPENREQUEST)GetProcAddress(hmWinINetDLL, "HttpOpenRequestW"); cout << pfHttpOpenRequest << endl; if (!pfInternetOpen) { cout << "HttpOpenRequestW" << endl; } pfINTERNETSETOPTION pfInternetSetOption = (pfINTERNETSETOPTION)GetProcAddress(hmWinINetDLL, "InternetSetOptionW"); cout << pfInternetSetOption << endl; if (!pfInternetOpen) { cout << "InternetSetOptionW" << endl; } pfHTTPSENDREQUEST pfHttpSendRequest = (pfHTTPSENDREQUEST)GetProcAddress(hmWinINetDLL, "HttpSendRequestW"); cout << pfHttpSendRequest << endl; if (!pfInternetOpen) { cout << "HttpSendRequestW" << endl; } pfHTTPQUERYINFO pfHttpQueryInfo = (pfHTTPQUERYINFO)GetProcAddress(hmWinINetDLL, "HttpQueryInfoW"); cout << pfHttpQueryInfo << endl; if (!pfInternetOpen) { cout << "HttpQueryInfoW" << endl; } pfINTERNETCLOSEHANDLE pfInternetCloseHandle = (pfINTERNETCLOSEHANDLE)GetProcAddress(hmWinINetDLL, "InternetCloseHandle"); cout << pfInternetCloseHandle << endl; if (!pfInternetOpen) { cout << "InternetCloseHandle" << endl; } pfINTERNETSETFILEPOINTER pfInternetSetFilePointer = (pfINTERNETSETFILEPOINTER)GetProcAddress(hmWinINetDLL, "InternetSetFilePointer"); cout << pfInternetSetFilePointer << endl; if (!pfInternetOpen) { cout << "InternetSetFilePointer" << endl; } pfINTERNETGETLASTRESPONSEINFO pfInternetGetLastResponseInfo = (pfINTERNETGETLASTRESPONSEINFO)GetProcAddress(hmWinINetDLL, "InternetGetLastResponseInfoW"); cout << pfInternetGetLastResponseInfo << endl; if (!pfInternetOpen) { cout << "InternetGetLastResponseInfoW" << endl; } pfINTERNETREADFILE pfInternetReadFile = (pfINTERNETREADFILE)GetProcAddress(hmWinINetDLL, "InternetReadFile"); cout << pfInternetReadFile << endl; if (!pfInternetOpen) { cout << "InternetReadFile" << endl; } FreeLibrary(hmWinINetDLL); system("pause"); return 0; }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155120.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • DDR2 ODT_ddr vtt电压

    DDR2 ODT_ddr vtt电压

    经常有人会说支持DDR2的主板存在偷工减料的现象。事实上这是由于DDR2内存中使用了一项新的ODT技术,它可以在提高内存信号稳定性的基础上节省不少电器元件(个人想法:ODT会增加功耗的阿)。主板终结是一种最为常见的终结主板内干扰信号的方法。在每一条信号传输路径的末端,都会安置一个终结电阻,它具备一定的阻值可以吸收反射回来的电子。但是目前DDR2内存的工作频率太高了,这种主板终结的方法并不能有效的阻止干扰信号。若硬要采用主板终结的方法得到纯净的DDR2时钟信号会花费巨额的制造成本。

  • 炸了!一口气问了我18个JVM问题!

    炸了!一口气问了我18个JVM问题!

    2020年11月20日
  • 项目活动图 – 举例说明

    项目活动图 – 举例说明关键路径:从开始到结束的所有路径中,时间最长的一条为关键路径。(特点:在关键路径上,所有任务的松弛时间都为0);松弛时间:第一种方法:最晚开始时间-最早开始时间;第二种方法:关键路径的总时间-包含该任务的任务路径花的时间;(前提条件:必须不影响该任务以后的任务路径)举例:关键路径:A-B-D-G-I-K-L=22由前向后推可得出…

  • linux命令mysql启动,linux下启动mysql的命令

    linux命令mysql启动,linux下启动mysql的命令linux下启动mysql的命令一、总结一下:1.linux下启动mysql的命令:mysqladminstart/ect/init.d/mysqlstart(前面为mysql的安装路径)2.linux下重启mysql的命令:mysqladminrestart/ect/init.d/mysqlrestart(前面为mysql的安装路径)3.linux下关闭mysql的命令:mysqla…

  • linux查看crontab状态_crontab -e

    linux查看crontab状态_crontab -elinux下定时执行任务的方法在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps-ef|grepcron]。cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间。cron的配置文件称为“crontab”,是“crontable”的简写。一、cron在3个地方查找配置文件:1、/var/spool/cron/这个目录下存放的是每个用户包括root的cron…

  • WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。「建议收藏」

    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。「建议收藏」WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号