OSDN Git Service

Fix bugs of automatic detection for Kanji code of filenames.
[ffftp/ffftp.git] / mbswrapper.c
1 // mbswrapper.c\r
2 // Copyright (C) 2011 Suguru Kawamoto\r
3 // マルチバイト文字ワイド文字APIラッパー\r
4 // マルチバイト文字はUTF-8、ワイド文字はUTF-16であるものとする\r
5 // 全ての制御用の文字はASCIIの範囲であるため、Shift_JISとUTF-8間の変換は不要\r
6 \r
7 #define UNICODE\r
8 #define _UNICODE\r
9 \r
10 #include <tchar.h>\r
11 #include <direct.h>\r
12 #include <windows.h>\r
13 #include <commctrl.h>\r
14 #include <shlobj.h>\r
15 #include <htmlhelp.h>\r
16 \r
17 #define DO_NOT_REPLACE\r
18 #include "mbswrapper.h"\r
19 \r
20 // マルチバイト文字列からワイド文字列へ変換\r
21 int MtoW(LPWSTR pDst, int size, LPCSTR pSrc, int count)\r
22 {\r
23         if(pSrc < (LPCSTR)0x00010000 || pSrc == (LPCSTR)~0)\r
24                 return 0;\r
25         if(pDst)\r
26                 return MultiByteToWideChar(CP_UTF8, 0, pSrc, count, pDst, size);\r
27         return MultiByteToWideChar(CP_UTF8, 0, pSrc, count, NULL, 0);\r
28 }\r
29 \r
30 // ワイド文字列からマルチバイト文字列へ変換\r
31 int WtoM(LPSTR pDst, int size, LPCWSTR pSrc, int count)\r
32 {\r
33         if(pSrc < (LPCWSTR)0x00010000 || pSrc == (LPCWSTR)~0)\r
34                 return 0;\r
35         if(pDst)\r
36                 return WideCharToMultiByte(CP_UTF8, 0, pSrc, count, pDst, size, NULL, NULL);\r
37         return WideCharToMultiByte(CP_UTF8, 0, pSrc, count, NULL, 0, NULL, NULL);\r
38 }\r
39 \r
40 // Shift_JIS文字列からワイド文字列へ変換\r
41 int AtoW(LPWSTR pDst, int size, LPCSTR pSrc, int count)\r
42 {\r
43         if(pSrc < (LPCSTR)0x00010000 || pSrc == (LPCSTR)~0)\r
44                 return 0;\r
45         if(pDst)\r
46                 return MultiByteToWideChar(CP_ACP, 0, pSrc, count, pDst, size);\r
47         return MultiByteToWideChar(CP_ACP, 0, pSrc, count, NULL, 0);\r
48 }\r
49 \r
50 // ワイド文字列からShift_JIS文字列へ変換\r
51 int WtoA(LPSTR pDst, int size, LPCWSTR pSrc, int count)\r
52 {\r
53         if(pSrc < (LPCWSTR)0x00010000 || pSrc == (LPCWSTR)~0)\r
54                 return 0;\r
55         if(pDst)\r
56                 return WideCharToMultiByte(CP_ACP, 0, pSrc, count, pDst, size, NULL, NULL);\r
57         return WideCharToMultiByte(CP_ACP, 0, pSrc, count, NULL, 0, NULL, NULL);\r
58 }\r
59 \r
60 // マルチバイト文字列バッファ終端を強制的にNULLで置換\r
61 int TerminateStringM(LPSTR lpString, int size)\r
62 {\r
63         int i;\r
64         if(lpString < (LPSTR)0x00010000 || lpString == (LPSTR)~0)\r
65                 return 0;\r
66         for(i = 0; i < size; i++)\r
67         {\r
68                 if(lpString[i] == '\0')\r
69                         return i;\r
70         }\r
71         i--;\r
72         lpString[i] = '\0';\r
73         return i;\r
74 }\r
75 \r
76 // ワイド文字列バッファ終端を強制的にNULLで置換\r
77 int TerminateStringW(LPWSTR lpString, int size)\r
78 {\r
79         int i;\r
80         if(lpString < (LPWSTR)0x00010000 || lpString == (LPWSTR)~0)\r
81                 return 0;\r
82         for(i = 0; i < size; i++)\r
83         {\r
84                 if(lpString[i] == L'\0')\r
85                         return i;\r
86         }\r
87         i--;\r
88         lpString[i] = L'\0';\r
89         return i;\r
90 }\r
91 \r
92 // Shift_JIS文字列バッファ終端を強制的にNULLで置換\r
93 int TerminateStringA(LPSTR lpString, int size)\r
94 {\r
95         int i;\r
96         if(lpString < (LPSTR)0x00010000 || lpString == (LPSTR)~0)\r
97                 return 0;\r
98         for(i = 0; i < size; i++)\r
99         {\r
100                 if(lpString[i] == '\0')\r
101                         return i;\r
102         }\r
103         i--;\r
104         lpString[i] = '\0';\r
105         return i;\r
106 }\r
107 \r
108 // NULL区切り複数マルチバイト文字列の長さを取得\r
109 size_t GetMultiStringLengthM(LPCSTR lpString)\r
110 {\r
111         size_t i;\r
112         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
113                 return 0;\r
114         i = 0;\r
115         while(lpString[i] != '\0' || lpString[i + 1] != '\0')\r
116         {\r
117                 i++;\r
118         }\r
119         i++;\r
120         return i;\r
121 }\r
122 \r
123 // NULL区切り複数ワイド文字列の長さを取得\r
124 size_t GetMultiStringLengthW(LPCWSTR lpString)\r
125 {\r
126         size_t i;\r
127         if(lpString < (LPCWSTR)0x00010000 || lpString == (LPCWSTR)~0)\r
128                 return 0;\r
129         i = 0;\r
130         while(lpString[i] != L'\0' || lpString[i + 1] != L'\0')\r
131         {\r
132                 i++;\r
133         }\r
134         i++;\r
135         return i;\r
136 }\r
137 \r
138 // NULL区切り複数Shift_JIS文字列の長さを取得\r
139 size_t GetMultiStringLengthA(LPCSTR lpString)\r
140 {\r
141         size_t i;\r
142         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
143                 return 0;\r
144         i = 0;\r
145         while(lpString[i] != '\0' || lpString[i + 1] != '\0')\r
146         {\r
147                 i++;\r
148         }\r
149         i++;\r
150         return i;\r
151 }\r
152 \r
153 // NULL区切りマルチバイト文字列からワイド文字列へ変換\r
154 int MtoWMultiString(LPWSTR pDst, int size, LPCSTR pSrc)\r
155 {\r
156         int i;\r
157         if(pSrc < (LPCSTR)0x00010000 || pSrc == (LPCSTR)~0)\r
158                 return 0;\r
159         if(!pDst)\r
160                 return GetMultiStringLengthM(pSrc);\r
161         i = 0;\r
162         while(*pSrc != '\0')\r
163         {\r
164                 i += MultiByteToWideChar(CP_UTF8, 0, pSrc, -1, pDst + i, size - i - 1);\r
165                 pSrc += strlen(pSrc) + 1;\r
166         }\r
167         pDst[i] = L'\0';\r
168         return i;\r
169 }\r
170 \r
171 // NULL区切りワイド文字列からマルチバイト文字列へ変換\r
172 int WtoMMultiString(LPSTR pDst, int size, LPCWSTR pSrc)\r
173 {\r
174         int i;\r
175         if(pSrc < (LPCWSTR)0x00010000 || pSrc == (LPCWSTR)~0)\r
176                 return 0;\r
177         if(!pDst)\r
178                 return GetMultiStringLengthW(pSrc);\r
179         i = 0;\r
180         while(*pSrc != L'\0')\r
181         {\r
182                 i += WideCharToMultiByte(CP_UTF8, 0, pSrc, -1, pDst + i, size - i - 1, NULL, NULL);\r
183                 pSrc += wcslen(pSrc) + 1;\r
184         }\r
185         pDst[i] = '\0';\r
186         return i;\r
187 }\r
188 \r
189 // NULL区切りShift_JIS文字列からワイド文字列へ変換\r
190 int AtoWMultiString(LPWSTR pDst, int size, LPCSTR pSrc)\r
191 {\r
192         int i;\r
193         if(pSrc < (LPCSTR)0x00010000 || pSrc == (LPCSTR)~0)\r
194                 return 0;\r
195         if(!pDst)\r
196                 return GetMultiStringLengthA(pSrc);\r
197         i = 0;\r
198         while(*pSrc != '\0')\r
199         {\r
200                 i += MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pDst + i, size - i - 1);\r
201                 pSrc += strlen(pSrc) + 1;\r
202         }\r
203         pDst[i] = L'\0';\r
204         return i;\r
205 }\r
206 \r
207 // NULL区切りワイド文字列からShift_JIS文字列へ変換\r
208 int WtoAMultiString(LPSTR pDst, int size, LPCWSTR pSrc)\r
209 {\r
210         int i;\r
211         if(pSrc < (LPCWSTR)0x00010000 || pSrc == (LPCWSTR)~0)\r
212                 return 0;\r
213         if(!pDst)\r
214                 return GetMultiStringLengthW(pSrc);\r
215         i = 0;\r
216         while(*pSrc != L'\0')\r
217         {\r
218                 i += WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDst + i, size - i - 1, NULL, NULL);\r
219                 pSrc += wcslen(pSrc) + 1;\r
220         }\r
221         pDst[i] = '\0';\r
222         return i;\r
223 }\r
224 \r
225 // マルチバイト文字列用のメモリを確保\r
226 char* AllocateStringM(int size)\r
227 {\r
228         char* p;\r
229         // 0が指定される場合があるため1文字分追加\r
230         p = (char*)malloc(sizeof(char) * (size + 1));\r
231         // 念のため先頭にNULL文字を代入\r
232         if(p)\r
233                 *p = '\0';\r
234         return p;\r
235 }\r
236 \r
237 // ワイド文字列用のメモリを確保\r
238 wchar_t* AllocateStringW(int size)\r
239 {\r
240         wchar_t* p;\r
241         // 0が指定される場合があるため1文字分追加\r
242         p = (wchar_t*)malloc(sizeof(wchar_t) * (size + 1));\r
243         // 念のため先頭にNULL文字を代入\r
244         if(p)\r
245                 *p = L'\0';\r
246         return p;\r
247 }\r
248 \r
249 // Shift_JIS文字列用のメモリを確保\r
250 char* AllocateStringA(int size)\r
251 {\r
252         char* p;\r
253         // 0が指定される場合があるため1文字分追加\r
254         p = (char*)malloc(sizeof(char) * (size + 1));\r
255         // 念のため先頭にNULL文字を代入\r
256         if(p)\r
257                 *p = '\0';\r
258         return p;\r
259 }\r
260 \r
261 // メモリを確保してマルチバイト文字列からワイド文字列へ変換\r
262 // リソースIDならば元の値を返す\r
263 wchar_t* DuplicateMtoW(LPCSTR lpString, int c)\r
264 {\r
265         wchar_t* p;\r
266         int i;\r
267         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
268                 return (wchar_t*)lpString;\r
269         if(c < 0)\r
270                 c = strlen(lpString);\r
271         p = AllocateStringW(MtoW(NULL, 0, lpString, c) + 1);\r
272         if(p)\r
273         {\r
274                 i = MtoW(p, 65535, lpString, c);\r
275                 p[i] = L'\0';\r
276         }\r
277         return p;\r
278 }\r
279 \r
280 // 指定したサイズのメモリを確保してマルチバイト文字列からワイド文字列へ変換\r
281 // リソースIDならば元の値を返す\r
282 wchar_t* DuplicateMtoWBuffer(LPCSTR lpString, int c, int size)\r
283 {\r
284         wchar_t* p;\r
285         int i;\r
286         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
287                 return (wchar_t*)lpString;\r
288         if(c < 0)\r
289                 c = strlen(lpString);\r
290         p = AllocateStringW(size);\r
291         if(p)\r
292         {\r
293                 i = MtoW(p, size, lpString, c);\r
294                 p[i] = L'\0';\r
295         }\r
296         return p;\r
297 }\r
298 \r
299 // メモリを確保してNULL区切りマルチバイト文字列からワイド文字列へ変換\r
300 // リソースIDならば元の値を返す\r
301 wchar_t* DuplicateMtoWMultiString(LPCSTR lpString)\r
302 {\r
303         int count;\r
304         wchar_t* p;\r
305         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
306                 return (wchar_t*)lpString;\r
307         count = GetMultiStringLengthM(lpString) + 1;\r
308         p = AllocateStringW(count);\r
309         if(p)\r
310                 MtoW(p, count, lpString, count);\r
311         return p;\r
312 }\r
313 \r
314 // 指定したサイズのメモリを確保してNULL区切りマルチバイト文字列からワイド文字列へ変換\r
315 // リソースIDならば元の値を返す\r
316 wchar_t* DuplicateMtoWMultiStringBuffer(LPCSTR lpString, int size)\r
317 {\r
318         int count;\r
319         wchar_t* p;\r
320         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
321                 return (wchar_t*)lpString;\r
322         count = GetMultiStringLengthM(lpString) + 1;\r
323         p = AllocateStringW(size);\r
324         if(p)\r
325         {\r
326                 MtoW(p, size, lpString, count);\r
327                 p[size - 2] = L'\0';\r
328                 p[size - 1] = L'\0';\r
329         }\r
330         return p;\r
331 }\r
332 \r
333 // メモリを確保してワイド文字列からマルチバイト文字列へ変換\r
334 // リソースIDならば元の値を返す\r
335 char* DuplicateWtoM(LPCWSTR lpString, int c)\r
336 {\r
337         char* p;\r
338         int i;\r
339         if(lpString < (LPCWSTR)0x00010000 || lpString == (LPCWSTR)~0)\r
340                 return (char*)lpString;\r
341         if(c < 0)\r
342                 c = wcslen(lpString);\r
343         p = AllocateStringM(WtoM(NULL, 0, lpString, c) + 1);\r
344         if(p)\r
345         {\r
346                 i = WtoM(p, 65535, lpString, c);\r
347                 p[i] = L'\0';\r
348         }\r
349         return p;\r
350 }\r
351 \r
352 // メモリを確保してShift_JIS文字列からワイド文字列へ変換\r
353 // リソースIDならば元の値を返す\r
354 wchar_t* DuplicateAtoW(LPCSTR lpString, int c)\r
355 {\r
356         wchar_t* p;\r
357         int i;\r
358         if(lpString < (LPCSTR)0x00010000 || lpString == (LPCSTR)~0)\r
359                 return (wchar_t*)lpString;\r
360         if(c < 0)\r
361                 c = strlen(lpString);\r
362         p = AllocateStringW(AtoW(NULL, 0, lpString, c) + 1);\r
363         if(p)\r
364         {\r
365                 i = AtoW(p, 65535, lpString, c);\r
366                 p[i] = L'\0';\r
367         }\r
368         return p;\r
369 }\r
370 \r
371 // メモリを確保してワイド文字列からShift_JIS文字列へ変換\r
372 // リソースIDならば元の値を返す\r
373 char* DuplicateWtoA(LPCWSTR lpString, int c)\r
374 {\r
375         char* p;\r
376         int i;\r
377         if(lpString < (LPCWSTR)0x00010000 || lpString == (LPCWSTR)~0)\r
378                 return (char*)lpString;\r
379         if(c < 0)\r
380                 c = wcslen(lpString);\r
381         p = AllocateStringA(WtoA(NULL, 0, lpString, c) + 1);\r
382         if(p)\r
383         {\r
384                 i = WtoA(p, 65535, lpString, c);\r
385                 p[i] = L'\0';\r
386         }\r
387         return p;\r
388 }\r
389 \r
390 // マルチバイト文字列からコードポイントと次のポインタを取得\r
391 // エンコードが不正な場合は0x80000000を返す\r
392 DWORD GetNextCharM(LPCSTR lpString, LPCSTR* ppNext)\r
393 {\r
394         DWORD Code;\r
395         int i;\r
396         Code = 0;\r
397         if((*lpString & 0xfe) == 0xfc)\r
398         {\r
399                 i = 5;\r
400                 Code |= (DWORD)*lpString & 0x01;\r
401         }\r
402         else if((*lpString & 0xfc) == 0xf8)\r
403         {\r
404                 i = 4;\r
405                 Code |= (DWORD)*lpString & 0x03;\r
406         }\r
407         else if((*lpString & 0xf8) == 0xf0)\r
408         {\r
409                 i = 3;\r
410                 Code |= (DWORD)*lpString & 0x07;\r
411         }\r
412         else if((*lpString & 0xf0) == 0xe0)\r
413         {\r
414                 i = 2;\r
415                 Code |= (DWORD)*lpString & 0x0f;\r
416         }\r
417         else if((*lpString & 0xe0) == 0xc0)\r
418         {\r
419                 i = 1;\r
420                 Code |= (DWORD)*lpString & 0x1f;\r
421         }\r
422         else if((*lpString & 0x80) == 0x00)\r
423         {\r
424                 i = 0;\r
425                 Code |= (DWORD)*lpString & 0x7f;\r
426         }\r
427         else\r
428                 i = -1;\r
429         lpString++;\r
430         while((*lpString & 0xc0) == 0x80)\r
431         {\r
432                 i--;\r
433                 Code = Code << 6;\r
434                 Code |= (DWORD)*lpString & 0x3f;\r
435                 lpString++;\r
436         }\r
437         if(i != 0)\r
438                 Code = 0x80000000;\r
439         if(ppNext)\r
440                 *ppNext = lpString;\r
441         return Code;\r
442 }\r
443 \r
444 // マルチバイト文字列の冗長表現を修正\r
445 // 修正があればTRUEを返す\r
446 // 修正後の文字列の長さは元の文字列の長さ以下のためpDstとpSrcに同じ値を指定可能\r
447 BOOL FixStringM(LPSTR pDst, LPCSTR pSrc)\r
448 {\r
449         BOOL bResult;\r
450         char* p;\r
451         DWORD Code;\r
452         int i;\r
453         char c;\r
454         bResult = FALSE;\r
455         p = (char*)pSrc;\r
456         while(*pSrc != '\0')\r
457         {\r
458                 Code = GetNextCharM(pSrc, &pSrc);\r
459                 if(Code & 0x80000000)\r
460                         continue;\r
461                 else if(Code & 0x7c000000)\r
462                 {\r
463                         i = 5;\r
464                         c = (char)(0xfc | (Code >> (6 * i)));\r
465                 }\r
466                 else if(Code & 0x03e00000)\r
467                 {\r
468                         i = 4;\r
469                         c = (char)(0xf8 | (Code >> (6 * i)));\r
470                 }\r
471                 else if(Code & 0x001f0000)\r
472                 {\r
473                         i = 3;\r
474                         c = (char)(0xf0 | (Code >> (6 * i)));\r
475                 }\r
476                 else if(Code & 0x0000f800)\r
477                 {\r
478                         i = 2;\r
479                         c = (char)(0xe0 | (Code >> (6 * i)));\r
480                 }\r
481                 else if(Code & 0x00000780)\r
482                 {\r
483                         i = 1;\r
484                         c = (char)(0xc0 | (Code >> (6 * i)));\r
485                 }\r
486                 else\r
487                 {\r
488                         i = 0;\r
489                         c = (char)Code;\r
490                 }\r
491                 if(c != *p)\r
492                         bResult = TRUE;\r
493                 p++;\r
494                 *pDst = c;\r
495                 pDst++;\r
496                 while(i > 0)\r
497                 {\r
498                         i--;\r
499                         c = (char)(0x80 | ((Code >> (6 * i)) & 0x3f));\r
500                         if(c != *p)\r
501                                 bResult = TRUE;\r
502                         p++;\r
503                         *pDst = c;\r
504                         pDst++;\r
505                 }\r
506         }\r
507         if(*p != '\0')\r
508                 bResult = TRUE;\r
509         *pDst = '\0';\r
510         return bResult;\r
511 }\r
512 \r
513 // NULL区切りマルチバイト文字列の冗長表現を修正\r
514 // 修正があればTRUEを返す\r
515 // 修正後の文字列の長さは元の文字列の長さ以下のためpDstとpSrcに同じ値を指定可能\r
516 BOOL FixMultiStringM(LPSTR pDst, LPCSTR pSrc)\r
517 {\r
518         BOOL bResult;\r
519         int Length;\r
520         bResult = FALSE;\r
521         while(*pSrc != '\0')\r
522         {\r
523                 Length = strlen(pSrc) + 1;\r
524                 bResult = bResult | FixStringM(pDst, pSrc);\r
525                 pSrc += Length;\r
526                 pDst += strlen(pDst) + 1;\r
527         }\r
528         *pDst = '\0';\r
529         return bResult;\r
530 }\r
531 \r
532 // マルチバイト文字列の冗長表現を確認\r
533 // 冗長表現があればTRUEを返す\r
534 BOOL CheckStringM(LPCSTR lpString)\r
535 {\r
536         BOOL bResult;\r
537         char* p;\r
538         bResult = FALSE;\r
539         p = AllocateStringM(strlen(lpString) + 1);\r
540         if(p)\r
541         {\r
542                 bResult = FixStringM(p, lpString);\r
543                 FreeDuplicatedString(p);\r
544         }\r
545         return bResult;\r
546 }\r
547 \r
548 // NULL区切りマルチバイト文字列の冗長表現を確認\r
549 // 冗長表現があればTRUEを返す\r
550 BOOL CheckMultiStringM(LPCSTR lpString)\r
551 {\r
552         BOOL bResult;\r
553         char* p;\r
554         bResult = FALSE;\r
555         p = AllocateStringM(GetMultiStringLengthM(lpString) + 1);\r
556         if(p)\r
557         {\r
558                 bResult = FixMultiStringM(p, lpString);\r
559                 FreeDuplicatedString(p);\r
560         }\r
561         return bResult;\r
562 }\r
563 \r
564 // 文字列用に確保したメモリを開放\r
565 // リソースIDならば何もしない\r
566 void FreeDuplicatedString(void* p)\r
567 {\r
568         if(p < (void*)0x00010000 || p == (void*)~0)\r
569                 return;\r
570         free(p);\r
571 }\r
572 \r
573 // 以下ラッパー\r
574 // 戻り値バッファ r\r
575 // ワイド文字バッファ pw%d\r
576 // マルチバイト文字バッファ pm%d\r
577 // 引数バッファ a%d\r
578 \r
579 #pragma warning(disable:4102)\r
580 #define START_ROUTINE                                   do{\r
581 #define END_ROUTINE                                             }while(0);end_of_routine:\r
582 #define QUIT_ROUTINE                                    goto end_of_routine;\r
583 \r
584 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)\r
585 {\r
586         int r = 0;\r
587         char* pm0 = NULL;\r
588 START_ROUTINE\r
589         pm0 = DuplicateWtoM(lpCmdLine, -1);\r
590         r = WinMainM(hInstance, hPrevInstance, pm0, nCmdShow);\r
591 END_ROUTINE\r
592         FreeDuplicatedString(pm0);\r
593         return r;\r
594 }\r
595 \r
596 HMODULE LoadLibraryM(LPCSTR lpLibFileName)\r
597 {\r
598         HMODULE r = NULL;\r
599         wchar_t* pw0 = NULL;\r
600 START_ROUTINE\r
601         pw0 = DuplicateMtoW(lpLibFileName, -1);\r
602         r = LoadLibraryW(pw0);\r
603 END_ROUTINE\r
604         FreeDuplicatedString(pw0);\r
605         return r;\r
606 }\r
607 \r
608 HANDLE CreateFileM(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)\r
609 {\r
610         HANDLE r = INVALID_HANDLE_VALUE;\r
611         wchar_t* pw0 = NULL;\r
612 START_ROUTINE\r
613         pw0 = DuplicateMtoW(lpFileName, -1);\r
614         r = CreateFileW(pw0, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);\r
615 END_ROUTINE\r
616         FreeDuplicatedString(pw0);\r
617         return r;\r
618 }\r
619 \r
620 int MessageBoxM(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)\r
621 {\r
622         int r = IDOK;\r
623         wchar_t* pw0 = NULL;\r
624         wchar_t* pw1 = NULL;\r
625 START_ROUTINE\r
626         pw0 = DuplicateMtoW(lpText, -1);\r
627         pw1 = DuplicateMtoW(lpCaption, -1);\r
628         r = MessageBoxW(hWnd, pw0, pw1, uType);\r
629 END_ROUTINE\r
630         FreeDuplicatedString(pw0);\r
631         FreeDuplicatedString(pw1);\r
632         return r;\r
633 }\r
634 \r
635 HANDLE FindFirstFileM(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)\r
636 {\r
637         HANDLE r = INVALID_HANDLE_VALUE;\r
638         wchar_t* pw0 = NULL;\r
639         WIN32_FIND_DATAW a0;\r
640 START_ROUTINE\r
641         pw0 = DuplicateMtoW(lpFileName, -1);\r
642         r = FindFirstFileW(pw0, &a0);\r
643         if(r != INVALID_HANDLE_VALUE)\r
644         {\r
645                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;\r
646                 lpFindFileData->ftCreationTime = a0.ftCreationTime;\r
647                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;\r
648                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;\r
649                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;\r
650                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;\r
651                 lpFindFileData->dwReserved0 = a0.dwReserved0;\r
652                 lpFindFileData->dwReserved1 = a0.dwReserved1;\r
653                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);\r
654                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);\r
655         }\r
656 END_ROUTINE\r
657         FreeDuplicatedString(pw0);\r
658         return r;\r
659 }\r
660 \r
661 BOOL FindNextFileM(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)\r
662 {\r
663         BOOL r = FALSE;\r
664         WIN32_FIND_DATAW a0;\r
665 START_ROUTINE\r
666         r = FindNextFileW(hFindFile, &a0);\r
667         if(r)\r
668         {\r
669                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;\r
670                 lpFindFileData->ftCreationTime = a0.ftCreationTime;\r
671                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;\r
672                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;\r
673                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;\r
674                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;\r
675                 lpFindFileData->dwReserved0 = a0.dwReserved0;\r
676                 lpFindFileData->dwReserved1 = a0.dwReserved1;\r
677                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);\r
678                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);\r
679         }\r
680 END_ROUTINE\r
681         return r;\r
682 }\r
683 \r
684 DWORD GetLogicalDriveStringsM(DWORD nBufferLength, LPSTR lpBuffer)\r
685 {\r
686         DWORD r = 0;\r
687         wchar_t* pw0 = NULL;\r
688 START_ROUTINE\r
689         pw0 = AllocateStringW(nBufferLength * 4);\r
690         GetLogicalDriveStringsW(nBufferLength * 4, pw0);\r
691         WtoMMultiString(lpBuffer, nBufferLength, pw0);\r
692         r = TerminateStringM(lpBuffer, nBufferLength);\r
693 END_ROUTINE\r
694         FreeDuplicatedString(pw0);\r
695         return r;\r
696 }\r
697 \r
698 ATOM RegisterClassExM(CONST WNDCLASSEXA * v0)\r
699 {\r
700         ATOM r = 0;\r
701 START_ROUTINE\r
702         // WNDPROCがShift_JIS用であるため\r
703         r = RegisterClassExA(v0);\r
704 END_ROUTINE\r
705         return r;\r
706 }\r
707 \r
708 HWND CreateWindowExM(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)\r
709 {\r
710         HWND r = NULL;\r
711         wchar_t* pw0 = NULL;\r
712         wchar_t* pw1 = NULL;\r
713 START_ROUTINE\r
714         pw0 = DuplicateMtoW(lpClassName, -1);\r
715         pw1 = DuplicateMtoW(lpWindowName, -1);\r
716         r = CreateWindowExW(dwExStyle, pw0, pw1, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);\r
717 END_ROUTINE\r
718         FreeDuplicatedString(pw0);\r
719         FreeDuplicatedString(pw1);\r
720         return r;\r
721 }\r
722 \r
723 LONG GetWindowLongM(HWND hWnd, int nIndex)\r
724 {\r
725         LRESULT r = 0;\r
726 START_ROUTINE\r
727         // WNDPROCがShift_JIS用であるため\r
728         if(IsWindowUnicode(hWnd))\r
729                 r = GetWindowLongW(hWnd, nIndex);\r
730         else\r
731                 r = GetWindowLongA(hWnd, nIndex);\r
732 END_ROUTINE\r
733         return r;\r
734 }\r
735 \r
736 LONG SetWindowLongM(HWND hWnd, int nIndex, LONG dwNewLong)\r
737 {\r
738         LRESULT r = 0;\r
739 START_ROUTINE\r
740         // WNDPROCがShift_JIS用であるため\r
741         if(IsWindowUnicode(hWnd))\r
742                 r = SetWindowLongW(hWnd, nIndex, dwNewLong);\r
743         else\r
744                 r = SetWindowLongA(hWnd, nIndex, dwNewLong);\r
745 END_ROUTINE\r
746         return r;\r
747 }\r
748 \r
749 LRESULT DefWindowProcM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
750 {\r
751         LRESULT r = 0;\r
752 START_ROUTINE\r
753         // WNDPROCがShift_JIS用であるため\r
754         if(IsWindowUnicode(hWnd))\r
755                 r = DefWindowProcW(hWnd, Msg, wParam, lParam);\r
756         else\r
757                 r = DefWindowProcA(hWnd, Msg, wParam, lParam);\r
758 END_ROUTINE\r
759         return r;\r
760 }\r
761 \r
762 LRESULT CallWindowProcM(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
763 {\r
764         LRESULT r = 0;\r
765 START_ROUTINE\r
766         // WNDPROCがShift_JIS用であるため\r
767         if(IsWindowUnicode(hWnd))\r
768                 r = CallWindowProcW(lpPrevWndFunc, hWnd, Msg, wParam, lParam);\r
769         else\r
770                 r = CallWindowProcA(lpPrevWndFunc, hWnd, Msg, wParam, lParam);\r
771 END_ROUTINE\r
772         return r;\r
773 }\r
774 \r
775 LRESULT SendMessageM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
776 {\r
777         LRESULT r = 0;\r
778         wchar_t* pw0 = NULL;\r
779         wchar_t* pw1 = NULL;\r
780         int Size;\r
781         LVITEMA* pmLVItem;\r
782         LVITEMW wLVItem;\r
783         LVFINDINFOA* pmLVFindInfo;\r
784         LVFINDINFOW wLVFindInfo;\r
785         LVCOLUMNA* pmLVColumn;\r
786         LVCOLUMNW wLVColumn;\r
787         TVITEMEXA* pmTVItem;\r
788         TVITEMEXW wTVItem;\r
789         TVINSERTSTRUCTA* pmTVInsert;\r
790         TVINSERTSTRUCTW wTVInsert;\r
791         wchar_t ClassName[MAX_PATH];\r
792 START_ROUTINE\r
793         switch(Msg)\r
794         {\r
795         case WM_SETTEXT:\r
796                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
797                 r = SendMessageW(hWnd, WM_SETTEXT, wParam, (LPARAM)pw0);\r
798                 break;\r
799         case WM_GETTEXT:\r
800                 pw0 = AllocateStringW(wParam * 4);\r
801                 SendMessageW(hWnd, WM_GETTEXT, wParam * 4, (LPARAM)pw0);\r
802                 WtoM((LPSTR)lParam, wParam, pw0, -1);\r
803                 r = TerminateStringM((LPSTR)lParam, wParam);\r
804                 break;\r
805         case WM_GETTEXTLENGTH:\r
806                 Size = SendMessageW(hWnd, WM_GETTEXTLENGTH, wParam, lParam) + 1;\r
807                 pw0 = AllocateStringW(Size);\r
808                 SendMessageW(hWnd, WM_GETTEXT, (WPARAM)Size, (LPARAM)pw0);\r
809                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
810                 break;\r
811         default:\r
812                 GetClassNameW(hWnd, ClassName, sizeof(ClassName) / sizeof(wchar_t));\r
813                 if(_wcsicmp(ClassName, WC_EDITW) == 0)\r
814                 {\r
815                         switch(Msg)\r
816                         {\r
817                         case EM_REPLACESEL:\r
818                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
819                                 r = SendMessageW(hWnd, EM_REPLACESEL, wParam, (LPARAM)pw0);\r
820                                 break;\r
821                         default:\r
822                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
823                                 break;\r
824                         }\r
825                 }\r
826                 else if(_wcsicmp(ClassName, WC_COMBOBOXW) == 0)\r
827                 {\r
828                         switch(Msg)\r
829                         {\r
830                         case CB_ADDSTRING:\r
831                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
832                                 r = SendMessageW(hWnd, CB_ADDSTRING, wParam, (LPARAM)pw0);\r
833                                 break;\r
834                         case CB_GETLBTEXT:\r
835                                 Size = SendMessageW(hWnd, CB_GETLBTEXTLEN, wParam, 0) + 1;\r
836                                 pw0 = AllocateStringW(Size);\r
837                                 SendMessageW(hWnd, CB_GETLBTEXT, wParam, (LPARAM)pw0);\r
838                                 // バッファ長不明のためオーバーランの可能性あり\r
839                                 WtoM((LPSTR)lParam, Size * 4, pw0, -1);\r
840                                 r = TerminateStringM((LPSTR)lParam, Size * 4);\r
841                                 break;\r
842                         case CB_GETLBTEXTLEN:\r
843                                 Size = SendMessageW(hWnd, CB_GETLBTEXTLEN, wParam, 0) + 1;\r
844                                 pw0 = AllocateStringW(Size);\r
845                                 SendMessageW(hWnd, WM_GETTEXT, wParam, (LPARAM)pw0);\r
846                                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
847                                 break;\r
848                         case CB_INSERTSTRING:\r
849                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
850                                 r = SendMessageW(hWnd, CB_INSERTSTRING, wParam, (LPARAM)pw0);\r
851                                 break;\r
852                         case CB_FINDSTRINGEXACT:\r
853                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
854                                 r = SendMessageW(hWnd, CB_FINDSTRINGEXACT, wParam, (LPARAM)pw0);\r
855                                 break;\r
856                         default:\r
857                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
858                                 break;\r
859                         }\r
860                 }\r
861                 else if(_wcsicmp(ClassName, WC_LISTBOXW) == 0)\r
862                 {\r
863                         switch(Msg)\r
864                         {\r
865                         case LB_ADDSTRING:\r
866                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
867                                 r = SendMessageW(hWnd, LB_ADDSTRING, wParam, (LPARAM)pw0);\r
868                                 break;\r
869                         case LB_INSERTSTRING:\r
870                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
871                                 r = SendMessageW(hWnd, LB_INSERTSTRING, wParam, (LPARAM)pw0);\r
872                                 break;\r
873                         case LB_GETTEXT:\r
874                                 Size = SendMessageW(hWnd, LB_GETTEXTLEN, wParam, 0) + 1;\r
875                                 pw0 = AllocateStringW(Size);\r
876                                 SendMessageW(hWnd, LB_GETTEXT, wParam, (LPARAM)pw0);\r
877                                 // バッファ長不明のためオーバーランの可能性あり\r
878                                 WtoM((LPSTR)lParam, Size * 4, pw0, -1);\r
879                                 r = TerminateStringM((LPSTR)lParam, Size * 4);\r
880                                 break;\r
881                         case LB_GETTEXTLEN:\r
882                                 Size = SendMessageW(hWnd, LB_GETTEXTLEN, wParam, 0) + 1;\r
883                                 pw0 = AllocateStringW(Size);\r
884                                 SendMessageW(hWnd, WM_GETTEXT, wParam, (LPARAM)pw0);\r
885                                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
886                                 break;\r
887                         default:\r
888                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
889                                 break;\r
890                         }\r
891                 }\r
892                 else if(_wcsicmp(ClassName, WC_LISTVIEWW) == 0)\r
893                 {\r
894                         switch(Msg)\r
895                         {\r
896                         case LVM_GETITEMA:\r
897                                 pmLVItem = (LVITEMA*)lParam;\r
898                                 wLVItem.mask = pmLVItem->mask;\r
899                                 wLVItem.iItem = pmLVItem->iItem;\r
900                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
901                                 wLVItem.state = pmLVItem->state;\r
902                                 wLVItem.stateMask = pmLVItem->stateMask;\r
903                                 if(pmLVItem->mask & LVIF_TEXT)\r
904                                 {\r
905                                         Size = pmLVItem->cchTextMax * 4;\r
906                                         pw0 = AllocateStringW(Size);\r
907                                         wLVItem.pszText = pw0;\r
908                                         wLVItem.cchTextMax = Size;\r
909                                 }\r
910                                 wLVItem.iImage = pmLVItem->iImage;\r
911                                 wLVItem.lParam = pmLVItem->lParam;\r
912                                 wLVItem.iIndent = pmLVItem->iIndent;\r
913                                 r = SendMessageW(hWnd, LVM_GETITEMW, wParam, (LPARAM)&wLVItem);\r
914                                 pmLVItem->mask = wLVItem.mask;\r
915                                 pmLVItem->iItem = wLVItem.iItem;\r
916                                 pmLVItem->iSubItem = wLVItem.iSubItem;\r
917                                 pmLVItem->state = wLVItem.state;\r
918                                 pmLVItem->stateMask = wLVItem.stateMask;\r
919                                 if(pmLVItem->mask & LVIF_TEXT)\r
920                                 {\r
921                                         WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);\r
922                                         TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);\r
923                                 }\r
924                                 pmLVItem->iImage = wLVItem.iImage;\r
925                                 pmLVItem->lParam = wLVItem.lParam;\r
926                                 pmLVItem->iIndent = wLVItem.iIndent;\r
927                                 break;\r
928                         case LVM_SETITEMA:\r
929                                 pmLVItem = (LVITEMA*)lParam;\r
930                                 wLVItem.mask = pmLVItem->mask;\r
931                                 wLVItem.iItem = pmLVItem->iItem;\r
932                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
933                                 wLVItem.state = pmLVItem->state;\r
934                                 wLVItem.stateMask = pmLVItem->stateMask;\r
935                                 if(pmLVItem->mask & LVIF_TEXT)\r
936                                 {\r
937                                         pw0 = DuplicateMtoW(pmLVItem->pszText, -1);\r
938                                         wLVItem.pszText = pw0;\r
939                                         // TODO: cchTextMaxの確認\r
940                                         wLVItem.cchTextMax = pmLVItem->cchTextMax;\r
941                                 }\r
942                                 wLVItem.iImage = pmLVItem->iImage;\r
943                                 wLVItem.lParam = pmLVItem->lParam;\r
944                                 wLVItem.iIndent = pmLVItem->iIndent;\r
945                                 r = SendMessageW(hWnd, LVM_SETITEMW, wParam, (LPARAM)&wLVItem);\r
946                                 break;\r
947                         case LVM_INSERTITEMA:\r
948                                 pmLVItem = (LVITEMA*)lParam;\r
949                                 wLVItem.mask = pmLVItem->mask;\r
950                                 wLVItem.iItem = pmLVItem->iItem;\r
951                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
952                                 wLVItem.state = pmLVItem->state;\r
953                                 wLVItem.stateMask = pmLVItem->stateMask;\r
954                                 if(pmLVItem->mask & LVIF_TEXT)\r
955                                 {\r
956                                         pw0 = DuplicateMtoW(pmLVItem->pszText, -1);\r
957                                         wLVItem.pszText = pw0;\r
958                                         // TODO: cchTextMaxの確認\r
959                                         wLVItem.cchTextMax = pmLVItem->cchTextMax;\r
960                                 }\r
961                                 wLVItem.iImage = pmLVItem->iImage;\r
962                                 wLVItem.lParam = pmLVItem->lParam;\r
963                                 wLVItem.iIndent = pmLVItem->iIndent;\r
964                                 r = SendMessageW(hWnd, LVM_INSERTITEMW, wParam, (LPARAM)&wLVItem);\r
965                                 break;\r
966                         case LVM_FINDITEMA:\r
967                                 pmLVFindInfo = (LVFINDINFOA*)lParam;\r
968                                 wLVFindInfo.flags = pmLVFindInfo->flags;\r
969                                 if(pmLVFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))\r
970                                 {\r
971                                         pw0 = DuplicateMtoW(pmLVFindInfo->psz, -1);\r
972                                         wLVFindInfo.psz = pw0;\r
973                                 }\r
974                                 wLVFindInfo.lParam = pmLVFindInfo->lParam;\r
975                                 wLVFindInfo.pt = pmLVFindInfo->pt;\r
976                                 wLVFindInfo.vkDirection = pmLVFindInfo->vkDirection;\r
977                                 r = SendMessageW(hWnd, LVM_FINDITEMW, wParam, (LPARAM)&wLVItem);\r
978                                 break;\r
979                         case LVM_GETCOLUMNA:\r
980                                 pmLVColumn = (LVCOLUMNA*)lParam;\r
981                                 wLVColumn.mask = pmLVColumn->mask;\r
982                                 wLVColumn.fmt = pmLVColumn->fmt;\r
983                                 wLVColumn.cx = pmLVColumn->cx;\r
984                                 Size = pmLVColumn->cchTextMax * 4;\r
985                                 if(pmLVColumn->mask & LVCF_TEXT)\r
986                                 {\r
987                                         pw0 = AllocateStringW(Size);\r
988                                         wLVColumn.pszText = pw0;\r
989                                         wLVColumn.cchTextMax = Size;\r
990                                 }\r
991                                 wLVColumn.iSubItem = pmLVColumn->iSubItem;\r
992                                 wLVColumn.iImage = pmLVColumn->iImage;\r
993                                 wLVColumn.iOrder = pmLVColumn->iOrder;\r
994                                 r = SendMessageW(hWnd, LVM_GETCOLUMNW, wParam, (LPARAM)&wLVColumn);\r
995                                 pmLVColumn->mask = wLVColumn.mask;\r
996                                 pmLVColumn->fmt = wLVColumn.fmt;\r
997                                 pmLVColumn->cx = wLVColumn.cx;\r
998                                 if(pmLVColumn->mask & LVCF_TEXT)\r
999                                 {\r
1000                                         WtoM(pmLVColumn->pszText, pmLVColumn->cchTextMax, wLVColumn.pszText, -1);\r
1001                                         TerminateStringM(pmLVColumn->pszText, pmLVColumn->cchTextMax);\r
1002                                 }\r
1003                                 pmLVColumn->iSubItem = wLVColumn.iSubItem;\r
1004                                 pmLVColumn->iImage = wLVColumn.iImage;\r
1005                                 pmLVColumn->iOrder = wLVColumn.iOrder;\r
1006                                 break;\r
1007                         case LVM_INSERTCOLUMNA:\r
1008                                 pmLVColumn = (LVCOLUMNA*)lParam;\r
1009                                 wLVColumn.mask = pmLVColumn->mask;\r
1010                                 wLVColumn.fmt = pmLVColumn->fmt;\r
1011                                 wLVColumn.cx = pmLVColumn->cx;\r
1012                                 if(pmLVColumn->mask & LVCF_TEXT)\r
1013                                 {\r
1014                                         pw0 = DuplicateMtoW(pmLVColumn->pszText, -1);\r
1015                                         wLVColumn.pszText = pw0;\r
1016                                         // TODO: cchTextMaxの確認\r
1017                                         wLVColumn.cchTextMax = pmLVColumn->cchTextMax;\r
1018                                 }\r
1019                                 wLVColumn.iSubItem = pmLVColumn->iSubItem;\r
1020                                 wLVColumn.iImage = pmLVColumn->iImage;\r
1021                                 wLVColumn.iOrder = pmLVColumn->iOrder;\r
1022                                 r = SendMessageW(hWnd, LVM_INSERTCOLUMNW, wParam, (LPARAM)&wLVColumn);\r
1023                                 break;\r
1024                         case LVM_GETITEMTEXTA:\r
1025                                 pmLVItem = (LVITEMA*)lParam;\r
1026                                 wLVItem.mask = pmLVItem->mask;\r
1027                                 wLVItem.iItem = pmLVItem->iItem;\r
1028                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
1029                                 wLVItem.state = pmLVItem->state;\r
1030                                 wLVItem.stateMask = pmLVItem->stateMask;\r
1031                                 Size = pmLVItem->cchTextMax * 4;\r
1032                                 pw0 = AllocateStringW(Size);\r
1033                                 wLVItem.pszText = pw0;\r
1034                                 wLVItem.cchTextMax = Size;\r
1035                                 wLVItem.iImage = pmLVItem->iImage;\r
1036                                 wLVItem.lParam = pmLVItem->lParam;\r
1037                                 wLVItem.iIndent = pmLVItem->iIndent;\r
1038                                 r = SendMessageW(hWnd, LVM_GETITEMTEXTW, wParam, (LPARAM)&wLVItem);\r
1039                                 pmLVItem->mask = wLVItem.mask;\r
1040                                 pmLVItem->iItem = wLVItem.iItem;\r
1041                                 pmLVItem->iSubItem = wLVItem.iSubItem;\r
1042                                 pmLVItem->state = wLVItem.state;\r
1043                                 pmLVItem->stateMask = wLVItem.stateMask;\r
1044                                 WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);\r
1045                                 TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);\r
1046                                 pmLVItem->iImage = wLVItem.iImage;\r
1047                                 pmLVItem->lParam = wLVItem.lParam;\r
1048                                 pmLVItem->iIndent = wLVItem.iIndent;\r
1049                                 break;\r
1050                         default:\r
1051                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1052                                 break;\r
1053                         }\r
1054                 }\r
1055                 else if(_wcsicmp(ClassName, STATUSCLASSNAMEW) == 0)\r
1056                 {\r
1057                         switch(Msg)\r
1058                         {\r
1059                         case SB_SETTEXTA:\r
1060                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1061                                 r = SendMessageW(hWnd, SB_SETTEXTW, wParam, (LPARAM)pw0);\r
1062                                 break;\r
1063                         default:\r
1064                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1065                                 break;\r
1066                         }\r
1067                 }\r
1068                 else if(_wcsicmp(ClassName, WC_TREEVIEWW) == 0)\r
1069                 {\r
1070                         switch(Msg)\r
1071                         {\r
1072                         case TVM_GETITEMA:\r
1073                                 pmTVItem = (TVITEMEXA*)lParam;\r
1074                                 wTVItem.mask = pmTVItem->mask;\r
1075                                 wTVItem.hItem = pmTVItem->hItem;\r
1076                                 wTVItem.state = pmTVItem->state;\r
1077                                 wTVItem.stateMask = pmTVItem->stateMask;\r
1078                                 if(pmTVItem->mask & TVIF_TEXT)\r
1079                                 {\r
1080                                         Size = pmTVItem->cchTextMax * 4;\r
1081                                         pw0 = AllocateStringW(Size);\r
1082                                         wTVItem.pszText = pw0;\r
1083                                         wTVItem.cchTextMax = Size;\r
1084                                 }\r
1085                                 wTVItem.iImage = pmTVItem->iImage;\r
1086                                 wTVItem.iSelectedImage = pmTVItem->iSelectedImage;\r
1087                                 wTVItem.cChildren = pmTVItem->cChildren;\r
1088                                 wTVItem.lParam = pmTVItem->lParam;\r
1089                                 wTVItem.iIntegral = pmTVItem->iIntegral;\r
1090 //                              wTVItem.uStateEx = pmTVItem->uStateEx;\r
1091 //                              wTVItem.hwnd = pmTVItem->hwnd;\r
1092 //                              wTVItem.iExpandedImage = pmTVItem->iExpandedImage;\r
1093 //                              wTVItem.iReserved = pmTVItem->iReserved;\r
1094                                 r = SendMessageW(hWnd, TVM_GETITEMW, wParam, (LPARAM)&wTVItem);\r
1095                                 pmTVItem->mask = wTVItem.mask;\r
1096                                 pmTVItem->hItem = wTVItem.hItem;\r
1097                                 pmTVItem->state = wTVItem.state;\r
1098                                 pmTVItem->stateMask = wTVItem.stateMask;\r
1099                                 if(pmTVItem->mask & TVIF_TEXT)\r
1100                                 {\r
1101                                         WtoM(pmTVItem->pszText, pmTVItem->cchTextMax, wTVItem.pszText, -1);\r
1102                                         TerminateStringM(pmTVItem->pszText, pmTVItem->cchTextMax);\r
1103                                 }\r
1104                                 pmTVItem->iImage = wTVItem.iImage;\r
1105                                 pmTVItem->iSelectedImage = wTVItem.iSelectedImage;\r
1106                                 pmTVItem->cChildren = wTVItem.cChildren;\r
1107                                 pmTVItem->lParam = wTVItem.lParam;\r
1108                                 pmTVItem->iIntegral = wTVItem.iIntegral;\r
1109 //                              pmTVItem->uStateEx = wTVItem.uStateEx;\r
1110 //                              pmTVItem->hwnd = wTVItem.hwnd;\r
1111 //                              pmTVItem->iExpandedImage = wTVItem.iExpandedImage;\r
1112 //                              pmTVItem->iReserved = wTVItem.iReserved;\r
1113                                 break;\r
1114                         case TVM_INSERTITEMA:\r
1115                                 pmTVInsert = (TVINSERTSTRUCTA*)lParam;\r
1116                                 wTVInsert.hParent = pmTVInsert->hParent;\r
1117                                 wTVInsert.hInsertAfter = pmTVInsert->hInsertAfter;\r
1118                                 wTVInsert.itemex.mask = pmTVInsert->itemex.mask;\r
1119                                 wTVInsert.itemex.hItem = pmTVInsert->itemex.hItem;\r
1120                                 wTVInsert.itemex.state = pmTVInsert->itemex.state;\r
1121                                 wTVInsert.itemex.stateMask = pmTVInsert->itemex.stateMask;\r
1122                                 if(pmTVInsert->itemex.mask & TVIF_TEXT)\r
1123                                 {\r
1124                                         pw0 = DuplicateMtoW(pmTVInsert->itemex.pszText, -1);\r
1125                                         wTVInsert.itemex.pszText = pw0;\r
1126                                         // TODO: cchTextMaxの確認\r
1127                                         wTVInsert.itemex.cchTextMax = pmTVInsert->itemex.cchTextMax;\r
1128                                 }\r
1129                                 wTVInsert.itemex.iImage = pmTVInsert->itemex.iImage;\r
1130                                 wTVInsert.itemex.iSelectedImage = pmTVInsert->itemex.iSelectedImage;\r
1131                                 wTVInsert.itemex.cChildren = pmTVInsert->itemex.cChildren;\r
1132                                 wTVInsert.itemex.lParam = pmTVInsert->itemex.lParam;\r
1133                                 wTVInsert.itemex.iIntegral = pmTVInsert->itemex.iIntegral;\r
1134 //                              wTVInsert.itemex.uStateEx = pmTVInsert->itemex.uStateEx;\r
1135 //                              wTVInsert.itemex.hwnd = pmTVInsert->itemex.hwnd;\r
1136 //                              wTVInsert.itemex.iExpandedImage = pmTVInsert->itemex.iExpandedImage;\r
1137 //                              wTVInsert.itemex.iReserved = pmTVInsert->itemex.iReserved;\r
1138                                 r = SendMessageW(hWnd, TVM_INSERTITEMW, wParam, (LPARAM)&wTVInsert);\r
1139                                 break;\r
1140                         default:\r
1141                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1142                                 break;\r
1143                         }\r
1144                 }\r
1145                 else\r
1146                         r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1147                 break;\r
1148         }\r
1149 END_ROUTINE\r
1150         FreeDuplicatedString(pw0);\r
1151         FreeDuplicatedString(pw1);\r
1152         return r;\r
1153 }\r
1154 \r
1155 LRESULT DefDlgProcM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1156 {\r
1157         LRESULT r = 0;\r
1158 START_ROUTINE\r
1159         // WNDPROCがShift_JIS用であるため\r
1160         if(IsWindowUnicode(hWnd))\r
1161                 r = DefDlgProcW(hWnd, Msg, wParam, lParam);\r
1162         else\r
1163                 r = DefDlgProcA(hWnd, Msg, wParam, lParam);\r
1164 END_ROUTINE\r
1165         return r;\r
1166 }\r
1167 \r
1168 LRESULT SendDlgItemMessageM(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1169 {\r
1170         LRESULT r = 0;\r
1171 START_ROUTINE\r
1172         r = SendMessageM(GetDlgItem(hDlg, nIDDlgItem), Msg, wParam, lParam);\r
1173 END_ROUTINE\r
1174         return r;\r
1175 }\r
1176 \r
1177 BOOL SetWindowTextM(HWND hWnd, LPCSTR lpString)\r
1178 {\r
1179         BOOL r = FALSE;\r
1180         wchar_t* pw0 = NULL;\r
1181 START_ROUTINE\r
1182         pw0 = DuplicateMtoW(lpString, -1);\r
1183         r = SetWindowTextW(hWnd, pw0);\r
1184 END_ROUTINE\r
1185         FreeDuplicatedString(pw0);\r
1186         return r;\r
1187 }\r
1188 \r
1189 UINT DragQueryFileM(HDROP hDrop, UINT iFile, LPSTR lpszFile, UINT cch)\r
1190 {\r
1191         UINT r = 0;\r
1192         wchar_t* pw0 = NULL;\r
1193 START_ROUTINE\r
1194         if(iFile == (UINT)-1)\r
1195                 r = DragQueryFileW(hDrop, iFile, (LPWSTR)lpszFile, cch);\r
1196         else\r
1197         {\r
1198                 pw0 = AllocateStringW(cch * 4);\r
1199                 DragQueryFileW(hDrop, iFile, pw0, cch * 4);\r
1200                 WtoM(lpszFile, cch, pw0, -1);\r
1201                 r = TerminateStringM(lpszFile, cch);\r
1202         }\r
1203 END_ROUTINE\r
1204         FreeDuplicatedString(pw0);\r
1205         return r;\r
1206 }\r
1207 \r
1208 LPSTR GetCommandLineM()\r
1209 {\r
1210         LPSTR r = 0;\r
1211         static char* pm0 = NULL;\r
1212 START_ROUTINE\r
1213         if(!pm0)\r
1214                 pm0 = DuplicateWtoM(GetCommandLineW(), -1);\r
1215         r = pm0;\r
1216 END_ROUTINE\r
1217         return r;\r
1218 }\r
1219 \r
1220 DWORD GetCurrentDirectoryM(DWORD nBufferLength, LPSTR lpBuffer)\r
1221 {\r
1222         DWORD r = 0;\r
1223         wchar_t* pw0 = NULL;\r
1224 START_ROUTINE\r
1225         // TODO: バッファが不十分な場合に必要なサイズを返す\r
1226         pw0 = AllocateStringW(nBufferLength * 4);\r
1227         GetCurrentDirectoryW(nBufferLength * 4, pw0);\r
1228         WtoM(lpBuffer, nBufferLength, pw0, -1);\r
1229         r = TerminateStringM(lpBuffer, nBufferLength);\r
1230 END_ROUTINE\r
1231         FreeDuplicatedString(pw0);\r
1232         return r;\r
1233 }\r
1234 \r
1235 BOOL SetCurrentDirectoryM(LPCSTR lpPathName)\r
1236 {\r
1237         BOOL r = FALSE;\r
1238         wchar_t* pw0 = NULL;\r
1239 START_ROUTINE\r
1240         pw0 = DuplicateMtoW(lpPathName, -1);\r
1241         r = SetCurrentDirectoryW(pw0);\r
1242 END_ROUTINE\r
1243         FreeDuplicatedString(pw0);\r
1244         return r;\r
1245 }\r
1246 \r
1247 DWORD GetTempPathM(DWORD nBufferLength, LPSTR lpBuffer)\r
1248 {\r
1249         DWORD r = 0;\r
1250         wchar_t* pw0 = NULL;\r
1251 START_ROUTINE\r
1252         pw0 = AllocateStringW(nBufferLength * 4);\r
1253         GetTempPathW(nBufferLength * 4, pw0);\r
1254         WtoM(lpBuffer, nBufferLength, pw0, -1);\r
1255         r = TerminateStringM(lpBuffer, nBufferLength);\r
1256 END_ROUTINE\r
1257         FreeDuplicatedString(pw0);\r
1258         return r;\r
1259 }\r
1260 \r
1261 DWORD GetFileAttributesM(LPCSTR lpFileName)\r
1262 {\r
1263         DWORD r = FALSE;\r
1264         wchar_t* pw0 = NULL;\r
1265 START_ROUTINE\r
1266         pw0 = DuplicateMtoW(lpFileName, -1);\r
1267         r = GetFileAttributesW(pw0);\r
1268 END_ROUTINE\r
1269         FreeDuplicatedString(pw0);\r
1270         return r;\r
1271 }\r
1272 \r
1273 DWORD GetModuleFileNameM(HMODULE hModule, LPCH lpFilename, DWORD nSize)\r
1274 {\r
1275         DWORD r = 0;\r
1276         wchar_t* pw0 = NULL;\r
1277 START_ROUTINE\r
1278         pw0 = AllocateStringW(nSize * 4);\r
1279         GetModuleFileNameW(hModule, pw0, nSize * 4);\r
1280         WtoM(lpFilename, nSize, pw0, -1);\r
1281         r = TerminateStringM(lpFilename, nSize);\r
1282 END_ROUTINE\r
1283         FreeDuplicatedString(pw0);\r
1284         return r;\r
1285 }\r
1286 \r
1287 LSTATUS RegOpenKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)\r
1288 {\r
1289         LSTATUS r = 0;\r
1290         wchar_t* pw0 = NULL;\r
1291 START_ROUTINE\r
1292         pw0 = DuplicateMtoW(lpSubKey, -1);\r
1293         r = RegOpenKeyExW(hKey, pw0, ulOptions, samDesired, phkResult);\r
1294 END_ROUTINE\r
1295         FreeDuplicatedString(pw0);\r
1296         return r;\r
1297 }\r
1298 \r
1299 LSTATUS RegCreateKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD Reserved, LPSTR lpClass, DWORD dwOptions, REGSAM samDesired, CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition)\r
1300 {\r
1301         LSTATUS r = 0;\r
1302         wchar_t* pw0 = NULL;\r
1303         wchar_t* pw1 = NULL;\r
1304 START_ROUTINE\r
1305         pw0 = DuplicateMtoW(lpSubKey, -1);\r
1306         pw1 = DuplicateMtoW(lpClass, -1);\r
1307         r = RegCreateKeyExW(hKey, pw0, Reserved, pw1, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);\r
1308 END_ROUTINE\r
1309         FreeDuplicatedString(pw0);\r
1310         FreeDuplicatedString(pw1);\r
1311         return r;\r
1312 }\r
1313 \r
1314 LSTATUS RegDeleteValueM(HKEY hKey, LPCSTR lpValueName)\r
1315 {\r
1316         LSTATUS r = 0;\r
1317         wchar_t* pw0 = NULL;\r
1318 START_ROUTINE\r
1319         pw0 = DuplicateMtoW(lpValueName, -1);\r
1320         r = RegDeleteValueW(hKey, pw0);\r
1321 END_ROUTINE\r
1322         FreeDuplicatedString(pw0);\r
1323         return r;\r
1324 }\r
1325 \r
1326 LSTATUS RegQueryValueExM(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)\r
1327 {\r
1328         LSTATUS r = 0;\r
1329         wchar_t* pw0 = NULL;\r
1330         wchar_t* pw1 = NULL;\r
1331         DWORD dwType;\r
1332         DWORD wcbData;\r
1333 START_ROUTINE\r
1334         pw0 = DuplicateMtoW(lpValueName, -1);\r
1335         if(RegQueryValueExW(hKey, pw0, NULL, &dwType, NULL, 0) == ERROR_SUCCESS)\r
1336         {\r
1337                 switch(dwType)\r
1338                 {\r
1339                 case REG_SZ:\r
1340                 case REG_EXPAND_SZ:\r
1341                 case REG_MULTI_SZ:\r
1342                         if(lpcbData)\r
1343                         {\r
1344                                 pw1 = AllocateStringW(*lpcbData / sizeof(char) * 4);\r
1345                                 wcbData = *lpcbData / sizeof(char) * 4;\r
1346                                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, (LPBYTE)pw1, &wcbData);\r
1347                                 if(lpData)\r
1348                                         *lpcbData = sizeof(char) * WtoM((char*)lpData, *lpcbData / sizeof(char), pw1, wcbData / sizeof(wchar_t));\r
1349                                 else\r
1350                                         *lpcbData = sizeof(char) * WtoM(NULL, 0, pw1, wcbData / sizeof(wchar_t));\r
1351                         }\r
1352                         break;\r
1353                 default:\r
1354                         r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);\r
1355                         break;\r
1356                 }\r
1357         }\r
1358         else\r
1359                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);\r
1360 END_ROUTINE\r
1361         FreeDuplicatedString(pw0);\r
1362         FreeDuplicatedString(pw1);\r
1363         return r;\r
1364 }\r
1365 \r
1366 LSTATUS RegSetValueExM(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE* lpData, DWORD cbData)\r
1367 {\r
1368         LSTATUS r = 0;\r
1369         wchar_t* pw0 = NULL;\r
1370         wchar_t* pw1 = NULL;\r
1371         DWORD wcbData;\r
1372 START_ROUTINE\r
1373         pw0 = DuplicateMtoW(lpValueName, -1);\r
1374         switch(dwType)\r
1375         {\r
1376         case REG_SZ:\r
1377         case REG_EXPAND_SZ:\r
1378         case REG_MULTI_SZ:\r
1379                 wcbData = MtoW(NULL, 0, (char*)lpData, cbData / sizeof(char));\r
1380                 pw1 = AllocateStringW(wcbData);\r
1381                 MtoW(pw1, wcbData, (char*)lpData, cbData / sizeof(char));\r
1382                 wcbData = sizeof(wchar_t) * wcbData;\r
1383                 lpData = (BYTE*)pw1;\r
1384                 cbData = wcbData;\r
1385                 break;\r
1386         }\r
1387         r = RegSetValueExW(hKey, pw0, Reserved, dwType, lpData, cbData);\r
1388 END_ROUTINE\r
1389         FreeDuplicatedString(pw0);\r
1390         FreeDuplicatedString(pw1);\r
1391         return r;\r
1392 }\r
1393 \r
1394 BOOL TextOutM(HDC hdc, int x, int y, LPCSTR lpString, int c)\r
1395 {\r
1396         BOOL r = FALSE;\r
1397         wchar_t* pw0 = NULL;\r
1398 START_ROUTINE\r
1399         pw0 = DuplicateMtoW(lpString, c);\r
1400         r = TextOutW(hdc, x, y, pw0, wcslen(pw0));\r
1401 END_ROUTINE\r
1402         FreeDuplicatedString(pw0);\r
1403         return r;\r
1404 }\r
1405 \r
1406 BOOL GetTextExtentPoint32M(HDC hdc, LPCSTR lpString, int c, LPSIZE psizl)\r
1407 {\r
1408         BOOL r = FALSE;\r
1409         wchar_t* pw0 = NULL;\r
1410 START_ROUTINE\r
1411         pw0 = DuplicateMtoW(lpString, c);\r
1412         r = GetTextExtentPoint32W(hdc, pw0, wcslen(pw0), psizl);\r
1413 END_ROUTINE\r
1414         FreeDuplicatedString(pw0);\r
1415         return r;\r
1416 }\r
1417 \r
1418 INT_PTR PropertySheetM(LPCPROPSHEETHEADERA v0)\r
1419 {\r
1420         INT_PTR r = 0;\r
1421         PROPSHEETHEADERW a0;\r
1422         PROPSHEETPAGEW* pwPage;\r
1423         UINT i;\r
1424 START_ROUTINE\r
1425         a0.dwSize = sizeof(PROPSHEETHEADERW);\r
1426         a0.dwFlags = v0->dwFlags;\r
1427         a0.hwndParent = v0->hwndParent;\r
1428         a0.hInstance = v0->hInstance;\r
1429         if(v0->dwFlags & PSH_USEICONID)\r
1430                 a0.pszIcon = DuplicateMtoW(v0->pszIcon, -1);\r
1431         else\r
1432                 a0.hIcon = v0->hIcon;\r
1433         a0.pszCaption = DuplicateMtoW(v0->pszCaption, -1);\r
1434         a0.nPages = v0->nPages;\r
1435         if(v0->dwFlags & PSH_USEPSTARTPAGE)\r
1436                 a0.pStartPage = DuplicateMtoW(v0->pStartPage, -1);\r
1437         else\r
1438                 a0.nStartPage = v0->nStartPage;\r
1439         if(v0->dwFlags & PSH_PROPSHEETPAGE)\r
1440         {\r
1441                 if(v0->ppsp && (pwPage = (PROPSHEETPAGEW*)malloc(sizeof(PROPSHEETPAGEW) * v0->nPages)))\r
1442                 {\r
1443                         for(i = 0; i < v0->nPages; i++)\r
1444                         {\r
1445                                 pwPage[i].dwSize = sizeof(PROPSHEETPAGEW);\r
1446                                 pwPage[i].dwFlags = v0->ppsp[i].dwFlags;\r
1447                                 pwPage[i].hInstance = v0->ppsp[i].hInstance;\r
1448                                 if(v0->ppsp[i].dwFlags & PSP_DLGINDIRECT)\r
1449                                         pwPage[i].pResource = v0->ppsp[i].pResource;\r
1450                                 else\r
1451                                         pwPage[i].pszTemplate = DuplicateMtoW(v0->ppsp[i].pszTemplate, -1);\r
1452                                 if(v0->ppsp[i].dwFlags & PSP_USEICONID)\r
1453                                         pwPage[i].pszIcon = DuplicateMtoW(v0->ppsp[i].pszIcon, -1);\r
1454                                 else\r
1455                                         pwPage[i].hIcon = v0->ppsp[i].hIcon;\r
1456                                 if(v0->ppsp[i].dwFlags & PSP_USETITLE)\r
1457                                         pwPage[i].pszTitle = DuplicateMtoW(v0->ppsp[i].pszTitle, -1);\r
1458                                 pwPage[i].pfnDlgProc = v0->ppsp[i].pfnDlgProc;\r
1459                                 pwPage[i].lParam = v0->ppsp[i].lParam;\r
1460                                 // TODO: pfnCallback\r
1461                                 pwPage[i].pfnCallback = (LPFNPSPCALLBACKW)v0->ppsp[i].pfnCallback;\r
1462                                 pwPage[i].pcRefParent = v0->ppsp[i].pcRefParent;\r
1463                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERTITLE)\r
1464                                         pwPage[i].pszHeaderTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderTitle, -1);\r
1465                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERSUBTITLE)\r
1466                                         pwPage[i].pszHeaderSubTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderSubTitle, -1);\r
1467                         }\r
1468                 }\r
1469                 else\r
1470                         pwPage = NULL;\r
1471                 a0.ppsp = pwPage;\r
1472         }\r
1473         else\r
1474                 a0.phpage = v0->phpage;\r
1475         a0.pfnCallback = v0->pfnCallback;\r
1476         if(v0->dwFlags & PSH_USEHBMWATERMARK)\r
1477                 a0.hbmWatermark = v0->hbmWatermark;\r
1478         else\r
1479                 a0.pszbmWatermark = DuplicateMtoW(v0->pszbmWatermark, -1);\r
1480         r = PropertySheetW(&a0);\r
1481         if(a0.dwFlags & PSH_USEICONID)\r
1482                 FreeDuplicatedString((void*)a0.pszIcon);\r
1483         FreeDuplicatedString((void*)a0.pszCaption);\r
1484         if(v0->dwFlags & PSH_USEPSTARTPAGE)\r
1485                 FreeDuplicatedString((void*)a0.pStartPage);\r
1486         if(v0->dwFlags & PSH_PROPSHEETPAGE)\r
1487         {\r
1488                 if(pwPage)\r
1489                 {\r
1490                         for(i = 0; i < v0->nPages; i++)\r
1491                         {\r
1492                                 if(!(v0->ppsp[i].dwFlags & PSP_DLGINDIRECT))\r
1493                                         FreeDuplicatedString((void*)pwPage[i].pszTemplate);\r
1494                                 if(v0->ppsp[i].dwFlags & PSP_USEICONID)\r
1495                                         FreeDuplicatedString((void*)pwPage[i].pszIcon);\r
1496                                 if(v0->ppsp[i].dwFlags & PSP_USETITLE)\r
1497                                         FreeDuplicatedString((void*)pwPage[i].pszTitle);\r
1498                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERTITLE)\r
1499                                         FreeDuplicatedString((void*)pwPage[i].pszHeaderTitle);\r
1500                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERSUBTITLE)\r
1501                                         FreeDuplicatedString((void*)pwPage[i].pszHeaderSubTitle);\r
1502                         }\r
1503                         free(pwPage);\r
1504                 }\r
1505         }\r
1506         if(!(v0->dwFlags & PSH_USEHBMWATERMARK))\r
1507                 FreeDuplicatedString((void*)a0.pszbmWatermark);\r
1508 END_ROUTINE\r
1509         return r;\r
1510 }\r
1511 \r
1512 BOOL GetOpenFileNameM(LPOPENFILENAMEA v0)\r
1513 {\r
1514         BOOL r = FALSE;\r
1515         wchar_t* pw0 = NULL;\r
1516         wchar_t* pw1 = NULL;\r
1517         wchar_t* pw2 = NULL;\r
1518         wchar_t* pw3 = NULL;\r
1519         wchar_t* pw4 = NULL;\r
1520         wchar_t* pw5 = NULL;\r
1521         wchar_t* pw6 = NULL;\r
1522         wchar_t* pw7 = NULL;\r
1523         wchar_t* pw8 = NULL;\r
1524         wchar_t* pw9 = NULL;\r
1525         OPENFILENAMEW wofn;\r
1526 START_ROUTINE\r
1527         wofn.lStructSize = sizeof(OPENFILENAMEW);\r
1528         wofn.hwndOwner = v0->hwndOwner;\r
1529         wofn.hInstance = v0->hInstance;\r
1530         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);\r
1531         wofn.lpstrFilter = pw0;\r
1532         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);\r
1533         wofn.lpstrCustomFilter = pw1;\r
1534         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;\r
1535         wofn.nFilterIndex = v0->nFilterIndex;\r
1536         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);\r
1537         wofn.lpstrFile = pw2;\r
1538         wofn.nMaxFile = v0->nMaxFile * 4;\r
1539         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);\r
1540         wofn.lpstrFileTitle = pw3;\r
1541         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;\r
1542         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);\r
1543         wofn.lpstrInitialDir = pw4;\r
1544         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);\r
1545         wofn.lpstrTitle = pw5;\r
1546         wofn.Flags = v0->Flags;\r
1547         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);\r
1548         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);\r
1549         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);\r
1550         wofn.lpstrDefExt = pw6;\r
1551         wofn.lCustData = v0->lCustData;\r
1552         wofn.lpfnHook = v0->lpfnHook;\r
1553         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);\r
1554         wofn.pvReserved = v0->pvReserved;\r
1555         wofn.FlagsEx = v0->FlagsEx;\r
1556         r = GetOpenFileNameW(&wofn);\r
1557         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);\r
1558         TerminateStringM(v0->lpstrFile, v0->nMaxFile);\r
1559         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);\r
1560         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);\r
1561 END_ROUTINE\r
1562         FreeDuplicatedString(pw0);\r
1563         FreeDuplicatedString(pw1);\r
1564         FreeDuplicatedString(pw2);\r
1565         FreeDuplicatedString(pw3);\r
1566         FreeDuplicatedString(pw4);\r
1567         FreeDuplicatedString(pw5);\r
1568         FreeDuplicatedString(pw6);\r
1569         return r;\r
1570 }\r
1571 \r
1572 BOOL GetSaveFileNameM(LPOPENFILENAMEA v0)\r
1573 {\r
1574         BOOL r = FALSE;\r
1575         wchar_t* pw0 = NULL;\r
1576         wchar_t* pw1 = NULL;\r
1577         wchar_t* pw2 = NULL;\r
1578         wchar_t* pw3 = NULL;\r
1579         wchar_t* pw4 = NULL;\r
1580         wchar_t* pw5 = NULL;\r
1581         wchar_t* pw6 = NULL;\r
1582         wchar_t* pw7 = NULL;\r
1583         wchar_t* pw8 = NULL;\r
1584         wchar_t* pw9 = NULL;\r
1585         OPENFILENAMEW wofn;\r
1586 START_ROUTINE\r
1587         wofn.lStructSize = sizeof(OPENFILENAMEW);\r
1588         wofn.hwndOwner = v0->hwndOwner;\r
1589         wofn.hInstance = v0->hInstance;\r
1590         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);\r
1591         wofn.lpstrFilter = pw0;\r
1592         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);\r
1593         wofn.lpstrCustomFilter = pw1;\r
1594         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;\r
1595         wofn.nFilterIndex = v0->nFilterIndex;\r
1596         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);\r
1597         wofn.lpstrFile = pw2;\r
1598         wofn.nMaxFile = v0->nMaxFile * 4;\r
1599         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);\r
1600         wofn.lpstrFileTitle = pw3;\r
1601         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;\r
1602         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);\r
1603         wofn.lpstrInitialDir = pw4;\r
1604         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);\r
1605         wofn.lpstrTitle = pw5;\r
1606         wofn.Flags = v0->Flags;\r
1607         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);\r
1608         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);\r
1609         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);\r
1610         wofn.lpstrDefExt = pw6;\r
1611         wofn.lCustData = v0->lCustData;\r
1612         wofn.lpfnHook = v0->lpfnHook;\r
1613         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);\r
1614         wofn.pvReserved = v0->pvReserved;\r
1615         wofn.FlagsEx = v0->FlagsEx;\r
1616         r = GetSaveFileNameW(&wofn);\r
1617         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);\r
1618         TerminateStringM(v0->lpstrFile, v0->nMaxFile);\r
1619         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);\r
1620         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);\r
1621 END_ROUTINE\r
1622         FreeDuplicatedString(pw0);\r
1623         FreeDuplicatedString(pw1);\r
1624         FreeDuplicatedString(pw2);\r
1625         FreeDuplicatedString(pw3);\r
1626         FreeDuplicatedString(pw4);\r
1627         FreeDuplicatedString(pw5);\r
1628         FreeDuplicatedString(pw6);\r
1629         return r;\r
1630 }\r
1631 \r
1632 HWND HtmlHelpM(HWND hwndCaller, LPCSTR pszFile, UINT uCommand, DWORD_PTR dwData)\r
1633 {\r
1634         HWND r = NULL;\r
1635         wchar_t* pw0 = NULL;\r
1636 START_ROUTINE\r
1637         pw0 = DuplicateMtoW(pszFile, -1);\r
1638         r = HtmlHelpW(hwndCaller, pw0, uCommand, dwData);\r
1639 END_ROUTINE\r
1640         FreeDuplicatedString(pw0);\r
1641         return r;\r
1642 }\r
1643 \r
1644 BOOL CreateProcessM(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)\r
1645 {\r
1646         BOOL r = FALSE;\r
1647         wchar_t* pw0 = NULL;\r
1648         wchar_t* pw1 = NULL;\r
1649         wchar_t* pw2 = NULL;\r
1650         wchar_t* pw3 = NULL;\r
1651         wchar_t* pw4 = NULL;\r
1652         wchar_t* pw5 = NULL;\r
1653         STARTUPINFOW wStartupInfo;\r
1654 START_ROUTINE\r
1655         pw0 = DuplicateMtoW(lpApplicationName, -1);\r
1656         pw1 = DuplicateMtoWBuffer(lpCommandLine, -1, (strlen(lpCommandLine) + 1) * 4);\r
1657         pw2 = DuplicateMtoW(lpCurrentDirectory, -1);\r
1658         wStartupInfo.cb = sizeof(LPSTARTUPINFOW);\r
1659         pw3 = DuplicateMtoW(lpStartupInfo->lpReserved, -1);\r
1660         wStartupInfo.lpReserved = pw3;\r
1661         pw4 = DuplicateMtoW(lpStartupInfo->lpDesktop, -1);\r
1662         wStartupInfo.lpDesktop = pw4;\r
1663         pw5 = DuplicateMtoW(lpStartupInfo->lpTitle, -1);\r
1664         wStartupInfo.lpTitle = pw5;\r
1665         wStartupInfo.dwX = lpStartupInfo->dwX;\r
1666         wStartupInfo.dwY = lpStartupInfo->dwY;\r
1667         wStartupInfo.dwXSize = lpStartupInfo->dwXSize;\r
1668         wStartupInfo.dwYSize = lpStartupInfo->dwYSize;\r
1669         wStartupInfo.dwXCountChars = lpStartupInfo->dwXCountChars;\r
1670         wStartupInfo.dwYCountChars = lpStartupInfo->dwYCountChars;\r
1671         wStartupInfo.dwFillAttribute = lpStartupInfo->dwFillAttribute;\r
1672         wStartupInfo.dwFlags = lpStartupInfo->dwFlags;\r
1673         wStartupInfo.wShowWindow = lpStartupInfo->wShowWindow;\r
1674         wStartupInfo.cbReserved2 = lpStartupInfo->cbReserved2;\r
1675         wStartupInfo.lpReserved2 = lpStartupInfo->lpReserved2;\r
1676         wStartupInfo.hStdInput = lpStartupInfo->hStdInput;\r
1677         wStartupInfo.hStdOutput = lpStartupInfo->hStdOutput;\r
1678         wStartupInfo.hStdError = lpStartupInfo->hStdError;\r
1679         r = CreateProcessW(pw0, pw1, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, pw2, &wStartupInfo, lpProcessInformation);\r
1680         WtoM(lpCommandLine, strlen(lpCommandLine) + 1, pw1, -1);\r
1681 END_ROUTINE\r
1682         FreeDuplicatedString(pw0);\r
1683         FreeDuplicatedString(pw1);\r
1684         FreeDuplicatedString(pw2);\r
1685         FreeDuplicatedString(pw3);\r
1686         FreeDuplicatedString(pw4);\r
1687         FreeDuplicatedString(pw5);\r
1688         return r;\r
1689 }\r
1690 \r
1691 HINSTANCE FindExecutableM(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)\r
1692 {\r
1693         HINSTANCE r = NULL;\r
1694         wchar_t* pw0 = NULL;\r
1695         wchar_t* pw1 = NULL;\r
1696         wchar_t* pw2 = NULL;\r
1697         wchar_t* pw3 = NULL;\r
1698 START_ROUTINE\r
1699         pw0 = DuplicateMtoW(lpFile, -1);\r
1700         pw1 = DuplicateMtoW(lpDirectory, -1);\r
1701         pw2 = AllocateStringW(MAX_PATH * 4);\r
1702         r = FindExecutableW(pw0, pw1, pw2);\r
1703         // バッファ長不明のためオーバーランの可能性あり\r
1704         WtoM(lpResult, MAX_PATH, pw2, -1);\r
1705         TerminateStringM(lpResult, MAX_PATH);\r
1706 END_ROUTINE\r
1707         FreeDuplicatedString(pw0);\r
1708         FreeDuplicatedString(pw1);\r
1709         FreeDuplicatedString(pw2);\r
1710         FreeDuplicatedString(pw3);\r
1711         return r;\r
1712 }\r
1713 \r
1714 HINSTANCE ShellExecuteM(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)\r
1715 {\r
1716         HINSTANCE r = NULL;\r
1717         wchar_t* pw0 = NULL;\r
1718         wchar_t* pw1 = NULL;\r
1719         wchar_t* pw2 = NULL;\r
1720         wchar_t* pw3 = NULL;\r
1721 START_ROUTINE\r
1722         pw0 = DuplicateMtoW(lpOperation, -1);\r
1723         pw1 = DuplicateMtoW(lpFile, -1);\r
1724         pw2 = DuplicateMtoW(lpParameters, -1);\r
1725         pw3 = DuplicateMtoW(lpDirectory, -1);\r
1726         r = ShellExecuteW(hwnd, pw0, pw1, pw2, pw3, nShowCmd);\r
1727 END_ROUTINE\r
1728         FreeDuplicatedString(pw0);\r
1729         FreeDuplicatedString(pw1);\r
1730         FreeDuplicatedString(pw2);\r
1731         FreeDuplicatedString(pw3);\r
1732         return r;\r
1733 }\r
1734 \r
1735 BOOL ShellExecuteExM(LPSHELLEXECUTEINFOA lpExecInfo)\r
1736 {\r
1737         BOOL r = FALSE;\r
1738         wchar_t* pw0 = NULL;\r
1739         wchar_t* pw1 = NULL;\r
1740         wchar_t* pw2 = NULL;\r
1741         wchar_t* pw3 = NULL;\r
1742         wchar_t* pw4 = NULL;\r
1743         SHELLEXECUTEINFOW wExecInfo;\r
1744 START_ROUTINE\r
1745         wExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);\r
1746         wExecInfo.fMask = lpExecInfo->fMask;\r
1747         wExecInfo.hwnd = lpExecInfo->hwnd;\r
1748         pw0 = DuplicateMtoW(lpExecInfo->lpVerb, -1);\r
1749         wExecInfo.lpVerb = pw0;\r
1750         pw1 = DuplicateMtoW(lpExecInfo->lpFile, -1);\r
1751         wExecInfo.lpFile = pw1;\r
1752         pw2 = DuplicateMtoW(lpExecInfo->lpParameters, -1);\r
1753         wExecInfo.lpParameters = pw2;\r
1754         pw3 = DuplicateMtoW(lpExecInfo->lpDirectory, -1);\r
1755         wExecInfo.lpDirectory = pw3;\r
1756         wExecInfo.nShow = lpExecInfo->nShow;\r
1757         wExecInfo.hInstApp = lpExecInfo->hInstApp;\r
1758         wExecInfo.lpIDList = lpExecInfo->lpIDList;\r
1759         if(lpExecInfo->fMask & SEE_MASK_CLASSNAME)\r
1760         {\r
1761                 pw4 = DuplicateMtoW(lpExecInfo->lpClass, -1);\r
1762                 wExecInfo.lpClass = pw4;\r
1763         }\r
1764         wExecInfo.hkeyClass = lpExecInfo->hkeyClass;\r
1765         wExecInfo.dwHotKey = lpExecInfo->dwHotKey;\r
1766         wExecInfo.hIcon = lpExecInfo->hIcon;\r
1767         wExecInfo.hProcess = lpExecInfo->hProcess;\r
1768         r = ShellExecuteExW(&wExecInfo);\r
1769         lpExecInfo->hInstApp = wExecInfo.hInstApp;\r
1770         lpExecInfo->hProcess = wExecInfo.hProcess;\r
1771 END_ROUTINE\r
1772         FreeDuplicatedString(pw0);\r
1773         FreeDuplicatedString(pw1);\r
1774         FreeDuplicatedString(pw2);\r
1775         FreeDuplicatedString(pw3);\r
1776         FreeDuplicatedString(pw4);\r
1777         return r;\r
1778 }\r
1779 \r
1780 PIDLIST_ABSOLUTE SHBrowseForFolderM(LPBROWSEINFOA lpbi)\r
1781 {\r
1782         PIDLIST_ABSOLUTE r = NULL;\r
1783         wchar_t* pw0 = NULL;\r
1784         wchar_t* pw1 = NULL;\r
1785         BROWSEINFOW wbi;\r
1786 START_ROUTINE\r
1787         wbi.hwndOwner = lpbi->hwndOwner;\r
1788         wbi.pidlRoot = lpbi->pidlRoot;\r
1789         pw0 = DuplicateMtoWBuffer(lpbi->pszDisplayName, -1, MAX_PATH * 4);\r
1790         wbi.pszDisplayName = pw0;\r
1791         pw1 = DuplicateMtoW(lpbi->lpszTitle, -1);\r
1792         wbi.lpszTitle = pw1;\r
1793         wbi.ulFlags = lpbi->ulFlags;\r
1794         // TODO: lpfn\r
1795         wbi.lpfn = lpbi->lpfn;\r
1796         wbi.lParam = lpbi->lParam;\r
1797         wbi.iImage = lpbi->iImage;\r
1798         r = SHBrowseForFolderW(&wbi);\r
1799         // バッファ長不明のためオーバーランの可能性あり\r
1800         WtoM(lpbi->pszDisplayName, MAX_PATH, wbi.pszDisplayName, -1);\r
1801         lpbi->iImage = wbi.iImage;\r
1802 END_ROUTINE\r
1803         FreeDuplicatedString(pw0);\r
1804         FreeDuplicatedString(pw1);\r
1805         return r;\r
1806 }\r
1807 \r
1808 BOOL SHGetPathFromIDListM(PCIDLIST_ABSOLUTE pidl, LPSTR pszPath)\r
1809 {\r
1810         BOOL r = FALSE;\r
1811         wchar_t* pw0 = NULL;\r
1812 START_ROUTINE\r
1813         pw0 = AllocateStringW(MAX_PATH * 4);\r
1814         r = SHGetPathFromIDListW(pidl, pw0);\r
1815         // バッファ長不明のためオーバーランの可能性あり\r
1816         WtoM(pszPath, MAX_PATH, pw0, -1);\r
1817         TerminateStringM(pszPath, MAX_PATH);\r
1818 END_ROUTINE\r
1819         FreeDuplicatedString(pw0);\r
1820         return r;\r
1821 }\r
1822 \r
1823 int SHFileOperationM(LPSHFILEOPSTRUCTA lpFileOp)\r
1824 {\r
1825         int r = 0;\r
1826         wchar_t* pw0 = NULL;\r
1827         wchar_t* pw1 = NULL;\r
1828         wchar_t* pw2 = NULL;\r
1829         SHFILEOPSTRUCTW wFileOp;\r
1830 START_ROUTINE\r
1831         wFileOp.hwnd = lpFileOp->hwnd;\r
1832         wFileOp.wFunc = lpFileOp->wFunc;\r
1833         pw0 = DuplicateMtoWMultiString(lpFileOp->pFrom);\r
1834         wFileOp.pFrom = pw0;\r
1835         pw1 = DuplicateMtoWMultiString(lpFileOp->pTo);\r
1836         wFileOp.pTo = pw1;\r
1837         wFileOp.fFlags = lpFileOp->fFlags;\r
1838         wFileOp.fAnyOperationsAborted = lpFileOp->fAnyOperationsAborted;\r
1839         wFileOp.hNameMappings = lpFileOp->hNameMappings;\r
1840         if(lpFileOp->fFlags & FOF_SIMPLEPROGRESS)\r
1841                 pw2 = DuplicateMtoW(lpFileOp->lpszProgressTitle, -1);\r
1842         r = SHFileOperationW(&wFileOp);\r
1843         lpFileOp->fAnyOperationsAborted = wFileOp.fAnyOperationsAborted;\r
1844 END_ROUTINE\r
1845         FreeDuplicatedString(pw0);\r
1846         FreeDuplicatedString(pw1);\r
1847         FreeDuplicatedString(pw2);\r
1848         return r;\r
1849 }\r
1850 \r
1851 BOOL AppendMenuM(HMENU hMenu, UINT uFlags, UINT_PTR uIDNewItem, LPCSTR lpNewItem)\r
1852 {\r
1853         int r = 0;\r
1854         wchar_t* pw0 = NULL;\r
1855 START_ROUTINE\r
1856         if(uFlags & (MF_BITMAP | MF_OWNERDRAW))\r
1857                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, (LPCWSTR)lpNewItem);\r
1858         else\r
1859         {\r
1860                 pw0 = DuplicateMtoW(lpNewItem, -1);\r
1861                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, pw0);\r
1862         }\r
1863 END_ROUTINE\r
1864         FreeDuplicatedString(pw0);\r
1865         return r;\r
1866 }\r
1867 \r
1868 BOOL GetMenuItemInfoM(HMENU hmenu, UINT item, BOOL fByPosition, LPMENUITEMINFOA lpmii)\r
1869 {\r
1870         BOOL r = FALSE;\r
1871         wchar_t* pw0 = NULL;\r
1872         MENUITEMINFOW wmii;\r
1873 START_ROUTINE\r
1874         wmii.cbSize = sizeof(MENUITEMINFOW);\r
1875         wmii.fMask = lpmii->fMask;\r
1876         wmii.fType = lpmii->fType;\r
1877         wmii.fState = lpmii->fState;\r
1878         wmii.wID = lpmii->wID;\r
1879         wmii.hSubMenu = lpmii->hSubMenu;\r
1880         wmii.hbmpChecked = lpmii->hbmpChecked;\r
1881         wmii.hbmpUnchecked = lpmii->hbmpUnchecked;\r
1882         wmii.dwItemData = lpmii->dwItemData;\r
1883         if(lpmii->fMask & MIIM_TYPE)\r
1884         {\r
1885                 pw0 = DuplicateMtoWBuffer(lpmii->dwTypeData, -1, lpmii->cch * 4);\r
1886                 wmii.dwTypeData = pw0;\r
1887                 wmii.cch = lpmii->cch * 4;\r
1888         }\r
1889         wmii.hbmpItem = lpmii->hbmpItem;\r
1890         r = GetMenuItemInfoW(hmenu, item, fByPosition, &wmii);\r
1891         lpmii->fType = wmii.fType;\r
1892         lpmii->fState = wmii.fState;\r
1893         lpmii->wID = wmii.wID;\r
1894         lpmii->hSubMenu = wmii.hSubMenu;\r
1895         lpmii->hbmpChecked = wmii.hbmpChecked;\r
1896         lpmii->hbmpUnchecked = wmii.hbmpUnchecked;\r
1897         lpmii->dwItemData = wmii.dwItemData;\r
1898         WtoM(lpmii->dwTypeData, lpmii->cch, wmii.dwTypeData, -1);\r
1899         TerminateStringM(lpmii->dwTypeData, lpmii->cch);\r
1900 END_ROUTINE\r
1901         FreeDuplicatedString(pw0);\r
1902         return r;\r
1903 }\r
1904 \r
1905 HFONT CreateFontIndirectM(CONST LOGFONTA *lplf)\r
1906 {\r
1907         HFONT r = NULL;\r
1908         LOGFONTW wlf;\r
1909 START_ROUTINE\r
1910         wlf.lfHeight = lplf->lfHeight;\r
1911         wlf.lfWidth = lplf->lfWidth;\r
1912         wlf.lfEscapement = lplf->lfEscapement;\r
1913         wlf.lfOrientation = lplf->lfOrientation;\r
1914         wlf.lfWeight = lplf->lfWeight;\r
1915         wlf.lfItalic = lplf->lfItalic;\r
1916         wlf.lfUnderline = lplf->lfUnderline;\r
1917         wlf.lfStrikeOut = lplf->lfStrikeOut;\r
1918         wlf.lfCharSet = lplf->lfCharSet;\r
1919         wlf.lfOutPrecision = lplf->lfOutPrecision;\r
1920         wlf.lfClipPrecision = lplf->lfClipPrecision;\r
1921         wlf.lfQuality = lplf->lfQuality;\r
1922         wlf.lfPitchAndFamily = lplf->lfPitchAndFamily;\r
1923         MtoW(wlf.lfFaceName, LF_FACESIZE, lplf->lfFaceName, -1);\r
1924         TerminateStringW(wlf.lfFaceName, LF_FACESIZE);\r
1925         r = CreateFontIndirect(&wlf);\r
1926 END_ROUTINE\r
1927         return r;\r
1928 }\r
1929 \r
1930 BOOL ChooseFontM(LPCHOOSEFONTA v0)\r
1931 {\r
1932         BOOL r = FALSE;\r
1933         wchar_t* pw0 = NULL;\r
1934         CHOOSEFONTW a0;\r
1935         LOGFONTW* pwlf;\r
1936 START_ROUTINE\r
1937         a0.lStructSize = sizeof(CHOOSEFONTW);\r
1938         a0.hwndOwner = v0->hwndOwner;\r
1939         a0.hDC = v0->hDC;\r
1940         if(v0->lpLogFont && (pwlf = (LOGFONTW*)malloc(sizeof(LOGFONTW))))\r
1941         {\r
1942                 pwlf->lfHeight = v0->lpLogFont->lfHeight;\r
1943                 pwlf->lfWidth = v0->lpLogFont->lfWidth;\r
1944                 pwlf->lfEscapement = v0->lpLogFont->lfEscapement;\r
1945                 pwlf->lfOrientation = v0->lpLogFont->lfOrientation;\r
1946                 pwlf->lfWeight = v0->lpLogFont->lfWeight;\r
1947                 pwlf->lfItalic = v0->lpLogFont->lfItalic;\r
1948                 pwlf->lfUnderline = v0->lpLogFont->lfUnderline;\r
1949                 pwlf->lfStrikeOut = v0->lpLogFont->lfStrikeOut;\r
1950                 pwlf->lfCharSet = v0->lpLogFont->lfCharSet;\r
1951                 pwlf->lfOutPrecision = v0->lpLogFont->lfOutPrecision;\r
1952                 pwlf->lfClipPrecision = v0->lpLogFont->lfClipPrecision;\r
1953                 pwlf->lfQuality = v0->lpLogFont->lfQuality;\r
1954                 pwlf->lfPitchAndFamily = v0->lpLogFont->lfPitchAndFamily;\r
1955                 MtoW(pwlf->lfFaceName, LF_FACESIZE, v0->lpLogFont->lfFaceName, -1);\r
1956                 TerminateStringW(pwlf->lfFaceName, LF_FACESIZE);\r
1957         }\r
1958         else\r
1959                 pwlf = NULL;\r
1960         a0.lpLogFont = pwlf;\r
1961         a0.iPointSize = v0->iPointSize;\r
1962         a0.Flags = v0->Flags;\r
1963         a0.rgbColors = v0->rgbColors;\r
1964         a0.lCustData = v0->lCustData;\r
1965         a0.lpfnHook = v0->lpfnHook;\r
1966         a0.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);\r
1967         a0.hInstance = v0->hInstance;\r
1968         a0.lpszStyle = DuplicateMtoWBuffer(v0->lpszStyle, -1, LF_FACESIZE * 4);\r
1969         a0.nFontType = v0->nFontType;\r
1970         a0.nSizeMin = v0->nSizeMin;\r
1971         a0.nSizeMax = v0->nSizeMax;\r
1972         r = ChooseFontW(&a0);\r
1973         if(v0->lpLogFont)\r
1974         {\r
1975                 v0->lpLogFont->lfHeight = pwlf->lfHeight;\r
1976                 v0->lpLogFont->lfWidth = pwlf->lfWidth;\r
1977                 v0->lpLogFont->lfEscapement = pwlf->lfEscapement;\r
1978                 v0->lpLogFont->lfOrientation = pwlf->lfOrientation;\r
1979                 v0->lpLogFont->lfWeight = pwlf->lfWeight;\r
1980                 v0->lpLogFont->lfItalic = pwlf->lfItalic;\r
1981                 v0->lpLogFont->lfUnderline = pwlf->lfUnderline;\r
1982                 v0->lpLogFont->lfStrikeOut = pwlf->lfStrikeOut;\r
1983                 v0->lpLogFont->lfCharSet = pwlf->lfCharSet;\r
1984                 v0->lpLogFont->lfOutPrecision = pwlf->lfOutPrecision;\r
1985                 v0->lpLogFont->lfClipPrecision = pwlf->lfClipPrecision;\r
1986                 v0->lpLogFont->lfQuality = pwlf->lfQuality;\r
1987                 v0->lpLogFont->lfPitchAndFamily = pwlf->lfPitchAndFamily;\r
1988                 WtoM(v0->lpLogFont->lfFaceName, LF_FACESIZE, pwlf->lfFaceName, -1);\r
1989                 TerminateStringM(v0->lpLogFont->lfFaceName, LF_FACESIZE);\r
1990         }\r
1991         v0->rgbColors = a0.rgbColors;\r
1992         WtoM(v0->lpszStyle, LF_FACESIZE, a0.lpszStyle, -1);\r
1993         TerminateStringM(v0->lpszStyle, LF_FACESIZE);\r
1994         v0->nFontType = a0.nFontType;\r
1995         if(pwlf)\r
1996                 free(pwlf);\r
1997         FreeDuplicatedString((void*)a0.lpTemplateName);\r
1998         FreeDuplicatedString(a0.lpszStyle);\r
1999 END_ROUTINE\r
2000         FreeDuplicatedString(pw0);\r
2001         return r;\r
2002 }\r
2003 \r
2004 INT_PTR DialogBoxParamM(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)\r
2005 {\r
2006         INT_PTR r = 0;\r
2007         wchar_t* pw0 = NULL;\r
2008 START_ROUTINE\r
2009         pw0 = DuplicateMtoW(lpTemplateName, -1);\r
2010         r = DialogBoxParamW(hInstance, pw0, hWndParent, lpDialogFunc, dwInitParam);\r
2011 END_ROUTINE\r
2012         FreeDuplicatedString(pw0);\r
2013         return r;\r
2014 }\r
2015 \r
2016 HWND CreateDialogParamM(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)\r
2017 {\r
2018         HWND r = NULL;\r
2019         wchar_t* pw0 = NULL;\r
2020 START_ROUTINE\r
2021         pw0 = DuplicateMtoW(lpTemplateName, -1);\r
2022         r = CreateDialogParamW(hInstance, pw0, hWndParent, lpDialogFunc, dwInitParam);\r
2023 END_ROUTINE\r
2024         FreeDuplicatedString(pw0);\r
2025         return r;\r
2026 }\r
2027 \r
2028 BOOL sndPlaySoundM(LPCSTR pszSound, UINT fuSound)\r
2029 {\r
2030         BOOL r = FALSE;\r
2031         wchar_t* pw0 = NULL;\r
2032 START_ROUTINE\r
2033         pw0 = DuplicateMtoW(pszSound, -1);\r
2034         r = sndPlaySoundW(pw0, fuSound);\r
2035 END_ROUTINE\r
2036         FreeDuplicatedString(pw0);\r
2037         return r;\r
2038 }\r
2039 \r
2040 HANDLE SetClipboardDataM(UINT uFormat, HANDLE hMem)\r
2041 {\r
2042         HANDLE r = NULL;\r
2043         char* p;\r
2044         int Length;\r
2045         int BufferLength;\r
2046         HGLOBAL hBufferMem;\r
2047 START_ROUTINE\r
2048         if(uFormat == CF_TEXT)\r
2049         {\r
2050                 p = (char*)GlobalLock(hMem);\r
2051                 Length = (int)GlobalSize(hMem);\r
2052                 BufferLength = MtoW(NULL, 0, p, Length);\r
2053                 if(hBufferMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t) * BufferLength))\r
2054                 {\r
2055                         MtoW((LPWSTR)GlobalLock(hBufferMem), BufferLength, p, Length);\r
2056                         GlobalUnlock(hBufferMem);\r
2057                         r = SetClipboardData(CF_UNICODETEXT, hBufferMem);\r
2058                 }\r
2059                 GlobalUnlock(hMem);\r
2060                 GlobalFree(hMem);\r
2061         }\r
2062         else\r
2063                 r = SetClipboardData(uFormat, hMem);\r
2064 END_ROUTINE\r
2065         return r;\r
2066 }\r
2067 \r
2068 BOOL CopyFileM(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExists)\r
2069 {\r
2070         BOOL r = FALSE;\r
2071         wchar_t* pw0 = NULL;\r
2072         wchar_t* pw1 = NULL;\r
2073 START_ROUTINE\r
2074         pw0 = DuplicateMtoW(lpExistingFileName, -1);\r
2075         pw1 = DuplicateMtoW(lpNewFileName, -1);\r
2076         r = CopyFileW(pw0, pw1, bFailIfExists);\r
2077 END_ROUTINE\r
2078         FreeDuplicatedString(pw0);\r
2079         FreeDuplicatedString(pw1);\r
2080         return r;\r
2081 }\r
2082 \r
2083 int mkdirM(const char * _Path)\r
2084 {\r
2085         int r = 0;\r
2086         wchar_t* pw0 = NULL;\r
2087 START_ROUTINE\r
2088         pw0 = DuplicateMtoW(_Path, -1);\r
2089         r = _wmkdir(pw0);\r
2090 END_ROUTINE\r
2091         FreeDuplicatedString(pw0);\r
2092         return r;\r
2093 }\r
2094 \r
2095 int _mkdirM(const char * _Path)\r
2096 {\r
2097         int r = 0;\r
2098         wchar_t* pw0 = NULL;\r
2099 START_ROUTINE\r
2100         pw0 = DuplicateMtoW(_Path, -1);\r
2101         r = _wmkdir(pw0);\r
2102 END_ROUTINE\r
2103         FreeDuplicatedString(pw0);\r
2104         return r;\r
2105 }\r
2106 \r
2107 int rmdirM(const char * _Path)\r
2108 {\r
2109         int r = 0;\r
2110         wchar_t* pw0 = NULL;\r
2111 START_ROUTINE\r
2112         pw0 = DuplicateMtoW(_Path, -1);\r
2113         r = _wrmdir(pw0);\r
2114 END_ROUTINE\r
2115         FreeDuplicatedString(pw0);\r
2116         return r;\r
2117 }\r
2118 \r
2119 int _rmdirM(const char * _Path)\r
2120 {\r
2121         int r = 0;\r
2122         wchar_t* pw0 = NULL;\r
2123 START_ROUTINE\r
2124         pw0 = DuplicateMtoW(_Path, -1);\r
2125         r = _wrmdir(pw0);\r
2126 END_ROUTINE\r
2127         FreeDuplicatedString(pw0);\r
2128         return r;\r
2129 }\r
2130 \r
2131 size_t _mbslenM(const unsigned char * _Str)\r
2132 {\r
2133         size_t r = 0;\r
2134 START_ROUTINE\r
2135         while(GetNextCharM(_Str, &_Str) > 0)\r
2136         {\r
2137                 r++;\r
2138         }\r
2139 END_ROUTINE\r
2140         return r;\r
2141 }\r
2142 \r
2143 unsigned char * _mbschrM(const unsigned char * _Str, unsigned int _Ch)\r
2144 {\r
2145         unsigned char* r = NULL;\r
2146         unsigned int c;\r
2147         unsigned char* p;\r
2148 START_ROUTINE\r
2149         while((c = GetNextCharM(_Str, &p)) > 0)\r
2150         {\r
2151                 if(c == _Ch)\r
2152                 {\r
2153                         r = (unsigned char*)_Str;\r
2154                         break;\r
2155                 }\r
2156                 _Str = p;\r
2157         }\r
2158 END_ROUTINE\r
2159         return r;\r
2160 }\r
2161 \r
2162 unsigned char * _mbsrchrM(const unsigned char * _Str, unsigned int _Ch)\r
2163 {\r
2164         unsigned char* r = NULL;\r
2165         unsigned int c;\r
2166         unsigned char* p;\r
2167 START_ROUTINE\r
2168         while((c = GetNextCharM(_Str, &p)) > 0)\r
2169         {\r
2170                 if(c == _Ch)\r
2171                         r = (unsigned char*)_Str;\r
2172                 _Str = p;\r
2173         }\r
2174 END_ROUTINE\r
2175         return r;\r
2176 }\r
2177 \r
2178 unsigned char * _mbsstrM(const unsigned char * _Str, const unsigned char * _Substr)\r
2179 {\r
2180         unsigned char* r = NULL;\r
2181 START_ROUTINE\r
2182         r = strstr(_Str, _Substr);\r
2183 END_ROUTINE\r
2184         return r;\r
2185 }\r
2186 \r
2187 int _mbscmpM(const unsigned char * _Str1, const unsigned char * _Str2)\r
2188 {\r
2189         int r = 0;\r
2190 START_ROUTINE\r
2191         r = strcmp(_Str1, _Str2);\r
2192 END_ROUTINE\r
2193         return r;\r
2194 }\r
2195 \r
2196 int _mbsicmpM(const unsigned char * _Str1, const unsigned char * _Str2)\r
2197 {\r
2198         int r = 0;\r
2199 START_ROUTINE\r
2200         r = _stricmp(_Str1, _Str2);\r
2201 END_ROUTINE\r
2202         return r;\r
2203 }\r
2204 \r
2205 int _mbsncmpM(const unsigned char * _Str1, const unsigned char * _Str2, size_t _MaxCount)\r
2206 {\r
2207         int r = 0;\r
2208         DWORD c1;\r
2209         DWORD c2;\r
2210 START_ROUTINE\r
2211         c1 = 0;\r
2212         c2 = 0;\r
2213         while(_MaxCount > 0)\r
2214         {\r
2215                 c1 = GetNextCharM(_Str1, &_Str1);\r
2216                 c2 = GetNextCharM(_Str2, &_Str2);\r
2217                 if(c1 != c2)\r
2218                         break;\r
2219                 _MaxCount--;\r
2220                 if(c1 == 0 || c2 == 0)\r
2221                         break;\r
2222         }\r
2223         r = c1 - c2;\r
2224 END_ROUTINE\r
2225         return r;\r
2226 }\r
2227 \r
2228 unsigned char * _mbslwrM(unsigned char * _String)\r
2229 {\r
2230         unsigned char* r = NULL;\r
2231 START_ROUTINE\r
2232         r = _strlwr(_String);\r
2233 END_ROUTINE\r
2234         return r;\r
2235 }\r
2236 \r
2237 unsigned char * _mbsuprM(unsigned char * _String)\r
2238 {\r
2239         unsigned char* r = NULL;\r
2240 START_ROUTINE\r
2241         r = _strupr(_String);\r
2242 END_ROUTINE\r
2243         return r;\r
2244 }\r
2245 \r
2246 unsigned char * _mbsnincM(const unsigned char * _Str, size_t _Count)\r
2247 {\r
2248         unsigned char* r = NULL;\r
2249 START_ROUTINE\r
2250         while(_Count > 0 && GetNextCharM(_Str, &_Str) > 0)\r
2251         {\r
2252                 _Count--;\r
2253         }\r
2254         r = (unsigned char*)_Str;\r
2255 END_ROUTINE\r
2256         return r;\r
2257 }\r
2258 \r
2259 FILE * fopenM(const char * _Filename, const char * _Mode)\r
2260 {\r
2261         FILE* r = NULL;\r
2262         wchar_t* pw0 = NULL;\r
2263         wchar_t* pw1 = NULL;\r
2264 START_ROUTINE\r
2265         pw0 = DuplicateMtoW(_Filename, -1);\r
2266         pw1 = DuplicateMtoW(_Mode, -1);\r
2267         r = _wfopen(pw0, pw1);\r
2268 END_ROUTINE\r
2269         FreeDuplicatedString(pw0);\r
2270         FreeDuplicatedString(pw1);\r
2271         return r;\r
2272 }\r
2273 \r