【问题标题】:WCHAR to String, how do i do it?WCHAR 到字符串,我该怎么做?
【发布时间】:2011-08-02 04:00:37
【问题描述】:
String* Adder::downloadUrl(String* url)
{
    DWORD dwSize = 0;
    LPVOID lpOutBuffer = NULL;
    BOOL  bResults = FALSE;
    HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0",
                             WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                             WINHTTP_NO_PROXY_NAME,
                             WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect( hSession, L"www.google.com",
                                   INTERNET_DEFAULT_HTTP_PORT, 0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                       NULL, WINHTTP_NO_REFERER,
                                       WINHTTP_DEFAULT_ACCEPT_TYPES,
                                       0);

    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                       WINHTTP_NO_ADDITIONAL_HEADERS,
                                       0, WINHTTP_NO_REQUEST_DATA, 0,
                                       0, 0);

    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);

    // First, use WinHttpQueryHeaders to obtain the size of the buffer.
    if (bResults)
    {
        WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
                             WINHTTP_HEADER_NAME_BY_INDEX, NULL,
                             &dwSize, WINHTTP_NO_HEADER_INDEX);

        // Allocate memory for the buffer.
        if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
        {
            lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

            // Now, use WinHttpQueryHeaders to retrieve the header.
            bResults = WinHttpQueryHeaders( hRequest,
                                       WINHTTP_QUERY_RAW_HEADERS_CRLF,
                                       WINHTTP_HEADER_NAME_BY_INDEX,
                                       lpOutBuffer, &dwSize,
                                       WINHTTP_NO_HEADER_INDEX);
        }
    }

    // Print the header contents.
    if (bResults)
        printf("Header contents: \n%S",lpOutBuffer);

    // Free the allocated memory.
    delete [] lpOutBuffer;

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n",GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);


    String* retOne;

    return retOne;
}

我想以字符串形式获得响应,我在 C# 中使用 dll,完全不懂 vc++,请提出一种方法。

String* retOne //如何得到响应;
返回 retOne;

更新

// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring &wstr)
{
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
    std::string strTo( size_needed, 0 );
    WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
    return strTo;
}

String* retOne = utf8_encode(lpOutBuffer);

给出错误:'utf8_encode':无法将参数 1 从 'LPVOID' 转换为 'const std::wstring

请不要发布建议使用 .net 库的 cmets。

【问题讨论】:

  • 我用过c#没用过c++,代码里所有的*和&:都让我头晕。
  • String* 和 std::string 不一样。您的 utf8_encode 函数看起来不错,但它创建的是 std::string 而不是 String*。我从来没有听说过 String 所以我建议你停止使用它并在你的程序中使用 std::string 。如果你不能这样做,那么你至少应该解释一下 String 是什么,它不是标准的 C++ 类型。
  • 我将 C++/CLI 系统字符串用于托管代码。

标签: c++ windows string visual-c++ managed


【解决方案1】:

【讨论】:

  • 仍然报错,请您提供更多详细信息,我收到错误,请查看上面的更新。
【解决方案2】:

这个过程的难点在于理解函数WideCharToMultiByte

为了获得整个字符串(并避免由于没有空终止字符串而导致垃圾),基本上你需要做的是首先使用:

int size_needed = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)lpOutBuffer, -1, NULL, 0, NULL, NULL);
char *stringMe = new char[size_needed + 1];

size_needed 将是 WideCharToMultiByte 函数中 lpMultiByteStr 所需的缓冲区大小。

之后您只需要再次使用该功能,因为现在您拥有完整的尺寸:

WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)lpOutBuffer, -1, stringMe, size_needed, NULL, NULL);
stringMe[size_needed + 1] = NULL;

现在你也可以将它转换为字符串:

std::string serverOutput(stringMe);

【讨论】:

    【解决方案3】:

    试试这个:

     String* retOne = utf8_encode(std::wstring(lpOutBuffer));
    

     String* retOne = utf8_encode(std::wstring((WCHAR*)lpOutBuffer));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2018-01-16
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多