{"id":62,"date":"2017-03-15T15:36:46","date_gmt":"2017-03-15T06:36:46","guid":{"rendered":"http:\/\/shinke1987.net\/?p=62"},"modified":"2017-03-15T15:38:28","modified_gmt":"2017-03-15T06:38:28","slug":"post-62","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=62","title":{"rendered":"C\u8a00\u8a9e\u3067\u306eWin32API(WriteFile, ReadFile)\u3001UTF-16\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\u30c6\u30b9\u30c8"},"content":{"rendered":"<p>Win32\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u5b9f\u884c\u3002<\/p>\n<h2>Win32\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u8a2d\u5b9a\u3057\u305f\u3053\u3068<\/h2>\n<ol>\n<li>\u7a7a\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210<\/li>\n<li>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3<\/li>\n<li>\u30ea\u30f3\u30ab\u30fc<\/li>\n<li>\u30b7\u30b9\u30c6\u30e0<\/li>\n<li>\u30b5\u30d6\u30b7\u30b9\u30c6\u30e0\u3092\u300c\u30b3\u30f3\u30bd\u30fc\u30eb\u300d\u306b\u6307\u5b9a\u3002<\/li>\n<\/ol>\n<ol>\n<li>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3<\/li>\n<li>\u5168\u822c\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u300cUnicode\u6587\u5b57\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u300d\u306b\u6307\u5b9a\u3002<\/li>\n<\/ol>\n<h2>\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001UTF-16\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u304c\u3067\u304d\u308b\u3002<\/h2>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;windows.h&gt;\r\n#include &lt;tchar.h&gt;\r\n#include &lt;local.h&gt;\r\n  \r\nint _tmain() \r\n{ \r\n  \/\/ \u5909\u6570\u5ba3\u8a00\u3002 \r\n  HANDLE hFile = NULL; \r\n  TCHAR tcFF = TEXT(&quot;\\xFF&quot;); \r\n  TCHAR tcFE = TEXT(&quot;\\xFE&quot;); \r\n  DWORD dwByteWritten = 0; \r\n  TCHAR *temp; \r\n  TCHAR tcTemp&#x5B;MAX_PATH]; \r\n  DWORD dwByteRead = 0; \r\n  \r\n  \/\/ \u30ed\u30b1\u30fc\u30eb\u6307\u5b9a(Unicode\u3092\u4f7f\u3046\u6642\u306b\u5fc5\u8981) \r\n  _tsetlocale(LC_ALL, TEXT(&quot;ja&quot;)); \r\n  \r\n  _tprintf(TEXT(&quot;aiueo\\n&quot;)); \r\n  _tprintf(TEXT(&quot;\u3042\u3044\u3046\u3048\u304a\\n&quot;)); \r\n  _tprintf(TEXT(&quot;sizeof(tchar) = %d\\n&quot;), sizeof(TCHAR)); \r\n  \r\n  \/\/ \u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\u3002 \r\n  hFile = CreateFile( \r\n    TEXT(&quot;d:\/visualstduio\/test\/test.ini&quot;), GENERIC_READ | GENERIC_WRITE, \r\n    0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL \r\n    ); \r\n  if (hFile == INVALID_HANDLE_VALUE || hFile == NUL) { \r\n    _tprintf(TEXT(&quot;hFile = NULL\\n&quot;)); \r\n    system(&quot;pause&quot;); \r\n    exit(0); \r\n  } \r\n  \r\n  \/\/ BOM\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u3080\u3002 \r\n  if (WriteFile(hFile, &amp;tcFF, 1, &amp;dwByteWritten, NULL) == 0) { \r\n    _tprintf(TEXT(&quot;\u66f8\u304d\u8fbc\u307f\u30a8\u30e9\u30fc(\\xFF)\\n&quot;); \r\n    system(&quot;pause&quot;); \r\n    exit(0); \r\n  } \r\n  _tprintf(TEXT(&quot;tcFF dwByteWritten = %d\\n&quot;), dwByteWritten); \r\n  \r\n  if (WriteFile(hFile, &amp;tcFE, 1, &amp;dwByteWritten, NULL) == 0) { \r\n    _tprintf(TEXT(&quot;\u66f8\u304d\u8fbc\u307f\u30a8\u30e9\u30fc(\\xFE)\\n&quot;); \r\n    system(&quot;pause&quot;); \r\n    exit(0); \r\n  } \r\n  \r\n  \/\/ \u6587\u5b57\u5217\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u3080\u3002 \r\n  \/\/ TCHAR * \u6587\u5b57\u5217\u9577\u3068\u3084\u3089\u306a\u3044\u3068\u672b\u5c3e\u306b\u30b4\u30df\uff08\u591a\u5206\u7d42\u7aef\u6587\u5b57\uff09\u304c\u5165\u308b\u3002 \r\n  temp = TEXT(&quot;\u3042\u3044\u3046\u3048\u304a&quot;); \r\n  if (WriteFile(hFile, temp, sizeof(TCHAR) * lstrlen(temp), &amp;dwByteWritten, NULL) == 0) { \r\n    _tprintf(TEXT(&quot;\u66f8\u304d\u8fbc\u307f\u30a8\u30e9\u30fc\uff08\u3042\u3044\u3046\u3048\u304a\uff09\\n&quot;); \r\n    system(&quot;pause&quot;); \r\n    exit(0); \r\n  } \r\n  \r\n  \/\/ \u30d5\u30a1\u30a4\u30eb\u304b\u3089\u6587\u5b57\u5217\u3092\u8aad\u307f\u8fbc\u3080\u3002 \r\n  \/\/ \u8aad\u307f\u8fbc\u3080\u4f4d\u7f6e\u3092\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u521d\u306b\u3057\u3066\u3001\u305d\u3053\u304b\u3089BOM\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002 \r\n  SetFilePointer(hFile, 2, NULL, FILE_BEGIN); \r\n  \/\/ \u6587\u5b57\u5217\u3092\u8aad\u307f\u8fbc\u3093\u3067\u8868\u793a\u3059\u308b\u3002 \r\n  if (ReadFile(hFile, tcTemp, sizeof(tcTemp), &amp;dwByteRead, NULL) == 0) { \r\n    _tprintf(TEXT(&quot;\u8aad\u307f\u8fbc\u307f\u30a8\u30e9\u30fc\\n&quot;)); \r\n    system(&quot;pause&quot;); \r\n    exit(0); \r\n  } \r\n  _tprintf(TEXT(&quot;lstrlen(tcTemp) = %d\\n&quot;), lstrlen(tcTemp)); \r\n  _tprintf(TEXT(&quot;\u8aad\u307f\u8fbc\u307f\uff1a%s\\n&quot;), tcTemp); \r\n  \r\n  \/\/ \u7d42\u4e86\u51e6\u7406\u3002 \r\n  CloseHandle(hFile); \r\n  \r\n  system(&quot;pause&quot;); \r\n  return 0; \r\n} \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Win32\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u5b9f\u884c\u3002 Win32\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u8a2d\u5b9a\u3057\u305f\u3053\u3068 \u7a7a\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3 \u30ea\u30f3\u30ab\u30fc \u30b7\u30b9\u30c6\u30e0 \u30b5\u30d6\u30b7\u30b9\u30c6\u30e0\u3092\u300c\u30b3\u30f3\u30bd\u30fc\u30eb\u300d\u306b\u6307\u5b9a\u3002 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[14,15],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-win32api"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":2,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}