Implemented localeconv() with libc function.
[wine.git] / dlls / shell32 / shell.c
bloba3e461c4482adeec4e7314464adf65f6da0c2029
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2000 Juergen Schmied
6 * Copyright 2002 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <ctype.h>
33 #include "windef.h"
34 #include "winerror.h"
35 #include "winreg.h"
36 #include "wownt32.h"
37 #include "dlgs.h"
38 #include "shellapi.h"
39 #include "shlobj.h"
40 #include "shlwapi.h"
41 #include "ddeml.h"
43 #include "wine/winbase16.h"
44 #include "wine/winuser16.h"
45 #include "shell32_main.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 WINE_DECLARE_DEBUG_CHANNEL(exec);
53 typedef struct { /* structure for dropped files */
54 WORD wSize;
55 POINT16 ptMousePos;
56 BOOL16 fInNonClientArea;
57 /* memory block with filenames follows */
58 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
60 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
61 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
62 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
64 static HWND SHELL_hWnd = 0;
65 static HHOOK SHELL_hHook = 0;
66 static UINT uMsgWndCreated = 0;
67 static UINT uMsgWndDestroyed = 0;
68 static UINT uMsgShellActivate = 0;
69 HINSTANCE16 SHELL_hInstance = 0;
70 HINSTANCE SHELL_hInstance32;
71 static int SHELL_Attach = 0;
73 /***********************************************************************
74 * DllEntryPoint [SHELL.101]
76 * Initialization code for shell.dll. Automatically loads the
77 * 32-bit shell32.dll to allow thunking up to 32-bit code.
79 * RETURNS
80 * Success: TRUE. Initialization completed successfully.
81 * Failure: FALSE.
83 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
84 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
86 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
87 Reason, hInst, ds, HeapSize, res1, res2);
89 switch(Reason)
91 case DLL_PROCESS_ATTACH:
92 if (SHELL_Attach++) break;
93 SHELL_hInstance = hInst;
94 if(!SHELL_hInstance32)
96 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
98 ERR("Could not load sibling shell32.dll\n");
99 return FALSE;
102 break;
104 case DLL_PROCESS_DETACH:
105 if(!--SHELL_Attach)
107 SHELL_hInstance = 0;
108 if(SHELL_hInstance32)
109 FreeLibrary(SHELL_hInstance32);
111 break;
113 return TRUE;
116 /*************************************************************************
117 * DragAcceptFiles [SHELL.9]
119 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
121 DragAcceptFiles(HWND_32(hWnd), b);
124 /*************************************************************************
125 * DragQueryFile [SHELL.11]
127 UINT16 WINAPI DragQueryFile16(
128 HDROP16 hDrop,
129 WORD wFile,
130 LPSTR lpszFile,
131 WORD wLength)
133 LPSTR lpDrop;
134 UINT i = 0;
135 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
137 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
139 if(!lpDropFileStruct) goto end;
141 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
142 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
144 while (i++ < wFile)
146 while (*lpDrop++); /* skip filename */
147 if (!*lpDrop)
149 i = (wFile == 0xFFFFFFFF) ? i : 0;
150 goto end;
154 i = strlen(lpDrop);
155 i++;
156 if (!lpszFile ) goto end; /* needed buffer size */
157 i = (wLength > i) ? i : wLength;
158 lstrcpynA (lpszFile, lpDrop, i);
159 end:
160 GlobalUnlock16(hDrop);
161 return i;
164 /*************************************************************************
165 * DragFinish [SHELL.12]
167 void WINAPI DragFinish16(HDROP16 h)
169 TRACE("\n");
170 GlobalFree16((HGLOBAL16)h);
174 /*************************************************************************
175 * DragQueryPoint [SHELL.13]
177 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
179 LPDROPFILESTRUCT16 lpDropFileStruct;
180 BOOL16 bRet;
181 TRACE("\n");
182 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
184 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
185 bRet = lpDropFileStruct->fInNonClientArea;
187 GlobalUnlock16(hDrop);
188 return bRet;
191 /*************************************************************************
192 * FindExecutable (SHELL.21)
194 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
195 LPSTR lpResult )
196 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
199 /*************************************************************************
200 * AboutDlgProc (SHELL.33)
202 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
203 LPARAM lParam )
204 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
208 /*************************************************************************
209 * ShellAbout (SHELL.22)
211 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
212 HICON16 hIcon )
213 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
216 /*************************************************************************
217 * InternalExtractIcon [SHELL.39]
219 * This abortion is called directly by Progman
221 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
222 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
224 HGLOBAL16 hRet = 0;
225 HICON16 *RetPtr = NULL;
226 OFSTRUCT ofs;
228 TRACE("(%04x,file %s,start %d,extract %d\n",
229 hInstance, lpszExeFileName, nIconIndex, n);
231 if (!n)
232 return 0;
234 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
235 RetPtr = (HICON16*)GlobalLock16(hRet);
237 if (nIconIndex == (UINT16)-1) /* get number of icons */
239 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
241 else
243 UINT ret;
244 HICON *icons;
246 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
247 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
248 GetSystemMetrics(SM_CXICON),
249 GetSystemMetrics(SM_CYICON),
250 icons, NULL, n, LR_DEFAULTCOLOR);
251 if ((ret != 0xffffffff) && ret)
253 int i;
254 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
256 else
258 GlobalFree16(hRet);
259 hRet = 0;
261 HeapFree(GetProcessHeap(), 0, icons);
263 return hRet;
266 /*************************************************************************
267 * ExtractIcon (SHELL.34)
269 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
270 UINT16 nIconIndex )
271 { TRACE("\n");
272 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
275 /*************************************************************************
276 * ExtractIconEx (SHELL.40)
278 HICON16 WINAPI ExtractIconEx16(
279 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
280 HICON16 *phiconSmall, UINT16 nIcons
282 HICON *ilarge,*ismall;
283 UINT16 ret;
284 int i;
286 if (phiconLarge)
287 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
288 else
289 ilarge = NULL;
290 if (phiconSmall)
291 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
292 else
293 ismall = NULL;
294 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
295 if (ilarge) {
296 for (i=0;i<nIcons;i++)
297 phiconLarge[i]=HICON_16(ilarge[i]);
298 HeapFree(GetProcessHeap(),0,ilarge);
300 if (ismall) {
301 for (i=0;i<nIcons;i++)
302 phiconSmall[i]=HICON_16(ismall[i]);
303 HeapFree(GetProcessHeap(),0,ismall);
305 return ret;
308 /*************************************************************************
309 * ExtractAssociatedIcon [SHELL.36]
311 * Return icon for given file (either from file itself or from associated
312 * executable) and patch parameters if needed.
314 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
316 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
317 lpiIcon));
320 /*************************************************************************
321 * FindEnvironmentString [SHELL.38]
323 * Returns a pointer into the DOS environment... Ugh.
325 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
326 { UINT16 l;
328 TRACE("\n");
330 l = strlen(entry);
331 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
332 { if( strncasecmp(lpEnv, entry, l) )
333 continue;
334 if( !*(lpEnv+l) )
335 return (lpEnv + l); /* empty entry */
336 else if ( *(lpEnv+l)== '=' )
337 return (lpEnv + l + 1);
339 return NULL;
342 /**********************************************************************/
344 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
345 { SEGPTR spEnv;
346 LPSTR lpEnv,lpString;
347 TRACE("\n");
349 spEnv = GetDOSEnvironment16();
351 lpEnv = MapSL(spEnv);
352 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
354 if( lpString ) /* offset should be small enough */
355 return spEnv + (lpString - lpEnv);
356 return (SEGPTR)NULL;
359 /*************************************************************************
360 * DoEnvironmentSubst [SHELL.37]
362 * Replace %KEYWORD% in the str with the value of variable KEYWORD
363 * from "DOS" environment.
365 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
367 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
368 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
369 LPSTR lpstr = str;
370 LPSTR lpbstr = lpBuffer;
372 CharToOemA(str,str);
374 TRACE("accept %s\n", str);
376 while( *lpstr && lpbstr - lpBuffer < length )
378 LPSTR lpend = lpstr;
380 if( *lpstr == '%' )
382 do { lpend++; } while( *lpend && *lpend != '%' );
383 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
385 LPSTR lpKey;
386 *lpend = '\0';
387 lpKey = SHELL_FindString(lpEnv, lpstr+1);
388 if( lpKey ) /* found key value */
390 int l = strlen(lpKey);
392 if( l > length - (lpbstr - lpBuffer) - 1 )
394 WARN("-- Env subst aborted - string too short\n");
395 *lpend = '%';
396 break;
398 strcpy(lpbstr, lpKey);
399 lpbstr += l;
401 else break;
402 *lpend = '%';
403 lpstr = lpend + 1;
405 else break; /* back off and whine */
407 continue;
410 *lpbstr++ = *lpstr++;
413 *lpbstr = '\0';
414 if( lpstr - str == strlen(str) )
416 strncpy(str, lpBuffer, length);
417 length = 1;
419 else
420 length = 0;
422 TRACE("-- return %s\n", str);
424 OemToCharA(str,str);
425 HeapFree( GetProcessHeap(), 0, lpBuffer);
427 /* Return str length in the LOWORD
428 * and 1 in HIWORD if subst was successful.
430 return (DWORD)MAKELONG(strlen(str), length);
433 /*************************************************************************
434 * SHELL_HookProc
436 * 32-bit version of the system-wide WH_SHELL hook.
438 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
440 TRACE("%i, %x, %08lx\n", code, wParam, lParam );
442 if (SHELL_hWnd)
444 switch( code )
446 case HSHELL_WINDOWCREATED:
447 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
448 break;
449 case HSHELL_WINDOWDESTROYED:
450 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
451 break;
452 case HSHELL_ACTIVATESHELLWINDOW:
453 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
454 break;
457 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
460 /*************************************************************************
461 * ShellHookProc [SHELL.103]
462 * System-wide WH_SHELL hook.
464 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
466 return SHELL_HookProc( code, wParam, lParam );
469 /*************************************************************************
470 * RegisterShellHook [SHELL.102]
472 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
474 TRACE("%04x [%u]\n", hWnd, uAction );
476 switch( uAction )
478 case 2: /* register hWnd as a shell window */
479 if( !SHELL_hHook )
481 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
482 GetModuleHandleA("shell32.dll"), 0 );
483 if ( SHELL_hHook )
485 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
486 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
487 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
489 else
490 WARN("-- unable to install ShellHookProc()!\n");
493 if ( SHELL_hHook )
494 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
495 break;
497 default:
498 WARN("-- unknown code %i\n", uAction );
499 SHELL_hWnd = 0; /* just in case */
501 return FALSE;
505 /***********************************************************************
506 * DriveType (SHELL.262)
508 UINT16 WINAPI DriveType16( UINT16 drive )
510 UINT ret;
511 char path[] = "A:\\";
512 path[0] += drive;
513 ret = GetDriveTypeA(path);
514 switch(ret) /* some values are not supported in Win16 */
516 case DRIVE_CDROM:
517 ret = DRIVE_REMOTE;
518 break;
519 case DRIVE_NO_ROOT_DIR:
520 ret = DRIVE_UNKNOWN;
521 break;
523 return ret;
527 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
528 * some programs. Do not remove those cases. -MM
530 static inline void fix_win16_hkey( HKEY *hkey )
532 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
535 /******************************************************************************
536 * RegOpenKey [SHELL.1]
538 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
540 fix_win16_hkey( &hkey );
541 return RegOpenKeyA( hkey, name, retkey );
544 /******************************************************************************
545 * RegCreateKey [SHELL.2]
547 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
549 fix_win16_hkey( &hkey );
550 return RegCreateKeyA( hkey, name, retkey );
553 /******************************************************************************
554 * RegCloseKey [SHELL.3]
556 DWORD WINAPI RegCloseKey16( HKEY hkey )
558 fix_win16_hkey( &hkey );
559 return RegCloseKey( hkey );
562 /******************************************************************************
563 * RegDeleteKey [SHELL.4]
565 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
567 fix_win16_hkey( &hkey );
568 return RegDeleteKeyA( hkey, name );
571 /******************************************************************************
572 * RegSetValue [SHELL.5]
574 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
576 fix_win16_hkey( &hkey );
577 return RegSetValueA( hkey, name, type, data, count );
580 /******************************************************************************
581 * RegQueryValue [SHELL.6]
583 * NOTES
584 * Is this HACK still applicable?
586 * HACK
587 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
588 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
589 * Aldus FH4)
591 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
594 fix_win16_hkey( &hkey );
595 if (count) *count &= 0xffff;
596 return RegQueryValueA( hkey, name, data, count );
599 /******************************************************************************
600 * RegEnumKey [SHELL.7]
602 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
604 fix_win16_hkey( &hkey );
605 return RegEnumKeyA( hkey, index, name, name_len );
608 /*************************************************************************
609 * SHELL_Execute16 [Internal]
611 static UINT SHELL_Execute16(char *lpCmd, LPSHELLEXECUTEINFOA sei, BOOL shWait)
613 UINT ret = WinExec16(lpCmd, sei->nShow);
614 sei->hInstApp = HINSTANCE_32(ret);
615 return ret;
618 /*************************************************************************
619 * ShellExecute [SHELL.20]
621 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
622 LPCSTR lpFile, LPCSTR lpParameters,
623 LPCSTR lpDirectory, INT16 iShowCmd )
625 SHELLEXECUTEINFOA sei;
626 HANDLE hProcess = 0;
628 sei.cbSize = sizeof(sei);
629 sei.fMask = 0;
630 sei.hwnd = HWND_32(hWnd);
631 sei.lpVerb = lpOperation;
632 sei.lpFile = lpFile;
633 sei.lpParameters = lpParameters;
634 sei.lpDirectory = lpDirectory;
635 sei.nShow = iShowCmd;
636 sei.lpIDList = 0;
637 sei.lpClass = 0;
638 sei.hkeyClass = 0;
639 sei.dwHotKey = 0;
640 sei.hProcess = hProcess;
642 ShellExecuteExA32 (&sei, SHELL_Execute16);
643 return HINSTANCE_16(sei.hInstApp);