OSDN Git Service

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