C-如何运行Winapi中使用InternetOpenfile()下载的.exe文件



我有一个使用winapi和一个我需要下载和执行的.exe文件编写的C应用程序。到目前为止,我的代码是:

if (NULL == (hRequest = HttpOpenRequest(hHTTP, "GET", "/~alexandru.antochi/exe_1.exe", NULL, NULL, rgpszAcceptTypes, NULL, INTERNET_FLAG_NO_COOKIES || INTERNET_FLAG_NO_AUTH)))
        {
            _error("HttpOpenRequest error.");
        }
        if (HttpSendRequest(hRequest, NULL, NULL, NULL, NULL))
        {
            if (InternetReadFile(hRequest, &buffer, 65536, &bytesRead))
            {
                if (bytesRead == 65536)
                {
                    printf("Warning: .exe file too big. Ignoring");
                    continue;
                }
            }
        }
        else
        {
            _error("Could not send HTTP request.");
        }
        closeHandles(2, hRequest, hHTTP);

我读了该文件,现在呢?如果我尝试将其写入本地文件,它将停在第一个定界符,这是在我的情况下为2个字母之后。该文件可以在http://students.info.uaic.ro/~alexandru.antochi/exe_1.exe

中找到。

您必须将EXE文件保存到本地文件,然后才能执行它。您不能从内存中执行EXE(如果不编写自己的EXE加载程序或使用第三方)。

InternetReadFile()读取任意数量的数据,因此您需要以循环为单位,直到达到响应结束为止。将收到的每个数据块写入您的本地文件。您声称您的写作"将停止在第一个定界符"中,这意味着您将收到的数据编写为无效的字符串,而不是原始二进制数据。二进制文件,尤其是可执行文件,其中包含大量0x00字节。因此,这是您需要修复的代码中的逻辑错误。

尝试更多这样的东西:

HINTERNET hInternet = InternetOpen(...);
if (!hInternet)
{
    _error("InternetOpen error.");
}
HINTERNET hHTTP = InternetConnect(hInternet, "students.info.uaic.ro", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hHTTP)
{
    InternetCloseHandle(hInternet);
    _error("InternetConnect error.");
}
const char* rgpszAcceptTypes[] = {"application/vnd.microsoft.portable-executable", "application/octet-stream", "application/x-msdownload", NULL};
HINTERNET hRequest = HttpOpenRequest(hHTTP, "GET", "/~alexandru.antochi/exe_1.exe", NULL, NULL, rgpszAcceptTypes, NULL, INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_AUTH);
if (!hRequest)
{
    InternetCloseHandle(hHTTP);
    InternetCloseHandle(hInternet);
    _error("HttpOpenRequest error.");
}
if (!HttpSendRequest(hRequest, NULL, NULL, NULL, NULL))
{
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hHTTP);
    InternetCloseHandle(hInternet);
    _error("Could not send HTTP request.");
}
DWORD statusCode;
DWORD size = sizeof(statusCode), index = 0;
if (!HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &statusCode, &size, &index))
{
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hHTTP);
    InternetCloseHandle(hInternet);
    _error("HttpQueryInfo error.");
}
if (statusCode != 200)
{
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hHTTP);
    InternetCloseHandle(hInternet);
    _error("HTTP request failed.");
}
HANDLE hFile = CreateFile("C:\path to\exe_1.exe" , GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL); 
if (hFile == INVALID_HANDLE_VALUE)
{
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hHTTP);
    InternetCloseHandle(hInternet);
    _error("Could not create local file.");
}
BYTE buffer[1024];
DWORD bytesRead, bytesWritten;
do
{
    if (!InternetReadFile(hRequest, buffer, sizeof(buffer), &bytesRead))
    {
        CloseHandle(hFile);
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hHTTP);
        InternetCloseHandle(hInternet);
        _error("Could not read HTTP response.");
    }
    if (bytesRead == 0)
        break;
    if (!WriteFile(hFile, buffer, bytesRead, &bytesWritten))
    {
        CloseHandle(hFile);
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hHTTP);
        InternetCloseHandle(hInternet);
        _error("Could not write to local file.");
    }
}
while (true);
CloseHandle(hFile);
InternetCloseHandle(hRequest);
InternetCloseHandle(hHTTP);
InternetCloseHandle(hInternet);
// use EXE file as needed...

有关更多详细信息,请参阅MSDN文档:

http Sessions

从www

下载资源

相关内容

  • 没有找到相关文章

最新更新