OSDN Git Service

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