Fix some -Wsign-compare warnings.
[wine/testsucceed.git] / dlls / shell32 / shell.c
blob16f12e0633b6984eab28b8fbe90d99b8a30c7c2d
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 <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 "winerror.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "dlgs.h"
40 #include "shellapi.h"
41 #include "winuser.h"
42 #include "wingdi.h"
43 #include "shlobj.h"
44 #include "shlwapi.h"
45 #include "ddeml.h"
47 #include "wine/winbase16.h"
48 #include "wine/winuser16.h"
49 #include "shell32_main.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54 WINE_DECLARE_DEBUG_CHANNEL(exec);
57 typedef struct { /* structure for dropped files */
58 WORD wSize;
59 POINT16 ptMousePos;
60 BOOL16 fInNonClientArea;
61 /* memory block with filenames follows */
62 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
64 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
65 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
66 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
68 static HWND SHELL_hWnd = 0;
69 static HHOOK SHELL_hHook = 0;
70 static UINT uMsgWndCreated = 0;
71 static UINT uMsgWndDestroyed = 0;
72 static UINT uMsgShellActivate = 0;
74 /***********************************************************************
75 * DllEntryPoint [SHELL.101]
77 * Initialization code for shell.dll. Automatically loads the
78 * 32-bit shell32.dll to allow thunking up to 32-bit code.
80 * RETURNS
81 * Success: TRUE. Initialization completed successfully.
82 * Failure: FALSE.
84 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
85 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
87 return TRUE;
90 /*************************************************************************
91 * DragAcceptFiles [SHELL.9]
93 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
95 DragAcceptFiles(HWND_32(hWnd), b);
98 /*************************************************************************
99 * DragQueryFile [SHELL.11]
101 UINT16 WINAPI DragQueryFile16(
102 HDROP16 hDrop,
103 WORD wFile,
104 LPSTR lpszFile,
105 WORD wLength)
107 LPSTR lpDrop;
108 UINT i = 0;
109 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
111 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
113 if(!lpDropFileStruct) goto end;
115 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
117 while (i++ < wFile)
119 while (*lpDrop++); /* skip filename */
120 if (!*lpDrop)
122 i = (wFile == 0xFFFF) ? i : 0;
123 goto end;
127 i = strlen(lpDrop);
128 i++;
129 if (!lpszFile ) goto end; /* needed buffer size */
130 i = (wLength > i) ? i : wLength;
131 lstrcpynA (lpszFile, lpDrop, i);
132 end:
133 GlobalUnlock16(hDrop);
134 return i;
137 /*************************************************************************
138 * DragFinish [SHELL.12]
140 void WINAPI DragFinish16(HDROP16 h)
142 TRACE("\n");
143 GlobalFree16((HGLOBAL16)h);
147 /*************************************************************************
148 * DragQueryPoint [SHELL.13]
150 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
152 LPDROPFILESTRUCT16 lpDropFileStruct;
153 BOOL16 bRet;
154 TRACE("\n");
155 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
157 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
158 bRet = lpDropFileStruct->fInNonClientArea;
160 GlobalUnlock16(hDrop);
161 return bRet;
164 /*************************************************************************
165 * FindExecutable (SHELL.21)
167 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
168 LPSTR lpResult )
169 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
172 /*************************************************************************
173 * AboutDlgProc (SHELL.33)
175 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
176 LPARAM lParam )
177 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
181 /*************************************************************************
182 * ShellAbout (SHELL.22)
184 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
185 HICON16 hIcon )
186 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
189 /*************************************************************************
190 * InternalExtractIcon [SHELL.39]
192 * This abortion is called directly by Progman
194 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
195 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
197 HGLOBAL16 hRet = 0;
198 HICON16 *RetPtr = NULL;
199 OFSTRUCT ofs;
201 TRACE("(%04x,file %s,start %d,extract %d\n",
202 hInstance, lpszExeFileName, nIconIndex, n);
204 if (!n)
205 return 0;
207 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
208 RetPtr = (HICON16*)GlobalLock16(hRet);
210 if (nIconIndex == (UINT16)-1) /* get number of icons */
212 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
214 else
216 UINT ret;
217 HICON *icons;
219 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
220 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
221 GetSystemMetrics(SM_CXICON),
222 GetSystemMetrics(SM_CYICON),
223 icons, NULL, n, LR_DEFAULTCOLOR);
224 if ((ret != 0xffffffff) && ret)
226 int i;
227 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
229 else
231 GlobalFree16(hRet);
232 hRet = 0;
234 HeapFree(GetProcessHeap(), 0, icons);
236 return hRet;
239 /*************************************************************************
240 * ExtractIcon (SHELL.34)
242 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
243 UINT16 nIconIndex )
244 { TRACE("\n");
245 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
248 /*************************************************************************
249 * ExtractIconEx (SHELL.40)
251 HICON16 WINAPI ExtractIconEx16(
252 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
253 HICON16 *phiconSmall, UINT16 nIcons
255 HICON *ilarge,*ismall;
256 UINT16 ret;
257 int i;
259 if (phiconLarge)
260 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
261 else
262 ilarge = NULL;
263 if (phiconSmall)
264 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
265 else
266 ismall = NULL;
267 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
268 if (ilarge) {
269 for (i=0;i<nIcons;i++)
270 phiconLarge[i]=HICON_16(ilarge[i]);
271 HeapFree(GetProcessHeap(),0,ilarge);
273 if (ismall) {
274 for (i=0;i<nIcons;i++)
275 phiconSmall[i]=HICON_16(ismall[i]);
276 HeapFree(GetProcessHeap(),0,ismall);
278 return ret;
281 /*************************************************************************
282 * ExtractAssociatedIcon [SHELL.36]
284 * Return icon for given file (either from file itself or from associated
285 * executable) and patch parameters if needed.
287 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
289 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
290 lpiIcon));
293 /*************************************************************************
294 * FindEnvironmentString [SHELL.38]
296 * Returns a pointer into the DOS environment... Ugh.
298 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
299 { UINT16 l;
301 TRACE("\n");
303 l = strlen(entry);
304 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
305 { if( strncasecmp(lpEnv, entry, l) )
306 continue;
307 if( !*(lpEnv+l) )
308 return (lpEnv + l); /* empty entry */
309 else if ( *(lpEnv+l)== '=' )
310 return (lpEnv + l + 1);
312 return NULL;
315 /**********************************************************************/
317 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
318 { SEGPTR spEnv;
319 LPSTR lpEnv,lpString;
320 TRACE("\n");
322 spEnv = GetDOSEnvironment16();
324 lpEnv = MapSL(spEnv);
325 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
327 if( lpString ) /* offset should be small enough */
328 return spEnv + (lpString - lpEnv);
329 return (SEGPTR)NULL;
332 /*************************************************************************
333 * DoEnvironmentSubst [SHELL.37]
335 * Replace %KEYWORD% in the str with the value of variable KEYWORD
336 * from "DOS" environment. If it is not found the %KEYWORD% is left
337 * intact. If the buffer is too small, str is not modified.
339 * str [I] '\0' terminated string with %keyword%.
340 * [O] '\0' terminated string with %keyword% substituted.
341 * length [I] size of str.
343 * Return
344 * str length in the LOWORD and 1 in HIWORD if subst was successful.
346 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
348 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
349 LPSTR lpstr = str;
350 LPSTR lpend;
351 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
352 WORD bufCnt = 0;
353 WORD envKeyLen;
354 LPSTR lpKey;
355 WORD retStatus = 0;
356 WORD retLength = length;
358 CharToOemA(str,str);
360 TRACE("accept %s\n", str);
362 while( *lpstr && bufCnt <= length - 1 ) {
363 if ( *lpstr != '%' ) {
364 lpBuffer[bufCnt++] = *lpstr++;
365 continue;
368 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
370 envKeyLen = lpend - lpstr - 1;
371 if( *lpend != '%' || envKeyLen == 0)
372 goto err; /* "%\0" or "%%" found; back off and whine */
374 *lpend = '\0';
375 lpKey = SHELL_FindString(lpEnv, lpstr+1);
376 *lpend = '%';
377 if( lpKey ) {
378 int l = strlen(lpKey);
380 if( bufCnt + l > length - 1 )
381 goto err;
383 memcpy(lpBuffer + bufCnt, lpKey, l);
384 bufCnt += l;
385 } else { /* Keyword not found; Leave the %KEYWORD% intact */
386 if( bufCnt + envKeyLen + 2 > length - 1 )
387 goto err;
389 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
390 bufCnt += envKeyLen + 2;
393 lpstr = lpend + 1;
396 if (!*lpstr && bufCnt <= length - 1) {
397 memcpy(str,lpBuffer, bufCnt);
398 str[bufCnt] = '\0';
399 retLength = bufCnt + 1;
400 retStatus = 1;
403 err:
404 if (!retStatus)
405 WARN("-- Env subst aborted - string too short or invalid input\n");
406 TRACE("-- return %s\n", str);
408 OemToCharA(str,str);
409 HeapFree( GetProcessHeap(), 0, lpBuffer);
411 return (DWORD)MAKELONG(retLength, retStatus);
414 /*************************************************************************
415 * SHELL_HookProc
417 * 32-bit version of the system-wide WH_SHELL hook.
419 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
421 TRACE("%i, %x, %08lx\n", code, wParam, lParam );
423 if (SHELL_hWnd)
425 switch( code )
427 case HSHELL_WINDOWCREATED:
428 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
429 break;
430 case HSHELL_WINDOWDESTROYED:
431 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
432 break;
433 case HSHELL_ACTIVATESHELLWINDOW:
434 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
435 break;
438 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
441 /*************************************************************************
442 * ShellHookProc [SHELL.103]
443 * System-wide WH_SHELL hook.
445 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
447 return SHELL_HookProc( code, wParam, lParam );
450 /*************************************************************************
451 * RegisterShellHook [SHELL.102]
453 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
455 TRACE("%04x [%u]\n", hWnd, uAction );
457 switch( uAction )
459 case 2: /* register hWnd as a shell window */
460 if( !SHELL_hHook )
462 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
463 GetModuleHandleA("shell32.dll"), 0 );
464 if ( SHELL_hHook )
466 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
467 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
468 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
470 else
471 WARN("-- unable to install ShellHookProc()!\n");
474 if ( SHELL_hHook )
475 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
476 break;
478 default:
479 WARN("-- unknown code %i\n", uAction );
480 SHELL_hWnd = 0; /* just in case */
482 return FALSE;
486 /***********************************************************************
487 * DriveType (SHELL.262)
489 UINT16 WINAPI DriveType16( UINT16 drive )
491 UINT ret;
492 char path[] = "A:\\";
493 path[0] += drive;
494 ret = GetDriveTypeA(path);
495 switch(ret) /* some values are not supported in Win16 */
497 case DRIVE_CDROM:
498 ret = DRIVE_REMOTE;
499 break;
500 case DRIVE_NO_ROOT_DIR:
501 ret = DRIVE_UNKNOWN;
502 break;
504 return ret;
508 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
509 * some programs. Do not remove those cases. -MM
511 static inline void fix_win16_hkey( HKEY *hkey )
513 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
516 /******************************************************************************
517 * RegOpenKey [SHELL.1]
519 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
521 fix_win16_hkey( &hkey );
522 return RegOpenKeyA( hkey, name, retkey );
525 /******************************************************************************
526 * RegCreateKey [SHELL.2]
528 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
530 fix_win16_hkey( &hkey );
531 return RegCreateKeyA( hkey, name, retkey );
534 /******************************************************************************
535 * RegCloseKey [SHELL.3]
537 DWORD WINAPI RegCloseKey16( HKEY hkey )
539 fix_win16_hkey( &hkey );
540 return RegCloseKey( hkey );
543 /******************************************************************************
544 * RegDeleteKey [SHELL.4]
546 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
548 fix_win16_hkey( &hkey );
549 return RegDeleteKeyA( hkey, name );
552 /******************************************************************************
553 * RegSetValue [SHELL.5]
555 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
557 fix_win16_hkey( &hkey );
558 return RegSetValueA( hkey, name, type, data, count );
561 /******************************************************************************
562 * RegQueryValue [SHELL.6]
564 * NOTES
565 * Is this HACK still applicable?
567 * HACK
568 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
569 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
570 * Aldus FH4)
572 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
575 fix_win16_hkey( &hkey );
576 if (count) *count &= 0xffff;
577 return RegQueryValueA( hkey, name, data, count );
580 /******************************************************************************
581 * RegEnumKey [SHELL.7]
583 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
585 fix_win16_hkey( &hkey );
586 return RegEnumKeyA( hkey, index, name, name_len );
589 /*************************************************************************
590 * SHELL_Execute16 [Internal]
592 static UINT SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
593 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
595 UINT ret;
596 char sCmd[MAX_PATH];
597 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
598 ret = WinExec16(sCmd, (UINT16)psei->nShow);
599 psei_out->hInstApp = HINSTANCE_32(ret);
600 return ret;
603 /*************************************************************************
604 * ShellExecute [SHELL.20]
606 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
607 LPCSTR lpFile, LPCSTR lpParameters,
608 LPCSTR lpDirectory, INT16 iShowCmd )
610 SHELLEXECUTEINFOW seiW;
611 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
612 HANDLE hProcess = 0;
614 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
615 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
616 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
617 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
619 seiW.cbSize = sizeof(seiW);
620 seiW.fMask = 0;
621 seiW.hwnd = HWND_32(hWnd);
622 seiW.nShow = iShowCmd;
623 seiW.lpIDList = 0;
624 seiW.lpClass = 0;
625 seiW.hkeyClass = 0;
626 seiW.dwHotKey = 0;
627 seiW.hProcess = hProcess;
629 ShellExecuteExW32 (&seiW, SHELL_Execute16);
631 if (wVerb) SHFree(wVerb);
632 if (wFile) SHFree(wFile);
633 if (wParameters) SHFree(wParameters);
634 if (wDirectory) SHFree(wDirectory);
636 return HINSTANCE_16(seiW.hInstApp);