OSDN Git Service

Fix bugs of text encoding routines.
[ffftp/ffftp.git] / mbswrapper.c
1 // mbswrapper.cpp
2 // Copyright (C) 2011 Suguru Kawamoto
3 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\83\8f\83C\83h\95\8e\9aAPI\83\89\83b\83p\81[
4 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\82ÍUTF-8\81A\83\8f\83C\83h\95\8e\9a\82ÍUTF-16\82Å\82 \82é\82à\82Ì\82Æ\82·\82é
5 // \91S\82Ä\82Ì\90§\8cä\97p\82Ì\95\8e\9a\82ÍASCII\82Ì\94Í\88Í\82Å\82 \82é\82½\82ß\81AShift_JIS\82ÆUTF-8\8aÔ\82Ì\95Ï\8a·\82Í\95s\97v
6
7 #define UNICODE
8 #define _WIN32_WINNT 0x0600
9 #undef _WIN32_IE
10 #define _WIN32_IE 0x0400
11
12 #include <tchar.h>
13 #include <windows.h>
14 #include <commctrl.h>
15 #include <shlobj.h>
16 #include <htmlhelp.h>
17
18 #define DO_NOT_REPLACE
19 #include "mbswrapper.h"
20
21 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82©\82ç\83\8f\83C\83h\95\8e\9a\97ñ\82Ö\95Ï\8a·
22 int MtoW(LPWSTR pDst, int size, LPCSTR pSrc, int count)
23 {
24         if(pSrc < (LPCSTR)0x00010000 || !((char*)pSrc + 1))
25                 return 0;
26         if(pDst)
27                 return MultiByteToWideChar(CP_UTF8, 0, pSrc, count, pDst, size);
28         return MultiByteToWideChar(CP_UTF8, 0, pSrc, count, NULL, 0);
29 }
30
31 // \83\8f\83C\83h\95\8e\9a\97ñ\82©\82ç\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82Ö\95Ï\8a·
32 int WtoM(LPSTR pDst, int size, LPCWSTR pSrc, int count)
33 {
34         if(pSrc < (LPCWSTR)0x00010000 || !((char*)pSrc + 1))
35                 return 0;
36         if(pDst)
37                 return WideCharToMultiByte(CP_UTF8, 0, pSrc, count, pDst, size, NULL, NULL);
38         return WideCharToMultiByte(CP_UTF8, 0, pSrc, count, NULL, 0, NULL, NULL);
39 }
40
41 // \83\8f\83C\83h\95\8e\9a\97ñ\82©\82çShift_JIS\95\8e\9a\97ñ\82Ö\95Ï\8a·
42 int WtoA(LPSTR pDst, int size, LPCWSTR pSrc, int count)
43 {
44         if(pSrc < (LPCWSTR)0x00010000 || !((char*)pSrc + 1))
45                 return 0;
46         if(pDst)
47                 return WideCharToMultiByte(CP_ACP, 0, pSrc, count, pDst, size, NULL, NULL);
48         return WideCharToMultiByte(CP_ACP, 0, pSrc, count, NULL, 0, NULL, NULL);
49 }
50
51 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\83o\83b\83t\83@\8fI\92[\82ð\8b­\90§\93I\82ÉNULL\82Å\92u\8a·
52 int TerminateStringM(LPSTR lpString, int size)
53 {
54         int i;
55         if(lpString < (LPSTR)0x00010000 || !((char*)lpString + 1))
56                 return 0;
57         for(i = 0; i < size; i++)
58         {
59                 if(lpString[i] == '\0')
60                         return i;
61         }
62         i--;
63         lpString[i] = '\0';
64         return i;
65 }
66
67 // \83\8f\83C\83h\95\8e\9a\97ñ\83o\83b\83t\83@\8fI\92[\82ð\8b­\90§\93I\82ÉNULL\82Å\92u\8a·
68 int TerminateStringW(LPWSTR lpString, int size)
69 {
70         int i;
71         if(lpString < (LPWSTR)0x00010000 || !((char*)lpString + 1))
72                 return 0;
73         for(i = 0; i < size; i++)
74         {
75                 if(lpString[i] == L'\0')
76                         return i;
77         }
78         i--;
79         lpString[i] = L'\0';
80         return i;
81 }
82
83 // NULL\8bæ\90Ø\82è\95¡\90\94\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82Ì\92·\82³\82ð\8eæ\93¾
84 size_t GetMultiStringLengthM(LPCSTR lpString)
85 {
86         size_t i;
87         if(lpString < (LPCSTR)0x00010000 || !((char*)lpString + 1))
88                 return 0;
89         i = 0;
90         while(lpString[i] != '\0' || lpString[i + 1] != '\0')
91         {
92                 i++;
93         }
94         i++;
95         return i;
96 }
97
98 // NULL\8bæ\90Ø\82è\95¡\90\94\83\8f\83C\83h\95\8e\9a\97ñ\82Ì\92·\82³\82ð\8eæ\93¾
99 size_t GetMultiStringLengthW(LPCWSTR lpString)
100 {
101         size_t i;
102         if(lpString < (LPCWSTR)0x00010000 || !((char*)lpString + 1))
103                 return 0;
104         i = 0;
105         while(lpString[i] != L'\0' || lpString[i + 1] != L'\0')
106         {
107                 i++;
108         }
109         i++;
110         return i;
111 }
112
113 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\97p\82Ì\83\81\83\82\83\8a\82ð\8am\95Û
114 char* AllocateStringM(int size)
115 {
116         char* p;
117         p = (char*)malloc(sizeof(char) * size);
118         if(p)
119                 *p = '\0';
120         return p;
121 }
122
123 // \83\8f\83C\83h\95\8e\9a\97ñ\97p\82Ì\83\81\83\82\83\8a\82ð\8am\95Û
124 wchar_t* AllocateStringW(int size)
125 {
126         wchar_t* p;
127         p = (wchar_t*)malloc(sizeof(wchar_t) * size);
128         if(p)
129                 *p = L'\0';
130         return p;
131 }
132
133 // Shift_JIS\95\8e\9a\97ñ\97p\82Ì\83\81\83\82\83\8a\82ð\8am\95Û
134 char* AllocateStringA(int size)
135 {
136         char* p;
137         p = (char*)malloc(sizeof(char) * size);
138         if(p)
139                 *p = '\0';
140         return p;
141 }
142
143 // \83\81\83\82\83\8a\82ð\8am\95Û\82µ\82Ä\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82©\82ç\83\8f\83C\83h\95\8e\9a\97ñ\82Ö\95Ï\8a·
144 wchar_t* DuplicateMtoW(LPCSTR lpString, int c)
145 {
146         wchar_t* p;
147         int i;
148         if(lpString < (LPCSTR)0x00010000 || !((char*)lpString + 1))
149                 return (wchar_t*)lpString;
150         if(c < 0)
151                 c = strlen(lpString);
152         p = AllocateStringW(MtoW(NULL, 0, lpString, c) + 1);
153         if(p)
154         {
155                 i = MtoW(p, 65535, lpString, c);
156                 p[i] = L'\0';
157         }
158         return p;
159 }
160
161 // \8ew\92è\82µ\82½\83T\83C\83Y\82Ì\83\81\83\82\83\8a\82ð\8am\95Û\82µ\82Ä\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82©\82ç\83\8f\83C\83h\95\8e\9a\97ñ\82Ö\95Ï\8a·
162 wchar_t* DuplicateMtoWBuffer(LPCSTR lpString, int c, int size)
163 {
164         wchar_t* p;
165         int i;
166         if(lpString < (LPCSTR)0x00010000 || !((char*)lpString + 1))
167                 return (wchar_t*)lpString;
168         if(c < 0)
169                 c = strlen(lpString);
170         p = AllocateStringW(size);
171         if(p)
172         {
173                 i = MtoW(p, size, lpString, c);
174                 p[i] = L'\0';
175         }
176         return p;
177 }
178
179 // \83\81\83\82\83\8a\82ð\8am\95Û\82µ\82ÄNULL\8bæ\90Ø\82è\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82©\82ç\83\8f\83C\83h\95\8e\9a\97ñ\82Ö\95Ï\8a·
180 wchar_t* DuplicateMtoWMultiString(LPCSTR lpString)
181 {
182         int count;
183         wchar_t* p;
184         if(lpString < (LPCSTR)0x00010000 || !((char*)lpString + 1))
185                 return (wchar_t*)lpString;
186         count = GetMultiStringLengthM(lpString) + 1;
187         p = AllocateStringW(count);
188         if(p)
189                 MtoW(p, count, lpString, count);
190         return p;
191 }
192
193 // \8ew\92è\82µ\82½\83T\83C\83Y\82Ì\83\81\83\82\83\8a\82ð\8am\95Û\82µ\82ÄNULL\8bæ\90Ø\82è\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82©\82ç\83\8f\83C\83h\95\8e\9a\97ñ\82Ö\95Ï\8a·
194 wchar_t* DuplicateMtoWMultiStringBuffer(LPCSTR lpString, int size)
195 {
196         int count;
197         wchar_t* p;
198         if(lpString < (LPCSTR)0x00010000 || !((char*)lpString + 1))
199                 return (wchar_t*)lpString;
200         count = GetMultiStringLengthM(lpString) + 1;
201         p = AllocateStringW(size);
202         if(p)
203         {
204                 MtoW(p, size, lpString, count);
205                 p[size - 2] = L'\0';
206                 p[size - 1] = L'\0';
207         }
208         return p;
209 }
210
211 // \83\81\83\82\83\8a\82ð\8am\95Û\82µ\82Ä\83\8f\83C\83h\95\8e\9a\97ñ\82©\82ç\83}\83\8b\83`\83o\83C\83g\95\8e\9a\97ñ\82Ö\95Ï\8a·
212 char* DuplicateWtoM(LPCWSTR lpString, int c)
213 {
214         char* p;
215         int i;
216         if(lpString < (LPCWSTR)0x00010000 || !((char*)lpString + 1))
217                 return (char*)lpString;
218         if(c < 0)
219                 c = wcslen(lpString);
220         p = AllocateStringM(WtoM(NULL, 0, lpString, c) + 1);
221         if(p)
222         {
223                 i = WtoM(p, 65535, lpString, c);
224                 p[i] = L'\0';
225         }
226         return p;
227 }
228
229 // \83\81\83\82\83\8a\82ð\8am\95Û\82µ\82Ä\83\8f\83C\83h\95\8e\9a\97ñ\82©\82çShift_JIS\95\8e\9a\97ñ\82Ö\95Ï\8a·
230 char* DuplicateWtoA(LPCWSTR lpString, int c)
231 {
232         char* p;
233         int i;
234         if(lpString < (LPCWSTR)0x00010000 || !((char*)lpString + 1))
235                 return (char*)lpString;
236         if(c < 0)
237                 c = wcslen(lpString);
238         p = AllocateStringA(WtoA(NULL, 0, lpString, c) + 1);
239         if(p)
240         {
241                 i = WtoA(p, 65535, lpString, c);
242                 p[i] = L'\0';
243         }
244         return p;
245 }
246
247 // \95\8e\9a\97ñ\97p\82É\8am\95Û\82µ\82½\83\81\83\82\83\8a\82ð\8aJ\95ú
248 void FreeDuplicatedString(void* p)
249 {
250         if(p < (void*)0x00010000 || !((char*)p + 1))
251                 return;
252         free(p);
253 }
254
255 // \88È\89º\83\89\83b\83p\81[
256 // \96ß\82è\92l\83o\83b\83t\83@ r
257 // \83\8f\83C\83h\95\8e\9a\83o\83b\83t\83@ pw%d
258 // \83}\83\8b\83`\83o\83C\83g\95\8e\9a\83o\83b\83t\83@ pm%d
259 // \88ø\90\94\83o\83b\83t\83@ a%d
260
261 #define START_ROUTINE                                   do{
262 #define END_ROUTINE                                             }while(0);end_of_routine:
263 #define QUIT_ROUTINE                                    goto end_of_routine;
264
265 HANDLE CreateFileM(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
266 {
267         HANDLE r = INVALID_HANDLE_VALUE;
268         wchar_t* pw0 = NULL;
269 START_ROUTINE
270         pw0 = DuplicateMtoW(lpFileName, -1);
271         r = CreateFileW(pw0, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
272 END_ROUTINE
273         FreeDuplicatedString(pw0);
274         return r;
275 }
276
277 int MessageBoxM(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
278 {
279         int r = IDOK;
280         wchar_t* pw0 = NULL;
281         wchar_t* pw1 = NULL;
282 START_ROUTINE
283         pw0 = DuplicateMtoW(lpText, -1);
284         pw1 = DuplicateMtoW(lpCaption, -1);
285         r = MessageBoxW(hWnd, pw0, pw1, uType);
286 END_ROUTINE
287         FreeDuplicatedString(pw0);
288         FreeDuplicatedString(pw1);
289         return r;
290 }
291
292 HANDLE FindFirstFileM(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
293 {
294         HANDLE r = INVALID_HANDLE_VALUE;
295         wchar_t* pw0 = NULL;
296         WIN32_FIND_DATAW a0;
297 START_ROUTINE
298         pw0 = DuplicateMtoW(lpFileName, -1);
299         r = FindFirstFileW(pw0, &a0);
300         if(r != INVALID_HANDLE_VALUE)
301         {
302                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;
303                 lpFindFileData->ftCreationTime = a0.ftCreationTime;
304                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;
305                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;
306                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;
307                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;
308                 lpFindFileData->dwReserved0 = a0.dwReserved0;
309                 lpFindFileData->dwReserved1 = a0.dwReserved1;
310                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);
311                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);
312         }
313 END_ROUTINE
314         FreeDuplicatedString(pw0);
315         return r;
316 }
317
318 BOOL FindNextFileM(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
319 {
320         BOOL r = FALSE;
321         WIN32_FIND_DATAW a0;
322 START_ROUTINE
323         r = FindNextFileW(hFindFile, &a0);
324         if(r)
325         {
326                 lpFindFileData->dwFileAttributes = a0.dwFileAttributes;
327                 lpFindFileData->ftCreationTime = a0.ftCreationTime;
328                 lpFindFileData->ftLastAccessTime = a0.ftLastAccessTime;
329                 lpFindFileData->ftLastWriteTime = a0.ftLastWriteTime;
330                 lpFindFileData->nFileSizeHigh = a0.nFileSizeHigh;
331                 lpFindFileData->nFileSizeLow = a0.nFileSizeLow;
332                 lpFindFileData->dwReserved0 = a0.dwReserved0;
333                 lpFindFileData->dwReserved1 = a0.dwReserved1;
334                 WtoM(lpFindFileData->cFileName, sizeof(lpFindFileData->cFileName), a0.cFileName, -1);
335                 WtoM(lpFindFileData->cAlternateFileName, sizeof(lpFindFileData->cAlternateFileName), a0.cAlternateFileName, -1);
336         }
337 END_ROUTINE
338         return r;
339 }
340
341 DWORD GetLogicalDriveStringsM(DWORD nBufferLength, LPSTR lpBuffer)
342 {
343         // TODO: \96{\97\88\82Í\95Ï\8a·\82ª\95K\97v\82¾\82ª\94¼\8ap\89p\90\94\82Ì\82Ý\82Æ\8ev\82í\82ê\82é\82Ì\82Å\8fÈ\97ª
344         return GetLogicalDriveStringsA(nBufferLength, lpBuffer);
345 }
346
347 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)
348 {
349         HWND r = NULL;
350         wchar_t* pw0 = NULL;
351         wchar_t* pw1 = NULL;
352 START_ROUTINE
353         pw0 = DuplicateMtoW(lpClassName, -1);
354         pw1 = DuplicateMtoW(lpWindowName, -1);
355         r = CreateWindowExW(dwExStyle, pw0, pw1, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
356 END_ROUTINE
357         FreeDuplicatedString(pw0);
358         FreeDuplicatedString(pw1);
359         return r;
360 }
361
362 LRESULT SendMessageM(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
363 {
364         LRESULT r = 0;
365         wchar_t* pw0 = NULL;
366         wchar_t* pw1 = NULL;
367         int Size;
368         LVITEMA* pmLVItem;
369         LVITEMW wLVItem;
370         LVFINDINFOA* pmLVFindInfo;
371         LVFINDINFOW wLVFindInfo;
372         LVCOLUMNA* pmLVColumn;
373         LVCOLUMNW wLVColumn;
374         wchar_t ClassName[MAX_PATH];
375 START_ROUTINE
376         switch(Msg)
377         {
378         case WM_SETTEXT:
379                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
380                 r = SendMessageW(hWnd, WM_SETTEXT, wParam, (LPARAM)pw0);
381                 break;
382         case WM_GETTEXT:
383                 pw0 = AllocateStringW(wParam * 4);
384                 SendMessageW(hWnd, WM_GETTEXT, wParam * 4, (LPARAM)pw0);
385                 WtoM((LPSTR)lParam, wParam, pw0, -1);
386                 r = TerminateStringM((LPSTR)lParam, wParam);
387                 break;
388         case WM_GETTEXTLENGTH:
389                 Size = SendMessageW(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1;
390                 pw0 = AllocateStringW(Size);
391                 SendMessageW(hWnd, WM_GETTEXT, (WPARAM)Size, (LPARAM)pw0);
392                 r = WtoM(NULL, 0, pw0, -1) - 1;
393                 break;
394         default:
395                 GetClassNameW(hWnd, ClassName, sizeof(ClassName) / sizeof(wchar_t));
396                 if(_wcsicmp(ClassName, WC_EDITW) == 0)
397                 {
398                         switch(Msg)
399                         {
400                         case EM_REPLACESEL:
401                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
402                                 r = SendMessageW(hWnd, EM_REPLACESEL, wParam, (LPARAM)pw0);
403                                 break;
404                         default:
405                                 r = SendMessageW(hWnd, Msg, wParam, lParam);
406                                 break;
407                         }
408                 }
409                 else if(_wcsicmp(ClassName, WC_COMBOBOXW) == 0)
410                 {
411                         switch(Msg)
412                         {
413                         case CB_ADDSTRING:
414                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
415                                 r = SendMessageW(hWnd, CB_ADDSTRING, wParam, (LPARAM)pw0);
416                                 break;
417                         case CB_INSERTSTRING:
418                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
419                                 r = SendMessageW(hWnd, CB_INSERTSTRING, wParam, (LPARAM)pw0);
420                                 break;
421                         case CB_FINDSTRINGEXACT:
422                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
423                                 r = SendMessageW(hWnd, CB_FINDSTRINGEXACT, wParam, (LPARAM)pw0);
424                                 break;
425                         default:
426                                 r = SendMessageW(hWnd, Msg, wParam, lParam);
427                                 break;
428                         }
429                 }
430                 else if(_wcsicmp(ClassName, WC_LISTBOXW) == 0)
431                 {
432                         switch(Msg)
433                         {
434                         case LB_ADDSTRING:
435                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
436                                 r = SendMessageW(hWnd, LB_ADDSTRING, wParam, (LPARAM)pw0);
437                                 break;
438                         case LB_INSERTSTRING:
439                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
440                                 r = SendMessageW(hWnd, LB_INSERTSTRING, wParam, (LPARAM)pw0);
441                                 break;
442                         case LB_GETTEXT:
443                                 Size = SendMessageW(hWnd, LB_GETTEXTLEN, wParam, 0) + 1;
444                                 pw0 = AllocateStringW(Size * 4);
445                                 SendMessageW(hWnd, LB_GETTEXT, wParam, (LPARAM)pw0);
446                                 // \83o\83b\83t\83@\92·\95s\96¾\82Ì\82½\82ß\83I\81[\83o\81[\83\89\83\93\82Ì\89Â\94\\90«\82 \82è
447                                 WtoM((LPSTR)lParam, Size * 4, pw0, -1);
448                                 r = TerminateStringM((LPSTR)lParam, Size * 4);
449                                 break;
450                         default:
451                                 r = SendMessageW(hWnd, Msg, wParam, lParam);
452                                 break;
453                         }
454                 }
455                 else if(_wcsicmp(ClassName, WC_LISTVIEWW) == 0)
456                 {
457                         switch(Msg)
458                         {
459                         case LVM_GETITEMA:
460                                 pmLVItem = (LVITEMA*)lParam;
461                                 wLVItem.mask = pmLVItem->mask;
462                                 wLVItem.iItem = pmLVItem->iItem;
463                                 wLVItem.iSubItem = pmLVItem->iSubItem;
464                                 wLVItem.state = pmLVItem->state;
465                                 wLVItem.stateMask = pmLVItem->stateMask;
466                                 Size = pmLVItem->cchTextMax * 4;
467                                 pw0 = AllocateStringW(Size);
468                                 wLVItem.pszText = pw0;
469                                 wLVItem.cchTextMax = Size;
470                                 wLVItem.iImage = pmLVItem->iImage;
471                                 wLVItem.lParam = pmLVItem->lParam;
472                                 wLVItem.iIndent = pmLVItem->iIndent;
473                                 r = SendMessageW(hWnd, LVM_GETITEMW, wParam, (LPARAM)&wLVItem);
474                                 pmLVItem->mask = wLVItem.mask;
475                                 pmLVItem->iItem = wLVItem.iItem;
476                                 pmLVItem->iSubItem = wLVItem.iSubItem;
477                                 pmLVItem->state = wLVItem.state;
478                                 pmLVItem->stateMask = wLVItem.stateMask;
479                                 WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);
480                                 TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);
481                                 pmLVItem->iImage = wLVItem.iImage;
482                                 pmLVItem->lParam = wLVItem.lParam;
483                                 pmLVItem->iIndent = wLVItem.iIndent;
484                                 break;
485                         case LVM_SETITEMA:
486                                 pmLVItem = (LVITEMA*)lParam;
487                                 wLVItem.mask = pmLVItem->mask;
488                                 wLVItem.iItem = pmLVItem->iItem;
489                                 wLVItem.iSubItem = pmLVItem->iSubItem;
490                                 wLVItem.state = pmLVItem->state;
491                                 wLVItem.stateMask = pmLVItem->stateMask;
492                                 pw0 = DuplicateMtoW(pmLVItem->pszText, -1);
493                                 wLVItem.pszText = pw0;
494                                 // TODO: cchTextMax\82Ì\8am\94F
495                                 wLVItem.cchTextMax = pmLVItem->cchTextMax;
496                                 wLVItem.iImage = pmLVItem->iImage;
497                                 wLVItem.lParam = pmLVItem->lParam;
498                                 wLVItem.iIndent = pmLVItem->iIndent;
499                                 r = SendMessageW(hWnd, LVM_SETITEMW, wParam, (LPARAM)&wLVItem);
500                                 break;
501                         case LVM_INSERTITEMA:
502                                 pmLVItem = (LVITEMA*)lParam;
503                                 wLVItem.mask = pmLVItem->mask;
504                                 wLVItem.iItem = pmLVItem->iItem;
505                                 wLVItem.iSubItem = pmLVItem->iSubItem;
506                                 wLVItem.state = pmLVItem->state;
507                                 wLVItem.stateMask = pmLVItem->stateMask;
508                                 pw0 = DuplicateMtoW(pmLVItem->pszText, -1);
509                                 wLVItem.pszText = pw0;
510                                 // TODO: cchTextMax\82Ì\8am\94F
511                                 wLVItem.cchTextMax = pmLVItem->cchTextMax;
512                                 wLVItem.iImage = pmLVItem->iImage;
513                                 wLVItem.lParam = pmLVItem->lParam;
514                                 wLVItem.iIndent = pmLVItem->iIndent;
515                                 r = SendMessageW(hWnd, LVM_INSERTITEMW, wParam, (LPARAM)&wLVItem);
516                                 break;
517                         case LVM_FINDITEMA:
518                                 pmLVFindInfo = (LVFINDINFOA*)lParam;
519                                 wLVFindInfo.flags = pmLVFindInfo->flags;
520                                 pw0 = DuplicateMtoW(pmLVFindInfo->psz, -1);
521                                 wLVFindInfo.psz = pw0;
522                                 wLVFindInfo.lParam = pmLVFindInfo->lParam;
523                                 wLVFindInfo.pt = pmLVFindInfo->pt;
524                                 wLVFindInfo.vkDirection = pmLVFindInfo->vkDirection;
525                                 r = SendMessageW(hWnd, LVM_FINDITEMW, wParam, (LPARAM)&wLVItem);
526                                 break;
527                         case LVM_INSERTCOLUMNA:
528                                 pmLVColumn = (LVCOLUMNA*)lParam;
529                                 wLVColumn.mask = pmLVColumn->mask;
530                                 wLVColumn.fmt = pmLVColumn->fmt;
531                                 wLVColumn.cx = pmLVColumn->cx;
532                                 pw0 = DuplicateMtoW(pmLVColumn->pszText, -1);
533                                 wLVColumn.pszText = pw0;
534                                 wLVColumn.cchTextMax = pmLVColumn->cchTextMax;
535                                 wLVColumn.iSubItem = pmLVColumn->iSubItem;
536                                 wLVColumn.iImage = pmLVColumn->iImage;
537                                 wLVColumn.iOrder = pmLVColumn->iOrder;
538                                 r = SendMessageW(hWnd, LVM_INSERTCOLUMNW, wParam, (LPARAM)&wLVColumn);
539                                 break;
540                         case LVM_GETITEMTEXTA:
541                                 pmLVItem = (LVITEMA*)lParam;
542                                 wLVItem.mask = pmLVItem->mask;
543                                 wLVItem.iItem = pmLVItem->iItem;
544                                 wLVItem.iSubItem = pmLVItem->iSubItem;
545                                 wLVItem.state = pmLVItem->state;
546                                 wLVItem.stateMask = pmLVItem->stateMask;
547                                 Size = pmLVItem->cchTextMax * 4;
548                                 pw0 = AllocateStringW(Size);
549                                 wLVItem.pszText = pw0;
550                                 wLVItem.cchTextMax = Size;
551                                 wLVItem.iImage = pmLVItem->iImage;
552                                 wLVItem.lParam = pmLVItem->lParam;
553                                 wLVItem.iIndent = pmLVItem->iIndent;
554                                 r = SendMessageW(hWnd, LVM_GETITEMTEXTW, wParam, (LPARAM)&wLVItem);
555                                 pmLVItem->mask = wLVItem.mask;
556                                 pmLVItem->iItem = wLVItem.iItem;
557                                 pmLVItem->iSubItem = wLVItem.iSubItem;
558                                 pmLVItem->state = wLVItem.state;
559                                 pmLVItem->stateMask = wLVItem.stateMask;
560                                 WtoM(pmLVItem->pszText, pmLVItem->cchTextMax, wLVItem.pszText, -1);
561                                 TerminateStringM(pmLVItem->pszText, pmLVItem->cchTextMax);
562                                 pmLVItem->iImage = wLVItem.iImage;
563                                 pmLVItem->lParam = wLVItem.lParam;
564                                 pmLVItem->iIndent = wLVItem.iIndent;
565                                 break;
566                         default:
567                                 r = SendMessageW(hWnd, Msg, wParam, lParam);
568                                 break;
569                         }
570                 }
571                 else if(_wcsicmp(ClassName, STATUSCLASSNAMEW) == 0)
572                 {
573                         switch(Msg)
574                         {
575                         case SB_SETTEXTA:
576                                 pw0 = DuplicateMtoW((LPCSTR)lParam, -1);
577                                 r = SendMessageW(hWnd, SB_SETTEXTW, wParam, (LPARAM)pw0);
578                                 break;
579                         default:
580                                 r = SendMessageW(hWnd, Msg, wParam, lParam);
581                                 break;
582                         }
583                 }
584                 else
585                         r = SendMessageW(hWnd, Msg, wParam, lParam);
586                 break;
587         }
588 END_ROUTINE
589         FreeDuplicatedString(pw0);
590         FreeDuplicatedString(pw1);
591         return r;
592 }
593
594 LRESULT SendDlgItemMessageM(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam)
595 {
596         LRESULT r = 0;
597 START_ROUTINE
598         r = SendMessageM(GetDlgItem(hDlg, nIDDlgItem), Msg, wParam, lParam);
599 END_ROUTINE
600         return r;
601 }
602
603 BOOL SetWindowTextM(HWND hWnd, LPCSTR lpString)
604 {
605         BOOL r = FALSE;
606         wchar_t* pw0 = NULL;
607 START_ROUTINE
608         pw0 = DuplicateMtoW(lpString, -1);
609         r = SetWindowTextW(hWnd, pw0);
610 END_ROUTINE
611         FreeDuplicatedString(pw0);
612         return r;
613 }
614
615 UINT DragQueryFileM(HDROP hDrop, UINT iFile, LPSTR lpszFile, UINT cch)
616 {
617         UINT r = 0;
618         wchar_t* pw0 = NULL;
619 START_ROUTINE
620         pw0 = AllocateStringW(cch * 4);
621         DragQueryFileW(hDrop, iFile, pw0, cch * 4);
622         WtoM(lpszFile, cch, pw0, -1);
623         r = TerminateStringM(lpszFile, cch);
624 END_ROUTINE
625         FreeDuplicatedString(pw0);
626         return r;
627 }
628
629 DWORD GetCurrentDirectoryM(DWORD nBufferLength, LPSTR lpBuffer)
630 {
631         DWORD r = 0;
632         wchar_t* pw0 = NULL;
633 START_ROUTINE
634         pw0 = AllocateStringW(nBufferLength * 4);
635         GetCurrentDirectoryW(nBufferLength * 4, pw0);
636         WtoM(lpBuffer, nBufferLength, pw0, -1);
637         r = TerminateStringM(lpBuffer, nBufferLength);
638 END_ROUTINE
639         FreeDuplicatedString(pw0);
640         return r;
641 }
642
643 BOOL SetCurrentDirectoryM(LPCSTR lpPathName)
644 {
645         BOOL r = FALSE;
646         wchar_t* pw0 = NULL;
647 START_ROUTINE
648         pw0 = DuplicateMtoW(lpPathName, -1);
649         r = SetCurrentDirectoryW(pw0);
650 END_ROUTINE
651         FreeDuplicatedString(pw0);
652         return r;
653 }
654
655 BOOL SetDllDirectoryM(LPCSTR lpPathName)
656 {
657         BOOL r = FALSE;
658         wchar_t* pw0 = NULL;
659 START_ROUTINE
660         pw0 = DuplicateMtoW(lpPathName, -1);
661         r = SetDllDirectoryW(pw0);
662 END_ROUTINE
663         FreeDuplicatedString(pw0);
664         return r;
665 }
666
667 DWORD GetFileAttributesM(LPCSTR lpFileName)
668 {
669         DWORD r = FALSE;
670         wchar_t* pw0 = NULL;
671 START_ROUTINE
672         pw0 = DuplicateMtoW(lpFileName, -1);
673         r = GetFileAttributesW(pw0);
674 END_ROUTINE
675         FreeDuplicatedString(pw0);
676         return r;
677 }
678
679 DWORD GetModuleFileNameM(HMODULE hModule, LPCH lpFilename, DWORD nSize)
680 {
681         DWORD r = 0;
682         wchar_t* pw0 = NULL;
683 START_ROUTINE
684         pw0 = AllocateStringW(nSize * 4);
685         GetModuleFileNameW(hModule, pw0, nSize * 4);
686         WtoM(lpFilename, nSize, pw0, -1);
687         r = TerminateStringM(lpFilename, nSize);
688 END_ROUTINE
689         FreeDuplicatedString(pw0);
690         return r;
691 }
692
693 LSTATUS RegOpenKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
694 {
695         LSTATUS r = 0;
696         wchar_t* pw0 = NULL;
697 START_ROUTINE
698         pw0 = DuplicateMtoW(lpSubKey, -1);
699         r = RegOpenKeyExW(hKey, pw0, ulOptions, samDesired, phkResult);
700 END_ROUTINE
701         FreeDuplicatedString(pw0);
702         return r;
703 }
704
705 LSTATUS RegCreateKeyExM(HKEY hKey, LPCSTR lpSubKey, DWORD Reserved, LPSTR lpClass, DWORD dwOptions, REGSAM samDesired, CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition)
706 {
707         LSTATUS r = 0;
708         wchar_t* pw0 = NULL;
709         wchar_t* pw1 = NULL;
710 START_ROUTINE
711         pw0 = DuplicateMtoW(lpSubKey, -1);
712         pw1 = DuplicateMtoW(lpClass, -1);
713         r = RegCreateKeyExW(hKey, pw0, Reserved, pw1, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
714 END_ROUTINE
715         FreeDuplicatedString(pw0);
716         FreeDuplicatedString(pw1);
717         return r;
718 }
719
720 LSTATUS RegDeleteValueM(HKEY hKey, LPCSTR lpValueName)
721 {
722         LSTATUS r = 0;
723         wchar_t* pw0 = NULL;
724 START_ROUTINE
725         pw0 = DuplicateMtoW(lpValueName, -1);
726         r = RegDeleteValueW(hKey, pw0);
727 END_ROUTINE
728         FreeDuplicatedString(pw0);
729         return r;
730 }
731
732 LSTATUS RegQueryValueExM(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)
733 {
734         LSTATUS r = 0;
735         wchar_t* pw0 = NULL;
736         wchar_t* pw1 = NULL;
737         DWORD dwType;
738         DWORD wcbData;
739 START_ROUTINE
740         pw0 = DuplicateMtoW(lpValueName, -1);
741         if(RegQueryValueExW(hKey, pw0, NULL, &dwType, NULL, 0) == ERROR_SUCCESS)
742         {
743                 switch(dwType)
744                 {
745                 case REG_SZ:
746                 case REG_EXPAND_SZ:
747                 case REG_MULTI_SZ:
748                         if(lpData && lpcbData)
749                         {
750                                 pw1 = AllocateStringW(*lpcbData / sizeof(char) * 4);
751                                 wcbData = *lpcbData / sizeof(char) * 4;
752                                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, (LPBYTE)pw1, &wcbData);
753                                 *lpcbData = sizeof(char) * WtoM((char*)lpData, *lpcbData / sizeof(char), pw1, wcbData / sizeof(wchar_t));
754                         }
755                         break;
756                 default:
757                         r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);
758                         break;
759                 }
760         }
761         else
762                 r = RegQueryValueExW(hKey, pw0, lpReserved, lpType, lpData, lpcbData);
763 END_ROUTINE
764         FreeDuplicatedString(pw0);
765         FreeDuplicatedString(pw1);
766         return r;
767 }
768
769 LSTATUS RegSetValueExM(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE* lpData, DWORD cbData)
770 {
771         LSTATUS r = 0;
772         wchar_t* pw0 = NULL;
773         wchar_t* pw1 = NULL;
774         DWORD wcbData;
775 START_ROUTINE
776         pw0 = DuplicateMtoW(lpValueName, -1);
777         switch(dwType)
778         {
779         case REG_SZ:
780         case REG_EXPAND_SZ:
781         case REG_MULTI_SZ:
782                 wcbData = MtoW(NULL, 0, (char*)lpData, cbData / sizeof(char));
783                 pw1 = AllocateStringW(wcbData);
784                 MtoW(pw1, wcbData, (char*)lpData, cbData / sizeof(char));
785                 wcbData = sizeof(wchar_t) * wcbData;
786                 lpData = (BYTE*)pw1;
787                 cbData = wcbData;
788                 break;
789         }
790         r = RegSetValueExW(hKey, pw0, Reserved, dwType, lpData, cbData);
791 END_ROUTINE
792         FreeDuplicatedString(pw0);
793         FreeDuplicatedString(pw1);
794         return r;
795 }
796
797 BOOL TextOutM(HDC hdc, int x, int y, LPCSTR lpString, int c)
798 {
799         BOOL r = FALSE;
800         wchar_t* pw0 = NULL;
801 START_ROUTINE
802         pw0 = DuplicateMtoW(lpString, c);
803         r = TextOutW(hdc, x, y, pw0, wcslen(pw0));
804 END_ROUTINE
805         FreeDuplicatedString(pw0);
806         return r;
807 }
808
809 BOOL GetTextExtentPoint32M(HDC hdc, LPCSTR lpString, int c, LPSIZE psizl)
810 {
811         BOOL r = FALSE;
812         wchar_t* pw0 = NULL;
813 START_ROUTINE
814         pw0 = DuplicateMtoW(lpString, c);
815         r = GetTextExtentPoint32W(hdc, pw0, wcslen(pw0), psizl);
816 END_ROUTINE
817         FreeDuplicatedString(pw0);
818         return r;
819 }
820
821 INT_PTR PropertySheetM(LPCPROPSHEETHEADERA v0)
822 {
823         INT_PTR r = 0;
824         PROPSHEETHEADERW a0;
825         PROPSHEETPAGEW* pwPage;
826         UINT i;
827 START_ROUTINE
828         a0.dwSize = sizeof(PROPSHEETHEADERW);
829         a0.dwFlags = v0->dwFlags;
830         a0.hwndParent = v0->hwndParent;
831         a0.hInstance = v0->hInstance;
832         if(v0->dwFlags & PSH_USEICONID)
833                 a0.pszIcon = DuplicateMtoW(v0->pszIcon, -1);
834         else
835                 a0.hIcon = v0->hIcon;
836         a0.pszCaption = DuplicateMtoW(v0->pszCaption, -1);
837         a0.nPages = v0->nPages;
838         a0.pStartPage = DuplicateMtoW(v0->pStartPage, -1);
839         if(v0->ppsp && (pwPage = (PROPSHEETPAGEW*)malloc(sizeof(PROPSHEETPAGEW) * v0->nPages)))
840         {
841                 for(i = 0; i < v0->nPages; i++)
842                 {
843                         pwPage[i].dwSize = sizeof(PROPSHEETPAGEW);
844                         pwPage[i].dwFlags = v0->ppsp[i].dwFlags;
845                         pwPage[i].hInstance = v0->ppsp[i].hInstance;
846                         pwPage[i].pszTemplate = DuplicateMtoW(v0->ppsp[i].pszTemplate, -1);
847                         if(v0->ppsp[i].dwFlags & PSP_USEICONID)
848                                 pwPage[i].pszIcon = DuplicateMtoW(v0->ppsp[i].pszIcon, -1);
849                         else
850                                 pwPage[i].hIcon = v0->ppsp[i].hIcon;
851                         if(v0->ppsp[i].dwFlags & PSP_USETITLE)
852                                 pwPage[i].pszTitle = DuplicateMtoW(v0->ppsp[i].pszTitle, -1);
853                         pwPage[i].pfnDlgProc = v0->ppsp[i].pfnDlgProc;
854                         pwPage[i].lParam = v0->ppsp[i].lParam;
855                         // TODO: pfnCallback
856                         pwPage[i].pfnCallback = v0->ppsp[i].pfnCallback;
857                         pwPage[i].pcRefParent = v0->ppsp[i].pcRefParent;
858 //                      pwPage[i].pszHeaderTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderTitle, -1);
859 //                      pwPage[i].pszHeaderSubTitle = DuplicateMtoW(v0->ppsp[i].pszHeaderSubTitle, -1);
860                         pwPage[i].hActCtx = v0->ppsp[i].hActCtx;
861 //                      pwPage[i].pszbmHeader = DuplicateMtoW(v0->ppsp[i].pszbmHeader, -1);
862                 }
863         }
864         else
865                 pwPage = NULL;
866         a0.ppsp = pwPage;
867         a0.pfnCallback = v0->pfnCallback;
868         r = PropertySheetW(&a0);
869         if(a0.dwFlags & PSH_USEICONID)
870                 FreeDuplicatedString(a0.pszIcon);
871         FreeDuplicatedString(a0.pszCaption);
872         FreeDuplicatedString(a0.pStartPage);
873         if(pwPage)
874         {
875                 for(i = 0; i < v0->nPages; i++)
876                 {
877                         FreeDuplicatedString(pwPage[i].pszTemplate);
878                         if(pwPage[i].dwFlags & PSP_USEICONID)
879                                 FreeDuplicatedString(pwPage[i].pszIcon);
880                         if(pwPage[i].dwFlags & PSP_USETITLE)
881                                 FreeDuplicatedString(pwPage[i].pszTitle);
882 //                      FreeDuplicatedString(pwPage[i].pszHeaderTitle);
883 //                      FreeDuplicatedString(pwPage[i].pszHeaderSubTitle);
884 //                      FreeDuplicatedString(pwPage[i].pszbmHeader);
885                 }
886                 free(pwPage);
887         }
888 END_ROUTINE
889         return r;
890 }
891
892 BOOL GetOpenFileNameM(LPOPENFILENAMEA v0)
893 {
894         BOOL r = FALSE;
895         wchar_t* pw0 = NULL;
896         wchar_t* pw1 = NULL;
897         wchar_t* pw2 = NULL;
898         wchar_t* pw3 = NULL;
899         wchar_t* pw4 = NULL;
900         wchar_t* pw5 = NULL;
901         wchar_t* pw6 = NULL;
902         wchar_t* pw7 = NULL;
903         wchar_t* pw8 = NULL;
904         wchar_t* pw9 = NULL;
905         OPENFILENAMEW wofn;
906 START_ROUTINE
907         wofn.lStructSize = sizeof(OPENFILENAMEW);
908         wofn.hwndOwner = v0->hwndOwner;
909         wofn.hInstance = v0->hInstance;
910         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);
911         wofn.lpstrFilter = pw0;
912         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);
913         wofn.lpstrCustomFilter = pw1;
914         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;
915         wofn.nFilterIndex = v0->nFilterIndex;
916         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);
917         wofn.lpstrFile = pw2;
918         wofn.nMaxFile = v0->nMaxFile * 4;
919         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);
920         wofn.lpstrFileTitle = pw3;
921         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;
922         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);
923         wofn.lpstrInitialDir = pw4;
924         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);
925         wofn.lpstrTitle = pw5;
926         wofn.Flags = v0->Flags;
927         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);
928         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);
929         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);
930         wofn.lpstrDefExt = pw6;
931         wofn.lCustData = v0->lCustData;
932         wofn.lpfnHook = v0->lpfnHook;
933         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);
934         wofn.pvReserved = v0->pvReserved;
935         wofn.FlagsEx = v0->FlagsEx;
936         r = GetOpenFileNameW(&wofn);
937         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);
938         TerminateStringM(v0->lpstrFile, v0->nMaxFile);
939         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);
940         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);
941 END_ROUTINE
942         FreeDuplicatedString(pw0);
943         FreeDuplicatedString(pw1);
944         FreeDuplicatedString(pw2);
945         FreeDuplicatedString(pw3);
946         FreeDuplicatedString(pw4);
947         FreeDuplicatedString(pw5);
948         FreeDuplicatedString(pw6);
949         return r;
950 }
951
952 BOOL GetSaveFileNameM(LPOPENFILENAMEA v0)
953 {
954         BOOL r = FALSE;
955         wchar_t* pw0 = NULL;
956         wchar_t* pw1 = NULL;
957         wchar_t* pw2 = NULL;
958         wchar_t* pw3 = NULL;
959         wchar_t* pw4 = NULL;
960         wchar_t* pw5 = NULL;
961         wchar_t* pw6 = NULL;
962         wchar_t* pw7 = NULL;
963         wchar_t* pw8 = NULL;
964         wchar_t* pw9 = NULL;
965         OPENFILENAMEW wofn;
966 START_ROUTINE
967         wofn.lStructSize = sizeof(OPENFILENAMEW);
968         wofn.hwndOwner = v0->hwndOwner;
969         wofn.hInstance = v0->hInstance;
970         pw0 = DuplicateMtoWMultiString(v0->lpstrFilter);
971         wofn.lpstrFilter = pw0;
972         pw1 = DuplicateMtoWBuffer(v0->lpstrCustomFilter, -1, v0->nMaxCustFilter * 4);
973         wofn.lpstrCustomFilter = pw1;
974         wofn.nMaxCustFilter = v0->nMaxCustFilter * 4;
975         wofn.nFilterIndex = v0->nFilterIndex;
976         pw2 = DuplicateMtoWMultiStringBuffer(v0->lpstrFile, v0->nMaxFile * 4);
977         wofn.lpstrFile = pw2;
978         wofn.nMaxFile = v0->nMaxFile * 4;
979         pw3 = DuplicateMtoWBuffer(v0->lpstrFileTitle, -1, v0->nMaxFileTitle * 4);
980         wofn.lpstrFileTitle = pw3;
981         wofn.nMaxFileTitle = v0->nMaxFileTitle * 4;
982         pw4 = DuplicateMtoW(v0->lpstrInitialDir, -1);
983         wofn.lpstrInitialDir = pw4;
984         pw5 = DuplicateMtoW(v0->lpstrTitle, -1);
985         wofn.lpstrTitle = pw5;
986         wofn.Flags = v0->Flags;
987         wofn.nFileOffset = MtoW(NULL, 0, v0->lpstrFile, v0->nFileOffset);
988         wofn.nFileExtension = MtoW(NULL, 0, v0->lpstrFile, v0->nFileExtension);
989         pw6 = DuplicateMtoW(v0->lpstrDefExt, -1);
990         wofn.lpstrDefExt = pw6;
991         wofn.lCustData = v0->lCustData;
992         wofn.lpfnHook = v0->lpfnHook;
993         wofn.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);
994         wofn.pvReserved = v0->pvReserved;
995         wofn.FlagsEx = v0->FlagsEx;
996         r = GetSaveFileNameW(&wofn);
997         WtoM(v0->lpstrFile, v0->nMaxFile, wofn.lpstrFile, -1);
998         TerminateStringM(v0->lpstrFile, v0->nMaxFile);
999         v0->nFileOffset = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileOffset);
1000         v0->nFileExtension = WtoM(NULL, 0, wofn.lpstrFile, wofn.nFileExtension);
1001 END_ROUTINE
1002         FreeDuplicatedString(pw0);
1003         FreeDuplicatedString(pw1);
1004         FreeDuplicatedString(pw2);
1005         FreeDuplicatedString(pw3);
1006         FreeDuplicatedString(pw4);
1007         FreeDuplicatedString(pw5);
1008         FreeDuplicatedString(pw6);
1009         return r;
1010 }
1011
1012 HWND HtmlHelpM(HWND hwndCaller, LPCSTR pszFile, UINT uCommand, DWORD_PTR dwData)
1013 {
1014         HWND r = NULL;
1015         wchar_t* pw0 = NULL;
1016 START_ROUTINE
1017         pw0 = DuplicateMtoW(pszFile, -1);
1018         r = HtmlHelpW(hwndCaller, pw0, uCommand, dwData);
1019 END_ROUTINE
1020         FreeDuplicatedString(pw0);
1021         return r;
1022 }
1023
1024 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)
1025 {
1026         BOOL r = FALSE;
1027         wchar_t* pw0 = NULL;
1028         wchar_t* pw1 = NULL;
1029         wchar_t* pw2 = NULL;
1030         wchar_t* pw3 = NULL;
1031         wchar_t* pw4 = NULL;
1032         wchar_t* pw5 = NULL;
1033         STARTUPINFOW wStartupInfo;
1034 START_ROUTINE
1035         pw0 = DuplicateMtoW(lpApplicationName, -1);
1036         pw1 = DuplicateMtoWBuffer(lpCommandLine, -1, (strlen(lpCommandLine) + 1) * 4);
1037         pw2 = DuplicateMtoW(lpCurrentDirectory, -1);
1038         wStartupInfo.cb = sizeof(LPSTARTUPINFOW);
1039         pw3 = DuplicateMtoW(lpStartupInfo->lpReserved, -1);
1040         wStartupInfo.lpReserved = pw3;
1041         pw4 = DuplicateMtoW(lpStartupInfo->lpDesktop, -1);
1042         wStartupInfo.lpDesktop = pw4;
1043         pw5 = DuplicateMtoW(lpStartupInfo->lpTitle, -1);
1044         wStartupInfo.lpTitle = pw5;
1045         wStartupInfo.dwX = lpStartupInfo->dwX;
1046         wStartupInfo.dwY = lpStartupInfo->dwY;
1047         wStartupInfo.dwXSize = lpStartupInfo->dwXSize;
1048         wStartupInfo.dwYSize = lpStartupInfo->dwYSize;
1049         wStartupInfo.dwXCountChars = lpStartupInfo->dwXCountChars;
1050         wStartupInfo.dwYCountChars = lpStartupInfo->dwYCountChars;
1051         wStartupInfo.dwFillAttribute = lpStartupInfo->dwFillAttribute;
1052         wStartupInfo.dwFlags = lpStartupInfo->dwFlags;
1053         wStartupInfo.wShowWindow = lpStartupInfo->wShowWindow;
1054         wStartupInfo.cbReserved2 = lpStartupInfo->cbReserved2;
1055         wStartupInfo.lpReserved2 = lpStartupInfo->lpReserved2;
1056         wStartupInfo.hStdInput = lpStartupInfo->hStdInput;
1057         wStartupInfo.hStdOutput = lpStartupInfo->hStdOutput;
1058         wStartupInfo.hStdError = lpStartupInfo->hStdError;
1059         r = CreateProcessW(pw0, pw1, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, pw2, &wStartupInfo, lpProcessInformation);
1060         WtoM(lpCommandLine, strlen(lpCommandLine) + 1, pw1, -1);
1061 END_ROUTINE
1062         FreeDuplicatedString(pw0);
1063         FreeDuplicatedString(pw1);
1064         FreeDuplicatedString(pw2);
1065         FreeDuplicatedString(pw3);
1066         FreeDuplicatedString(pw4);
1067         FreeDuplicatedString(pw5);
1068         return r;
1069 }
1070
1071 HINSTANCE FindExecutableM(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1072 {
1073         HINSTANCE r = NULL;
1074         wchar_t* pw0 = NULL;
1075         wchar_t* pw1 = NULL;
1076         wchar_t* pw2 = NULL;
1077         wchar_t* pw3 = NULL;
1078 START_ROUTINE
1079         pw0 = DuplicateMtoW(lpFile, -1);
1080         pw1 = DuplicateMtoW(lpDirectory, -1);
1081         pw2 = AllocateStringW(MAX_PATH * 4);
1082         r = FindExecutableW(pw0, pw1, pw2);
1083         // \83o\83b\83t\83@\92·\95s\96¾\82Ì\82½\82ß\83I\81[\83o\81[\83\89\83\93\82Ì\89Â\94\\90«\82 \82è
1084         WtoM(lpResult, MAX_PATH, pw2, -1);
1085         TerminateStringM(lpResult, MAX_PATH);
1086 END_ROUTINE
1087         FreeDuplicatedString(pw0);
1088         FreeDuplicatedString(pw1);
1089         FreeDuplicatedString(pw2);
1090         FreeDuplicatedString(pw3);
1091         return r;
1092 }
1093
1094 HINSTANCE ShellExecuteM(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)
1095 {
1096         HINSTANCE r = NULL;
1097         wchar_t* pw0 = NULL;
1098         wchar_t* pw1 = NULL;
1099         wchar_t* pw2 = NULL;
1100         wchar_t* pw3 = NULL;
1101 START_ROUTINE
1102         pw0 = DuplicateMtoW(lpOperation, -1);
1103         pw1 = DuplicateMtoW(lpFile, -1);
1104         pw2 = DuplicateMtoW(lpParameters, -1);
1105         pw3 = DuplicateMtoW(lpDirectory, -1);
1106         r = ShellExecuteW(hwnd, pw0, pw1, pw2, pw3, nShowCmd);
1107 END_ROUTINE
1108         FreeDuplicatedString(pw0);
1109         FreeDuplicatedString(pw1);
1110         FreeDuplicatedString(pw2);
1111         FreeDuplicatedString(pw3);
1112         return r;
1113 }
1114
1115 PIDLIST_ABSOLUTE SHBrowseForFolderM(LPBROWSEINFOA lpbi)
1116 {
1117         PIDLIST_ABSOLUTE r = NULL;
1118         wchar_t* pw0 = NULL;
1119         wchar_t* pw1 = NULL;
1120         BROWSEINFOW wbi;
1121 START_ROUTINE
1122         wbi.hwndOwner = lpbi->hwndOwner;
1123         wbi.pidlRoot = lpbi->pidlRoot;
1124         pw0 = DuplicateMtoWBuffer(lpbi->pszDisplayName, -1, MAX_PATH * 4);
1125         wbi.pszDisplayName = pw0;
1126         pw1 = DuplicateMtoW(lpbi->lpszTitle, -1);
1127         wbi.lpszTitle = pw1;
1128         wbi.ulFlags = lpbi->ulFlags;
1129         // TODO: lpfn
1130         wbi.lpfn = lpbi->lpfn;
1131         wbi.lParam = lpbi->lParam;
1132         wbi.iImage = lpbi->iImage;
1133         r = SHBrowseForFolderW(&wbi);
1134         // \83o\83b\83t\83@\92·\95s\96¾\82Ì\82½\82ß\83I\81[\83o\81[\83\89\83\93\82Ì\89Â\94\\90«\82 \82è
1135         WtoM(lpbi->pszDisplayName, MAX_PATH, wbi.pszDisplayName, -1);
1136         lpbi->iImage = wbi.iImage;
1137 END_ROUTINE
1138         FreeDuplicatedString(pw0);
1139         FreeDuplicatedString(pw1);
1140         return r;
1141 }
1142
1143 BOOL SHGetPathFromIDListM(PCIDLIST_ABSOLUTE pidl, LPSTR pszPath)
1144 {
1145         BOOL r = FALSE;
1146         wchar_t* pw0 = NULL;
1147 START_ROUTINE
1148         pw0 = AllocateStringW(MAX_PATH * 4);
1149         r = SHGetPathFromIDListW(pidl, pw0);
1150         // \83o\83b\83t\83@\92·\95s\96¾\82Ì\82½\82ß\83I\81[\83o\81[\83\89\83\93\82Ì\89Â\94\\90«\82 \82è
1151         WtoM(pszPath, MAX_PATH, pw0, -1);
1152         TerminateStringM(pszPath, MAX_PATH);
1153 END_ROUTINE
1154         FreeDuplicatedString(pw0);
1155         return r;
1156 }
1157
1158 int SHFileOperationM(LPSHFILEOPSTRUCTA lpFileOp)
1159 {
1160         int r = 0;
1161         wchar_t* pw0 = NULL;
1162         wchar_t* pw1 = NULL;
1163         wchar_t* pw2 = NULL;
1164         SHFILEOPSTRUCTW wFileOp;
1165 START_ROUTINE
1166         wFileOp.hwnd = lpFileOp->hwnd;
1167         wFileOp.wFunc = lpFileOp->wFunc;
1168         pw0 = DuplicateMtoWMultiString(lpFileOp->pFrom);
1169         wFileOp.pFrom = pw0;
1170         pw1 = DuplicateMtoWMultiString(lpFileOp->pTo);
1171         wFileOp.pTo = pw1;
1172         wFileOp.fFlags = lpFileOp->fFlags;
1173         wFileOp.fAnyOperationsAborted = lpFileOp->fAnyOperationsAborted;
1174         wFileOp.hNameMappings = lpFileOp->hNameMappings;
1175         pw2 = DuplicateMtoW(lpFileOp->lpszProgressTitle, -1);
1176         r = SHFileOperationW(&wFileOp);
1177         lpFileOp->fAnyOperationsAborted = wFileOp.fAnyOperationsAborted;
1178 END_ROUTINE
1179         FreeDuplicatedString(pw0);
1180         FreeDuplicatedString(pw1);
1181         FreeDuplicatedString(pw2);
1182         return r;
1183 }
1184
1185 BOOL AppendMenuM(HMENU hMenu, UINT uFlags, UINT_PTR uIDNewItem, LPCSTR lpNewItem)
1186 {
1187         int r = 0;
1188         wchar_t* pw0 = NULL;
1189 START_ROUTINE
1190         if(uFlags & (MF_BITMAP | MF_OWNERDRAW))
1191                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, (LPCWSTR)lpNewItem);
1192         else
1193         {
1194                 pw0 = DuplicateMtoW(lpNewItem, -1);
1195                 r = AppendMenuW(hMenu, uFlags, uIDNewItem, pw0);
1196         }
1197 END_ROUTINE
1198         FreeDuplicatedString(pw0);
1199         return r;
1200 }
1201
1202 BOOL GetMenuItemInfoM(HMENU hmenu, UINT item, BOOL fByPosition, LPMENUITEMINFOA lpmii)
1203 {
1204         BOOL r = FALSE;
1205         wchar_t* pw0 = NULL;
1206         MENUITEMINFOW wmii;
1207 START_ROUTINE
1208         wmii.cbSize = sizeof(MENUITEMINFOW);
1209         wmii.fMask = lpmii->fMask;
1210         wmii.fType = lpmii->fType;
1211         wmii.fState = lpmii->fState;
1212         wmii.wID = lpmii->wID;
1213         wmii.hSubMenu = lpmii->hSubMenu;
1214         wmii.hbmpChecked = lpmii->hbmpChecked;
1215         wmii.hbmpUnchecked = lpmii->hbmpUnchecked;
1216         wmii.dwItemData = lpmii->dwItemData;
1217         pw0 = DuplicateMtoWBuffer(lpmii->dwTypeData, -1, lpmii->cch * 4);
1218         wmii.dwTypeData = pw0;
1219         wmii.cch = lpmii->cch * 4;
1220         r = GetMenuItemInfoW(hmenu, item, fByPosition, &wmii);
1221         lpmii->fType = wmii.fType;
1222         lpmii->fState = wmii.fState;
1223         lpmii->wID = wmii.wID;
1224         lpmii->hSubMenu = wmii.hSubMenu;
1225         lpmii->hbmpChecked = wmii.hbmpChecked;
1226         lpmii->hbmpUnchecked = wmii.hbmpUnchecked;
1227         lpmii->dwItemData = wmii.dwItemData;
1228         WtoM(lpmii->dwTypeData, lpmii->cch, wmii.dwTypeData, -1);
1229         TerminateStringM(lpmii->dwTypeData, lpmii->cch);
1230 END_ROUTINE
1231         FreeDuplicatedString(pw0);
1232         return r;
1233 }
1234
1235 HFONT CreateFontIndirectM(CONST LOGFONTA *lplf)
1236 {
1237         HFONT r = NULL;
1238         LOGFONTW wlf;
1239 START_ROUTINE
1240         wlf.lfHeight = lplf->lfHeight;
1241         wlf.lfWidth = lplf->lfWidth;
1242         wlf.lfEscapement = lplf->lfEscapement;
1243         wlf.lfOrientation = lplf->lfOrientation;
1244         wlf.lfWeight = lplf->lfWeight;
1245         wlf.lfItalic = lplf->lfItalic;
1246         wlf.lfUnderline = lplf->lfUnderline;
1247         wlf.lfStrikeOut = lplf->lfStrikeOut;
1248         wlf.lfCharSet = lplf->lfCharSet;
1249         wlf.lfOutPrecision = lplf->lfOutPrecision;
1250         wlf.lfClipPrecision = lplf->lfClipPrecision;
1251         wlf.lfQuality = lplf->lfQuality;
1252         wlf.lfPitchAndFamily = lplf->lfPitchAndFamily;
1253         MtoW(wlf.lfFaceName, LF_FACESIZE, lplf->lfFaceName, -1);
1254         TerminateStringW(wlf.lfFaceName, LF_FACESIZE);
1255         r = CreateFontIndirect(&wlf);
1256 END_ROUTINE
1257         return r;
1258 }
1259
1260 BOOL ChooseFontM(LPCHOOSEFONTA v0)
1261 {
1262         BOOL r = FALSE;
1263         wchar_t* pw0 = NULL;
1264         CHOOSEFONTW a0;
1265         LOGFONTW* pwlf;
1266 START_ROUTINE
1267         a0.lStructSize = sizeof(CHOOSEFONTW);
1268         a0.hwndOwner = v0->hwndOwner;
1269         a0.hDC = v0->hDC;
1270         if(v0->lpLogFont && (pwlf = (LOGFONTW*)malloc(sizeof(LOGFONTW))))
1271         {
1272                 pwlf->lfHeight = v0->lpLogFont->lfHeight;
1273                 pwlf->lfWidth = v0->lpLogFont->lfWidth;
1274                 pwlf->lfEscapement = v0->lpLogFont->lfEscapement;
1275                 pwlf->lfOrientation = v0->lpLogFont->lfOrientation;
1276                 pwlf->lfWeight = v0->lpLogFont->lfWeight;
1277                 pwlf->lfItalic = v0->lpLogFont->lfItalic;
1278                 pwlf->lfUnderline = v0->lpLogFont->lfUnderline;
1279                 pwlf->lfStrikeOut = v0->lpLogFont->lfStrikeOut;
1280                 pwlf->lfCharSet = v0->lpLogFont->lfCharSet;
1281                 pwlf->lfOutPrecision = v0->lpLogFont->lfOutPrecision;
1282                 pwlf->lfClipPrecision = v0->lpLogFont->lfClipPrecision;
1283                 pwlf->lfQuality = v0->lpLogFont->lfQuality;
1284                 pwlf->lfPitchAndFamily = v0->lpLogFont->lfPitchAndFamily;
1285                 MtoW(pwlf->lfFaceName, LF_FACESIZE, v0->lpLogFont->lfFaceName, -1);
1286                 TerminateStringW(pwlf->lfFaceName, LF_FACESIZE);
1287         }
1288         else
1289                 pwlf = NULL;
1290         a0.lpLogFont = pwlf;
1291         a0.iPointSize = v0->iPointSize;
1292         a0.Flags = v0->Flags;
1293         a0.rgbColors = v0->rgbColors;
1294         a0.lCustData = v0->lCustData;
1295         a0.lpfnHook = v0->lpfnHook;
1296         a0.lpTemplateName = DuplicateMtoW(v0->lpTemplateName, -1);
1297         a0.hInstance = v0->hInstance;
1298         a0.lpszStyle = DuplicateMtoWBuffer(v0->lpszStyle, -1, LF_FACESIZE * 4);
1299         a0.nFontType = v0->nFontType;
1300         a0.nSizeMin = v0->nSizeMin;
1301         a0.nSizeMax = v0->nSizeMax;
1302         r = ChooseFontW(&a0);
1303         if(v0->lpLogFont)
1304         {
1305                 v0->lpLogFont->lfHeight = pwlf->lfHeight;
1306                 v0->lpLogFont->lfWidth = pwlf->lfWidth;
1307                 v0->lpLogFont->lfEscapement = pwlf->lfEscapement;
1308                 v0->lpLogFont->lfOrientation = pwlf->lfOrientation;
1309                 v0->lpLogFont->lfWeight = pwlf->lfWeight;
1310                 v0->lpLogFont->lfItalic = pwlf->lfItalic;
1311                 v0->lpLogFont->lfUnderline = pwlf->lfUnderline;
1312                 v0->lpLogFont->lfStrikeOut = pwlf->lfStrikeOut;
1313                 v0->lpLogFont->lfCharSet = pwlf->lfCharSet;
1314                 v0->lpLogFont->lfOutPrecision = pwlf->lfOutPrecision;
1315                 v0->lpLogFont->lfClipPrecision = pwlf->lfClipPrecision;
1316                 v0->lpLogFont->lfQuality = pwlf->lfQuality;
1317                 v0->lpLogFont->lfPitchAndFamily = pwlf->lfPitchAndFamily;
1318                 WtoM(v0->lpLogFont->lfFaceName, LF_FACESIZE, pwlf->lfFaceName, -1);
1319                 TerminateStringM(v0->lpLogFont->lfFaceName, LF_FACESIZE);
1320         }
1321         v0->rgbColors = a0.rgbColors;
1322         WtoM(v0->lpszStyle, LF_FACESIZE, a0.lpszStyle, -1);
1323         TerminateStringM(v0->lpszStyle, LF_FACESIZE);
1324         v0->nFontType = a0.nFontType;
1325         if(pwlf)
1326                 free(pwlf);
1327         FreeDuplicatedString(a0.lpTemplateName);
1328         FreeDuplicatedString(a0.lpszStyle);
1329 END_ROUTINE
1330         FreeDuplicatedString(pw0);
1331         return r;
1332 }
1333
1334 INT_PTR DialogBoxParamM(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
1335 {
1336         INT_PTR r = 0;
1337         wchar_t* pw0 = NULL;
1338 START_ROUTINE
1339         pw0 = DuplicateMtoW(lpTemplateName, -1);
1340         r = DialogBoxParamW(hInstance, pw0, hWndParent, lpDialogFunc, dwInitParam);
1341 END_ROUTINE
1342         FreeDuplicatedString(pw0);
1343         return r;
1344 }
1345
1346
1347
1348
1349
1350