gpt4 book ai didi

c++ - The parameter is incorrect 错误与 InternetReadFileEx

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:24 27 4
gpt4 key购买 nike

所以我尝试从我使用 WinInet 函数的网站中的文件中读取,所以我能够成功执行所有 InternetConnect() 函数和其他操作,但是当我尝试执行 InternetReadFileExA() 时,我不断收到错误消息87 这意味着参数不正确,当然它没有告诉我哪个参数不正确所以我不知道如何修复它,我认为它可能是我设置为 NULL 的函数中的第 4 个参数,因为像往常一样 Microsoft 从不告诉您如何获取特定值,它只是说“调用者提供的用于异步操作的上下文值”。它没有告诉我应该使用什么值。谁能告诉我我做错了什么以及如何解决?这是我的代码

HANDLE fileToSend;
HINTERNET iNetOpenHandle = InternetOpen(L"FileTransfer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);

using namespace std;

int main()
{
HINTERNET connectHandle = InternetConnect(iNetOpenHandle, L"IMhidingTheIpHere", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_NO_CALLBACK, NULL);

PCWSTR acceptTypes[2] = {L"text/html", NULL};
HINTERNET httpReq = HttpOpenRequest(connectHandle, NULL, L"Tutorials.html", NULL, L"http://www.dominihq.hoxty.com/Tutorials.html", acceptTypes, INTERNET_FLAG_RELOAD, NULL);



if (httpReq != NULL) {
cout << "Opened http request successfully" << endl;
}
else {
cout << "Could not open http request" << endl;
int errCode = GetLastError();
cout << "Error Code: " << errCode << endl;
}

INTERNET_BUFFERSA iNetBuffer;
bool readFileStatus = InternetReadFileExA(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

if (readFileStatus == true) {
cout << "ReadFile completed successfully!" << endl;
cout << "Data: " << iNetBuffer.lpvBuffer << endl;
}
else {
cout << "Could not read file" << endl;
int errCode = GetLastError();
cout << "Error Code: " << errCode << endl;
}

system("Pause");
return 0;
}

这里是错误的地方

bool readFileStatus = InternetReadFileExA(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

最佳答案

如果我们获取您的代码,修复缺失的包含,并粘贴到 online Visual Studio compiler

#include <Windows.h>
#include <Wininet.h>
#include <WinSock.h>
#include <iostream>

HANDLE fileToSend;
HINTERNET iNetOpenHandle = InternetOpen(L"FileTransfer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);

using namespace std;

int main()
{
HINTERNET connectHandle = InternetConnect(iNetOpenHandle, L"IMhidingTheIpHere", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_NO_CALLBACK, NULL);

PCWSTR acceptTypes[2] = {L"text/html", NULL};
HINTERNET httpReq = HttpOpenRequest(connectHandle, NULL, L"Tutorials.html", NULL, L"http://www.dominihq.hoxty.com/Tutorials.html", acceptTypes, INTERNET_FLAG_RELOAD, NULL);



if (httpReq != NULL) {
cout << "Opened http request successfully" << endl;
}
else {
cout << "Could not open http request" << endl;
int errCode = GetLastError();
cout << "Error Code: " << errCode << endl;
}

INTERNET_BUFFERSA iNetBuffer;
bool readFileStatus = InternetReadFileExA(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

if (readFileStatus == true) {
cout << "ReadFile completed successfully!" << endl;
cout << "Data: " << iNetBuffer.lpvBuffer << endl;
}
else {
cout << "Could not read file" << endl;
int errCode = GetLastError();
cout << "Error Code: " << errCode << endl;
}

system("Pause");
return 0;
}

我们可以看到默认的项目设置给了我们一些与字符类型转换相关的警告——你指定的是 L"." 而不是 _T("...") 只有在我们使用 Unicode/MBCS 时才可以。

source_file.cpp(7): error C2664: 'HINTERNET InternetOpenA(LPCSTR,DWORD,LPCSTR,LPCSTR,DWORD)': cannot convert argument 1 from 'const wchar_t [13]' to 'LPCSTR'
source_file.cpp(7): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
source_file.cpp(13): error C2664: 'HINTERNET InternetConnectA(HINTERNET,LPCSTR,INTERNET_PORT,LPCSTR,LPCSTR,DWORD,DWORD,DWORD_PTR)': cannot convert argument 2 from 'const wchar_t [18]' to 'LPCSTR'
source_file.cpp(13): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
source_file.cpp(16): error C2664: 'HINTERNET HttpOpenRequestA(HINTERNET,LPCSTR,LPCSTR,LPCSTR,LPCSTR,LPCSTR *,DWORD,DWORD_PTR)': cannot convert argument 3 from 'const wchar_t [15]' to 'LPCSTR'
source_file.cpp(16): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

这告诉我们您遇到了字符宽度问题,果然如此

INTERNET_BUFFERSA iNetBuffer;
bool readFileStatus = InternetReadFileExA(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

您正在指定一个字符类型结构和函数。

如果我们启用 Unicode 和 remove your 'A' specifiers

    INTERNET_BUFFERS iNetBuffer;
bool readFileStatus = InternetReadFileEx(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

然后程序编译(它不会在 rextester 上链接,因为我没有指定要包含的库)。

我在任何时候都无法使用 VS2015、VS2013 或 VS2010 重现您报告的模棱两可的错误,但后来我展开了描述框,以便我可以阅读所有内容。

  • 避免使用 L"" 除非您尝试编写可移植代码,在这种情况下您无论如何都不会使用 Windows 特定调用。使用 _T("...") 代替

    InternetOpen(_T("FileTransfer"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);

  • 避免使用 A/W 后缀,除非您绝对必须这样做(例如,您只能编写函数来获取“char*”指针)

    INTERNET_BUFFERS iNetBuffer;
    bool readFileStatus = InternetReadFileEx(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

请注意,Microsoft 的 BOOL 类型与 bool 不同,因此您可能要考虑将最后一行写为

    BOOL readFileStatus = InternetReadFileEx(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

    auto readFileStatus = InternetReadFileEx(httpReq, &iNetBuffer, WININET_API_FLAG_ASYNC, NULL);

关于c++ - The parameter is incorrect 错误与 InternetReadFileEx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39810936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com