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
39 #include "wine/winbase16.h"
40 #include "wine/winuser16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
46 extern HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
47 LPCSTR lpParameters
,LPCSTR lpDirectory
,
48 INT iShowCmd
, void *callback
);
50 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
51 #define HINSTANCE_16(h32) (LOWORD(h32))
53 typedef struct { /* structure for dropped files */
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;
70 static HICON
convert_icon_to_32( HICON16 icon16
)
72 CURSORICONINFO
*info
= GlobalLock16( icon16
);
73 void *and_bits
= info
+ 1;
74 void *xor_bits
= (BYTE
*)and_bits
+ info
->nHeight
* 2 * ((info
->nWidth
+ 15) / 16);
75 HICON ret
= CreateIcon( 0, info
->nWidth
, info
->nHeight
, info
->bPlanes
, info
->bBitsPerPixel
,
77 GlobalUnlock16( icon16
);
81 static HICON16
convert_icon_to_16( HINSTANCE16 inst
, HICON icon
)
83 static HICON16 (WINAPI
*pCreateIcon16
)(HINSTANCE16
,INT16
,INT16
,BYTE
,BYTE
,LPCVOID
,LPCVOID
);
86 UINT and_size
, xor_size
;
87 void *xor_bits
= NULL
, *and_bits
;
91 !(pCreateIcon16
= (void *)GetProcAddress( GetModuleHandleA("user.exe16"), "CreateIcon16" )))
94 if (!(GetIconInfo( icon
, &info
))) return 0;
95 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
96 and_size
= bm
.bmHeight
* bm
.bmWidthBytes
;
97 if (!(and_bits
= HeapAlloc( GetProcessHeap(), 0, and_size
))) goto done
;
98 GetBitmapBits( info
.hbmMask
, and_size
, and_bits
);
101 GetObjectW( info
.hbmColor
, sizeof(bm
), &bm
);
102 xor_size
= bm
.bmHeight
* bm
.bmWidthBytes
;
103 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), 0, xor_size
))) goto done
;
104 GetBitmapBits( info
.hbmColor
, xor_size
, xor_bits
);
109 xor_bits
= (char *)and_bits
+ and_size
/ 2;
111 handle
= pCreateIcon16( inst
, bm
.bmWidth
, bm
.bmHeight
, bm
.bmPlanes
, bm
.bmBitsPixel
,
112 and_bits
, xor_bits
);
114 HeapFree( GetProcessHeap(), 0, and_bits
);
117 HeapFree( GetProcessHeap(), 0, xor_bits
);
118 DeleteObject( info
.hbmColor
);
120 DeleteObject( info
.hbmMask
);
125 /***********************************************************************
126 * DllEntryPoint [SHELL.101]
128 * Initialization code for shell.dll. Automatically loads the
129 * 32-bit shell32.dll to allow thunking up to 32-bit code.
132 * Success: TRUE. Initialization completed successfully.
135 BOOL WINAPI
SHELL_DllEntryPoint(DWORD Reason
, HINSTANCE16 hInst
,
136 WORD ds
, WORD HeapSize
, DWORD res1
, WORD res2
)
141 /*************************************************************************
142 * DragAcceptFiles [SHELL.9]
144 void WINAPI
DragAcceptFiles16(HWND16 hWnd
, BOOL16 b
)
146 DragAcceptFiles(HWND_32(hWnd
), b
);
149 /*************************************************************************
150 * DragQueryFile [SHELL.11]
152 UINT16 WINAPI
DragQueryFile16(
160 LPDROPFILESTRUCT16 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
162 TRACE("(%04x, %x, %p, %u)\n", hDrop
,wFile
,lpszFile
,wLength
);
164 if(!lpDropFileStruct
) goto end
;
166 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
170 while (*lpDrop
++); /* skip filename */
173 i
= (wFile
== 0xFFFF) ? i
: 0;
179 if (!lpszFile
) goto end
; /* needed buffer size */
180 lstrcpynA (lpszFile
, lpDrop
, wLength
);
182 GlobalUnlock16(hDrop
);
186 /*************************************************************************
187 * DragFinish [SHELL.12]
189 void WINAPI
DragFinish16(HDROP16 h
)
196 /*************************************************************************
197 * DragQueryPoint [SHELL.13]
199 BOOL16 WINAPI
DragQueryPoint16(HDROP16 hDrop
, POINT16
*p
)
201 LPDROPFILESTRUCT16 lpDropFileStruct
;
204 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
206 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
207 bRet
= lpDropFileStruct
->fInNonClientArea
;
209 GlobalUnlock16(hDrop
);
213 /*************************************************************************
214 * FindExecutable (SHELL.21)
216 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
218 { return HINSTANCE_16(FindExecutableA( lpFile
, lpDirectory
, lpResult
));
221 /*************************************************************************
222 * AboutDlgProc (SHELL.33)
224 BOOL16 WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
232 /*************************************************************************
233 * ShellAbout (SHELL.22)
235 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
, HICON16 icon16
)
237 HICON icon
= convert_icon_to_32( icon16
);
238 BOOL ret
= ShellAboutA( HWND_32(hWnd
), szApp
, szOtherStuff
, icon
);
243 /*************************************************************************
244 * InternalExtractIcon [SHELL.39]
246 * This abortion is called directly by Progman
248 HGLOBAL16 WINAPI
InternalExtractIcon16(HINSTANCE16 hInstance
,
249 LPCSTR lpszExeFileName
, UINT16 nIconIndex
, WORD n
)
252 HICON16
*RetPtr
= NULL
;
254 TRACE("(%04x,file %s,start %d,extract %d\n",
255 hInstance
, lpszExeFileName
, nIconIndex
, n
);
260 hRet
= GlobalAlloc16(GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(*RetPtr
) * n
);
261 RetPtr
= GlobalLock16(hRet
);
263 if (nIconIndex
== (UINT16
)-1) /* get number of icons */
265 RetPtr
[0] = PrivateExtractIconsA(lpszExeFileName
, 0, 0, 0, NULL
, NULL
, 0, LR_DEFAULTCOLOR
);
272 icons
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(*icons
));
273 ret
= PrivateExtractIconsA(lpszExeFileName
, nIconIndex
,
274 GetSystemMetrics(SM_CXICON
),
275 GetSystemMetrics(SM_CYICON
),
276 icons
, NULL
, n
, LR_DEFAULTCOLOR
);
277 if ((ret
!= 0xffffffff) && ret
)
280 for (i
= 0; i
< n
; i
++) RetPtr
[i
] = convert_icon_to_16(hInstance
, icons
[i
]);
287 HeapFree(GetProcessHeap(), 0, icons
);
292 /*************************************************************************
293 * ExtractIcon (SHELL.34)
295 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
298 return convert_icon_to_16( hInstance
, ExtractIconA(NULL
, lpszExeFileName
, nIconIndex
) );
301 /*************************************************************************
302 * ExtractIconEx (SHELL.40)
304 UINT16 WINAPI
ExtractIconEx16(
305 LPCSTR lpszFile
, INT16 nIconIndex
, HICON16
*phiconLarge
,
306 HICON16
*phiconSmall
, UINT16 nIcons
308 HICON
*ilarge
,*ismall
;
312 ilarge
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
316 ismall
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
319 ret
= ExtractIconExA(lpszFile
,nIconIndex
,ilarge
,ismall
,nIcons
);
322 phiconLarge
[i
] = convert_icon_to_16(0, ilarge
[i
]);
323 HeapFree(GetProcessHeap(),0,ilarge
);
327 phiconSmall
[i
] = convert_icon_to_16(0, ismall
[i
]);
328 HeapFree(GetProcessHeap(),0,ismall
);
333 /*************************************************************************
334 * ExtractAssociatedIcon [SHELL.36]
336 * Return icon for given file (either from file itself or from associated
337 * executable) and patch parameters if needed.
339 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
341 return convert_icon_to_16( hInst
, ExtractAssociatedIconA(NULL
, lpIconPath
, lpiIcon
) );
344 /*************************************************************************
345 * FindEnvironmentString [SHELL.38]
347 * Returns a pointer into the DOS environment... Ugh.
349 static LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
355 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
356 { if( _strnicmp(lpEnv
, entry
, l
) )
359 return (lpEnv
+ l
); /* empty entry */
360 else if ( *(lpEnv
+l
)== '=' )
361 return (lpEnv
+ l
+ 1);
366 /**********************************************************************/
368 SEGPTR WINAPI
FindEnvironmentString16(LPCSTR str
)
370 LPSTR lpEnv
,lpString
;
373 spEnv
= GetDOSEnvironment16();
375 lpEnv
= MapSL(spEnv
);
376 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
378 if( lpString
) /* offset should be small enough */
379 return spEnv
+ (lpString
- lpEnv
);
383 /*************************************************************************
384 * DoEnvironmentSubst [SHELL.37]
386 * Replace %KEYWORD% in the str with the value of variable KEYWORD
387 * from "DOS" environment. If it is not found the %KEYWORD% is left
388 * intact. If the buffer is too small, str is not modified.
391 * str [I] '\0' terminated string with %keyword%.
392 * [O] '\0' terminated string with %keyword% substituted.
393 * length [I] size of str.
396 * str length in the LOWORD and 1 in HIWORD if subst was successful.
398 DWORD WINAPI
DoEnvironmentSubst16(LPSTR str
,WORD length
)
400 LPSTR lpEnv
= MapSL(GetDOSEnvironment16());
403 LPSTR lpBuffer
= HeapAlloc( GetProcessHeap(), 0, length
);
408 WORD retLength
= length
;
412 TRACE("accept %s\n", str
);
414 while( *lpstr
&& bufCnt
<= length
- 1 ) {
415 if ( *lpstr
!= '%' ) {
416 lpBuffer
[bufCnt
++] = *lpstr
++;
420 for( lpend
= lpstr
+ 1; *lpend
&& *lpend
!= '%'; lpend
++) /**/;
422 envKeyLen
= lpend
- lpstr
- 1;
423 if( *lpend
!= '%' || envKeyLen
== 0)
424 goto err
; /* "%\0" or "%%" found; back off and whine */
427 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
430 int l
= strlen(lpKey
);
432 if( bufCnt
+ l
> length
- 1 )
435 memcpy(lpBuffer
+ bufCnt
, lpKey
, l
);
437 } else { /* Keyword not found; Leave the %KEYWORD% intact */
438 if( bufCnt
+ envKeyLen
+ 2 > length
- 1 )
441 memcpy(lpBuffer
+ bufCnt
, lpstr
, envKeyLen
+ 2);
442 bufCnt
+= envKeyLen
+ 2;
448 if (!*lpstr
&& bufCnt
<= length
- 1) {
449 memcpy(str
,lpBuffer
, bufCnt
);
451 retLength
= bufCnt
+ 1;
457 WARN("-- Env subst aborted - string too short or invalid input\n");
458 TRACE("-- return %s\n", str
);
461 HeapFree( GetProcessHeap(), 0, lpBuffer
);
463 return (DWORD
)MAKELONG(retLength
, retStatus
);
466 /*************************************************************************
469 * 32-bit version of the system-wide WH_SHELL hook.
471 static LRESULT WINAPI
SHELL_HookProc(INT code
, WPARAM wParam
, LPARAM lParam
)
473 TRACE("%i, %x, %08Ix\n", code
, wParam
, lParam
);
479 case HSHELL_WINDOWCREATED
:
480 PostMessageA( SHELL_hWnd
, uMsgWndCreated
, wParam
, 0 );
482 case HSHELL_WINDOWDESTROYED
:
483 PostMessageA( SHELL_hWnd
, uMsgWndDestroyed
, wParam
, 0 );
485 case HSHELL_ACTIVATESHELLWINDOW
:
486 PostMessageA( SHELL_hWnd
, uMsgShellActivate
, wParam
, 0 );
490 return CallNextHookEx( SHELL_hHook
, code
, wParam
, lParam
);
493 /*************************************************************************
494 * ShellHookProc [SHELL.103]
495 * System-wide WH_SHELL hook.
497 LRESULT WINAPI
ShellHookProc16(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
499 return SHELL_HookProc( code
, wParam
, lParam
);
502 /*************************************************************************
503 * RegisterShellHook [SHELL.102]
505 BOOL WINAPI
RegisterShellHook16(HWND16 hWnd
, UINT16 uAction
)
507 TRACE("%04x [%u]\n", hWnd
, uAction
);
511 case 2: /* register hWnd as a shell window */
514 SHELL_hHook
= SetWindowsHookExA( WH_SHELL
, SHELL_HookProc
,
515 GetModuleHandleA("shell32.dll"), 0 );
518 uMsgWndCreated
= RegisterWindowMessageA( lpstrMsgWndCreated
);
519 uMsgWndDestroyed
= RegisterWindowMessageA( lpstrMsgWndDestroyed
);
520 uMsgShellActivate
= RegisterWindowMessageA( lpstrMsgShellActivate
);
523 WARN("-- unable to install ShellHookProc()!\n");
527 return ((SHELL_hWnd
= HWND_32(hWnd
)) != 0);
531 WARN("-- unknown code %i\n", uAction
);
532 SHELL_hWnd
= 0; /* just in case */
538 /***********************************************************************
539 * DriveType (SHELL.262)
541 UINT16 WINAPI
DriveType16( UINT16 drive
)
544 char path
[] = "A:\\";
546 ret
= GetDriveTypeA(path
);
547 switch(ret
) /* some values are not supported in Win16 */
552 case DRIVE_NO_ROOT_DIR
:
560 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
561 * some programs. Do not remove those cases. -MM
563 static inline void fix_win16_hkey( HKEY
*hkey
)
565 if (*hkey
== 0 || *hkey
== (HKEY
)1) *hkey
= HKEY_CLASSES_ROOT
;
568 /******************************************************************************
569 * RegOpenKey [SHELL.1]
571 DWORD WINAPI
RegOpenKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
573 fix_win16_hkey( &hkey
);
574 return RegOpenKeyA( hkey
, name
, retkey
);
577 /******************************************************************************
578 * RegCreateKey [SHELL.2]
580 DWORD WINAPI
RegCreateKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
582 fix_win16_hkey( &hkey
);
583 return RegCreateKeyA( hkey
, name
, retkey
);
586 /******************************************************************************
587 * RegCloseKey [SHELL.3]
589 DWORD WINAPI
RegCloseKey16( HKEY hkey
)
591 fix_win16_hkey( &hkey
);
592 return RegCloseKey( hkey
);
595 /******************************************************************************
596 * RegDeleteKey [SHELL.4]
598 DWORD WINAPI
RegDeleteKey16( HKEY hkey
, LPCSTR name
)
600 fix_win16_hkey( &hkey
);
601 return RegDeleteKeyA( hkey
, name
);
604 /******************************************************************************
605 * RegSetValue [SHELL.5]
607 DWORD WINAPI
RegSetValue16( HKEY hkey
, LPCSTR name
, DWORD type
, LPCSTR data
, DWORD count
)
609 fix_win16_hkey( &hkey
);
610 return RegSetValueA( hkey
, name
, type
, data
, count
);
613 /******************************************************************************
614 * RegQueryValue [SHELL.6]
617 * Is this HACK still applicable?
620 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
621 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
624 DWORD WINAPI
RegQueryValue16( HKEY hkey
, LPCSTR name
, LPSTR data
, LPDWORD count
627 fix_win16_hkey( &hkey
);
628 if (count
) *count
&= 0xffff;
629 return RegQueryValueA( hkey
, name
, data
, (LONG
*) count
);
632 /******************************************************************************
633 * RegEnumKey [SHELL.7]
635 DWORD WINAPI
RegEnumKey16( HKEY hkey
, DWORD index
, LPSTR name
, DWORD name_len
)
637 fix_win16_hkey( &hkey
);
638 return RegEnumKeyA( hkey
, index
, name
, name_len
);
641 /*************************************************************************
642 * SHELL_Execute16 [Internal]
644 static UINT_PTR
SHELL_Execute16(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
645 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
649 WideCharToMultiByte(CP_ACP
, 0, lpCmd
, -1, sCmd
, MAX_PATH
, NULL
, NULL
);
650 ret
= WinExec16(sCmd
, (UINT16
)psei
->nShow
);
651 psei_out
->hInstApp
= HINSTANCE_32(ret
);
655 /*************************************************************************
656 * ShellExecute [SHELL.20]
658 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
659 LPCSTR lpFile
, LPCSTR lpParameters
,
660 LPCSTR lpDirectory
, INT16 iShowCmd
)
662 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd
), lpOperation
, lpFile
, lpParameters
,
663 lpDirectory
, iShowCmd
, SHELL_Execute16
));
667 /*************************************************************************
670 * Only exported from shell32 on Windows, probably imported
671 * from shell through the 16/32 thunks.
673 void WINAPI
RunDLL_CallEntry16( DWORD proc
, HWND hwnd
, HINSTANCE inst
, LPSTR cmdline
, INT cmdshow
)
678 TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
679 proc
, hwnd
, inst
, debugstr_a(cmdline
), cmdshow
);
681 cmdline_seg
= MapLS( cmdline
);
682 args
[4] = HWND_16(hwnd
);
683 args
[3] = MapHModuleLS(inst
);
684 args
[2] = SELECTOROF(cmdline_seg
);
685 args
[1] = OFFSETOF(cmdline_seg
);
687 WOWCallback16Ex( proc
, WCB16_PASCAL
, sizeof(args
), args
, NULL
);
688 UnMapLS( cmdline_seg
);