Skip to content

Instantly share code, notes, and snippets.

@ichensky
Created March 6, 2015 20:23
Show Gist options
  • Save ichensky/1f185189dc2bd33b811b to your computer and use it in GitHub Desktop.
Save ichensky/1f185189dc2bd33b811b to your computer and use it in GitHub Desktop.
у тебя есть код, который парсит командную строку в utf-8. Тебе надо под вендой получить командную строку в utf-16 и преобразовать в utf-8, чтобы твой код её корректно прочёл.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#define MAX_UTF8_BYTES_PER_CHAR 4
int main()
{
LPWSTR commandLine = GetCommandLineW();
size_t commandLineLength = wcslen(commandLine);
size_t utf8BufferSize = commandLineLength * MAX_UTF8_BYTES_PER_CHAR + 1;
char * utf8Buffer = (char *)malloc(utf8BufferSize);
memset(utf8Buffer, 0, utf8BufferSize);
iconv_t utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
char * pInBuffer = (char *)commandLine;
size_t inBytesLeft = commandLineLength * sizeof(wchar_t);
char * pOutBuffer = utf8Buffer;
size_t outBytesLeft = utf8BufferSize - 1;
iconv(utf16_to_utf8, &pInBuffer, &inBytesLeft, &pOutBuffer, &outBytesLeft);
iconv_close(utf16_to_utf8);
FILE * testFile = fopen("testfile.txt", "w");
fwrite(utf8Buffer, strlen(utf8Buffer), 1, testFile);
fclose(testFile);
free(utf8Buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment