2011-08-02 8 views
1
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++はまったく分かりません。文字列へのWCHAR、どうすればよいですか?

文字列* retOne //応答を取得する方法。
return retOne;

UPDATE

// 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; 
} 

ストリング* = retOneはutf8_encode(lpOutBuffer)。

はエラーを与える:'はutf8_encodeは':「constのSTDに 'LPVOID' :: .NETライブラリの使用を示唆

投稿しないでくださいコメントをwstringのからパラメータ1を変換することはできません。

+0

私は使ったことがないのC#を使用していましたC++、すべての*と&::はコードでめまいを感じさせます。 –

+0

String *とstd :: stringは同じではありません。 utf8_encode関数はうまく見えますが、文字列*ではなくstd :: stringを作成しています。私はあなたのプログラムを使用してstd :: stringを使用することをお勧めしますので、文字列を聞いたことがない。もしそれができないのであれば、少なくともStringが何であるかを説明する必要があります。これは標準のC++型ではありません。 – jahhaj

+0

私はマネージコード用にC++/CLI System Stringを使用しています。 –

答えて

1
+0

はまだエラーが発生していますが、plsは詳細を提供できますか、エラーが発生しています。上記のアップデートをご覧ください。 –

0

はこれを試してみてください必要があるように見える:

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

または

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

いただきましたハードこのプロセスでは、関数にWideCharToMultiByteを理解することです。なりますをsize_needed

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

中:

は、基本的には文字列全体を取得(ヌル終端文字列を持っていないの結果としてゴミを避ける)ために何をする必要があるか最初の使用でありますWideCharToMultiByte機能では、lpMultiByteStrに必要なバッファサイズ。

今、あなたは完全な大きさを持っているので、その後、あなただけ、再びその機能を使用する必要があります。

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

今、あなたはまた、文字列に変換することができます:

std::string serverOutput(stringMe); 
関連する問題