OSDN Git Service

Prepare for release of 1.98f.
[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 pLimit, LPCSTR* ppNext)\r
394 {\r
395         DWORD Code;\r
396         int i;\r
397         Code = 0;\r
398         i = -1;\r
399         if(!pLimit)\r
400                 pLimit = (LPCSTR)(~0);\r
401         if(lpString < pLimit)\r
402         {\r
403                 if((*lpString & 0xfe) == 0xfc)\r
404                 {\r
405                         i = 5;\r
406                         Code |= (DWORD)*lpString & 0x01;\r
407                 }\r
408                 else if((*lpString & 0xfc) == 0xf8)\r
409                 {\r
410                         i = 4;\r
411                         Code |= (DWORD)*lpString & 0x03;\r
412                 }\r
413                 else if((*lpString & 0xf8) == 0xf0)\r
414                 {\r
415                         i = 3;\r
416                         Code |= (DWORD)*lpString & 0x07;\r
417                 }\r
418                 else if((*lpString & 0xf0) == 0xe0)\r
419                 {\r
420                         i = 2;\r
421                         Code |= (DWORD)*lpString & 0x0f;\r
422                 }\r
423                 else if((*lpString & 0xe0) == 0xc0)\r
424                 {\r
425                         i = 1;\r
426                         Code |= (DWORD)*lpString & 0x1f;\r
427                 }\r
428                 else if((*lpString & 0x80) == 0x00)\r
429                 {\r
430                         i = 0;\r
431                         Code |= (DWORD)*lpString & 0x7f;\r
432                 }\r
433                 lpString++;\r
434                 while(lpString < pLimit && i > 0 && (*lpString & 0xc0) == 0x80)\r
435                 {\r
436                         i--;\r
437                         Code = Code << 6;\r
438                         Code |= (DWORD)*lpString & 0x3f;\r
439                         lpString++;\r
440                 }\r
441         }\r
442         if(i != 0)\r
443                 Code = 0x80000000;\r
444         if(ppNext)\r
445                 *ppNext = lpString;\r
446         return Code;\r
447 }\r
448 \r
449 // マルチバイト文字列へコードポイントの文字を追加して次のポインタを取得\r
450 // 文字の長さを返す\r
451 int PutNextCharM(LPSTR lpString, LPSTR pLimit, LPSTR* ppNext, DWORD Code)\r
452 {\r
453         int Count;\r
454         int i;\r
455         Count = 0;\r
456         i = -1;\r
457         if(!pLimit)\r
458                 pLimit = (LPSTR)(~0);\r
459         if(lpString < pLimit)\r
460         {\r
461                 if(Code & 0x7c000000)\r
462                 {\r
463                         i = 5;\r
464                         *lpString = 0xfc | ((CHAR)(Code >> 30) & 0x01);\r
465                 }\r
466                 else if(Code & 0x03e00000)\r
467                 {\r
468                         i = 4;\r
469                         *lpString = 0xf8 | ((CHAR)(Code >> 24) & 0x03);\r
470                 }\r
471                 else if(Code & 0x001f0000)\r
472                 {\r
473                         i = 3;\r
474                         *lpString = 0xf0 | ((CHAR)(Code >> 18) & 0x07);\r
475                 }\r
476                 else if(Code & 0x0000f800)\r
477                 {\r
478                         i = 2;\r
479                         *lpString = 0xe0 | ((CHAR)(Code >> 12) & 0x0f);\r
480                 }\r
481                 else if(Code & 0x00000780)\r
482                 {\r
483                         i = 1;\r
484                         *lpString = 0xc0 | ((CHAR)(Code >> 6) & 0x1f);\r
485                 }\r
486                 else\r
487                 {\r
488                         i = 0;\r
489                         *lpString = (CHAR)Code & 0x7f;\r
490                 }\r
491                 Count = i + 1;\r
492                 lpString++;\r
493                 while(lpString < pLimit && i > 0)\r
494                 {\r
495                         i--;\r
496                         *lpString = 0x80 | ((CHAR)(Code >> (i * 6)) & 0x3f);\r
497                         lpString++;\r
498                 }\r
499         }\r
500         if(i != 0)\r
501                 Count = 0;\r
502         if(ppNext)\r
503                 *ppNext = lpString;\r
504         return Count;\r
505 }\r
506 \r
507 // ワイド文字列からコードポイントと次のポインタを取得\r
508 // エンコードが不正な場合は0x80000000を返す\r
509 DWORD GetNextCharW(LPCWSTR lpString, LPCWSTR pLimit, LPCWSTR* ppNext)\r
510 {\r
511         DWORD Code;\r
512         Code = 0x80000000;\r
513         if(!pLimit)\r
514                 pLimit = (LPCWSTR)(~0);\r
515         if(lpString < pLimit)\r
516         {\r
517                 if((*lpString & 0xf800) == 0xd800)\r
518                 {\r
519                         if((*lpString & 0x0400) == 0x0400)\r
520                         {\r
521                                 Code = (DWORD)*lpString & 0x03ff;\r
522                                 lpString++;\r
523                                 if(lpString < pLimit)\r
524                                 {\r
525                                         if((*lpString & 0x0400) == 0x0000)\r
526                                         {\r
527                                                 Code |= ((DWORD)*lpString & 0x03ff) << 10;\r
528                                                 lpString++;\r
529                                         }\r
530                                         else\r
531                                                 Code = 0x80000000;\r
532                                 }\r
533                                 else\r
534                                         Code = 0x80000000;\r
535                         }\r
536                 }\r
537                 else\r
538                 {\r
539                         Code = (DWORD)*lpString;\r
540                         lpString++;\r
541                 }\r
542         }\r
543         if(ppNext)\r
544                 *ppNext = lpString;\r
545         return Code;\r
546 }\r
547 \r
548 // ワイド文字列へコードポイントの文字を追加して次のポインタを取得\r
549 // 文字の長さを返す\r
550 int PutNextCharW(LPWSTR lpString, LPWSTR pLimit, LPWSTR* ppNext, DWORD Code)\r
551 {\r
552         int Count;\r
553         Count = 0;\r
554         if(!pLimit)\r
555                 pLimit = (LPWSTR)(~0);\r
556         if(lpString < pLimit)\r
557         {\r
558                 if((Code & 0x7fff0000) || (Code & 0x0000f800) == 0x0000d800)\r
559                 {\r
560                         *lpString = 0xdc00 | ((WCHAR)Code & 0x03ff);\r
561                         lpString++;\r
562                         if(lpString < pLimit)\r
563                         {\r
564                                 *lpString = 0xd800 | ((WCHAR)(Code >> 10) & 0x03ff);\r
565                                 lpString++;\r
566                                 Count = 2;\r
567                         }\r
568                 }\r
569                 else\r
570                 {\r
571                         *lpString = (WCHAR)Code;\r
572                         lpString++;\r
573                         Count = 1;\r
574                 }\r
575         }\r
576         if(ppNext)\r
577                 *ppNext = lpString;\r
578         return Count;\r
579 }\r
580 \r
581 // マルチバイト文字列の冗長表現を修正\r
582 // 修正があればTRUEを返す\r
583 // 修正後の文字列の長さは元の文字列の長さ以下のためpDstとpSrcに同じ値を指定可能\r
584 BOOL FixStringM(LPSTR pDst, LPCSTR pSrc)\r
585 {\r
586         BOOL bResult;\r
587         char* p;\r
588         DWORD Code;\r
589         int i;\r
590         char c;\r
591         bResult = FALSE;\r
592         p = (char*)pSrc;\r
593         while(*pSrc != '\0')\r
594         {\r
595                 Code = GetNextCharM(pSrc, NULL, &pSrc);\r
596                 if(Code & 0x80000000)\r
597                         continue;\r
598                 else if(Code & 0x7c000000)\r
599                 {\r
600                         i = 5;\r
601                         c = (char)(0xfc | (Code >> (6 * i)));\r
602                 }\r
603                 else if(Code & 0x03e00000)\r
604                 {\r
605                         i = 4;\r
606                         c = (char)(0xf8 | (Code >> (6 * i)));\r
607                 }\r
608                 else if(Code & 0x001f0000)\r
609                 {\r
610                         i = 3;\r
611                         c = (char)(0xf0 | (Code >> (6 * i)));\r
612                 }\r
613                 else if(Code & 0x0000f800)\r
614                 {\r
615                         i = 2;\r
616                         c = (char)(0xe0 | (Code >> (6 * i)));\r
617                 }\r
618                 else if(Code & 0x00000780)\r
619                 {\r
620                         i = 1;\r
621                         c = (char)(0xc0 | (Code >> (6 * i)));\r
622                 }\r
623                 else\r
624                 {\r
625                         i = 0;\r
626                         c = (char)Code;\r
627                 }\r
628                 if(c != *p)\r
629                         bResult = TRUE;\r
630                 p++;\r
631                 *pDst = c;\r
632                 pDst++;\r
633                 while(i > 0)\r
634                 {\r
635                         i--;\r
636                         c = (char)(0x80 | ((Code >> (6 * i)) & 0x3f));\r
637                         if(c != *p)\r
638                                 bResult = TRUE;\r
639                         p++;\r
640                         *pDst = c;\r
641                         pDst++;\r
642                 }\r
643         }\r
644         if(*p != '\0')\r
645                 bResult = TRUE;\r
646         *pDst = '\0';\r
647         return bResult;\r
648 }\r
649 \r
650 // NULL区切りマルチバイト文字列の冗長表現を修正\r
651 // 修正があればTRUEを返す\r
652 // 修正後の文字列の長さは元の文字列の長さ以下のためpDstとpSrcに同じ値を指定可能\r
653 BOOL FixMultiStringM(LPSTR pDst, LPCSTR pSrc)\r
654 {\r
655         BOOL bResult;\r
656         int Length;\r
657         bResult = FALSE;\r
658         while(*pSrc != '\0')\r
659         {\r
660                 Length = strlen(pSrc) + 1;\r
661                 bResult = bResult | FixStringM(pDst, pSrc);\r
662                 pSrc += Length;\r
663                 pDst += strlen(pDst) + 1;\r
664         }\r
665         *pDst = '\0';\r
666         return bResult;\r
667 }\r
668 \r
669 // マルチバイト文字列の冗長表現を確認\r
670 // 冗長表現があればTRUEを返す\r
671 BOOL CheckStringM(LPCSTR lpString)\r
672 {\r
673         BOOL bResult;\r
674         char* p;\r
675         bResult = FALSE;\r
676         p = AllocateStringM(strlen(lpString) + 1);\r
677         if(p)\r
678         {\r
679                 bResult = FixStringM(p, lpString);\r
680                 FreeDuplicatedString(p);\r
681         }\r
682         return bResult;\r
683 }\r
684 \r
685 // NULL区切りマルチバイト文字列の冗長表現を確認\r
686 // 冗長表現があればTRUEを返す\r
687 BOOL CheckMultiStringM(LPCSTR lpString)\r
688 {\r
689         BOOL bResult;\r
690         char* p;\r
691         bResult = FALSE;\r
692         p = AllocateStringM(GetMultiStringLengthM(lpString) + 1);\r
693         if(p)\r
694         {\r
695                 bResult = FixMultiStringM(p, lpString);\r
696                 FreeDuplicatedString(p);\r
697         }\r
698         return bResult;\r
699 }\r
700 \r
701 // 文字列用に確保したメモリを開放\r
702 // リソースIDならば何もしない\r
703 void FreeDuplicatedString(void* p)\r
704 {\r
705         if(p < (void*)0x00010000 || p == (void*)~0)\r
706                 return;\r
707         free(p);\r
708 }\r
709 \r
710 // マルチバイト文字列からワイド文字列へ変換\r
711 // UTF-8からUTF-16 LEへの変換専用\r
712 int MultiByteToWideCharAlternative(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar)\r
713 {\r
714         int WideCount;\r
715         LPCSTR pMultiLimit;\r
716         LPWSTR pWideLimit;\r
717         DWORD Code;\r
718         WCHAR Temp[8];\r
719         if(CodePage != CP_UTF8 || dwFlags != 0)\r
720                 return MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);\r
721         WideCount = 0;\r
722         if(cbMultiByte == -1)\r
723                 pMultiLimit = lpMultiByteStr + strlen(lpMultiByteStr) + 1;\r
724         else\r
725                 pMultiLimit = lpMultiByteStr + cbMultiByte;\r
726         pWideLimit = lpWideCharStr + cchWideChar;\r
727         while(lpMultiByteStr < pMultiLimit)\r
728         {\r
729                 Code = GetNextCharM(lpMultiByteStr, pMultiLimit, &lpMultiByteStr);\r
730                 if(Code == 0x80000000)\r
731                         continue;\r
732                 if(lpWideCharStr)\r
733                 {\r
734                         WideCount += PutNextCharW(lpWideCharStr, pWideLimit, &lpWideCharStr, Code);\r
735                         if(lpWideCharStr >= pWideLimit)\r
736                         {\r
737                                 WideCount = 0;\r
738                                 break;\r
739                         }\r
740                 }\r
741                 else\r
742                         WideCount += PutNextCharW(Temp, NULL, NULL, Code);\r
743         }\r
744         return WideCount;\r
745 }\r
746 \r
747 // ワイド文字列からマルチバイト文字列へ変換\r
748 // UTF-16 LEからUTF-8への変換専用\r
749 int WideCharToMultiByteAlternative(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)\r
750 {\r
751         int MultiCount;\r
752         LPCWSTR pWideLimit;\r
753         LPSTR pMultiLimit;\r
754         DWORD Code;\r
755         CHAR Temp[8];\r
756         if(CodePage != CP_UTF8 || dwFlags != 0)\r
757                 return WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar);\r
758         MultiCount = 0;\r
759         if(cchWideChar == -1)\r
760                 pWideLimit = lpWideCharStr + wcslen(lpWideCharStr) + 1;\r
761         else\r
762                 pWideLimit = lpWideCharStr + cchWideChar;\r
763         pMultiLimit = lpMultiByteStr + cbMultiByte;\r
764         while(lpWideCharStr < pWideLimit)\r
765         {\r
766                 Code = GetNextCharW(lpWideCharStr, pWideLimit, &lpWideCharStr);\r
767                 if(Code == 0x80000000)\r
768                         continue;\r
769                 if(lpMultiByteStr)\r
770                 {\r
771                         MultiCount += PutNextCharM(lpMultiByteStr, pMultiLimit, &lpMultiByteStr, Code);\r
772                         if(lpMultiByteStr >= pMultiLimit)\r
773                         {\r
774                                 MultiCount = 0;\r
775                                 break;\r
776                         }\r
777                 }\r
778                 else\r
779                         MultiCount += PutNextCharM(Temp, NULL, NULL, Code);\r
780         }\r
781         if(lpUsedDefaultChar)\r
782                 *lpUsedDefaultChar = FALSE;\r
783         return MultiCount;\r
784 }\r
785 \r
786 // 以下ラッパー\r
787 // 戻り値バッファ r\r
788 // ワイド文字バッファ pw%d\r
789 // マルチバイト文字バッファ pm%d\r
790 // 引数バッファ a%d\r
791 \r
792 #pragma warning(disable:4102)\r
793 #define START_ROUTINE                                   do{\r
794 #define END_ROUTINE                                             }while(0);end_of_routine:\r
795 #define QUIT_ROUTINE                                    goto end_of_routine;\r
796 \r
797 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)\r
798 {\r
799         int r = 0;\r
800         char* pm0 = NULL;\r
801 START_ROUTINE\r
802         pm0 = DuplicateWtoM(lpCmdLine, -1);\r
803         r = WinMainM(hInstance, hPrevInstance, pm0, nCmdShow);\r
804 END_ROUTINE\r
805         FreeDuplicatedString(pm0);\r
806         return r;\r
807 }\r
808 \r
809 HMODULE LoadLibraryM(LPCSTR lpLibFileName)\r
810 {\r
811         HMODULE r = NULL;\r
812         wchar_t* pw0 = NULL;\r
813 START_ROUTINE\r
814         pw0 = DuplicateMtoW(lpLibFileName, -1);\r
815         r = LoadLibraryW(pw0);\r
816 END_ROUTINE\r
817         FreeDuplicatedString(pw0);\r
818         return r;\r
819 }\r
820 \r
821 HANDLE CreateFileM(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)\r
822 {\r
823         HANDLE r = INVALID_HANDLE_VALUE;\r
824         wchar_t* pw0 = NULL;\r
825 START_ROUTINE\r
826         pw0 = DuplicateMtoW(lpFileName, -1);\r
827         r = CreateFileW(pw0, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);\r
828 END_ROUTINE\r
829         FreeDuplicatedString(pw0);\r
830         return r;\r
831 }\r
832 \r
833 int MessageBoxM(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)\r
834 {\r
835         int r = IDOK;\r
836         wchar_t* pw0 = NULL;\r
837         wchar_t* pw1 = NULL;\r
838 START_ROUTINE\r
839         pw0 = DuplicateMtoW(lpText, -1);\r
840         pw1 = DuplicateMtoW(lpCaption, -1);\r
841         r = MessageBoxW(hWnd, pw0, pw1, uType);\r
842 END_ROUTINE\r
843         FreeDuplicatedString(pw0);\r
844         FreeDuplicatedString(pw1);\r
845         return r;\r
846 }\r
847 \r
848 HANDLE FindFirstFileM(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)\r
849 {\r
850         HANDLE r = INVALID_HANDLE_VALUE;\r
851         wchar_t* pw0 = NULL;\r
852         WIN32_FIND_DATAW a0;\r
853 START_ROUTINE\r
854         pw0 = DuplicateMtoW(lpFileName, -1);\r
855         r = FindFirstFileW(pw0, &a0);\r
856         if(r != INVALID_HANDLE_VALUE)\r
857         {\r
858                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;\r
859                 lpFindFileData->ftCreationTime = a0.ftCreationTime;\r
860                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;\r
861                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;\r
862                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;\r
863                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;\r
864                 lpFindFileData->dwReserved0 = a0.dwReserved0;\r
865                 lpFindFileData->dwReserved1 = a0.dwReserved1;\r
866                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);\r
867                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);\r
868         }\r
869 END_ROUTINE\r
870         FreeDuplicatedString(pw0);\r
871         return r;\r
872 }\r
873 \r
874 BOOL FindNextFileM(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)\r
875 {\r
876         BOOL r = FALSE;\r
877         WIN32_FIND_DATAW a0;\r
878 START_ROUTINE\r
879         r = FindNextFileW(hFindFile, &a0);\r
880         if(r)\r
881         {\r
882                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;\r
883                 lpFindFileData->ftCreationTime = a0.ftCreationTime;\r
884                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;\r
885                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;\r
886                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;\r
887                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;\r
888                 lpFindFileData->dwReserved0 = a0.dwReserved0;\r
889                 lpFindFileData->dwReserved1 = a0.dwReserved1;\r
890                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);\r
891                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);\r
892         }\r
893 END_ROUTINE\r
894         return r;\r
895 }\r
896 \r
897 DWORD GetLogicalDriveStringsM(DWORD nBufferLength, LPSTR lpBuffer)\r
898 {\r
899         DWORD r = 0;\r
900         wchar_t* pw0 = NULL;\r
901 START_ROUTINE\r
902         pw0 = AllocateStringW(nBufferLength * 4);\r
903         GetLogicalDriveStringsW(nBufferLength * 4, pw0);\r
904         WtoMMultiString(lpBuffer, nBufferLength, pw0);\r
905         r = TerminateStringM(lpBuffer, nBufferLength);\r
906 END_ROUTINE\r
907         FreeDuplicatedString(pw0);\r
908         return r;\r
909 }\r
910 \r
911 ATOM RegisterClassExM(CONST WNDCLASSEXA * v0)\r
912 {\r
913         ATOM r = 0;\r
914         wchar_t* pw0 = NULL;\r
915         wchar_t* pw1 = NULL;\r
916         WNDCLASSEXW a0;\r
917 START_ROUTINE\r
918         a0.cbSize = sizeof(WNDCLASSEXW);\r
919         a0.style = v0->style;\r
920         a0.lpfnWndProc = v0->lpfnWndProc;\r
921         a0.cbClsExtra = v0->cbClsExtra;\r
922         a0.cbWndExtra = v0->cbWndExtra;\r
923         a0.hInstance = v0->hInstance;\r
924         a0.hIcon = v0->hIcon;\r
925         a0.hCursor = v0->hCursor;\r
926         a0.hbrBackground = v0->hbrBackground;\r
927         pw0 = DuplicateMtoW(v0->lpszMenuName, -1);\r
928         a0.lpszMenuName = pw0;\r
929         pw1 = DuplicateMtoW(v0->lpszClassName, -1);\r
930         a0.lpszClassName = pw1;\r
931         a0.hIconSm = v0->hIconSm;\r
932         r = RegisterClassExW(&a0);\r
933 END_ROUTINE\r
934         FreeDuplicatedString(pw0);\r
935         FreeDuplicatedString(pw1);\r
936         return r;\r
937 }\r
938 \r
939 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
940 {\r
941         HWND r = NULL;\r
942         wchar_t* pw0 = NULL;\r
943         wchar_t* pw1 = NULL;\r
944 START_ROUTINE\r
945         pw0 = DuplicateMtoW(lpClassName, -1);\r
946         pw1 = DuplicateMtoW(lpWindowName, -1);\r
947         r = CreateWindowExW(dwExStyle, pw0, pw1, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);\r
948 END_ROUTINE\r
949         FreeDuplicatedString(pw0);\r
950         FreeDuplicatedString(pw1);\r
951         return r;\r
952 }\r
953 \r
954 LONG GetWindowLongM(HWND hWnd, int nIndex)\r
955 {\r
956         LONG r = 0;\r
957 START_ROUTINE\r
958         if(IsWindowUnicode(hWnd))\r
959                 r = GetWindowLongW(hWnd, nIndex);\r
960         else\r
961                 r = GetWindowLongA(hWnd, nIndex);\r
962 END_ROUTINE\r
963         return r;\r
964 }\r
965 \r
966 LONG SetWindowLongM(HWND hWnd, int nIndex, LONG dwNewLong)\r
967 {\r
968         LONG r = 0;\r
969 START_ROUTINE\r
970         if(IsWindowUnicode(hWnd))\r
971                 r = SetWindowLongW(hWnd, nIndex, dwNewLong);\r
972         else\r
973                 r = SetWindowLongA(hWnd, nIndex, dwNewLong);\r
974 END_ROUTINE\r
975         return r;\r
976 }\r
977 \r
978 LONG_PTR GetWindowLongPtrM(HWND hWnd, int nIndex)\r
979 {\r
980         LONG_PTR r = 0;\r
981 START_ROUTINE\r
982         if(IsWindowUnicode(hWnd))\r
983                 r = GetWindowLongPtrW(hWnd, nIndex);\r
984         else\r
985                 r = GetWindowLongPtrA(hWnd, nIndex);\r
986 END_ROUTINE\r
987         return r;\r
988 }\r
989 \r
990 LONG_PTR SetWindowLongPtrM(HWND hWnd, int nIndex, LONG_PTR dwNewLong)\r
991 {\r
992         LONG_PTR r = 0;\r
993 START_ROUTINE\r
994         if(IsWindowUnicode(hWnd))\r
995                 r = SetWindowLongPtrW(hWnd, nIndex, dwNewLong);\r
996         else\r
997                 r = SetWindowLongPtrA(hWnd, nIndex, dwNewLong);\r
998 END_ROUTINE\r
999         return r;\r
1000 }\r
1001 \r
1002 LRESULT DefWindowProcM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1003 {\r
1004         LRESULT r = 0;\r
1005 START_ROUTINE\r
1006         if(IsWindowUnicode(hWnd))\r
1007                 r = DefWindowProcW(hWnd, Msg, wParam, lParam);\r
1008         else\r
1009                 r = DefWindowProcA(hWnd, Msg, wParam, lParam);\r
1010 END_ROUTINE\r
1011         return r;\r
1012 }\r
1013 \r
1014 LRESULT CallWindowProcM(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1015 {\r
1016         LRESULT r = 0;\r
1017 START_ROUTINE\r
1018         if(IsWindowUnicode(hWnd))\r
1019                 r = CallWindowProcW(lpPrevWndFunc, hWnd, Msg, wParam, lParam);\r
1020         else\r
1021                 r = CallWindowProcA(lpPrevWndFunc, hWnd, Msg, wParam, lParam);\r
1022 END_ROUTINE\r
1023         return r;\r
1024 }\r
1025 \r
1026 LRESULT SendMessageM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1027 {\r
1028         LRESULT r = 0;\r
1029         wchar_t* pw0 = NULL;\r
1030         wchar_t* pw1 = NULL;\r
1031         int Size;\r
1032         LVITEMA* pmLVItem;\r
1033         LVITEMW wLVItem;\r
1034         LVFINDINFOA* pmLVFindInfo;\r
1035         LVFINDINFOW wLVFindInfo;\r
1036         LVCOLUMNA* pmLVColumn;\r
1037         LVCOLUMNW wLVColumn;\r
1038         TVITEMEXA* pmTVItem;\r
1039         TVITEMEXW wTVItem;\r
1040         TVINSERTSTRUCTA* pmTVInsert;\r
1041         TVINSERTSTRUCTW wTVInsert;\r
1042         wchar_t ClassName[MAX_PATH];\r
1043 START_ROUTINE\r
1044         switch(Msg)\r
1045         {\r
1046         case WM_SETTEXT:\r
1047                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1048                 r = SendMessageW(hWnd, WM_SETTEXT, wParam, (LPARAM)pw0);\r
1049                 break;\r
1050         case WM_GETTEXT:\r
1051                 pw0 = AllocateStringW(wParam * 4);\r
1052                 SendMessageW(hWnd, WM_GETTEXT, wParam * 4, (LPARAM)pw0);\r
1053                 WtoM((LPSTR)lParam, wParam, pw0, -1);\r
1054                 r = TerminateStringM((LPSTR)lParam, wParam);\r
1055                 break;\r
1056         case WM_GETTEXTLENGTH:\r
1057                 Size = SendMessageW(hWnd, WM_GETTEXTLENGTH, wParam, lParam) + 1;\r
1058                 pw0 = AllocateStringW(Size);\r
1059                 SendMessageW(hWnd, WM_GETTEXT, (WPARAM)Size, (LPARAM)pw0);\r
1060                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
1061                 break;\r
1062         default:\r
1063                 GetClassNameW(hWnd, ClassName, sizeof(ClassName) / sizeof(wchar_t));\r
1064                 if(_wcsicmp(ClassName, WC_EDITW) == 0)\r
1065                 {\r
1066                         switch(Msg)\r
1067                         {\r
1068                         case EM_REPLACESEL:\r
1069                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1070                                 r = SendMessageW(hWnd, EM_REPLACESEL, wParam, (LPARAM)pw0);\r
1071                                 break;\r
1072                         default:\r
1073                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1074                                 break;\r
1075                         }\r
1076                 }\r
1077                 else if(_wcsicmp(ClassName, WC_COMBOBOXW) == 0)\r
1078                 {\r
1079                         switch(Msg)\r
1080                         {\r
1081                         case CB_ADDSTRING:\r
1082                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1083                                 r = SendMessageW(hWnd, CB_ADDSTRING, wParam, (LPARAM)pw0);\r
1084                                 break;\r
1085                         case CB_GETLBTEXT:\r
1086                                 Size = SendMessageW(hWnd, CB_GETLBTEXTLEN, wParam, 0) + 1;\r
1087                                 pw0 = AllocateStringW(Size);\r
1088                                 SendMessageW(hWnd, CB_GETLBTEXT, wParam, (LPARAM)pw0);\r
1089                                 // バッファ長不明のためオーバーランの可能性あり\r
1090                                 WtoM((LPSTR)lParam, Size * 4, pw0, -1);\r
1091                                 r = TerminateStringM((LPSTR)lParam, Size * 4);\r
1092                                 break;\r
1093                         case CB_GETLBTEXTLEN:\r
1094                                 Size = SendMessageW(hWnd, CB_GETLBTEXTLEN, wParam, 0) + 1;\r
1095                                 pw0 = AllocateStringW(Size);\r
1096                                 SendMessageW(hWnd, WM_GETTEXT, wParam, (LPARAM)pw0);\r
1097                                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
1098                                 break;\r
1099                         case CB_INSERTSTRING:\r
1100                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1101                                 r = SendMessageW(hWnd, CB_INSERTSTRING, wParam, (LPARAM)pw0);\r
1102                                 break;\r
1103                         case CB_FINDSTRINGEXACT:\r
1104                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1105                                 r = SendMessageW(hWnd, CB_FINDSTRINGEXACT, wParam, (LPARAM)pw0);\r
1106                                 break;\r
1107                         default:\r
1108                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1109                                 break;\r
1110                         }\r
1111                 }\r
1112                 else if(_wcsicmp(ClassName, WC_LISTBOXW) == 0)\r
1113                 {\r
1114                         switch(Msg)\r
1115                         {\r
1116                         case LB_ADDSTRING:\r
1117                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1118                                 r = SendMessageW(hWnd, LB_ADDSTRING, wParam, (LPARAM)pw0);\r
1119                                 break;\r
1120                         case LB_INSERTSTRING:\r
1121                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1122                                 r = SendMessageW(hWnd, LB_INSERTSTRING, wParam, (LPARAM)pw0);\r
1123                                 break;\r
1124                         case LB_GETTEXT:\r
1125                                 Size = SendMessageW(hWnd, LB_GETTEXTLEN, wParam, 0) + 1;\r
1126                                 pw0 = AllocateStringW(Size);\r
1127                                 SendMessageW(hWnd, LB_GETTEXT, wParam, (LPARAM)pw0);\r
1128                                 // バッファ長不明のためオーバーランの可能性あり\r
1129                                 WtoM((LPSTR)lParam, Size * 4, pw0, -1);\r
1130                                 r = TerminateStringM((LPSTR)lParam, Size * 4);\r
1131                                 break;\r
1132                         case LB_GETTEXTLEN:\r
1133                                 Size = SendMessageW(hWnd, LB_GETTEXTLEN, wParam, 0) + 1;\r
1134                                 pw0 = AllocateStringW(Size);\r
1135                                 SendMessageW(hWnd, WM_GETTEXT, wParam, (LPARAM)pw0);\r
1136                                 r = WtoM(NULL, 0, pw0, -1) - 1;\r
1137                                 break;\r
1138                         default:\r
1139                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1140                                 break;\r
1141                         }\r
1142                 }\r
1143                 else if(_wcsicmp(ClassName, WC_LISTVIEWW) == 0)\r
1144                 {\r
1145                         switch(Msg)\r
1146                         {\r
1147                         case LVM_GETITEMA:\r
1148                                 pmLVItem = (LVITEMA*)lParam;\r
1149                                 wLVItem.mask = pmLVItem->mask;\r
1150                                 wLVItem.iItem = pmLVItem->iItem;\r
1151                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
1152                                 wLVItem.state = pmLVItem->state;\r
1153                                 wLVItem.stateMask = pmLVItem->stateMask;\r
1154                                 if(pmLVItem->mask & LVIF_TEXT)\r
1155                                 {\r
1156                                         Size = pmLVItem->cchTextMax * 4;\r
1157                                         pw0 = AllocateStringW(Size);\r
1158                                         wLVItem.pszText = pw0;\r
1159                                         wLVItem.cchTextMax = Size;\r
1160                                 }\r
1161                                 wLVItem.iImage = pmLVItem->iImage;\r
1162                                 wLVItem.lParam = pmLVItem->lParam;\r
1163                                 wLVItem.iIndent = pmLVItem->iIndent;\r
1164                                 r = SendMessageW(hWnd, LVM_GETITEMW, wParam, (LPARAM)&wLVItem);\r
1165                                 pmLVItem->mask = wLVItem.mask;\r
1166                                 pmLVItem->iItem = wLVItem.iItem;\r
1167                                 pmLVItem->iSubItem = wLVItem.iSubItem;\r
1168                                 pmLVItem->state = wLVItem.state;\r
1169                                 pmLVItem->stateMask = wLVItem.stateMask;\r
1170                                 if(pmLVItem->mask & LVIF_TEXT)\r
1171                                 {\r
1172                                         WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);\r
1173                                         TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);\r
1174                                 }\r
1175                                 pmLVItem->iImage = wLVItem.iImage;\r
1176                                 pmLVItem->lParam = wLVItem.lParam;\r
1177                                 pmLVItem->iIndent = wLVItem.iIndent;\r
1178                                 break;\r
1179                         case LVM_SETITEMA:\r
1180                                 pmLVItem = (LVITEMA*)lParam;\r
1181                                 wLVItem.mask = pmLVItem->mask;\r
1182                                 wLVItem.iItem = pmLVItem->iItem;\r
1183                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
1184                                 wLVItem.state = pmLVItem->state;\r
1185                                 wLVItem.stateMask = pmLVItem->stateMask;\r
1186                                 if(pmLVItem->mask & LVIF_TEXT)\r
1187                                 {\r
1188                                         pw0 = DuplicateMtoW(pmLVItem->pszText, -1);\r
1189                                         wLVItem.pszText = pw0;\r
1190                                         // TODO: cchTextMaxの確認\r
1191                                         wLVItem.cchTextMax = pmLVItem->cchTextMax;\r
1192                                 }\r
1193                                 wLVItem.iImage = pmLVItem->iImage;\r
1194                                 wLVItem.lParam = pmLVItem->lParam;\r
1195                                 wLVItem.iIndent = pmLVItem->iIndent;\r
1196                                 r = SendMessageW(hWnd, LVM_SETITEMW, wParam, (LPARAM)&wLVItem);\r
1197                                 break;\r
1198                         case LVM_INSERTITEMA:\r
1199                                 pmLVItem = (LVITEMA*)lParam;\r
1200                                 wLVItem.mask = pmLVItem->mask;\r
1201                                 wLVItem.iItem = pmLVItem->iItem;\r
1202                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
1203                                 wLVItem.state = pmLVItem->state;\r
1204                                 wLVItem.stateMask = pmLVItem->stateMask;\r
1205                                 if(pmLVItem->mask & LVIF_TEXT)\r
1206                                 {\r
1207                                         pw0 = DuplicateMtoW(pmLVItem->pszText, -1);\r
1208                                         wLVItem.pszText = pw0;\r
1209                                         // TODO: cchTextMaxの確認\r
1210                                         wLVItem.cchTextMax = pmLVItem->cchTextMax;\r
1211                                 }\r
1212                                 wLVItem.iImage = pmLVItem->iImage;\r
1213                                 wLVItem.lParam = pmLVItem->lParam;\r
1214                                 wLVItem.iIndent = pmLVItem->iIndent;\r
1215                                 r = SendMessageW(hWnd, LVM_INSERTITEMW, wParam, (LPARAM)&wLVItem);\r
1216                                 break;\r
1217                         case LVM_FINDITEMA:\r
1218                                 pmLVFindInfo = (LVFINDINFOA*)lParam;\r
1219                                 wLVFindInfo.flags = pmLVFindInfo->flags;\r
1220                                 if(pmLVFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))\r
1221                                 {\r
1222                                         pw0 = DuplicateMtoW(pmLVFindInfo->psz, -1);\r
1223                                         wLVFindInfo.psz = pw0;\r
1224                                 }\r
1225                                 wLVFindInfo.lParam = pmLVFindInfo->lParam;\r
1226                                 wLVFindInfo.pt = pmLVFindInfo->pt;\r
1227                                 wLVFindInfo.vkDirection = pmLVFindInfo->vkDirection;\r
1228                                 r = SendMessageW(hWnd, LVM_FINDITEMW, wParam, (LPARAM)&wLVItem);\r
1229                                 break;\r
1230                         case LVM_GETCOLUMNA:\r
1231                                 pmLVColumn = (LVCOLUMNA*)lParam;\r
1232                                 wLVColumn.mask = pmLVColumn->mask;\r
1233                                 wLVColumn.fmt = pmLVColumn->fmt;\r
1234                                 wLVColumn.cx = pmLVColumn->cx;\r
1235                                 Size = pmLVColumn->cchTextMax * 4;\r
1236                                 if(pmLVColumn->mask & LVCF_TEXT)\r
1237                                 {\r
1238                                         pw0 = AllocateStringW(Size);\r
1239                                         wLVColumn.pszText = pw0;\r
1240                                         wLVColumn.cchTextMax = Size;\r
1241                                 }\r
1242                                 wLVColumn.iSubItem = pmLVColumn->iSubItem;\r
1243                                 wLVColumn.iImage = pmLVColumn->iImage;\r
1244                                 wLVColumn.iOrder = pmLVColumn->iOrder;\r
1245                                 r = SendMessageW(hWnd, LVM_GETCOLUMNW, wParam, (LPARAM)&wLVColumn);\r
1246                                 pmLVColumn->mask = wLVColumn.mask;\r
1247                                 pmLVColumn->fmt = wLVColumn.fmt;\r
1248                                 pmLVColumn->cx = wLVColumn.cx;\r
1249                                 if(pmLVColumn->mask & LVCF_TEXT)\r
1250                                 {\r
1251                                         WtoM(pmLVColumn->pszText, pmLVColumn->cchTextMax, wLVColumn.pszText, -1);\r
1252                                         TerminateStringM(pmLVColumn->pszText, pmLVColumn->cchTextMax);\r
1253                                 }\r
1254                                 pmLVColumn->iSubItem = wLVColumn.iSubItem;\r
1255                                 pmLVColumn->iImage = wLVColumn.iImage;\r
1256                                 pmLVColumn->iOrder = wLVColumn.iOrder;\r
1257                                 break;\r
1258                         case LVM_INSERTCOLUMNA:\r
1259                                 pmLVColumn = (LVCOLUMNA*)lParam;\r
1260                                 wLVColumn.mask = pmLVColumn->mask;\r
1261                                 wLVColumn.fmt = pmLVColumn->fmt;\r
1262                                 wLVColumn.cx = pmLVColumn->cx;\r
1263                                 if(pmLVColumn->mask & LVCF_TEXT)\r
1264                                 {\r
1265                                         pw0 = DuplicateMtoW(pmLVColumn->pszText, -1);\r
1266                                         wLVColumn.pszText = pw0;\r
1267                                         // TODO: cchTextMaxの確認\r
1268                                         wLVColumn.cchTextMax = pmLVColumn->cchTextMax;\r
1269                                 }\r
1270                                 wLVColumn.iSubItem = pmLVColumn->iSubItem;\r
1271                                 wLVColumn.iImage = pmLVColumn->iImage;\r
1272                                 wLVColumn.iOrder = pmLVColumn->iOrder;\r
1273                                 r = SendMessageW(hWnd, LVM_INSERTCOLUMNW, wParam, (LPARAM)&wLVColumn);\r
1274                                 break;\r
1275                         case LVM_GETITEMTEXTA:\r
1276                                 pmLVItem = (LVITEMA*)lParam;\r
1277                                 wLVItem.mask = pmLVItem->mask;\r
1278                                 wLVItem.iItem = pmLVItem->iItem;\r
1279                                 wLVItem.iSubItem = pmLVItem->iSubItem;\r
1280                                 wLVItem.state = pmLVItem->state;\r
1281                                 wLVItem.stateMask = pmLVItem->stateMask;\r
1282                                 Size = pmLVItem->cchTextMax * 4;\r
1283                                 pw0 = AllocateStringW(Size);\r
1284                                 wLVItem.pszText = pw0;\r
1285                                 wLVItem.cchTextMax = Size;\r
1286                                 wLVItem.iImage = pmLVItem->iImage;\r
1287                                 wLVItem.lParam = pmLVItem->lParam;\r
1288                                 wLVItem.iIndent = pmLVItem->iIndent;\r
1289                                 r = SendMessageW(hWnd, LVM_GETITEMTEXTW, wParam, (LPARAM)&wLVItem);\r
1290                                 pmLVItem->mask = wLVItem.mask;\r
1291                                 pmLVItem->iItem = wLVItem.iItem;\r
1292                                 pmLVItem->iSubItem = wLVItem.iSubItem;\r
1293                                 pmLVItem->state = wLVItem.state;\r
1294                                 pmLVItem->stateMask = wLVItem.stateMask;\r
1295                                 WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);\r
1296                                 TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);\r
1297                                 pmLVItem->iImage = wLVItem.iImage;\r
1298                                 pmLVItem->lParam = wLVItem.lParam;\r
1299                                 pmLVItem->iIndent = wLVItem.iIndent;\r
1300                                 break;\r
1301                         default:\r
1302                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1303                                 break;\r
1304                         }\r
1305                 }\r
1306                 else if(_wcsicmp(ClassName, STATUSCLASSNAMEW) == 0)\r
1307                 {\r
1308                         switch(Msg)\r
1309                         {\r
1310                         case SB_SETTEXTA:\r
1311                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);\r
1312                                 r = SendMessageW(hWnd, SB_SETTEXTW, wParam, (LPARAM)pw0);\r
1313                                 break;\r
1314                         default:\r
1315                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1316                                 break;\r
1317                         }\r
1318                 }\r
1319                 else if(_wcsicmp(ClassName, WC_TREEVIEWW) == 0)\r
1320                 {\r
1321                         switch(Msg)\r
1322                         {\r
1323                         case TVM_GETITEMA:\r
1324                                 pmTVItem = (TVITEMEXA*)lParam;\r
1325                                 wTVItem.mask = pmTVItem->mask;\r
1326                                 wTVItem.hItem = pmTVItem->hItem;\r
1327                                 wTVItem.state = pmTVItem->state;\r
1328                                 wTVItem.stateMask = pmTVItem->stateMask;\r
1329                                 if(pmTVItem->mask & TVIF_TEXT)\r
1330                                 {\r
1331                                         Size = pmTVItem->cchTextMax * 4;\r
1332                                         pw0 = AllocateStringW(Size);\r
1333                                         wTVItem.pszText = pw0;\r
1334                                         wTVItem.cchTextMax = Size;\r
1335                                 }\r
1336                                 wTVItem.iImage = pmTVItem->iImage;\r
1337                                 wTVItem.iSelectedImage = pmTVItem->iSelectedImage;\r
1338                                 wTVItem.cChildren = pmTVItem->cChildren;\r
1339                                 wTVItem.lParam = pmTVItem->lParam;\r
1340                                 wTVItem.iIntegral = pmTVItem->iIntegral;\r
1341 //                              wTVItem.uStateEx = pmTVItem->uStateEx;\r
1342 //                              wTVItem.hwnd = pmTVItem->hwnd;\r
1343 //                              wTVItem.iExpandedImage = pmTVItem->iExpandedImage;\r
1344 //                              wTVItem.iReserved = pmTVItem->iReserved;\r
1345                                 r = SendMessageW(hWnd, TVM_GETITEMW, wParam, (LPARAM)&wTVItem);\r
1346                                 pmTVItem->mask = wTVItem.mask;\r
1347                                 pmTVItem->hItem = wTVItem.hItem;\r
1348                                 pmTVItem->state = wTVItem.state;\r
1349                                 pmTVItem->stateMask = wTVItem.stateMask;\r
1350                                 if(pmTVItem->mask & TVIF_TEXT)\r
1351                                 {\r
1352                                         WtoM(pmTVItem->pszText, pmTVItem->cchTextMax, wTVItem.pszText, -1);\r
1353                                         TerminateStringM(pmTVItem->pszText, pmTVItem->cchTextMax);\r
1354                                 }\r
1355                                 pmTVItem->iImage = wTVItem.iImage;\r
1356                                 pmTVItem->iSelectedImage = wTVItem.iSelectedImage;\r
1357                                 pmTVItem->cChildren = wTVItem.cChildren;\r
1358                                 pmTVItem->lParam = wTVItem.lParam;\r
1359                                 pmTVItem->iIntegral = wTVItem.iIntegral;\r
1360 //                              pmTVItem->uStateEx = wTVItem.uStateEx;\r
1361 //                              pmTVItem->hwnd = wTVItem.hwnd;\r
1362 //                              pmTVItem->iExpandedImage = wTVItem.iExpandedImage;\r
1363 //                              pmTVItem->iReserved = wTVItem.iReserved;\r
1364                                 break;\r
1365                         case TVM_INSERTITEMA:\r
1366                                 pmTVInsert = (TVINSERTSTRUCTA*)lParam;\r
1367                                 wTVInsert.hParent = pmTVInsert->hParent;\r
1368                                 wTVInsert.hInsertAfter = pmTVInsert->hInsertAfter;\r
1369                                 wTVInsert.itemex.mask = pmTVInsert->itemex.mask;\r
1370                                 wTVInsert.itemex.hItem = pmTVInsert->itemex.hItem;\r
1371                                 wTVInsert.itemex.state = pmTVInsert->itemex.state;\r
1372                                 wTVInsert.itemex.stateMask = pmTVInsert->itemex.stateMask;\r
1373                                 if(pmTVInsert->itemex.mask & TVIF_TEXT)\r
1374                                 {\r
1375                                         pw0 = DuplicateMtoW(pmTVInsert->itemex.pszText, -1);\r
1376                                         wTVInsert.itemex.pszText = pw0;\r
1377                                         // TODO: cchTextMaxの確認\r
1378                                         wTVInsert.itemex.cchTextMax = pmTVInsert->itemex.cchTextMax;\r
1379                                 }\r
1380                                 wTVInsert.itemex.iImage = pmTVInsert->itemex.iImage;\r
1381                                 wTVInsert.itemex.iSelectedImage = pmTVInsert->itemex.iSelectedImage;\r
1382                                 wTVInsert.itemex.cChildren = pmTVInsert->itemex.cChildren;\r
1383                                 wTVInsert.itemex.lParam = pmTVInsert->itemex.lParam;\r
1384                                 wTVInsert.itemex.iIntegral = pmTVInsert->itemex.iIntegral;\r
1385 //                              wTVInsert.itemex.uStateEx = pmTVInsert->itemex.uStateEx;\r
1386 //                              wTVInsert.itemex.hwnd = pmTVInsert->itemex.hwnd;\r
1387 //                              wTVInsert.itemex.iExpandedImage = pmTVInsert->itemex.iExpandedImage;\r
1388 //                              wTVInsert.itemex.iReserved = pmTVInsert->itemex.iReserved;\r
1389                                 r = SendMessageW(hWnd, TVM_INSERTITEMW, wParam, (LPARAM)&wTVInsert);\r
1390                                 break;\r
1391                         default:\r
1392                                 r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1393                                 break;\r
1394                         }\r
1395                 }\r
1396                 else\r
1397                         r = SendMessageW(hWnd, Msg, wParam, lParam);\r
1398                 break;\r
1399         }\r
1400 END_ROUTINE\r
1401         FreeDuplicatedString(pw0);\r
1402         FreeDuplicatedString(pw1);\r
1403         return r;\r
1404 }\r
1405 \r
1406 LRESULT DefDlgProcM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1407 {\r
1408         LRESULT r = 0;\r
1409 START_ROUTINE\r
1410         // WNDPROCがShift_JIS用であるため\r
1411         if(IsWindowUnicode(hWnd))\r
1412                 r = DefDlgProcW(hWnd, Msg, wParam, lParam);\r
1413         else\r
1414                 r = DefDlgProcA(hWnd, Msg, wParam, lParam);\r
1415 END_ROUTINE\r
1416         return r;\r
1417 }\r
1418 \r
1419 LRESULT SendDlgItemMessageM(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam)\r
1420 {\r
1421         LRESULT r = 0;\r
1422 START_ROUTINE\r
1423         r = SendMessageM(GetDlgItem(hDlg, nIDDlgItem), Msg, wParam, lParam);\r
1424 END_ROUTINE\r
1425         return r;\r
1426 }\r
1427 \r
1428 BOOL SetWindowTextM(HWND hWnd, LPCSTR lpString)\r
1429 {\r
1430         BOOL r = FALSE;\r
1431         wchar_t* pw0 = NULL;\r
1432 START_ROUTINE\r
1433         pw0 = DuplicateMtoW(lpString, -1);\r
1434         r = SetWindowTextW(hWnd, pw0);\r
1435 END_ROUTINE\r
1436         FreeDuplicatedString(pw0);\r
1437         return r;\r
1438 }\r
1439 \r
1440 UINT DragQueryFileM(HDROP hDrop, UINT iFile, LPSTR lpszFile, UINT cch)\r
1441 {\r
1442         UINT r = 0;\r
1443         wchar_t* pw0 = NULL;\r
1444 START_ROUTINE\r
1445         if(iFile == (UINT)-1)\r
1446                 r = DragQueryFileW(hDrop, iFile, (LPWSTR)lpszFile, cch);\r
1447         else\r
1448         {\r
1449                 pw0 = AllocateStringW(cch * 4);\r
1450                 DragQueryFileW(hDrop, iFile, pw0, cch * 4);\r
1451                 WtoM(lpszFile, cch, pw0, -1);\r
1452                 r = TerminateStringM(lpszFile, cch);\r
1453         }\r
1454 END_ROUTINE\r
1455         FreeDuplicatedString(pw0);\r
1456         return r;\r
1457 }\r
1458 \r
1459 LPSTR GetCommandLineM()\r
1460 {\r
1461         LPSTR r = 0;\r
1462         static char* pm0 = NULL;\r
1463 START_ROUTINE\r
1464         if(!pm0)\r
1465                 pm0 = DuplicateWtoM(GetCommandLineW(), -1);\r
1466         r = pm0;\r
1467 END_ROUTINE\r
1468         return r;\r
1469 }\r
1470 \r
1471 DWORD GetCurrentDirectoryM(DWORD nBufferLength, LPSTR lpBuffer)\r
1472 {\r
1473         DWORD r = 0;\r
1474         wchar_t* pw0 = NULL;\r
1475 START_ROUTINE\r
1476         // TODO: バッファが不十分な場合に必要なサイズを返す\r
1477         pw0 = AllocateStringW(nBufferLength * 4);\r
1478         GetCurrentDirectoryW(nBufferLength * 4, pw0);\r
1479         WtoM(lpBuffer, nBufferLength, pw0, -1);\r
1480         r = TerminateStringM(lpBuffer, nBufferLength);\r
1481 END_ROUTINE\r
1482         FreeDuplicatedString(pw0);\r
1483         return r;\r
1484 }\r
1485 \r
1486 BOOL SetCurrentDirectoryM(LPCSTR lpPathName)\r
1487 {\r
1488         BOOL r = FALSE;\r
1489         wchar_t* pw0 = NULL;\r
1490 START_ROUTINE\r
1491         pw0 = DuplicateMtoW(lpPathName, -1);\r
1492         r = SetCurrentDirectoryW(pw0);\r
1493 END_ROUTINE\r
1494         FreeDuplicatedString(pw0);\r
1495         return r;\r
1496 }\r
1497 \r
1498 DWORD GetTempPathM(DWORD nBufferLength, LPSTR lpBuffer)\r
1499 {\r
1500         DWORD r = 0;\r
1501         wchar_t* pw0 = NULL;\r
1502 START_ROUTINE\r
1503         pw0 = AllocateStringW(nBufferLength * 4);\r
1504         GetTempPathW(nBufferLength * 4, pw0);\r
1505         WtoM(lpBuffer, nBufferLength, pw0, -1);\r
1506         r = TerminateStringM(lpBuffer, nBufferLength);\r
1507 END_ROUTINE\r
1508         FreeDuplicatedString(pw0);\r
1509         return r;\r
1510 }\r
1511 \r
1512 DWORD GetFileAttributesM(LPCSTR lpFileName)\r
1513 {\r
1514         DWORD r = FALSE;\r
1515         wchar_t* pw0 = NULL;\r
1516 START_ROUTINE\r
1517         pw0 = DuplicateMtoW(lpFileName, -1);\r
1518         r = GetFileAttributesW(pw0);\r
1519 END_ROUTINE\r
1520         FreeDuplicatedString(pw0);\r
1521         return r;\r
1522 }\r
1523 \r
1524 DWORD GetModuleFileNameM(HMODULE hModule, LPCH lpFilename, DWORD nSize)\r
1525 {\r
1526         DWORD r = 0;\r
1527         wchar_t* pw0 = NULL;\r
1528 START_ROUTINE\r
1529         pw0 = AllocateStringW(nSize * 4);\r
1530         GetModuleFileNameW(hModule, pw0, nSize * 4);\r
1531         WtoM(lpFilename, nSize, pw0, -1);\r
1532         r = TerminateStringM(lpFilename, nSize);\r
1533 END_ROUTINE\r
1534         FreeDuplicatedString(pw0);\r
1535         return r;\r
1536 }\r
1537 \r
1538 LSTATUS RegOpenKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)\r
1539 {\r
1540         LSTATUS r = 0;\r
1541         wchar_t* pw0 = NULL;\r
1542 START_ROUTINE\r
1543         pw0 = DuplicateMtoW(lpSubKey, -1);\r
1544         r = RegOpenKeyExW(hKey, pw0, ulOptions, samDesired, phkResult);\r
1545 END_ROUTINE\r
1546         FreeDuplicatedString(pw0);\r
1547         return r;\r
1548 }\r
1549 \r
1550 LSTATUS RegCreateKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD Reserved, LPSTR lpClass, DWORD dwOptions, REGSAM samDesired, CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition)\r
1551 {\r
1552         LSTATUS r = 0;\r
1553         wchar_t* pw0 = NULL;\r
1554         wchar_t* pw1 = NULL;\r
1555 START_ROUTINE\r
1556         pw0 = DuplicateMtoW(lpSubKey, -1);\r
1557         pw1 = DuplicateMtoW(lpClass, -1);\r
1558         r = RegCreateKeyExW(hKey, pw0, Reserved, pw1, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);\r
1559 END_ROUTINE\r
1560         FreeDuplicatedString(pw0);\r
1561         FreeDuplicatedString(pw1);\r
1562         return r;\r
1563 }\r
1564 \r
1565 LSTATUS RegDeleteValueM(HKEY hKey, LPCSTR lpValueName)\r
1566 {\r
1567         LSTATUS r = 0;\r
1568         wchar_t* pw0 = NULL;\r
1569 START_ROUTINE\r
1570         pw0 = DuplicateMtoW(lpValueName, -1);\r
1571         r = RegDeleteValueW(hKey, pw0);\r
1572 END_ROUTINE\r
1573         FreeDuplicatedString(pw0);\r
1574         return r;\r
1575 }\r
1576 \r
1577 LSTATUS RegQueryValueExM(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)\r
1578 {\r
1579         LSTATUS r = 0;\r
1580         wchar_t* pw0 = NULL;\r
1581         wchar_t* pw1 = NULL;\r
1582         DWORD dwType;\r
1583         DWORD wcbData;\r
1584 START_ROUTINE\r
1585         pw0 = DuplicateMtoW(lpValueName, -1);\r
1586         if(RegQueryValueExW(hKey, pw0, NULL, &dwType, NULL, 0) == ERROR_SUCCESS)\r
1587         {\r
1588                 switch(dwType)\r
1589                 {\r
1590                 case REG_SZ:\r
1591                 case REG_EXPAND_SZ:\r
1592                 case REG_MULTI_SZ:\r
1593                         if(lpcbData)\r
1594                         {\r
1595                                 pw1 = AllocateStringW(*lpcbData / sizeof(char) * 4);\r
1596                                 wcbData = *lpcbData / sizeof(char) * 4;\r
1597                                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, (LPBYTE)pw1, &wcbData);\r
1598                                 if(lpData)\r
1599                                         *lpcbData = sizeof(char) * WtoM((char*)lpData, *lpcbData / sizeof(char), pw1, wcbData / sizeof(wchar_t));\r
1600                                 else\r
1601                                         *lpcbData = sizeof(char) * WtoM(NULL, 0, pw1, wcbData / sizeof(wchar_t));\r
1602                         }\r
1603                         break;\r
1604                 default:\r
1605                         r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);\r
1606                         break;\r
1607                 }\r
1608         }\r
1609         else\r
1610                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);\r
1611 END_ROUTINE\r
1612         FreeDuplicatedString(pw0);\r
1613         FreeDuplicatedString(pw1);\r
1614         return r;\r
1615 }\r
1616 \r
1617 LSTATUS RegSetValueExM(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE* lpData, DWORD cbData)\r
1618 {\r
1619         LSTATUS r = 0;\r
1620         wchar_t* pw0 = NULL;\r
1621         wchar_t* pw1 = NULL;\r
1622         DWORD wcbData;\r
1623 START_ROUTINE\r
1624         pw0 = DuplicateMtoW(lpValueName, -1);\r
1625         switch(dwType)\r
1626         {\r
1627         case REG_SZ:\r
1628         case REG_EXPAND_SZ:\r
1629         case REG_MULTI_SZ:\r
1630                 wcbData = MtoW(NULL, 0, (char*)lpData, cbData / sizeof(char));\r
1631                 pw1 = AllocateStringW(wcbData);\r
1632                 MtoW(pw1, wcbData, (char*)lpData, cbData / sizeof(char));\r
1633                 wcbData = sizeof(wchar_t) * wcbData;\r
1634                 lpData = (BYTE*)pw1;\r
1635                 cbData = wcbData;\r
1636                 break;\r
1637         }\r
1638         r = RegSetValueExW(hKey, pw0, Reserved, dwType, lpData, cbData);\r
1639 END_ROUTINE\r
1640         FreeDuplicatedString(pw0);\r
1641         FreeDuplicatedString(pw1);\r
1642         return r;\r
1643 }\r
1644 \r
1645 BOOL TextOutM(HDC hdc, int x, int y, LPCSTR lpString, int c)\r
1646 {\r
1647         BOOL r = FALSE;\r
1648         wchar_t* pw0 = NULL;\r
1649 START_ROUTINE\r
1650         pw0 = DuplicateMtoW(lpString, c);\r
1651         r = TextOutW(hdc, x, y, pw0, wcslen(pw0));\r
1652 END_ROUTINE\r
1653         FreeDuplicatedString(pw0);\r
1654         return r;\r
1655 }\r
1656 \r
1657 BOOL GetTextExtentPoint32M(HDC hdc, LPCSTR lpString, int c, LPSIZE psizl)\r
1658 {\r
1659         BOOL r = FALSE;\r
1660         wchar_t* pw0 = NULL;\r
1661 START_ROUTINE\r
1662         pw0 = DuplicateMtoW(lpString, c);\r
1663         r = GetTextExtentPoint32W(hdc, pw0, wcslen(pw0), psizl);\r
1664 END_ROUTINE\r
1665         FreeDuplicatedString(pw0);\r
1666         return r;\r
1667 }\r
1668 \r
1669 INT_PTR PropertySheetM(LPCPROPSHEETHEADERA v0)\r
1670 {\r
1671         INT_PTR r = 0;\r
1672         PROPSHEETHEADERW a0;\r
1673         PROPSHEETPAGEW* pwPage;\r
1674         UINT i;\r
1675 START_ROUTINE\r
1676         a0.dwSize = sizeof(PROPSHEETHEADERW);\r
1677         a0.dwFlags = v0->dwFlags;\r
1678         a0.hwndParent = v0->hwndParent;\r
1679         a0.hInstance = v0->hInstance;\r
1680         if(v0->dwFlags & PSH_USEICONID)\r
1681                 a0.pszIcon = DuplicateMtoW(v0->pszIcon, -1);\r
1682         else\r
1683                 a0.hIcon = v0->hIcon;\r
1684         a0.pszCaption = DuplicateMtoW(v0->pszCaption, -1);\r
1685         a0.nPages = v0->nPages;\r
1686         if(v0->dwFlags & PSH_USEPSTARTPAGE)\r
1687                 a0.pStartPage = DuplicateMtoW(v0->pStartPage, -1);\r
1688         else\r
1689                 a0.nStartPage = v0->nStartPage;\r
1690         if(v0->dwFlags & PSH_PROPSHEETPAGE)\r
1691         {\r
1692                 if(v0->ppsp && (pwPage = (PROPSHEETPAGEW*)malloc(sizeof(PROPSHEETPAGEW) * v0->nPages)))\r
1693                 {\r
1694                         for(i = 0; i < v0->nPages; i++)\r
1695                         {\r
1696                                 pwPage[i].dwSize = sizeof(PROPSHEETPAGEW);\r
1697                                 pwPage[i].dwFlags = v0->ppsp[i].dwFlags;\r
1698                                 pwPage[i].hInstance = v0->ppsp[i].hInstance;\r
1699                                 if(v0->ppsp[i].dwFlags & PSP_DLGINDIRECT)\r
1700                                         pwPage[i].pResource = v0->ppsp[i].pResource;\r
1701                                 else\r
1702                                         pwPage[i].pszTemplate = DuplicateMtoW(v0->ppsp[i].pszTemplate, -1);\r
1703                                 if(v0->ppsp[i].dwFlags & PSP_USEICONID)\r
1704                                         pwPage[i].pszIcon = DuplicateMtoW(v0->ppsp[i].pszIcon, -1);\r
1705                                 else\r
1706                                         pwPage[i].hIcon = v0->ppsp[i].hIcon;\r
1707                                 if(v0->ppsp[i].dwFlags & PSP_USETITLE)\r
1708                                         pwPage[i].pszTitle = DuplicateMtoW(v0->ppsp[i].pszTitle, -1);\r
1709                                 pwPage[i].pfnDlgProc = v0->ppsp[i].pfnDlgProc;\r
1710                                 pwPage[i].lParam = v0->ppsp[i].lParam;\r
1711                                 // TODO: pfnCallback\r
1712                                 pwPage[i].pfnCallback = (LPFNPSPCALLBACKW)v0->ppsp[i].pfnCallback;\r
1713                                 pwPage[i].pcRefParent = v0->ppsp[i].pcRefParent;\r
1714                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERTITLE)\r
1715                                         pwPage[i].pszHeaderTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderTitle, -1);\r
1716                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERSUBTITLE)\r
1717                                         pwPage[i].pszHeaderSubTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderSubTitle, -1);\r
1718                         }\r
1719                 }\r
1720                 else\r
1721                         pwPage = NULL;\r
1722                 a0.ppsp = pwPage;\r
1723         }\r
1724         else\r
1725                 a0.phpage = v0->phpage;\r
1726         a0.pfnCallback = v0->pfnCallback;\r
1727         if(v0->dwFlags & PSH_USEHBMWATERMARK)\r
1728                 a0.hbmWatermark = v0->hbmWatermark;\r
1729         else\r
1730                 a0.pszbmWatermark = DuplicateMtoW(v0->pszbmWatermark, -1);\r
1731         r = PropertySheetW(&a0);\r
1732         if(a0.dwFlags & PSH_USEICONID)\r
1733                 FreeDuplicatedString((void*)a0.pszIcon);\r
1734         FreeDuplicatedString((void*)a0.pszCaption);\r
1735         if(v0->dwFlags & PSH_USEPSTARTPAGE)\r
1736                 FreeDuplicatedString((void*)a0.pStartPage);\r
1737         if(v0->dwFlags & PSH_PROPSHEETPAGE)\r
1738         {\r
1739                 if(pwPage)\r
1740                 {\r
1741                         for(i = 0; i < v0->nPages; i++)\r
1742                         {\r
1743                                 if(!(v0->ppsp[i].dwFlags & PSP_DLGINDIRECT))\r
1744                                         FreeDuplicatedString((void*)pwPage[i].pszTemplate);\r
1745                                 if(v0->ppsp[i].dwFlags & PSP_USEICONID)\r
1746                                         FreeDuplicatedString((void*)pwPage[i].pszIcon);\r
1747                                 if(v0->ppsp[i].dwFlags & PSP_USETITLE)\r
1748                                         FreeDuplicatedString((void*)pwPage[i].pszTitle);\r
1749                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERTITLE)\r
1750                                         FreeDuplicatedString((void*)pwPage[i].pszHeaderTitle);\r
1751                                 if(v0->ppsp[i].dwFlags & PSP_USEHEADERSUBTITLE)\r
1752                                         FreeDuplicatedString((void*)pwPage[i].pszHeaderSubTitle);\r
1753                         }\r
1754                         free(pwPage);\r
1755                 }\r
1756         }\r
1757         if(!(v0->dwFlags & PSH_USEHBMWATERMARK))\r
1758                 FreeDuplicatedString((void*)a0.pszbmWatermark);\r
1759 END_ROUTINE\r
1760         return r;\r
1761 }\r
1762 \r
1763 BOOL GetOpenFileNameM(LPOPENFILENAMEA v0)\r
1764 {\r
1765         BOOL r = FALSE;\r
1766         wchar_t* pw0 = NULL;\r
1767         wchar_t* pw1 = NULL;\r
1768         wchar_t* pw2 = NULL;\r
1769         wchar_t* pw3 = NULL;\r
1770         wchar_t* pw4 = NULL;\r
1771         wchar_t* pw5 = NULL;\r
1772         wchar_t* pw6 = NULL;\r
1773         wchar_t* pw7 = NULL;\r
1774         wchar_t* pw8 = NULL;\r
1775         wchar_t* pw9 = NULL;\r
1776         OPENFILENAMEW wofn;\r
1777 START_ROUTINE\r
1778         wofn.lStructSize = sizeof(OPENFILENAMEW);\r
1779         wofn.hwndOwner = v0->hwndOwner;\r
1780         wofn.hInstance = v0->hInstance;\r
1781         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);\r
1782         wofn.lpstrFilter = pw0;\r
1783         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);\r
1784         wofn.lpstrCustomFilter = pw1;\r
1785         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;\r
1786         wofn.nFilterIndex = v0->nFilterIndex;\r
1787         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);\r
1788         wofn.lpstrFile = pw2;\r
1789         wofn.nMaxFile = v0->nMaxFile * 4;\r
1790         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);\r
1791         wofn.lpstrFileTitle = pw3;\r
1792         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;\r
1793         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);\r
1794         wofn.lpstrInitialDir = pw4;\r
1795         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);\r
1796         wofn.lpstrTitle = pw5;\r
1797         wofn.Flags = v0->Flags;\r
1798         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);\r
1799         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);\r
1800         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);\r
1801         wofn.lpstrDefExt = pw6;\r
1802         wofn.lCustData = v0->lCustData;\r
1803         wofn.lpfnHook = v0->lpfnHook;\r
1804         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);\r
1805         wofn.pvReserved = v0->pvReserved;\r
1806         wofn.FlagsEx = v0->FlagsEx;\r
1807         r = GetOpenFileNameW(&wofn);\r
1808         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);\r
1809         TerminateStringM(v0->lpstrFile, v0->nMaxFile);\r
1810         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);\r
1811         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);\r
1812 END_ROUTINE\r
1813         FreeDuplicatedString(pw0);\r
1814         FreeDuplicatedString(pw1);\r
1815         FreeDuplicatedString(pw2);\r
1816         FreeDuplicatedString(pw3);\r
1817         FreeDuplicatedString(pw4);\r
1818         FreeDuplicatedString(pw5);\r
1819         FreeDuplicatedString(pw6);\r
1820         return r;\r
1821 }\r
1822 \r
1823 BOOL GetSaveFileNameM(LPOPENFILENAMEA v0)\r
1824 {\r
1825         BOOL r = FALSE;\r
1826         wchar_t* pw0 = NULL;\r
1827         wchar_t* pw1 = NULL;\r
1828         wchar_t* pw2 = NULL;\r
1829         wchar_t* pw3 = NULL;\r
1830         wchar_t* pw4 = NULL;\r
1831         wchar_t* pw5 = NULL;\r
1832         wchar_t* pw6 = NULL;\r
1833         wchar_t* pw7 = NULL;\r
1834         wchar_t* pw8 = NULL;\r
1835         wchar_t* pw9 = NULL;\r
1836         OPENFILENAMEW wofn;\r
1837 START_ROUTINE\r
1838         wofn.lStructSize = sizeof(OPENFILENAMEW);\r
1839         wofn.hwndOwner = v0->hwndOwner;\r
1840         wofn.hInstance = v0->hInstance;\r
1841         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);\r
1842         wofn.lpstrFilter = pw0;\r
1843         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);\r
1844         wofn.lpstrCustomFilter = pw1;\r
1845         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;\r
1846         wofn.nFilterIndex = v0->nFilterIndex;\r
1847         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);\r
1848         wofn.lpstrFile = pw2;\r
1849         wofn.nMaxFile = v0->nMaxFile * 4;\r
1850         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);\r
1851         wofn.lpstrFileTitle = pw3;\r
1852         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;\r
1853         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);\r
1854         wofn.lpstrInitialDir = pw4;\r
1855         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);\r
1856         wofn.lpstrTitle = pw5;\r
1857         wofn.Flags = v0->Flags;\r
1858         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);\r
1859         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);\r
1860         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);\r
1861         wofn.lpstrDefExt = pw6;\r
1862         wofn.lCustData = v0->lCustData;\r
1863         wofn.lpfnHook = v0->lpfnHook;\r
1864         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);\r
1865         wofn.pvReserved = v0->pvReserved;\r
1866         wofn.FlagsEx = v0->FlagsEx;\r
1867         r = GetSaveFileNameW(&wofn);\r
1868         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);\r
1869         TerminateStringM(v0->lpstrFile, v0->nMaxFile);\r
1870         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);\r
1871         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);\r
1872 END_ROUTINE\r
1873         FreeDuplicatedString(pw0);\r
1874         FreeDuplicatedString(pw1);\r
1875         FreeDuplicatedString(pw2);\r
1876         FreeDuplicatedString(pw3);\r
1877         FreeDuplicatedString(pw4);\r
1878         FreeDuplicatedString(pw5);\r
1879         FreeDuplicatedString(pw6);\r
1880         return r;\r
1881 }\r
1882 \r
1883 HWND HtmlHelpM(HWND hwndCaller, LPCSTR pszFile, UINT uCommand, DWORD_PTR dwData)\r
1884 {\r
1885         HWND r = NULL;\r
1886         wchar_t* pw0 = NULL;\r
1887 START_ROUTINE\r
1888         pw0 = DuplicateMtoW(pszFile, -1);\r
1889         r = HtmlHelpW(hwndCaller, pw0, uCommand, dwData);\r
1890 END_ROUTINE\r
1891         FreeDuplicatedString(pw0);\r
1892         return r;\r
1893 }\r
1894 \r
1895 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
1896 {\r
1897         BOOL r = FALSE;\r
1898         wchar_t* pw0 = NULL;\r
1899         wchar_t* pw1 = NULL;\r
1900         wchar_t* pw2 = NULL;\r
1901         wchar_t* pw3 = NULL;\r
1902         wchar_t* pw4 = NULL;\r
1903         wchar_t* pw5 = NULL;\r
1904         STARTUPINFOW wStartupInfo;\r
1905 START_ROUTINE\r
1906         pw0 = DuplicateMtoW(lpApplicationName, -1);\r
1907         pw1 = DuplicateMtoWBuffer(lpCommandLine, -1, (strlen(lpCommandLine) + 1) * 4);\r
1908         pw2 = DuplicateMtoW(lpCurrentDirectory, -1);\r
1909         wStartupInfo.cb = sizeof(LPSTARTUPINFOW);\r
1910         pw3 = DuplicateMtoW(lpStartupInfo->lpReserved, -1);\r
1911         wStartupInfo.lpReserved = pw3;\r
1912         pw4 = DuplicateMtoW(lpStartupInfo->lpDesktop, -1);\r
1913         wStartupInfo.lpDesktop = pw4;\r
1914         pw5 = DuplicateMtoW(lpStartupInfo->lpTitle, -1);\r
1915         wStartupInfo.lpTitle = pw5;\r
1916         wStartupInfo.dwX = lpStartupInfo->dwX;\r
1917         wStartupInfo.dwY = lpStartupInfo->dwY;\r
1918         wStartupInfo.dwXSize = lpStartupInfo->dwXSize;\r
1919         wStartupInfo.dwYSize = lpStartupInfo->dwYSize;\r
1920         wStartupInfo.dwXCountChars = lpStartupInfo->dwXCountChars;\r
1921         wStartupInfo.dwYCountChars = lpStartupInfo->dwYCountChars;\r
1922         wStartupInfo.dwFillAttribute = lpStartupInfo->dwFillAttribute;\r
1923         wStartupInfo.dwFlags = lpStartupInfo->dwFlags;\r
1924         wStartupInfo.wShowWindow = lpStartupInfo->wShowWindow;\r
1925         wStartupInfo.cbReserved2 = lpStartupInfo->cbReserved2;\r
1926         wStartupInfo.lpReserved2 = lpStartupInfo->lpReserved2;\r
1927         wStartupInfo.hStdInput = lpStartupInfo->hStdInput;\r
1928         wStartupInfo.hStdOutput = lpStartupInfo->hStdOutput;\r
1929         wStartupInfo.hStdError = lpStartupInfo->hStdError;\r
1930         r = CreateProcessW(pw0, pw1, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, pw2, &wStartupInfo, lpProcessInformation);\r
1931         WtoM(lpCommandLine, strlen(lpCommandLine) + 1, pw1, -1);\r
1932 END_ROUTINE\r
1933         FreeDuplicatedString(pw0);\r
1934         FreeDuplicatedString(pw1);\r
1935         FreeDuplicatedString(pw2);\r
1936         FreeDuplicatedString(pw3);\r
1937         FreeDuplicatedString(pw4);\r
1938         FreeDuplicatedString(pw5);\r
1939         return r;\r
1940 }\r
1941 \r
1942 HINSTANCE FindExecutableM(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)\r
1943 {\r
1944         HINSTANCE r = NULL;\r
1945         wchar_t* pw0 = NULL;\r
1946         wchar_t* pw1 = NULL;\r
1947         wchar_t* pw2 = NULL;\r
1948         wchar_t* pw3 = NULL;\r
1949 START_ROUTINE\r
1950         pw0 = DuplicateMtoW(lpFile, -1);\r
1951         pw1 = DuplicateMtoW(lpDirectory, -1);\r
1952         pw2 = AllocateStringW(MAX_PATH * 4);\r
1953         r = FindExecutableW(pw0, pw1, pw2);\r
1954         // バッファ長不明のためオーバーランの可能性あり\r
1955         WtoM(lpResult, MAX_PATH, pw2, -1);\r
1956         TerminateStringM(lpResult, MAX_PATH);\r
1957 END_ROUTINE\r
1958         FreeDuplicatedString(pw0);\r
1959         FreeDuplicatedString(pw1);\r
1960         FreeDuplicatedString(pw2);\r
1961         FreeDuplicatedString(pw3);\r
1962         return r;\r
1963 }\r
1964 \r
1965 HINSTANCE ShellExecuteM(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)\r
1966 {\r
1967         HINSTANCE r = NULL;\r
1968         wchar_t* pw0 = NULL;\r
1969         wchar_t* pw1 = NULL;\r
1970         wchar_t* pw2 = NULL;\r
1971         wchar_t* pw3 = NULL;\r
1972 START_ROUTINE\r
1973         pw0 = DuplicateMtoW(lpOperation, -1);\r
1974         pw1 = DuplicateMtoW(lpFile, -1);\r
1975         pw2 = DuplicateMtoW(lpParameters, -1);\r
1976         pw3 = DuplicateMtoW(lpDirectory, -1);\r
1977         r = ShellExecuteW(hwnd, pw0, pw1, pw2, pw3, nShowCmd);\r
1978 END_ROUTINE\r
1979         FreeDuplicatedString(pw0);\r
1980         FreeDuplicatedString(pw1);\r
1981         FreeDuplicatedString(pw2);\r
1982         FreeDuplicatedString(pw3);\r
1983         return r;\r
1984 }\r
1985 \r
1986 BOOL ShellExecuteExM(LPSHELLEXECUTEINFOA lpExecInfo)\r
1987 {\r
1988         BOOL r = FALSE;\r
1989         wchar_t* pw0 = NULL;\r
1990         wchar_t* pw1 = NULL;\r
1991         wchar_t* pw2 = NULL;\r
1992         wchar_t* pw3 = NULL;\r
1993         wchar_t* pw4 = NULL;\r
1994         SHELLEXECUTEINFOW wExecInfo;\r
1995 START_ROUTINE\r
1996         wExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);\r
1997         wExecInfo.fMask = lpExecInfo->fMask;\r
1998         wExecInfo.hwnd = lpExecInfo->hwnd;\r
1999         pw0 = DuplicateMtoW(lpExecInfo->lpVerb, -1);\r
2000         wExecInfo.lpVerb = pw0;\r
2001         pw1 = DuplicateMtoW(lpExecInfo->lpFile, -1);\r
2002         wExecInfo.lpFile = pw1;\r
2003         pw2 = DuplicateMtoW(lpExecInfo->lpParameters, -1);\r
2004         wExecInfo.lpParameters = pw2;\r
2005         pw3 = DuplicateMtoW(lpExecInfo->lpDirectory, -1);\r
2006         wExecInfo.lpDirectory = pw3;\r
2007         wExecInfo.nShow = lpExecInfo->nShow;\r
2008         wExecInfo.hInstApp = lpExecInfo->hInstApp;\r
2009         wExecInfo.lpIDList = lpExecInfo->lpIDList;\r
2010         if(lpExecInfo->fMask & SEE_MASK_CLASSNAME)\r
2011         {\r
2012                 pw4 = DuplicateMtoW(lpExecInfo->lpClass, -1);\r
2013                 wExecInfo.lpClass = pw4;\r
2014         }\r
2015         wExecInfo.hkeyClass = lpExecInfo->hkeyClass;\r
2016         wExecInfo.dwHotKey = lpExecInfo->dwHotKey;\r
2017         wExecInfo.hIcon = lpExecInfo->hIcon;\r
2018         wExecInfo.hProcess = lpExecInfo->hProcess;\r
2019         r = ShellExecuteExW(&wExecInfo);\r
2020         lpExecInfo->hInstApp = wExecInfo.hInstApp;\r
2021         lpExecInfo->hProcess = wExecInfo.hProcess;\r
2022 END_ROUTINE\r
2023         FreeDuplicatedString(pw0);\r
2024         FreeDuplicatedString(pw1);\r
2025         FreeDuplicatedString(pw2);\r
2026         FreeDuplicatedString(pw3);\r
2027         FreeDuplicatedString(pw4);\r
2028         return r;\r
2029 }\r
2030 \r
2031 PIDLIST_ABSOLUTE SHBrowseForFolderM(LPBROWSEINFOA lpbi)\r
2032 {\r
2033         PIDLIST_ABSOLUTE r = NULL;\r
2034         wchar_t* pw0 = NULL;\r
2035         wchar_t* pw1 = NULL;\r
2036         BROWSEINFOW wbi;\r
2037 START_ROUTINE\r
2038         wbi.hwndOwner = lpbi->hwndOwner;\r
2039         wbi.pidlRoot = lpbi->pidlRoot;\r
2040         pw0 = DuplicateMtoWBuffer(lpbi->pszDisplayName, -1, MAX_PATH * 4);\r
2041         wbi.pszDisplayName = pw0;\r
2042         pw1 = DuplicateMtoW(lpbi->lpszTitle, -1);\r
2043         wbi.lpszTitle = pw1;\r
2044         wbi.ulFlags = lpbi->ulFlags;\r
2045         // TODO: lpfn\r
2046         wbi.lpfn = lpbi->lpfn;\r
2047         wbi.lParam = lpbi->lParam;\r
2048         wbi.iImage = lpbi->iImage;\r
2049         r = SHBrowseForFolderW(&wbi);\r
2050         // バッファ長不明のためオーバーランの可能性あり\r
2051         WtoM(lpbi->pszDisplayName, MAX_PATH, wbi.pszDisplayName, -1);\r
2052         lpbi->iImage = wbi.iImage;\r
2053 END_ROUTINE\r
2054         FreeDuplicatedString(pw0);\r
2055         FreeDuplicatedString(pw1);\r
2056         return r;\r
2057 }\r
2058 \r
2059 BOOL SHGetPathFromIDListM(PCIDLIST_ABSOLUTE pidl, LPSTR pszPath)\r
2060 {\r
2061         BOOL r = FALSE;\r
2062         wchar_t* pw0 = NULL;\r
2063 START_ROUTINE\r
2064         pw0 = AllocateStringW(MAX_PATH * 4);\r
2065         r = SHGetPathFromIDListW(pidl, pw0);\r
2066         // バッファ長不明のためオーバーランの可能性あり\r
2067         WtoM(pszPath, MAX_PATH, pw0, -1);\r
2068         TerminateStringM(pszPath, MAX_PATH);\r
2069 END_ROUTINE\r
2070         FreeDuplicatedString(pw0);\r
2071         return r;\r
2072 }\r
2073 \r
2074 int SHFileOperationM(LPSHFILEOPSTRUCTA lpFileOp)\r
2075 {\r
2076         int r = 0;\r
2077         wchar_t* pw0 = NULL;\r
2078         wchar_t* pw1 = NULL;\r
2079         wchar_t* pw2 = NULL;\r
2080         SHFILEOPSTRUCTW wFileOp;\r
2081 START_ROUTINE\r
2082         wFileOp.hwnd = lpFileOp->hwnd;\r
2083         wFileOp.wFunc = lpFileOp->wFunc;\r
2084         pw0 = DuplicateMtoWMultiString(lpFileOp->pFrom);\r
2085         wFileOp.pFrom = pw0;\r
2086         pw1 = DuplicateMtoWMultiString(lpFileOp->pTo);\r
2087         wFileOp.pTo = pw1;\r
2088         wFileOp.fFlags = lpFileOp->fFlags;\r
2089         wFileOp.fAnyOperationsAborted = lpFileOp->fAnyOperationsAborted;\r
2090         wFileOp.hNameMappings = lpFileOp->hNameMappings;\r
2091         if(lpFileOp->fFlags & FOF_SIMPLEPROGRESS)\r
2092                 pw2 = DuplicateMtoW(lpFileOp->lpszProgressTitle, -1);\r
2093         r = SHFileOperationW(&wFileOp);\r
2094         lpFileOp->fAnyOperationsAborted = wFileOp.fAnyOperationsAborted;\r
2095 END_ROUTINE\r
2096         FreeDuplicatedString(pw0);\r
2097         FreeDuplicatedString(pw1);\r
2098         FreeDuplicatedString(pw2);\r
2099         return r;\r
2100 }\r
2101 \r
2102 DWORD_PTR SHGetFileInfoM(LPCSTR pszPath, DWORD dwFileAttributes, SHFILEINFOA *psfi, UINT cbFileInfo, UINT uFlags)\r
2103 {\r
2104         DWORD_PTR r = 0;\r
2105         wchar_t* pw0 = NULL;\r
2106         SHFILEINFOW wsfi;\r
2107 START_ROUTINE\r
2108         pw0 = DuplicateMtoWMultiString(pszPath);\r
2109         if((r = SHGetFileInfoW(pw0, dwFileAttributes, &wsfi, cbFileInfo, uFlags)) != 0)\r
2110         {\r
2111                 psfi->hIcon = wsfi.hIcon;\r
2112                 psfi->iIcon = wsfi.iIcon;\r
2113                 psfi->dwAttributes = wsfi.dwAttributes;\r
2114                 WtoM(psfi->szDisplayName, MAX_PATH, wsfi.szDisplayName, -1);\r
2115                 WtoM(psfi->szTypeName, 80, wsfi.szTypeName, -1);\r
2116         }\r
2117 END_ROUTINE\r
2118         FreeDuplicatedString(pw0);\r
2119         return r;\r
2120 }\r
2121 \r
2122 BOOL AppendMenuM(HMENU hMenu, UINT uFlags, UINT_PTR uIDNewItem, LPCSTR lpNewItem)\r
2123 {\r
2124         int r = 0;\r
2125         wchar_t* pw0 = NULL;\r
2126 START_ROUTINE\r
2127         if(uFlags & (MF_BITMAP | MF_OWNERDRAW))\r
2128                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, (LPCWSTR)lpNewItem);\r
2129         else\r
2130         {\r
2131                 pw0 = DuplicateMtoW(lpNewItem, -1);\r
2132                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, pw0);\r
2133         }\r
2134 END_ROUTINE\r
2135         FreeDuplicatedString(pw0);\r
2136         return r;\r
2137 }\r
2138 \r
2139 BOOL GetMenuItemInfoM(HMENU hmenu, UINT item, BOOL fByPosition, LPMENUITEMINFOA lpmii)\r
2140 {\r
2141         BOOL r = FALSE;\r
2142         wchar_t* pw0 = NULL;\r
2143         MENUITEMINFOW wmii;\r
2144 START_ROUTINE\r
2145         wmii.cbSize = sizeof(MENUITEMINFOW);\r
2146         wmii.fMask = lpmii->fMask;\r
2147         wmii.fType = lpmii->fType;\r
2148         wmii.fState = lpmii->fState;\r
2149         wmii.wID = lpmii->wID;\r
2150         wmii.hSubMenu = lpmii->hSubMenu;\r
2151         wmii.hbmpChecked = lpmii->hbmpChecked;\r
2152         wmii.hbmpUnchecked = lpmii->hbmpUnchecked;\r
2153         wmii.dwItemData = lpmii->dwItemData;\r
2154         if(lpmii->fMask & MIIM_TYPE)\r
2155         {\r
2156                 pw0 = DuplicateMtoWBuffer(lpmii->dwTypeData, -1, lpmii->cch * 4);\r
2157                 wmii.dwTypeData = pw0;\r
2158                 wmii.cch = lpmii->cch * 4;\r
2159         }\r
2160         wmii.hbmpItem = lpmii->hbmpItem;\r
2161         r = GetMenuItemInfoW(hmenu, item, fByPosition, &wmii);\r
2162         lpmii->fType = wmii.fType;\r
2163         lpmii->fState = wmii.fState;\r
2164         lpmii->wID = wmii.wID;\r
2165         lpmii->hSubMenu = wmii.hSubMenu;\r
2166         lpmii->hbmpChecked = wmii.hbmpChecked;\r
2167         lpmii->hbmpUnchecked = wmii.hbmpUnchecked;\r
2168         lpmii->dwItemData = wmii.dwItemData;\r
2169         WtoM(lpmii->dwTypeData, lpmii->cch, wmii.dwTypeData, -1);\r
2170         TerminateStringM(lpmii->dwTypeData, lpmii->cch);\r
2171 END_ROUTINE\r
2172         FreeDuplicatedString(pw0);\r
2173         return r;\r
2174 }\r
2175 \r
2176 HFONT CreateFontIndirectM(CONST LOGFONTA *lplf)\r
2177 {\r
2178         HFONT r = NULL;\r
2179         LOGFONTW wlf;\r
2180 START_ROUTINE\r
2181         wlf.lfHeight = lplf->lfHeight;\r
2182         wlf.lfWidth = lplf->lfWidth;\r
2183         wlf.lfEscapement = lplf->lfEscapement;\r
2184         wlf.lfOrientation = lplf->lfOrientation;\r
2185         wlf.lfWeight = lplf->lfWeight;\r
2186         wlf.lfItalic = lplf->lfItalic;\r
2187         wlf.lfUnderline = lplf->lfUnderline;\r
2188         wlf.lfStrikeOut = lplf->lfStrikeOut;\r
2189         wlf.lfCharSet = lplf->lfCharSet;\r
2190         wlf.lfOutPrecision = lplf->lfOutPrecision;\r
2191         wlf.lfClipPrecision = lplf->lfClipPrecision;\r
2192         wlf.lfQuality = lplf->lfQuality;\r
2193         wlf.lfPitchAndFamily = lplf->lfPitchAndFamily;\r
2194         MtoW(wlf.lfFaceName, LF_FACESIZE, lplf->lfFaceName, -1);\r
2195         TerminateStringW(wlf.lfFaceName, LF_FACESIZE);\r
2196         r = CreateFontIndirect(&wlf);\r
2197 END_ROUTINE\r
2198         return r;\r
2199 }\r
2200 \r
2201 BOOL ChooseFontM(LPCHOOSEFONTA v0)\r
2202 {\r
2203         BOOL r = FALSE;\r
2204         wchar_t* pw0 = NULL;\r
2205         wchar_t* pw1 = NULL;\r
2206         CHOOSEFONTW a0;\r
2207         LOGFONTW* pwlf;\r
2208 START_ROUTINE\r
2209         a0.lStructSize = sizeof(CHOOSEFONTW);\r
2210         a0.hwndOwner = v0->hwndOwner;\r
2211         a0.hDC = v0->hDC;\r
2212         if(v0->lpLogFont && (pwlf = (LOGFONTW*)malloc(sizeof(LOGFONTW))))\r
2213         {\r
2214                 pwlf->lfHeight = v0->lpLogFont->lfHeight;\r
2215                 pwlf->lfWidth = v0->lpLogFont->lfWidth;\r
2216                 pwlf->lfEscapement = v0->lpLogFont->lfEscapement;\r
2217                 pwlf->lfOrientation = v0->lpLogFont->lfOrientation;\r
2218                 pwlf->lfWeight = v0->lpLogFont->lfWeight;\r
2219                 pwlf->lfItalic = v0->lpLogFont->lfItalic;\r
2220                 pwlf->lfUnderline = v0->lpLogFont->lfUnderline;\r
2221                 pwlf->lfStrikeOut = v0->lpLogFont->lfStrikeOut;\r
2222                 pwlf->lfCharSet = v0->lpLogFont->lfCharSet;\r
2223                 pwlf->lfOutPrecision = v0->lpLogFont->lfOutPrecision;\r
2224                 pwlf->lfClipPrecision = v0->lpLogFont->lfClipPrecision;\r
2225                 pwlf->lfQuality = v0->lpLogFont->lfQuality;\r
2226                 pwlf->lfPitchAndFamily = v0->lpLogFont->lfPitchAndFamily;\r
2227                 MtoW(pwlf->lfFaceName, LF_FACESIZE, v0->lpLogFont->lfFaceName, -1);\r
2228                 TerminateStringW(pwlf->lfFaceName, LF_FACESIZE);\r
2229         }\r
2230         else\r
2231                 pwlf = NULL;\r
2232         a0.lpLogFont = pwlf;\r
2233         a0.iPointSize = v0->iPointSize;\r
2234         a0.Flags = v0->Flags;\r
2235         a0.rgbColors = v0->rgbColors;\r
2236         a0.lCustData = v0->lCustData;\r
2237         a0.lpfnHook = v0->lpfnHook;\r
2238         pw0 = DuplicateMtoW(v0->lpTemplateName, -1);\r
2239         a0.lpTemplateName = pw0;\r
2240         a0.hInstance = v0->hInstance;\r
2241         pw1 = DuplicateMtoWBuffer(v0->lpszStyle, -1, LF_FACESIZE * 4);\r
2242         a0.lpszStyle = pw1;\r
2243         a0.nFontType = v0->nFontType;\r
2244         a0.nSizeMin = v0->nSizeMin;\r
2245         a0.nSizeMax = v0->nSizeMax;\r
2246         r = ChooseFontW(&a0);\r
2247         if(v0->lpLogFont)\r
2248         {\r
2249                 v0->lpLogFont->lfHeight = pwlf->lfHeight;\r
2250                 v0->lpLogFont->lfWidth = pwlf->lfWidth;\r
2251                 v0->lpLogFont->lfEscapement = pwlf->lfEscapement;\r
2252                 v0->lpLogFont->lfOrientation = pwlf->lfOrientation;\r
2253                 v0->lpLogFont->lfWeight = pwlf->lfWeight;\r
2254                 v0->lpLogFont->lfItalic = pwlf->lfItalic;\r
2255                 v0->lpLogFont->lfUnderline = pwlf->lfUnderline;\r
2256                 v0->lpLogFont->lfStrikeOut = pwlf->lfStrikeOut;\r
2257                 v0->lpLogFont->lfCharSet = pwlf->lfCharSet;\r
2258                 v0->lpLogFont->lfOutPrecision = pwlf->lfOutPrecision;\r
2259                 v0->lpLogFont->lfClipPrecision = pwlf->lfClipPrecision;\r
2260                 v0->lpLogFont->lfQuality = pwlf->lfQuality;\r
2261                 v0->lpLogFont->lfPitchAndFamily = pwlf->lfPitchAndFamily;\r
2262                 WtoM(v0->lpLogFont->lfFaceName, LF_FACESIZE, pwlf->lfFaceName, -1);\r
2263                 TerminateStringM(v0->lpLogFont->lfFaceName, LF_FACESIZE);\r
2264         }\r
2265         v0->rgbColors = a0.rgbColors;\r
2266         WtoM(v0->lpszStyle, LF_FACESIZE, pw1, -1);\r
2267         TerminateStringM(v0->lpszStyle, LF_FACESIZE);\r
2268         v0->nFontType = a0.nFontType;\r
2269         if(pwlf)\r
2270                 free(pwlf);\r
2271 END_ROUTINE\r
2272         FreeDuplicatedString(pw0);\r
2273         FreeDuplicatedString(pw1);\r
2274         return r;\r
2275 }\r
2276 \r
2277 INT_PTR DialogBoxParamM(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)\r
2278 {\r
2279         INT_PTR r = 0;\r
2280         wchar_t* pw0 = NULL;\r
2281 START_ROUTINE\r
2282         pw0 = DuplicateMtoW(lpTemplateName, -1);\r
2283         r = DialogBoxParamW(hInstance, pw0, hWndParent, lpDialogFunc, dwInitParam);\r
2284 END_ROUTINE\r
2285         FreeDuplicatedString(pw0);\r
2286         return r;\r
2287 }\r
2288 \r
2289 HWND CreateDialogParamM(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)\r
2290 {\r
2291         HWND r = NULL;\r
2292         wchar_t* pw0 = NULL;\r
2293 START_ROUTINE\r
2294         pw0 = DuplicateMtoW(lpTemplateName, -1);\r
2295         r = CreateDialogParamW(hInstance, pw0, hWndParent, lpDialogFunc, dwInitParam);\r
2296 END_ROUTINE\r
2297         FreeDuplicatedString(pw0);\r
2298         return r;\r
2299 }\r
2300 \r
2301 BOOL sndPlaySoundM(LPCSTR pszSound, UINT fuSound)\r
2302 {\r
2303         BOOL r = FALSE;\r
2304         wchar_t* pw0 = NULL;\r
2305 START_ROUTINE\r
2306         pw0 = DuplicateMtoW(pszSound, -1);\r
2307         r = sndPlaySoundW(pw0, fuSound);\r
2308 END_ROUTINE\r
2309         FreeDuplicatedString(pw0);\r
2310         return r;\r
2311 }\r
2312 \r
2313 HANDLE SetClipboardDataM(UINT uFormat, HANDLE hMem)\r
2314 {\r
2315         HANDLE r = NULL;\r
2316         char* p;\r
2317         int Length;\r
2318         int BufferLength;\r
2319         HGLOBAL hBufferMem;\r
2320 START_ROUTINE\r
2321         if(uFormat == CF_TEXT)\r
2322         {\r
2323                 p = (char*)GlobalLock(hMem);\r
2324                 Length = (int)GlobalSize(hMem);\r
2325                 BufferLength = MtoW(NULL, 0, p, Length);\r
2326                 if(hBufferMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t) * BufferLength))\r
2327                 {\r
2328                         MtoW((LPWSTR)GlobalLock(hBufferMem), BufferLength, p, Length);\r
2329                         GlobalUnlock(hBufferMem);\r
2330                         r = SetClipboardData(CF_UNICODETEXT, hBufferMem);\r
2331                 }\r
2332                 GlobalUnlock(hMem);\r
2333                 GlobalFree(hMem);\r
2334         }\r
2335         else\r
2336                 r = SetClipboardData(uFormat, hMem);\r
2337 END_ROUTINE\r
2338         return r;\r
2339 }\r
2340 \r
2341 BOOL CopyFileM(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExists)\r
2342 {\r
2343         BOOL r = FALSE;\r
2344         wchar_t* pw0 = NULL;\r
2345         wchar_t* pw1 = NULL;\r
2346 START_ROUTINE\r
2347         pw0 = DuplicateMtoW(lpExistingFileName, -1);\r
2348         pw1 = DuplicateMtoW(lpNewFileName, -1);\r
2349         r = CopyFileW(pw0, pw1, bFailIfExists);\r
2350 END_ROUTINE\r
2351         FreeDuplicatedString(pw0);\r
2352         FreeDuplicatedString(pw1);\r
2353         return r;\r
2354 }\r
2355 \r
2356 BOOL MoveFileM(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)\r
2357 {\r
2358         BOOL r = FALSE;\r
2359         wchar_t* pw0 = NULL;\r
2360         wchar_t* pw1 = NULL;\r
2361 START_ROUTINE\r
2362         pw0 = DuplicateMtoW(lpExistingFileName, -1);\r
2363         pw1 = DuplicateMtoW(lpNewFileName, -1);\r
2364         r = MoveFileW(pw0, pw1);\r
2365 END_ROUTINE\r
2366         FreeDuplicatedString(pw0);\r
2367         FreeDuplicatedString(pw1);\r
2368         return r;\r
2369 }\r
2370 \r
2371 BOOL DeleteFileM(LPCSTR lpFileName)\r
2372 {\r
2373         BOOL r = FALSE;\r
2374         wchar_t* pw0 = NULL;\r
2375 START_ROUTINE\r
2376         pw0 = DuplicateMtoW(lpFileName, -1);\r
2377         r = DeleteFileW(pw0);\r
2378 END_ROUTINE\r
2379         FreeDuplicatedString(pw0);\r
2380         return r;\r
2381 }\r
2382 \r
2383 int mkdirM(const char * _Path)\r
2384 {\r
2385         int r = -1;\r
2386         wchar_t* pw0 = NULL;\r
2387 START_ROUTINE\r
2388         pw0 = DuplicateMtoW(_Path, -1);\r
2389         r = _wmkdir(pw0);\r
2390 END_ROUTINE\r
2391         FreeDuplicatedString(pw0);\r
2392         return r;\r
2393 }\r
2394 \r
2395 int _mkdirM(const char * _Path)\r
2396 {\r
2397         int r = -1;\r
2398         wchar_t* pw0 = NULL;\r
2399 START_ROUTINE\r
2400         pw0 = DuplicateMtoW(_Path, -1);\r
2401         r = _wmkdir(pw0);\r
2402 END_ROUTINE\r
2403         FreeDuplicatedString(pw0);\r
2404         return r;\r
2405 }\r
2406 \r
2407 int rmdirM(const char * _Path)\r
2408 {\r
2409         int r = -1;\r
2410         wchar_t* pw0 = NULL;\r
2411 START_ROUTINE\r
2412         pw0 = DuplicateMtoW(_Path, -1);\r
2413         r = _wrmdir(pw0);\r
2414 END_ROUTINE\r
2415         FreeDuplicatedString(pw0);\r
2416         return r;\r
2417 }\r
2418 \r
2419 int _rmdirM(const char * _Path)\r
2420 {\r
2421         int r = -1;\r
2422         wchar_t* pw0 = NULL;\r
2423 START_ROUTINE\r
2424         pw0 = DuplicateMtoW(_Path, -1);\r
2425         r = _wrmdir(pw0);\r
2426 END_ROUTINE\r
2427         FreeDuplicatedString(pw0);\r
2428         return r;\r
2429 }\r
2430 \r
2431 int removeM(const char * _Filename)\r
2432 {\r
2433         int r = -1;\r
2434         wchar_t* pw0 = NULL;\r
2435 START_ROUTINE\r
2436         pw0 = DuplicateMtoW(_Filename, -1);\r
2437         r = _wremove(pw0);\r
2438 END_ROUTINE\r
2439         FreeDuplicatedString(pw0);\r
2440         return r;\r
2441 }\r
2442 \r
2443 int _removeM(const char * _Filename)\r
2444 {\r
2445         int r = -1;\r
2446         wchar_t* pw0 = NULL;\r
2447 START_ROUTINE\r
2448         pw0 = DuplicateMtoW(_Filename, -1);\r
2449         r = _wremove(pw0);\r
2450 END_ROUTINE\r
2451         FreeDuplicatedString(pw0);\r
2452         return r;\r
2453 }\r
2454 \r
2455 int _unlinkM(const char * _Filename)\r
2456 {\r
2457         int r = -1;\r
2458         wchar_t* pw0 = NULL;\r
2459 START_ROUTINE\r
2460         pw0 = DuplicateMtoW(_Filename, -1);\r
2461         r = _wunlink(pw0);\r
2462 END_ROUTINE\r
2463         FreeDuplicatedString(pw0);\r
2464         return r;\r
2465 }\r
2466 \r
2467 size_t _mbslenM(const unsigned char * _Str)\r
2468 {\r
2469         size_t r = 0;\r
2470 START_ROUTINE\r
2471         while(GetNextCharM(_Str, NULL, &_Str) > 0)\r
2472         {\r
2473                 r++;\r
2474         }\r
2475 END_ROUTINE\r
2476         return r;\r
2477 }\r
2478 \r
2479 unsigned char * _mbschrM(const unsigned char * _Str, unsigned int _Ch)\r
2480 {\r
2481         unsigned char* r = NULL;\r
2482         unsigned int c;\r
2483         unsigned char* p;\r
2484 START_ROUTINE\r
2485         while((c = GetNextCharM(_Str, NULL, &p)) > 0)\r
2486         {\r
2487                 if(c == _Ch)\r
2488                         break;\r
2489                 _Str = p;\r
2490         }\r
2491         if(c == _Ch)\r
2492                 r = (unsigned char*)_Str;\r
2493 END_ROUTINE\r
2494         return r;\r
2495 }\r
2496 \r
2497 unsigned char * _mbsrchrM(const unsigned char * _Str, unsigned int _Ch)\r
2498 {\r
2499         unsigned char* r = NULL;\r
2500         unsigned int c;\r
2501         unsigned char* p;\r
2502 START_ROUTINE\r
2503         while((c = GetNextCharM(_Str, NULL, &p)) > 0)\r
2504         {\r
2505                 if(c == _Ch)\r
2506                         r = (unsigned char*)_Str;\r
2507                 _Str = p;\r
2508         }\r
2509         if(c == _Ch)\r
2510                 r = (unsigned char*)_Str;\r
2511 END_ROUTINE\r
2512         return r;\r
2513 }\r
2514 \r
2515 unsigned char * _mbsstrM(const unsigned char * _Str, const unsigned char * _Substr)\r
2516 {\r
2517         unsigned char* r = NULL;\r
2518 START_ROUTINE\r
2519         r = strstr(_Str, _Substr);\r
2520 END_ROUTINE\r
2521         return r;\r
2522 }\r
2523 \r
2524 int _mbscmpM(const unsigned char * _Str1, const unsigned char * _Str2)\r
2525 {\r
2526         int r = 0;\r
2527 START_ROUTINE\r
2528         r = strcmp(_Str1, _Str2);\r
2529 END_ROUTINE\r
2530         return r;\r
2531 }\r
2532 \r
2533 int _mbsicmpM(const unsigned char * _Str1, const unsigned char * _Str2)\r
2534 {\r
2535         int r = 0;\r
2536 START_ROUTINE\r
2537         r = _stricmp(_Str1, _Str2);\r
2538 END_ROUTINE\r
2539         return r;\r
2540 }\r
2541 \r
2542 int _mbsncmpM(const unsigned char * _Str1, const unsigned char * _Str2, size_t _MaxCount)\r
2543 {\r
2544         int r = 0;\r
2545         DWORD c1;\r
2546         DWORD c2;\r
2547 START_ROUTINE\r
2548         c1 = 0;\r
2549         c2 = 0;\r
2550         while(_MaxCount > 0)\r
2551         {\r
2552                 c1 = GetNextCharM(_Str1, NULL, &_Str1);\r
2553                 c2 = GetNextCharM(_Str2, NULL, &_Str2);\r
2554                 if(c1 != c2)\r
2555                         break;\r
2556                 _MaxCount--;\r
2557                 if(c1 == 0 || c2 == 0)\r
2558                         break;\r
2559         }\r
2560         r = c1 - c2;\r
2561 END_ROUTINE\r
2562         return r;\r
2563 }\r
2564 \r
2565 unsigned char * _mbslwrM(unsigned char * _String)\r
2566 {\r
2567         unsigned char* r = NULL;\r
2568 START_ROUTINE\r
2569         r = _strlwr(_String);\r
2570 END_ROUTINE\r
2571         return r;\r
2572 }\r
2573 \r
2574 unsigned char * _mbsuprM(unsigned char * _String)\r
2575 {\r
2576         unsigned char* r = NULL;\r
2577 START_ROUTINE\r
2578         r = _strupr(_String);\r
2579 END_ROUTINE\r
2580         return r;\r
2581 }\r
2582 \r
2583 unsigned char * _mbsnincM(const unsigned char * _Str, size_t _Count)\r
2584 {\r
2585         unsigned char* r = NULL;\r
2586 START_ROUTINE\r
2587         while(_Count > 0 && GetNextCharM(_Str, NULL, &_Str) > 0)\r
2588         {\r
2589                 _Count--;\r
2590         }\r
2591         r = (unsigned char*)_Str;\r
2592 END_ROUTINE\r
2593         return r;\r
2594 }\r
2595 \r
2596 FILE * fopenM(const char * _Filename, const char * _Mode)\r
2597 {\r
2598         FILE* r = NULL;\r
2599         wchar_t* pw0 = NULL;\r
2600         wchar_t* pw1 = NULL;\r
2601 START_ROUTINE\r
2602         pw0 = DuplicateMtoW(_Filename, -1);\r
2603         pw1 = DuplicateMtoW(_Mode, -1);\r
2604         r = _wfopen(pw0, pw1);\r
2605 END_ROUTINE\r
2606         FreeDuplicatedString(pw0);\r
2607         FreeDuplicatedString(pw1);\r
2608         return r;\r
2609 }\r
2610 \r