我剛剛開始爲IIS7創建本地模塊。我正在使用來自http://msdn.microsoft.com/en-us/library/ms689348(v=vs.90).aspx的Hello World示例之一。寫在那裏的示例代碼工作正常。現在我正在嘗試更改此代碼以支持在瀏覽器中顯示的UTF-8編碼。我做了如下修改:UTF-8支持VC++的Web應用程序
//1. Changed here to include ;charset=UTF-8 in Http Header Content Type
pHttpResponse->SetHeader(HttpHeaderContentType, "text/plain;charset=UTF-8", (USHORT)strlen("text/plain;charset=UTF-8"), TRUE);
//2. Changed here data type as PCTSTR which is PCWSTR(16-bit) since I have UNICODE defined.
PCTSTR pszBuffer = L"Hello World!";
HTTP_DATA_CHUNK dataChunk;
dataChunk.DataChunkType = HttpDataChunkFromMemory;
DWORD cbSent;
dataChunk.FromMemory.pBuffer = (PVOID)pszBuffer;
//3. Changed here to calculate length using wcslen.
dataChunk.FromMemory.BufferLength = (USHORT)wcslen(pszBuffer);
hr = pHttpResponse->WriteEntityChunks(&dataChunk, 1, FALSE, TRUE, &cbSent);
我的第一個疑問是如何讓pszBuffer的UTF-8字符串?
第二個疑問是如何正確計算此長度(代碼在評論編號3)?
而且是有寫入響應,而不是創建緩衝器,chunck等任何替代方法?
您可以使用庫使用Unicode工作,如http://utfcpp.sourceforge.net/(!只是避免MBCS)還要檢查MSDN:http://msdn.microsoft.com/en-us/library/ cwe8bzh0.aspx – Dai