回答編集履歴
1
サンプルコードのインデントを揃えた。
answer
CHANGED
@@ -17,27 +17,27 @@
|
|
17
17
|
|
18
18
|
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
|
19
19
|
{
|
20
|
-
|
20
|
+
int nRetCode = 0;
|
21
21
|
|
22
|
-
|
22
|
+
// MFC の初期化および初期化失敗時のエラーの出力
|
23
|
-
|
23
|
+
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
|
24
|
-
|
24
|
+
{
|
25
|
-
|
25
|
+
fprintf(stderr, "Fatal Error: MFC initialization failed\n");
|
26
|
-
|
26
|
+
nRetCode = 1;
|
27
|
-
|
27
|
+
}
|
28
|
-
|
28
|
+
else
|
29
|
-
|
29
|
+
{
|
30
|
-
|
30
|
+
CString str("Initial String.");
|
31
31
|
|
32
|
-
|
32
|
+
printf("#1: size of TCHAR=%d\n", sizeof(str[0]));
|
33
|
-
|
33
|
+
printf("#2: length=%d\n", str.GetLength());
|
34
|
-
|
34
|
+
printf("#3: str [%s]\n", (const char*)str);
|
35
35
|
|
36
|
-
|
36
|
+
// 10文字分に切り詰め
|
37
37
|
char* pref = str.GetBufferSetLength(10);
|
38
|
-
|
38
|
+
printf("#4: length=%d\n", str.GetLength());
|
39
39
|
|
40
|
-
|
40
|
+
*(pref + 0) = '0';
|
41
41
|
*(pref + 1) = '1';
|
42
42
|
*(pref + 2) = '2';
|
43
43
|
*(pref + 3) = '3';
|
@@ -48,38 +48,37 @@
|
|
48
48
|
*(pref + 8) = (char)0xf8;
|
49
49
|
*(pref + 9) = (char)0xf9;
|
50
50
|
|
51
|
-
|
51
|
+
int i;
|
52
|
-
|
52
|
+
printf("#5: --------\n");
|
53
|
-
|
53
|
+
for (i = 0; i < str.GetLength(); i++) {
|
54
|
-
|
54
|
+
printf("%02x ", (unsigned char)*(pref + i));
|
55
|
-
|
55
|
+
}
|
56
56
|
printf("\n");
|
57
57
|
|
58
|
-
|
58
|
+
printf("#6: str=[%s]\n", (const char*)str);
|
59
59
|
str.ReleaseBuffer();
|
60
60
|
|
61
|
-
|
61
|
+
// CString.ReleaseBuffer(); の実行後
|
62
|
-
|
62
|
+
printf("#7: length=%d\n", str.GetLength());
|
63
|
-
|
63
|
+
printf("#8: alloc length=%d\n", str.GetAllocLength());
|
64
64
|
printf("#9: --------\n");
|
65
65
|
for (i = 0; i < str.GetLength(); i++) {
|
66
|
-
|
66
|
+
printf("%02x ", (unsigned char)str[i]);
|
67
|
-
|
67
|
+
}
|
68
|
-
|
68
|
+
printf("\n");
|
69
69
|
|
70
|
-
|
70
|
+
printf("#10: ------\n");
|
71
|
-
|
71
|
+
// CString.GetBuffer()でバッファー内を覗いてみる
|
72
|
-
|
72
|
+
pref = str.GetBuffer(str.GetAllocLength());
|
73
73
|
for (i = 0; i < str.GetAllocLength(); i++) {
|
74
|
-
|
74
|
+
printf("%02x ", (unsigned char)*(pref + i));
|
75
|
+
}
|
76
|
+
printf("\n");
|
77
|
+
str.ReleaseBuffer();
|
75
78
|
}
|
76
|
-
printf("\n");
|
77
|
-
str.ReleaseBuffer();
|
78
|
-
}
|
79
79
|
|
80
|
-
|
80
|
+
return nRetCode;
|
81
81
|
}
|
82
|
-
|
83
82
|
```
|
84
83
|
|
85
84
|
上記のプログラムをコマンドプロンプト上で動作させると、以下のような実行結果となります。格納したバイナリデータが取り出せていることが分かります。Visual C++6.0でビルドし、Windows XP上で動作させたものと、Visual Studio 2019でビルドし、Windows 7で動作させたもの、どちらも同じ結果となります。
|
@@ -98,5 +97,4 @@
|
|
98
97
|
30 31 32 33
|
99
98
|
#10: ------
|
100
99
|
30 31 32 33 00 f5 f6 f7 f8 f9 00 69 6e 67 2e
|
101
|
-
|
102
100
|
```
|