Вопрос

I've got some code

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hFile;
    DWORD dwRWBytes;
    TCHAR frmdata1[] = _T("-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\Windows\\Temp\\hi.exe\"\r\nContent-Type: application/octet-stream\r\n\r\n");
    TCHAR frmdata2[] = _T("\r\n-----------------------------7d82751e2bc0858--\r\n");
    TCHAR hdrs[] = _T("Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"); 

    hFile = CreateFile(L"D://log.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    WriteFile(hFile, frmdata1,  wcslen(frmdata1), &dwRWBytes, NULL);
    CloseHandle(hFile);
    exit(0);
}

I can't understand why in log.txt there is only a part of the string -----------------------------7d82751e2bc0858 Content-Disposition: form-data; name="

What is the problem? When I've tried to save frmdata2 the string in log was incomplete too. What should I do? I think there is some problem in wcslen() function. Also, I am using VC 2008.

Это было полезно?

Решение

Because wcslen tells you how many characters something is in length, and WriteFile writes bytes and TCHAR is multi-byte (or two bytes). So, you've asked WriteFile to write half of the characters in the string you've passed to it.

If you want to write all the characters or get the size of the string in bytes, you could use wcslen(frmdata1) * sizeof(TCHAR)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top