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
24 #include "wine/port.h"
44 #include "wine/winbase16.h"
45 #include "wine/winuser16.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
51 extern HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
52 LPCSTR lpParameters
,LPCSTR lpDirectory
,
53 INT iShowCmd
, void *callback
);
55 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
56 #define HINSTANCE_16(h32) (LOWORD(h32))
58 typedef struct { /* structure for dropped files */
61 BOOL16 fInNonClientArea
;
62 /* memory block with filenames follows */
63 } DROPFILESTRUCT16
, *LPDROPFILESTRUCT16
;
65 static const char lpstrMsgWndCreated
[] = "OTHERWINDOWCREATED";
66 static const char lpstrMsgWndDestroyed
[] = "OTHERWINDOWDESTROYED";
67 static const char lpstrMsgShellActivate
[] = "ACTIVATESHELLWINDOW";
69 static HWND SHELL_hWnd
= 0;
70 static HHOOK SHELL_hHook
= 0;
71 static UINT uMsgWndCreated
= 0;
72 static UINT uMsgWndDestroyed
= 0;
73 static UINT uMsgShellActivate
= 0;
75 static HICON
convert_icon_to_32( HICON16 icon16
)
77 CURSORICONINFO
*info
= GlobalLock16( icon16
);
78 void *and_bits
= info
+ 1;
79 void *xor_bits
= (BYTE
*)and_bits
+ info
->nHeight
* 2 * ((info
->nWidth
+ 15) / 16);
80 HICON ret
= CreateIcon( 0, info
->nWidth
, info
->nHeight
, info
->bPlanes
, info
->bBitsPerPixel
,
82 GlobalUnlock16( icon16
);
86 static HICON16
convert_icon_to_16( HINSTANCE16 inst
, HICON icon
)
88 static HICON16 (WINAPI
*pCreateIcon16
)(HINSTANCE16
,INT16
,INT16
,BYTE
,BYTE
,LPCVOID
,LPCVOID
);
91 UINT and_size
, xor_size
;
92 void *xor_bits
= NULL
, *and_bits
;
96 !(pCreateIcon16
= (void *)GetProcAddress( GetModuleHandleA("user.exe16"), "CreateIcon16" )))
99 if (!(GetIconInfo( icon
, &info
))) return 0;
100 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
101 and_size
= bm
.bmHeight
* bm
.bmWidthBytes
;
102 if (!(and_bits
= HeapAlloc( GetProcessHeap(), 0, and_size
))) goto done
;
103 GetBitmapBits( info
.hbmMask
, and_size
, and_bits
);
106 GetObjectW( info
.hbmColor
, sizeof(bm
), &bm
);
107 xor_size
= bm
.bmHeight
* bm
.bmWidthBytes
;
108 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), 0, xor_size
))) goto done
;
109 GetBitmapBits( info
.hbmColor
, xor_size
, xor_bits
);
114 xor_bits
= (char *)and_bits
+ and_size
/ 2;
116 handle
= pCreateIcon16( inst
, bm
.bmWidth
, bm
.bmHeight
, bm
.bmPlanes
, bm
.bmBitsPixel
,
117 and_bits
, xor_bits
);
119 HeapFree( GetProcessHeap(), 0, and_bits
);
122 HeapFree( GetProcessHeap(), 0, xor_bits
);
123 DeleteObject( info
.hbmColor
);
125 DeleteObject( info
.hbmMask
);
130 /***********************************************************************
131 * DllEntryPoint [SHELL.101]
133 * Initialization code for shell.dll. Automatically loads the
134 * 32-bit shell32.dll to allow thunking up to 32-bit code.
137 * Success: TRUE. Initialization completed successfully.
140 BOOL WINAPI
SHELL_DllEntryPoint(DWORD Reason
, HINSTANCE16 hInst
,
141 WORD ds
, WORD HeapSize
, DWORD res1
, WORD res2
)
146 /*************************************************************************
147 * DragAcceptFiles [SHELL.9]
149 void WINAPI
DragAcceptFiles16(HWND16 hWnd
, BOOL16 b
)
151 DragAcceptFiles(HWND_32(hWnd
), b
);
154 /*************************************************************************
155 * DragQueryFile [SHELL.11]
157 UINT16 WINAPI
DragQueryFile16(
165 LPDROPFILESTRUCT16 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
167 TRACE("(%04x, %x, %p, %u)\n", hDrop
,wFile
,lpszFile
,wLength
);
169 if(!lpDropFileStruct
) goto end
;
171 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
175 while (*lpDrop
++); /* skip filename */
178 i
= (wFile
== 0xFFFF) ? i
: 0;
184 if (!lpszFile
) goto end
; /* needed buffer size */
185 lstrcpynA (lpszFile
, lpDrop
, wLength
);
187 GlobalUnlock16(hDrop
);
191 /*************************************************************************
192 * DragFinish [SHELL.12]
194 void WINAPI
DragFinish16(HDROP16 h
)
201 /*************************************************************************
202 * DragQueryPoint [SHELL.13]
204 BOOL16 WINAPI
DragQueryPoint16(HDROP16 hDrop
, POINT16
*p
)
206 LPDROPFILESTRUCT16 lpDropFileStruct
;
209 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
211 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
212 bRet
= lpDropFileStruct
->fInNonClientArea
;
214 GlobalUnlock16(hDrop
);
218 /*************************************************************************
219 * FindExecutable (SHELL.21)
221 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
223 { return HINSTANCE_16(FindExecutableA( lpFile
, lpDirectory
, lpResult
));
226 /*************************************************************************
227 * AboutDlgProc (SHELL.33)
229 BOOL16 WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
237 /*************************************************************************
238 * ShellAbout (SHELL.22)
240 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
, HICON16 icon16
)
242 HICON icon
= convert_icon_to_32( icon16
);
243 BOOL ret
= ShellAboutA( HWND_32(hWnd
), szApp
, szOtherStuff
, icon
);
248 /*************************************************************************
249 * InternalExtractIcon [SHELL.39]
251 * This abortion is called directly by Progman
253 HGLOBAL16 WINAPI
InternalExtractIcon16(HINSTANCE16 hInstance
,
254 LPCSTR lpszExeFileName
, UINT16 nIconIndex
, WORD n
)
257 HICON16
*RetPtr
= NULL
;
259 TRACE("(%04x,file %s,start %d,extract %d\n",
260 hInstance
, lpszExeFileName
, nIconIndex
, n
);
265 hRet
= GlobalAlloc16(GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(*RetPtr
) * n
);
266 RetPtr
= GlobalLock16(hRet
);
268 if (nIconIndex
== (UINT16
)-1) /* get number of icons */
270 RetPtr
[0] = PrivateExtractIconsA(lpszExeFileName
, 0, 0, 0, NULL
, NULL
, 0, LR_DEFAULTCOLOR
);
277 icons
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(*icons
));
278 ret
= PrivateExtractIconsA(lpszExeFileName
, nIconIndex
,
279 GetSystemMetrics(SM_CXICON
),
280 GetSystemMetrics(SM_CYICON
),
281 icons
, NULL
, n
, LR_DEFAULTCOLOR
);
282 if ((ret
!= 0xffffffff) && ret
)
285 for (i
= 0; i
< n
; i
++) RetPtr
[i
] = convert_icon_to_16(hInstance
, icons
[i
]);
292 HeapFree(GetProcessHeap(), 0, icons
);
297 /*************************************************************************
298 * ExtractIcon (SHELL.34)
300 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
303 return convert_icon_to_16( hInstance
, ExtractIconA(NULL
, lpszExeFileName
, nIconIndex
) );
306 /*************************************************************************
307 * ExtractIconEx (SHELL.40)
309 UINT16 WINAPI
ExtractIconEx16(
310 LPCSTR lpszFile
, INT16 nIconIndex
, HICON16
*phiconLarge
,
311 HICON16
*phiconSmall
, UINT16 nIcons
313 HICON
*ilarge
,*ismall
;
317 ilarge
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
321 ismall
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
324 ret
= ExtractIconExA(lpszFile
,nIconIndex
,ilarge
,ismall
,nIcons
);
327 phiconLarge
[i
] = convert_icon_to_16(0, ilarge
[i
]);
328 HeapFree(GetProcessHeap(),0,ilarge
);
332 phiconSmall
[i
] = convert_icon_to_16(0, ismall
[i
]);
333 HeapFree(GetProcessHeap(),0,ismall
);
338 /*************************************************************************
339 * ExtractAssociatedIcon [SHELL.36]
341 * Return icon for given file (either from file itself or from associated
342 * executable) and patch parameters if needed.
344 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
346 return convert_icon_to_16( hInst
, ExtractAssociatedIconA(NULL
, lpIconPath
, lpiIcon
) );
349 /*************************************************************************
350 * FindEnvironmentString [SHELL.38]
352 * Returns a pointer into the DOS environment... Ugh.
354 static LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
360 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
361 { if( strncasecmp(lpEnv
, entry
, l
) )
364 return (lpEnv
+ l
); /* empty entry */
365 else if ( *(lpEnv
+l
)== '=' )
366 return (lpEnv
+ l
+ 1);
371 /**********************************************************************/
373 SEGPTR WINAPI
FindEnvironmentString16(LPCSTR str
)
375 LPSTR lpEnv
,lpString
;
378 spEnv
= GetDOSEnvironment16();
380 lpEnv
= MapSL(spEnv
);
381 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
383 if( lpString
) /* offset should be small enough */
384 return spEnv
+ (lpString
- lpEnv
);
388 /*************************************************************************
389 * DoEnvironmentSubst [SHELL.37]
391 * Replace %KEYWORD% in the str with the value of variable KEYWORD
392 * from "DOS" environment. If it is not found the %KEYWORD% is left
393 * intact. If the buffer is too small, str is not modified.
396 * str [I] '\0' terminated string with %keyword%.
397 * [O] '\0' terminated string with %keyword% substituted.
398 * length [I] size of str.
401 * str length in the LOWORD and 1 in HIWORD if subst was successful.
403 DWORD WINAPI
DoEnvironmentSubst16(LPSTR str
,WORD length
)
405 LPSTR lpEnv
= MapSL(GetDOSEnvironment16());
408 LPSTR lpBuffer
= HeapAlloc( GetProcessHeap(), 0, length
);
413 WORD retLength
= length
;
417 TRACE("accept %s\n", str
);
419 while( *lpstr
&& bufCnt
<= length
- 1 ) {
420 if ( *lpstr
!= '%' ) {
421 lpBuffer
[bufCnt
++] = *lpstr
++;
425 for( lpend
= lpstr
+ 1; *lpend
&& *lpend
!= '%'; lpend
++) /**/;
427 envKeyLen
= lpend
- lpstr
- 1;
428 if( *lpend
!= '%' || envKeyLen
== 0)
429 goto err
; /* "%\0" or "%%" found; back off and whine */
432 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
435 int l
= strlen(lpKey
);
437 if( bufCnt
+ l
> length
- 1 )
440 memcpy(lpBuffer
+ bufCnt
, lpKey
, l
);
442 } else { /* Keyword not found; Leave the %KEYWORD% intact */
443 if( bufCnt
+ envKeyLen
+ 2 > length
- 1 )
446 memcpy(lpBuffer
+ bufCnt
, lpstr
, envKeyLen
+ 2);
447 bufCnt
+= envKeyLen
+ 2;
453 if (!*lpstr
&& bufCnt
<= length
- 1) {
454 memcpy(str
,lpBuffer
, bufCnt
);
456 retLength
= bufCnt
+ 1;
462 WARN("-- Env subst aborted - string too short or invalid input\n");
463 TRACE("-- return %s\n", str
);
466 HeapFree( GetProcessHeap(), 0, lpBuffer
);
468 return (DWORD
)MAKELONG(retLength
, retStatus
);
471 /*************************************************************************
474 * 32-bit version of the system-wide WH_SHELL hook.
476 static LRESULT WINAPI
SHELL_HookProc(INT code
, WPARAM wParam
, LPARAM lParam
)
478 TRACE("%i, %lx, %08lx\n", code
, wParam
, lParam
);
484 case HSHELL_WINDOWCREATED
:
485 PostMessageA( SHELL_hWnd
, uMsgWndCreated
, wParam
, 0 );
487 case HSHELL_WINDOWDESTROYED
:
488 PostMessageA( SHELL_hWnd
, uMsgWndDestroyed
, wParam
, 0 );
490 case HSHELL_ACTIVATESHELLWINDOW
:
491 PostMessageA( SHELL_hWnd
, uMsgShellActivate
, wParam
, 0 );
495 return CallNextHookEx( SHELL_hHook
, code
, wParam
, lParam
);
498 /*************************************************************************
499 * ShellHookProc [SHELL.103]
500 * System-wide WH_SHELL hook.
502 LRESULT WINAPI
ShellHookProc16(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
504 return SHELL_HookProc( code
, wParam
, lParam
);
507 /*************************************************************************
508 * RegisterShellHook [SHELL.102]
510 BOOL WINAPI
RegisterShellHook16(HWND16 hWnd
, UINT16 uAction
)
512 TRACE("%04x [%u]\n", hWnd
, uAction
);
516 case 2: /* register hWnd as a shell window */
519 SHELL_hHook
= SetWindowsHookExA( WH_SHELL
, SHELL_HookProc
,
520 GetModuleHandleA("shell32.dll"), 0 );
523 uMsgWndCreated
= RegisterWindowMessageA( lpstrMsgWndCreated
);
524 uMsgWndDestroyed
= RegisterWindowMessageA( lpstrMsgWndDestroyed
);
525 uMsgShellActivate
= RegisterWindowMessageA( lpstrMsgShellActivate
);
528 WARN("-- unable to install ShellHookProc()!\n");
532 return ((SHELL_hWnd
= HWND_32(hWnd
)) != 0);
536 WARN("-- unknown code %i\n", uAction
);
537 SHELL_hWnd
= 0; /* just in case */
543 /***********************************************************************
544 * DriveType (SHELL.262)
546 UINT16 WINAPI
DriveType16( UINT16 drive
)
549 char path
[] = "A:\\";
551 ret
= GetDriveTypeA(path
);
552 switch(ret
) /* some values are not supported in Win16 */
557 case DRIVE_NO_ROOT_DIR
:
565 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
566 * some programs. Do not remove those cases. -MM
568 static inline void fix_win16_hkey( HKEY
*hkey
)
570 if (*hkey
== 0 || *hkey
== (HKEY
)1) *hkey
= HKEY_CLASSES_ROOT
;
573 /******************************************************************************
574 * RegOpenKey [SHELL.1]
576 DWORD WINAPI
RegOpenKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
578 fix_win16_hkey( &hkey
);
579 return RegOpenKeyA( hkey
, name
, retkey
);
582 /******************************************************************************
583 * RegCreateKey [SHELL.2]
585 DWORD WINAPI
RegCreateKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
587 fix_win16_hkey( &hkey
);
588 return RegCreateKeyA( hkey
, name
, retkey
);
591 /******************************************************************************
592 * RegCloseKey [SHELL.3]
594 DWORD WINAPI
RegCloseKey16( HKEY hkey
)
596 fix_win16_hkey( &hkey
);
597 return RegCloseKey( hkey
);
600 /******************************************************************************
601 * RegDeleteKey [SHELL.4]
603 DWORD WINAPI
RegDeleteKey16( HKEY hkey
, LPCSTR name
)
605 fix_win16_hkey( &hkey
);
606 return RegDeleteKeyA( hkey
, name
);
609 /******************************************************************************
610 * RegSetValue [SHELL.5]
612 DWORD WINAPI
RegSetValue16( HKEY hkey
, LPCSTR name
, DWORD type
, LPCSTR data
, DWORD count
)
614 fix_win16_hkey( &hkey
);
615 return RegSetValueA( hkey
, name
, type
, data
, count
);
618 /******************************************************************************
619 * RegQueryValue [SHELL.6]
622 * Is this HACK still applicable?
625 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
626 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
629 DWORD WINAPI
RegQueryValue16( HKEY hkey
, LPCSTR name
, LPSTR data
, LPDWORD count
632 fix_win16_hkey( &hkey
);
633 if (count
) *count
&= 0xffff;
634 return RegQueryValueA( hkey
, name
, data
, (LONG
*) count
);
637 /******************************************************************************
638 * RegEnumKey [SHELL.7]
640 DWORD WINAPI
RegEnumKey16( HKEY hkey
, DWORD index
, LPSTR name
, DWORD name_len
)
642 fix_win16_hkey( &hkey
);
643 return RegEnumKeyA( hkey
, index
, name
, name_len
);
646 /*************************************************************************
647 * SHELL_Execute16 [Internal]
649 static UINT_PTR
SHELL_Execute16(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
650 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
654 WideCharToMultiByte(CP_ACP
, 0, lpCmd
, -1, sCmd
, MAX_PATH
, NULL
, NULL
);
655 ret
= WinExec16(sCmd
, (UINT16
)psei
->nShow
);
656 psei_out
->hInstApp
= HINSTANCE_32(ret
);
660 /*************************************************************************
661 * ShellExecute [SHELL.20]
663 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
664 LPCSTR lpFile
, LPCSTR lpParameters
,
665 LPCSTR lpDirectory
, INT16 iShowCmd
)
667 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd
), lpOperation
, lpFile
, lpParameters
,
668 lpDirectory
, iShowCmd
, SHELL_Execute16
));
672 /*************************************************************************
675 * Only exported from shell32 on Windows, probably imported
676 * from shell through the 16/32 thunks.
678 void WINAPI
RunDLL_CallEntry16( DWORD proc
, HWND hwnd
, HINSTANCE inst
, LPCSTR cmdline
, INT cmdshow
)
683 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
684 proc
, hwnd
, inst
, debugstr_a(cmdline
), cmdshow
);
686 cmdline_seg
= MapLS( cmdline
);
687 args
[4] = HWND_16(hwnd
);
688 args
[3] = MapHModuleLS(inst
);
689 args
[2] = SELECTOROF(cmdline_seg
);
690 args
[1] = OFFSETOF(cmdline_seg
);
692 WOWCallback16Ex( proc
, WCB16_PASCAL
, sizeof(args
), args
, NULL
);
693 UnMapLS( cmdline_seg
);