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
24 #include "wine/port.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 */
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;
73 HINSTANCE16 SHELL_hInstance
= 0;
74 HINSTANCE SHELL_hInstance32
;
75 static int SHELL_Attach
= 0;
77 /***********************************************************************
78 * DllEntryPoint [SHELL.101]
80 * Initialization code for shell.dll. Automatically loads the
81 * 32-bit shell32.dll to allow thunking up to 32-bit code.
84 * Success: TRUE. Initialization completed successfully.
87 BOOL WINAPI
SHELL_DllEntryPoint(DWORD Reason
, HINSTANCE16 hInst
,
88 WORD ds
, WORD HeapSize
, DWORD res1
, WORD res2
)
90 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
91 Reason
, hInst
, ds
, HeapSize
, res1
, res2
);
95 case DLL_PROCESS_ATTACH
:
96 if (SHELL_Attach
++) break;
97 SHELL_hInstance
= hInst
;
98 if(!SHELL_hInstance32
)
100 if(!(SHELL_hInstance32
= LoadLibraryA("shell32.dll")))
102 ERR("Could not load sibling shell32.dll\n");
108 case DLL_PROCESS_DETACH
:
112 if(SHELL_hInstance32
)
113 FreeLibrary(SHELL_hInstance32
);
120 /*************************************************************************
121 * DragAcceptFiles [SHELL.9]
123 void WINAPI
DragAcceptFiles16(HWND16 hWnd
, BOOL16 b
)
125 DragAcceptFiles(HWND_32(hWnd
), b
);
128 /*************************************************************************
129 * DragQueryFile [SHELL.11]
131 UINT16 WINAPI
DragQueryFile16(
139 LPDROPFILESTRUCT16 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
141 TRACE("(%04x, %x, %p, %u)\n", hDrop
,wFile
,lpszFile
,wLength
);
143 if(!lpDropFileStruct
) goto end
;
145 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
149 while (*lpDrop
++); /* skip filename */
152 i
= (wFile
== 0xFFFF) ? i
: 0;
159 if (!lpszFile
) goto end
; /* needed buffer size */
160 i
= (wLength
> i
) ? i
: wLength
;
161 lstrcpynA (lpszFile
, lpDrop
, i
);
163 GlobalUnlock16(hDrop
);
167 /*************************************************************************
168 * DragFinish [SHELL.12]
170 void WINAPI
DragFinish16(HDROP16 h
)
173 GlobalFree16((HGLOBAL16
)h
);
177 /*************************************************************************
178 * DragQueryPoint [SHELL.13]
180 BOOL16 WINAPI
DragQueryPoint16(HDROP16 hDrop
, POINT16
*p
)
182 LPDROPFILESTRUCT16 lpDropFileStruct
;
185 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
187 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
188 bRet
= lpDropFileStruct
->fInNonClientArea
;
190 GlobalUnlock16(hDrop
);
194 /*************************************************************************
195 * FindExecutable (SHELL.21)
197 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
199 { return HINSTANCE_16(FindExecutableA( lpFile
, lpDirectory
, lpResult
));
202 /*************************************************************************
203 * AboutDlgProc (SHELL.33)
205 BOOL16 WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
207 { return (BOOL16
)AboutDlgProc( HWND_32(hWnd
), msg
, wParam
, lParam
);
211 /*************************************************************************
212 * ShellAbout (SHELL.22)
214 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
,
216 { return ShellAboutA( HWND_32(hWnd
), szApp
, szOtherStuff
, HICON_32(hIcon
) );
219 /*************************************************************************
220 * InternalExtractIcon [SHELL.39]
222 * This abortion is called directly by Progman
224 HGLOBAL16 WINAPI
InternalExtractIcon16(HINSTANCE16 hInstance
,
225 LPCSTR lpszExeFileName
, UINT16 nIconIndex
, WORD n
)
228 HICON16
*RetPtr
= NULL
;
231 TRACE("(%04x,file %s,start %d,extract %d\n",
232 hInstance
, lpszExeFileName
, nIconIndex
, n
);
237 hRet
= GlobalAlloc16(GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(*RetPtr
) * n
);
238 RetPtr
= (HICON16
*)GlobalLock16(hRet
);
240 if (nIconIndex
== (UINT16
)-1) /* get number of icons */
242 RetPtr
[0] = PrivateExtractIconsA(ofs
.szPathName
, 0, 0, 0, NULL
, NULL
, 0, LR_DEFAULTCOLOR
);
249 icons
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(*icons
));
250 ret
= PrivateExtractIconsA(ofs
.szPathName
, nIconIndex
,
251 GetSystemMetrics(SM_CXICON
),
252 GetSystemMetrics(SM_CYICON
),
253 icons
, NULL
, n
, LR_DEFAULTCOLOR
);
254 if ((ret
!= 0xffffffff) && ret
)
257 for (i
= 0; i
< n
; i
++) RetPtr
[i
] = HICON_16(icons
[i
]);
264 HeapFree(GetProcessHeap(), 0, icons
);
269 /*************************************************************************
270 * ExtractIcon (SHELL.34)
272 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
275 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance
), lpszExeFileName
, nIconIndex
));
278 /*************************************************************************
279 * ExtractIconEx (SHELL.40)
281 HICON16 WINAPI
ExtractIconEx16(
282 LPCSTR lpszFile
, INT16 nIconIndex
, HICON16
*phiconLarge
,
283 HICON16
*phiconSmall
, UINT16 nIcons
285 HICON
*ilarge
,*ismall
;
290 ilarge
= (HICON
*)HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
294 ismall
= (HICON
*)HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
297 ret
= HICON_16(ExtractIconExA(lpszFile
,nIconIndex
,ilarge
,ismall
,nIcons
));
299 for (i
=0;i
<nIcons
;i
++)
300 phiconLarge
[i
]=HICON_16(ilarge
[i
]);
301 HeapFree(GetProcessHeap(),0,ilarge
);
304 for (i
=0;i
<nIcons
;i
++)
305 phiconSmall
[i
]=HICON_16(ismall
[i
]);
306 HeapFree(GetProcessHeap(),0,ismall
);
311 /*************************************************************************
312 * ExtractAssociatedIcon [SHELL.36]
314 * Return icon for given file (either from file itself or from associated
315 * executable) and patch parameters if needed.
317 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
319 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst
), lpIconPath
,
323 /*************************************************************************
324 * FindEnvironmentString [SHELL.38]
326 * Returns a pointer into the DOS environment... Ugh.
328 static LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
334 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
335 { if( strncasecmp(lpEnv
, entry
, l
) )
338 return (lpEnv
+ l
); /* empty entry */
339 else if ( *(lpEnv
+l
)== '=' )
340 return (lpEnv
+ l
+ 1);
345 /**********************************************************************/
347 SEGPTR WINAPI
FindEnvironmentString16(LPSTR str
)
349 LPSTR lpEnv
,lpString
;
352 spEnv
= GetDOSEnvironment16();
354 lpEnv
= MapSL(spEnv
);
355 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
357 if( lpString
) /* offset should be small enough */
358 return spEnv
+ (lpString
- lpEnv
);
362 /*************************************************************************
363 * DoEnvironmentSubst [SHELL.37]
365 * Replace %KEYWORD% in the str with the value of variable KEYWORD
366 * from "DOS" environment.
368 DWORD WINAPI
DoEnvironmentSubst16(LPSTR str
,WORD length
)
370 LPSTR lpEnv
= MapSL(GetDOSEnvironment16());
371 LPSTR lpBuffer
= (LPSTR
)HeapAlloc( GetProcessHeap(), 0, length
);
373 LPSTR lpbstr
= lpBuffer
;
377 TRACE("accept %s\n", str
);
379 while( *lpstr
&& lpbstr
- lpBuffer
< length
)
385 do { lpend
++; } while( *lpend
&& *lpend
!= '%' );
386 if( *lpend
== '%' && lpend
- lpstr
> 1 ) /* found key */
390 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
391 if( lpKey
) /* found key value */
393 int l
= strlen(lpKey
);
395 if( l
> length
- (lpbstr
- lpBuffer
) - 1 )
397 WARN("-- Env subst aborted - string too short\n");
401 strcpy(lpbstr
, lpKey
);
408 else break; /* back off and whine */
413 *lpbstr
++ = *lpstr
++;
417 if( lpstr
- str
== strlen(str
) )
419 strncpy(str
, lpBuffer
, length
);
425 TRACE("-- return %s\n", str
);
428 HeapFree( GetProcessHeap(), 0, lpBuffer
);
430 /* Return str length in the LOWORD
431 * and 1 in HIWORD if subst was successful.
433 return (DWORD
)MAKELONG(strlen(str
), length
);
436 /*************************************************************************
439 * 32-bit version of the system-wide WH_SHELL hook.
441 static LRESULT WINAPI
SHELL_HookProc(INT code
, WPARAM wParam
, LPARAM lParam
)
443 TRACE("%i, %x, %08lx\n", code
, wParam
, lParam
);
449 case HSHELL_WINDOWCREATED
:
450 PostMessageA( SHELL_hWnd
, uMsgWndCreated
, wParam
, 0 );
452 case HSHELL_WINDOWDESTROYED
:
453 PostMessageA( SHELL_hWnd
, uMsgWndDestroyed
, wParam
, 0 );
455 case HSHELL_ACTIVATESHELLWINDOW
:
456 PostMessageA( SHELL_hWnd
, uMsgShellActivate
, wParam
, 0 );
460 return CallNextHookEx( SHELL_hHook
, code
, wParam
, lParam
);
463 /*************************************************************************
464 * ShellHookProc [SHELL.103]
465 * System-wide WH_SHELL hook.
467 LRESULT WINAPI
ShellHookProc16(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
469 return SHELL_HookProc( code
, wParam
, lParam
);
472 /*************************************************************************
473 * RegisterShellHook [SHELL.102]
475 BOOL WINAPI
RegisterShellHook16(HWND16 hWnd
, UINT16 uAction
)
477 TRACE("%04x [%u]\n", hWnd
, uAction
);
481 case 2: /* register hWnd as a shell window */
484 SHELL_hHook
= SetWindowsHookExA( WH_SHELL
, SHELL_HookProc
,
485 GetModuleHandleA("shell32.dll"), 0 );
488 uMsgWndCreated
= RegisterWindowMessageA( lpstrMsgWndCreated
);
489 uMsgWndDestroyed
= RegisterWindowMessageA( lpstrMsgWndDestroyed
);
490 uMsgShellActivate
= RegisterWindowMessageA( lpstrMsgShellActivate
);
493 WARN("-- unable to install ShellHookProc()!\n");
497 return ((SHELL_hWnd
= HWND_32(hWnd
)) != 0);
501 WARN("-- unknown code %i\n", uAction
);
502 SHELL_hWnd
= 0; /* just in case */
508 /***********************************************************************
509 * DriveType (SHELL.262)
511 UINT16 WINAPI
DriveType16( UINT16 drive
)
514 char path
[] = "A:\\";
516 ret
= GetDriveTypeA(path
);
517 switch(ret
) /* some values are not supported in Win16 */
522 case DRIVE_NO_ROOT_DIR
:
530 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
531 * some programs. Do not remove those cases. -MM
533 static inline void fix_win16_hkey( HKEY
*hkey
)
535 if (*hkey
== 0 || *hkey
== (HKEY
)1) *hkey
= HKEY_CLASSES_ROOT
;
538 /******************************************************************************
539 * RegOpenKey [SHELL.1]
541 DWORD WINAPI
RegOpenKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
543 fix_win16_hkey( &hkey
);
544 return RegOpenKeyA( hkey
, name
, retkey
);
547 /******************************************************************************
548 * RegCreateKey [SHELL.2]
550 DWORD WINAPI
RegCreateKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
552 fix_win16_hkey( &hkey
);
553 return RegCreateKeyA( hkey
, name
, retkey
);
556 /******************************************************************************
557 * RegCloseKey [SHELL.3]
559 DWORD WINAPI
RegCloseKey16( HKEY hkey
)
561 fix_win16_hkey( &hkey
);
562 return RegCloseKey( hkey
);
565 /******************************************************************************
566 * RegDeleteKey [SHELL.4]
568 DWORD WINAPI
RegDeleteKey16( HKEY hkey
, LPCSTR name
)
570 fix_win16_hkey( &hkey
);
571 return RegDeleteKeyA( hkey
, name
);
574 /******************************************************************************
575 * RegSetValue [SHELL.5]
577 DWORD WINAPI
RegSetValue16( HKEY hkey
, LPCSTR name
, DWORD type
, LPCSTR data
, DWORD count
)
579 fix_win16_hkey( &hkey
);
580 return RegSetValueA( hkey
, name
, type
, data
, count
);
583 /******************************************************************************
584 * RegQueryValue [SHELL.6]
587 * Is this HACK still applicable?
590 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
591 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
594 DWORD WINAPI
RegQueryValue16( HKEY hkey
, LPCSTR name
, LPSTR data
, LPDWORD count
597 fix_win16_hkey( &hkey
);
598 if (count
) *count
&= 0xffff;
599 return RegQueryValueA( hkey
, name
, data
, count
);
602 /******************************************************************************
603 * RegEnumKey [SHELL.7]
605 DWORD WINAPI
RegEnumKey16( HKEY hkey
, DWORD index
, LPSTR name
, DWORD name_len
)
607 fix_win16_hkey( &hkey
);
608 return RegEnumKeyA( hkey
, index
, name
, name_len
);
611 /*************************************************************************
612 * SHELL_Execute16 [Internal]
614 static UINT
SHELL_Execute16(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
615 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
619 WideCharToMultiByte(CP_ACP
, 0, lpCmd
, -1, sCmd
, MAX_PATH
, NULL
, NULL
);
620 ret
= WinExec16(sCmd
, (UINT16
)psei
->nShow
);
621 psei_out
->hInstApp
= HINSTANCE_32(ret
);
625 /*************************************************************************
626 * ShellExecute [SHELL.20]
628 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
629 LPCSTR lpFile
, LPCSTR lpParameters
,
630 LPCSTR lpDirectory
, INT16 iShowCmd
)
632 SHELLEXECUTEINFOW seiW
;
633 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
636 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
637 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
638 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
639 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
641 seiW
.cbSize
= sizeof(seiW
);
643 seiW
.hwnd
= HWND_32(hWnd
);
644 seiW
.nShow
= iShowCmd
;
649 seiW
.hProcess
= hProcess
;
651 ShellExecuteExW32 (&seiW
, SHELL_Execute16
);
653 if (wVerb
) SHFree(wVerb
);
654 if (wFile
) SHFree(wFile
);
655 if (wParameters
) SHFree(wParameters
);
656 if (wDirectory
) SHFree(wDirectory
);
658 return HINSTANCE_16(seiW
.hInstApp
);