c++ - 错误: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"

标签 c++ wchar

我有以下 WinHTTPRequest 代码:

DWORD WinHTTPRequest(LPCTSTR pServerName, LPCTSTR pRequest, WCHAR* sCommand, LPVOID pPostData, int nPostDataLength, LPCWSTR pwszHeaders, char **dataOut, int *nRead, WCHAR **dataHeaderOut, BOOL bTestProxy, BOOL bSecure, WCHAR* wsRedirect, DWORD *dwReturnStatus)
{
    HINTERNET hCurrentOpen = NULL;
    if (bTestProxy)
    {
        WCHAR sProxy[255] = L"";
        GetProxy(sProxy);
        if (lstrcmp(sProxy, L"") == 0)
            hCurrentOpen = hOpen;
        else if (lstrcmp(sProxy, g_wsCurrentProxy) != 0)
        {
            if (hOpenProxy)
                WinHttpCloseHandle(hOpenProxy);
            hOpenProxy = WinHttpOpen(L"Test", WINHTTP_ACCESS_TYPE_NAMED_PROXY, sProxy, NULL, 0/*INTERNET_FLAG_ASYNC*/);
            lstrcpy(g_wsCurrentProxy, sProxy);
            hCurrentOpen = hOpenProxy;
        }
        else
            hCurrentOpen = hOpenProxy;
    }
    else
        hCurrentOpen = hOpen;

    HINTERNET hConnect = NULL;
    if (bSecure)
        hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTPS_PORT, 0);
    else
        hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTP_PORT, 0);

    if (!hConnect)
    {
        DWORD dwError = GetLastError();
        return dwError;
    }

    DWORD dwFlags;
    if (bSecure)
        dwFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH;
    else
        dwFlags = WINHTTP_FLAG_REFRESH;

    HINTERNET hRequest = WinHttpOpenRequest(hConnect, sCommand, pRequest, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
    if (!hRequest)
    {
        DWORD dwError = GetLastError();
        WinHttpCloseHandle(hConnect);
        return dwError;
    }

    WinHttpAddRequestHeaders(hRequest, pwszHeaders, -1, WINHTTP_ADDREQ_FLAG_ADD);
    int nLengthPostData;
    if (nPostDataLength == NULL)
    {
        if (pPostData)
            nLengthPostData = strlen((char*)pPostData);
        else
            nLengthPostData = 0;
    }
    else
        nLengthPostData = nPostDataLength;

    BOOL bSuccess;
    if (wsRedirect != NULL)
    {
        DWORD dwOption;
        DWORD dwOptionSize;
        dwOption = WINHTTP_OPTION_REDIRECT_POLICY_NEVER;
        dwOptionSize = sizeof(DWORD);
        bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, (LPVOID)&dwOption, dwOptionSize);
        DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
        bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
    }
    BOOL b = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, pPostData, pPostData == NULL ? 0 : nLengthPostData, nLengthPostData, 0);
    if (!b)
    {
        DWORD dwError = GetLastError();
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hRequest);
        return dwError;
    }
    WinHttpReceiveResponse(hRequest, NULL);
    DWORD dwStatus = 0;
    DWORD dwStatusSize = sizeof(DWORD);
    if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatus, &dwStatusSize, NULL))
    {
        if (HTTP_STATUS_REDIRECT == dwStatus || HTTP_STATUS_MOVED == dwStatus)
        {
            DWORD dwSize;
            WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
            if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                return 500;
            LPWSTR pwsRedirectURL = new WCHAR[dwSize];
            bSuccess = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, pwsRedirectURL, &dwSize, WINHTTP_NO_HEADER_INDEX);
            if (!bSuccess)
                return 500;
            if (wsRedirect != NULL)
                lstrcpy(wsRedirect, pwsRedirectURL);
            if (dwReturnStatus != NULL)
                *dwReturnStatus = dwStatus;
            delete[] pwsRedirectURL;
        }
        else if (dwStatus != HTTP_STATUS_OK && dwStatus != HTTP_STATUS_BAD_REQUEST && dwStatus != HTTP_STATUS_CREATED)
        {
            DWORD dwError = GetLastError();
            WinHttpCloseHandle(hConnect);
            WinHttpCloseHandle(hRequest);
            if (dwReturnStatus != NULL)
                *dwReturnStatus = dwStatus;
            return dwError;
        }
    }
    if (dataHeaderOut != NULL)
    {
        DWORD dwSize = 0;
        WCHAR *pOutBuffer = NULL;
        if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX))
        {
            DWORD dwErr = GetLastError();
            if (dwErr != ERROR_INSUFFICIENT_BUFFER)
            {
                DWORD dwError = GetLastError();
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hRequest);
                return dwError;
            }
        }
        pOutBuffer = new WCHAR[dwSize];
        if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, pOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
        {
            pOutBuffer[dwSize] = '\0';
            *dataHeaderOut = (WCHAR*)pOutBuffer;
        }
        //delete[] pOutBuffer;
    }

    char *sReadBuffer = NULL;
    DWORD nTotalRead = 0;
    DWORD nToRead = 0;
    DWORD nBytesRead = 0;
    do {
        if (!WinHttpQueryDataAvailable(hRequest, &nToRead))
            break;
        if (nToRead == 0)
            break;
        sReadBuffer = (char*)((sReadBuffer == NULL) ? malloc(nToRead) : realloc(sReadBuffer, nTotalRead + nToRead + 1));
        if (WinHttpReadData(hRequest, sReadBuffer + nTotalRead, nToRead, &nBytesRead))
        {
            nTotalRead += nBytesRead;
        }
    } while (nToRead > 0);
    if (sReadBuffer != NULL && nTotalRead > 0)
    {
        {
            char *sBuffer = new char[nTotalRead + 1];
            memcpy(sBuffer, sReadBuffer, nTotalRead + 1);
            sBuffer[nTotalRead] = '\0';
            *dataOut = sBuffer;
        }
        free(sReadBuffer);
    }

    *nRead = nTotalRead;
    WinHttpCloseHandle(hConnect);
    WinHttpCloseHandle(hRequest);
    return ERROR_SUCCESS;
}

