riched20: Sign-compare warnings fix.
[wine/multimedia.git] / dlls / shell32 / shell.c
blob3b883091e48bb860db1a99500a75ef389c5e8116
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "wownt32.h"
38 #include "shellapi.h"
39 #include "winuser.h"
40 #include "wingdi.h"
41 #include "shlobj.h"
42 #include "shlwapi.h"
44 #include "wine/winbase16.h"
45 #include "shell32_main.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52 typedef struct { /* structure for dropped files */
53 WORD wSize;
54 POINT16 ptMousePos;
55 BOOL16 fInNonClientArea;
56 /* memory block with filenames follows */
57 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
59 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
60 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
61 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
63 static HWND SHELL_hWnd = 0;
64 static HHOOK SHELL_hHook = 0;
65 static UINT uMsgWndCreated = 0;
66 static UINT uMsgWndDestroyed = 0;
67 static UINT uMsgShellActivate = 0;
69 /***********************************************************************
70 * DllEntryPoint [SHELL.101]
72 * Initialization code for shell.dll. Automatically loads the
73 * 32-bit shell32.dll to allow thunking up to 32-bit code.
75 * RETURNS
76 * Success: TRUE. Initialization completed successfully.
77 * Failure: FALSE.
79 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
80 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
82 return TRUE;
85 /*************************************************************************
86 * DragAcceptFiles [SHELL.9]
88 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
90 DragAcceptFiles(HWND_32(hWnd), b);
93 /*************************************************************************
94 * DragQueryFile [SHELL.11]
96 UINT16 WINAPI DragQueryFile16(
97 HDROP16 hDrop,
98 WORD wFile,
99 LPSTR lpszFile,
100 WORD wLength)
102 LPSTR lpDrop;
103 UINT i = 0;
104 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
106 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
108 if(!lpDropFileStruct) goto end;
110 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
112 while (i++ < wFile)
114 while (*lpDrop++); /* skip filename */
115 if (!*lpDrop)
117 i = (wFile == 0xFFFF) ? i : 0;
118 goto end;
122 i = strlen(lpDrop);
123 if (!lpszFile ) goto end; /* needed buffer size */
124 lstrcpynA (lpszFile, lpDrop, wLength);
125 end:
126 GlobalUnlock16(hDrop);
127 return i;
130 /*************************************************************************
131 * DragFinish [SHELL.12]
133 void WINAPI DragFinish16(HDROP16 h)
135 TRACE("\n");
136 GlobalFree16(h);
140 /*************************************************************************
141 * DragQueryPoint [SHELL.13]
143 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
145 LPDROPFILESTRUCT16 lpDropFileStruct;
146 BOOL16 bRet;
147 TRACE("\n");
148 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
150 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
151 bRet = lpDropFileStruct->fInNonClientArea;
153 GlobalUnlock16(hDrop);
154 return bRet;
157 /*************************************************************************
158 * FindExecutable (SHELL.21)
160 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
161 LPSTR lpResult )
162 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
165 /*************************************************************************
166 * AboutDlgProc (SHELL.33)
168 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
169 LPARAM lParam )
170 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
174 /*************************************************************************
175 * ShellAbout (SHELL.22)
177 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
178 HICON16 hIcon )
179 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
182 /*************************************************************************
183 * InternalExtractIcon [SHELL.39]
185 * This abortion is called directly by Progman
187 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
188 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
190 HGLOBAL16 hRet = 0;
191 HICON16 *RetPtr = NULL;
192 OFSTRUCT ofs;
194 TRACE("(%04x,file %s,start %d,extract %d\n",
195 hInstance, lpszExeFileName, nIconIndex, n);
197 if (!n)
198 return 0;
200 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
201 RetPtr = (HICON16*)GlobalLock16(hRet);
203 if (nIconIndex == (UINT16)-1) /* get number of icons */
205 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
207 else
209 UINT ret;
210 HICON *icons;
212 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
213 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
214 GetSystemMetrics(SM_CXICON),
215 GetSystemMetrics(SM_CYICON),
216 icons, NULL, n, LR_DEFAULTCOLOR);
217 if ((ret != 0xffffffff) && ret)
219 int i;
220 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
222 else
224 GlobalFree16(hRet);
225 hRet = 0;
227 HeapFree(GetProcessHeap(), 0, icons);
229 return hRet;
232 /*************************************************************************
233 * ExtractIcon (SHELL.34)
235 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
236 UINT16 nIconIndex )
237 { TRACE("\n");
238 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
241 /*************************************************************************
242 * ExtractIconEx (SHELL.40)
244 HICON16 WINAPI ExtractIconEx16(
245 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
246 HICON16 *phiconSmall, UINT16 nIcons
248 HICON *ilarge,*ismall;
249 UINT16 ret;
250 int i;
252 if (phiconLarge)
253 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
254 else
255 ilarge = NULL;
256 if (phiconSmall)
257 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
258 else
259 ismall = NULL;
260 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
261 if (ilarge) {
262 for (i=0;i<nIcons;i++)
263 phiconLarge[i]=HICON_16(ilarge[i]);
264 HeapFree(GetProcessHeap(),0,ilarge);
266 if (ismall) {
267 for (i=0;i<nIcons;i++)
268 phiconSmall[i]=HICON_16(ismall[i]);
269 HeapFree(GetProcessHeap(),0,ismall);
271 return ret;
274 /*************************************************************************
275 * ExtractAssociatedIcon [SHELL.36]
277 * Return icon for given file (either from file itself or from associated
278 * executable) and patch parameters if needed.
280 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
282 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
283 lpiIcon));
286 /*************************************************************************
287 * FindEnvironmentString [SHELL.38]
289 * Returns a pointer into the DOS environment... Ugh.
291 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
292 { UINT16 l;
294 TRACE("\n");
296 l = strlen(entry);
297 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
298 { if( strncasecmp(lpEnv, entry, l) )
299 continue;
300 if( !*(lpEnv+l) )
301 return (lpEnv + l); /* empty entry */
302 else if ( *(lpEnv+l)== '=' )
303 return (lpEnv + l + 1);
305 return NULL;
308 /**********************************************************************/
310 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
311 { SEGPTR spEnv;
312 LPSTR lpEnv,lpString;
313 TRACE("\n");
315 spEnv = GetDOSEnvironment16();
317 lpEnv = MapSL(spEnv);
318 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
320 if( lpString ) /* offset should be small enough */
321 return spEnv + (lpString - lpEnv);
322 return 0;
325 /*************************************************************************
326 * DoEnvironmentSubst [SHELL.37]
328 * Replace %KEYWORD% in the str with the value of variable KEYWORD
329 * from "DOS" environment. If it is not found the %KEYWORD% is left
330 * intact. If the buffer is too small, str is not modified.
332 * PARAMS
333 * str [I] '\0' terminated string with %keyword%.
334 * [O] '\0' terminated string with %keyword% substituted.
335 * length [I] size of str.
337 * RETURNS
338 * str length in the LOWORD and 1 in HIWORD if subst was successful.
340 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
342 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
343 LPSTR lpstr = str;
344 LPSTR lpend;
345 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
346 WORD bufCnt = 0;
347 WORD envKeyLen;
348 LPSTR lpKey;
349 WORD retStatus = 0;
350 WORD retLength = length;
352 CharToOemA(str,str);
354 TRACE("accept %s\n", str);
356 while( *lpstr && bufCnt <= length - 1 ) {
357 if ( *lpstr != '%' ) {
358 lpBuffer[bufCnt++] = *lpstr++;
359 continue;
362 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
364 envKeyLen = lpend - lpstr - 1;
365 if( *lpend != '%' || envKeyLen == 0)
366 goto err; /* "%\0" or "%%" found; back off and whine */
368 *lpend = '\0';
369 lpKey = SHELL_FindString(lpEnv, lpstr+1);
370 *lpend = '%';
371 if( lpKey ) {
372 int l = strlen(lpKey);
374 if( bufCnt + l > length - 1 )
375 goto err;
377 memcpy(lpBuffer + bufCnt, lpKey, l);
378 bufCnt += l;
379 } else { /* Keyword not found; Leave the %KEYWORD% intact */
380 if( bufCnt + envKeyLen + 2 > length - 1 )
381 goto err;
383 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
384 bufCnt += envKeyLen + 2;
387 lpstr = lpend + 1;
390 if (!*lpstr && bufCnt <= length - 1) {
391 memcpy(str,lpBuffer, bufCnt);
392 str[bufCnt] = '\0';
393 retLength = bufCnt + 1;
394 retStatus = 1;
397 err:
398 if (!retStatus)
399 WARN("-- Env subst aborted - string too short or invalid input\n");
400 TRACE("-- return %s\n", str);
402 OemToCharA(str,str);
403 HeapFree( GetProcessHeap(), 0, lpBuffer);
405 return (DWORD)MAKELONG(retLength, retStatus);
408 /*************************************************************************
409 * SHELL_HookProc
411 * 32-bit version of the system-wide WH_SHELL hook.
413 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
415 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
417 if (SHELL_hWnd)
419 switch( code )
421 case HSHELL_WINDOWCREATED:
422 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
423 break;
424 case HSHELL_WINDOWDESTROYED:
425 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
426 break;
427 case HSHELL_ACTIVATESHELLWINDOW:
428 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
429 break;
432 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
435 /*************************************************************************
436 * ShellHookProc [SHELL.103]
437 * System-wide WH_SHELL hook.
439 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
441 return SHELL_HookProc( code, wParam, lParam );
444 /*************************************************************************
445 * RegisterShellHook [SHELL.102]
447 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
449 TRACE("%04x [%u]\n", hWnd, uAction );
451 switch( uAction )
453 case 2: /* register hWnd as a shell window */
454 if( !SHELL_hHook )
456 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
457 GetModuleHandleA("shell32.dll"), 0 );
458 if ( SHELL_hHook )
460 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
461 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
462 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
464 else
465 WARN("-- unable to install ShellHookProc()!\n");
468 if ( SHELL_hHook )
469 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
470 break;
472 default:
473 WARN("-- unknown code %i\n", uAction );
474 SHELL_hWnd = 0; /* just in case */
476 return FALSE;
480 /***********************************************************************
481 * DriveType (SHELL.262)
483 UINT16 WINAPI DriveType16( UINT16 drive )
485 UINT ret;
486 char path[] = "A:\\";
487 path[0] += drive;
488 ret = GetDriveTypeA(path);
489 switch(ret) /* some values are not supported in Win16 */
491 case DRIVE_CDROM:
492 ret = DRIVE_REMOTE;
493 break;
494 case DRIVE_NO_ROOT_DIR:
495 ret = DRIVE_UNKNOWN;
496 break;
498 return ret;
502 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
503 * some programs. Do not remove those cases. -MM
505 static inline void fix_win16_hkey( HKEY *hkey )
507 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
510 /******************************************************************************
511 * RegOpenKey [SHELL.1]
513 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
515 fix_win16_hkey( &hkey );
516 return RegOpenKeyA( hkey, name, retkey );
519 /******************************************************************************
520 * RegCreateKey [SHELL.2]
522 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
524 fix_win16_hkey( &hkey );
525 return RegCreateKeyA( hkey, name, retkey );
528 /******************************************************************************
529 * RegCloseKey [SHELL.3]
531 DWORD WINAPI RegCloseKey16( HKEY hkey )
533 fix_win16_hkey( &hkey );
534 return RegCloseKey( hkey );
537 /******************************************************************************
538 * RegDeleteKey [SHELL.4]
540 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
542 fix_win16_hkey( &hkey );
543 return RegDeleteKeyA( hkey, name );
546 /******************************************************************************
547 * RegSetValue [SHELL.5]
549 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
551 fix_win16_hkey( &hkey );
552 return RegSetValueA( hkey, name, type, data, count );
555 /******************************************************************************
556 * RegQueryValue [SHELL.6]
558 * NOTES
559 * Is this HACK still applicable?
561 * HACK
562 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
563 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
564 * Aldus FH4)
566 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
569 fix_win16_hkey( &hkey );
570 if (count) *count &= 0xffff;
571 return RegQueryValueA( hkey, name, data, (LONG*) count );
574 /******************************************************************************
575 * RegEnumKey [SHELL.7]
577 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
579 fix_win16_hkey( &hkey );
580 return RegEnumKeyA( hkey, index, name, name_len );
583 /*************************************************************************
584 * SHELL_Execute16 [Internal]
586 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
587 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
589 UINT ret;
590 char sCmd[MAX_PATH];
591 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
592 ret = WinExec16(sCmd, (UINT16)psei->nShow);
593 psei_out->hInstApp = HINSTANCE_32(ret);
594 return ret;
597 /*************************************************************************
598 * ShellExecute [SHELL.20]
600 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
601 LPCSTR lpFile, LPCSTR lpParameters,
602 LPCSTR lpDirectory, INT16 iShowCmd )
604 SHELLEXECUTEINFOW seiW;
605 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
606 HANDLE hProcess = 0;
608 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
609 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
610 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
611 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
613 seiW.cbSize = sizeof(seiW);
614 seiW.fMask = 0;
615 seiW.hwnd = HWND_32(hWnd);
616 seiW.nShow = iShowCmd;
617 seiW.lpIDList = 0;
618 seiW.lpClass = 0;
619 seiW.hkeyClass = 0;
620 seiW.dwHotKey = 0;
621 seiW.hProcess = hProcess;
623 SHELL_execute( &seiW, SHELL_Execute16 );
625 SHFree(wVerb);
626 SHFree(wFile);
627 SHFree(wParameters);
628 SHFree(wDirectory);
630 return HINSTANCE_16(seiW.hInstApp);