我将上述函数称为:

dwReturn = WinHTTPRequest(wsHostName, wsURLPathPost, L"POST", sPostData, NULL, wsAdditionalHeaders, &sHTTPData, &nDataRead, &wsDataHeader, 0, 0, wsRedirect, &dwStatus);

但是在L"POST"处,它给了我以下错误:

Error (active)  E0167   argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *" 

Error   C2664   'DWORD WinHTTPRequest(LPCTSTR,LPCTSTR,WCHAR *,LPVOID,int,LPCWSTR,char **,int *,WCHAR **,BOOL,BOOL,WCHAR *,DWORD *)': cannot convert argument 3 from 'const unsigned short [4]' to 'WCHAR *' 

我尝试将 L"POST" 更改为 _T("POST") 但没有成功。我该如何解决这个问题。谢谢

最佳答案

您将sCommand作为指向非常量的指针,这表明您计划修改它指向的缓冲区。但是您在那里传递了一个无法修改的字符串文字。要么使参数为常量,要么传递可修改的缓冲区。

关于c++ - 错误: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56641579/

相关文章:

c++ - C++ 集合中的迭代器

android - 为android平台编译NITE2

c++ - 使所有类方法调用同一个函数

c++ - 在 Qt 中将 WCHAR 转换为 QString

c++ - 打印此地址的值

c++ - 如何计算复杂矩阵的指数?

c++ - 使用 gcc 插件获取类注释

c++ - 如何声明 wchar_t 并稍后设置其字符串值?

visual-c++ - 为什么 _wtoi 函数中 wtoi 前面有一个下划线,而 ansi 版本是 atoi?

C - 如何避免变音符号/口音敏感问题