Fixed wownt32.h to make it usable from inside Wine, and use it to
[wine/dcerpc.git] / dlls / shell32 / shell.c
blobd4314719e4350f5e98f4cbad11d6aca2b3ac7b9f
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <ctype.h>
32 #include "windef.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "wownt32.h"
36 #include "dlgs.h"
37 #include "shellapi.h"
38 #include "shlobj.h"
39 #include "shlwapi.h"
40 #include "ddeml.h"
42 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
44 #include "shell32_main.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49 WINE_DECLARE_DEBUG_CHANNEL(exec);
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 HWND16 SHELL_hWnd = 0;
64 static HHOOK SHELL_hHook = 0;
65 static UINT16 uMsgWndCreated = 0;
66 static UINT16 uMsgWndDestroyed = 0;
67 static UINT16 uMsgShellActivate = 0;
68 HINSTANCE16 SHELL_hInstance = 0;
69 HINSTANCE SHELL_hInstance32;
70 static int SHELL_Attach = 0;
72 /***********************************************************************
73 * DllEntryPoint [SHELL.101]
75 * Initialization code for shell.dll. Automatically loads the
76 * 32-bit shell32.dll to allow thunking up to 32-bit code.
78 * RETURNS:
80 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
81 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
83 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
84 Reason, hInst, ds, HeapSize, res1, res2);
86 switch(Reason)
88 case DLL_PROCESS_ATTACH:
89 if (SHELL_Attach++) break;
90 SHELL_hInstance = hInst;
91 if(!SHELL_hInstance32)
93 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
95 ERR("Could not load sibling shell32.dll\n");
96 return FALSE;
99 break;
101 case DLL_PROCESS_DETACH:
102 if(!--SHELL_Attach)
104 SHELL_hInstance = 0;
105 if(SHELL_hInstance32)
106 FreeLibrary(SHELL_hInstance32);
108 break;
110 return TRUE;
113 /*************************************************************************
114 * DragAcceptFiles [SHELL.9]
116 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
118 DragAcceptFiles(HWND_32(hWnd), b);
121 /*************************************************************************
122 * DragQueryFile [SHELL.11]
124 UINT16 WINAPI DragQueryFile16(
125 HDROP16 hDrop,
126 WORD wFile,
127 LPSTR lpszFile,
128 WORD wLength)
130 LPSTR lpDrop;
131 UINT i = 0;
132 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
134 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
136 if(!lpDropFileStruct) goto end;
138 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
139 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
141 while (i++ < wFile)
143 while (*lpDrop++); /* skip filename */
144 if (!*lpDrop)
146 i = (wFile == 0xFFFFFFFF) ? i : 0;
147 goto end;
151 i = strlen(lpDrop);
152 i++;
153 if (!lpszFile ) goto end; /* needed buffer size */
154 i = (wLength > i) ? i : wLength;
155 lstrcpynA (lpszFile, lpDrop, i);
156 end:
157 GlobalUnlock16(hDrop);
158 return i;
161 /*************************************************************************
162 * DragFinish [SHELL.12]
164 void WINAPI DragFinish16(HDROP16 h)
166 TRACE("\n");
167 GlobalFree16((HGLOBAL16)h);
171 /*************************************************************************
172 * DragQueryPoint [SHELL.13]
174 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
176 LPDROPFILESTRUCT16 lpDropFileStruct;
177 BOOL16 bRet;
178 TRACE("\n");
179 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
181 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
182 bRet = lpDropFileStruct->fInNonClientArea;
184 GlobalUnlock16(hDrop);
185 return bRet;
188 /*************************************************************************
189 * FindExecutable (SHELL.21)
191 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
192 LPSTR lpResult )
193 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
197 /*************************************************************************
198 * AboutDlgProc (SHELL.33)
200 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
201 LPARAM lParam )
202 { return AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
206 /*************************************************************************
207 * ShellAbout (SHELL.22)
209 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
210 HICON16 hIcon )
211 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
214 /*************************************************************************
215 * InternalExtractIcon [SHELL.39]
217 * This abortion is called directly by Progman
219 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
220 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
222 HGLOBAL16 hRet = 0;
223 HICON16 *RetPtr = NULL;
224 OFSTRUCT ofs;
225 HFILE hFile;
227 TRACE("(%04x,file %s,start %d,extract %d\n",
228 hInstance, lpszExeFileName, nIconIndex, n);
230 if( !n )
231 return 0;
233 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ|OF_EXIST );
235 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
236 RetPtr = (HICON16*)GlobalLock16(hRet);
238 if (hFile == HFILE_ERROR)
239 { /* not found - load from builtin module if available */
240 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
242 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
243 hInst = LoadLibraryA(lpszExeFileName);
244 if (hInst)
246 int i;
247 for (i=nIconIndex; i < nIconIndex + n; i++)
248 RetPtr[i-nIconIndex] =
249 HICON_16(LoadIconA(hInst, (LPCSTR)(DWORD)i));
250 FreeLibrary(hInst);
251 return hRet;
253 GlobalFree16( hRet );
254 return 0;
257 if (nIconIndex == (UINT16)-1) /* get number of icons */
259 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
261 else
263 HRESULT res;
264 HICON *icons;
265 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
266 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
267 GetSystemMetrics(SM_CXICON),
268 GetSystemMetrics(SM_CYICON),
269 icons, 0, n, 0 );
270 if (!res)
272 int i;
273 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
275 else
277 GlobalFree16( hRet );
278 hRet = 0;
280 HeapFree( GetProcessHeap(), 0, icons );
282 return hRet;
285 /*************************************************************************
286 * ExtractIcon (SHELL.34)
288 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
289 UINT16 nIconIndex )
290 { TRACE("\n");
291 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
294 /*************************************************************************
295 * ExtractIconEx (SHELL.40)
297 HICON16 WINAPI ExtractIconEx16(
298 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
299 HICON16 *phiconSmall, UINT16 nIcons
301 HICON *ilarge,*ismall;
302 UINT16 ret;
303 int i;
305 if (phiconLarge)
306 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
307 else
308 ilarge = NULL;
309 if (phiconSmall)
310 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
311 else
312 ismall = NULL;
313 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
314 if (ilarge) {
315 for (i=0;i<nIcons;i++)
316 phiconLarge[i]=HICON_16(ilarge[i]);
317 HeapFree(GetProcessHeap(),0,ilarge);
319 if (ismall) {
320 for (i=0;i<nIcons;i++)
321 phiconSmall[i]=HICON_16(ismall[i]);
322 HeapFree(GetProcessHeap(),0,ismall);
324 return ret;
327 /*************************************************************************
328 * ExtractAssociatedIcon [SHELL.36]
330 * Return icon for given file (either from file itself or from associated
331 * executable) and patch parameters if needed.
333 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
335 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
336 lpiIcon));
339 /*************************************************************************
340 * FindEnvironmentString [SHELL.38]
342 * Returns a pointer into the DOS environment... Ugh.
344 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
345 { UINT16 l;
347 TRACE("\n");
349 l = strlen(entry);
350 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
351 { if( strncasecmp(lpEnv, entry, l) )
352 continue;
353 if( !*(lpEnv+l) )
354 return (lpEnv + l); /* empty entry */
355 else if ( *(lpEnv+l)== '=' )
356 return (lpEnv + l + 1);
358 return NULL;
361 /**********************************************************************/
363 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
364 { SEGPTR spEnv;
365 LPSTR lpEnv,lpString;
366 TRACE("\n");
368 spEnv = GetDOSEnvironment16();
370 lpEnv = MapSL(spEnv);
371 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
373 if( lpString ) /* offset should be small enough */
374 return spEnv + (lpString - lpEnv);
375 return (SEGPTR)NULL;
378 /*************************************************************************
379 * DoEnvironmentSubst [SHELL.37]
381 * Replace %KEYWORD% in the str with the value of variable KEYWORD
382 * from "DOS" environment.
384 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
386 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
387 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
388 LPSTR lpstr = str;
389 LPSTR lpbstr = lpBuffer;
391 CharToOemA(str,str);
393 TRACE("accept %s\n", str);
395 while( *lpstr && lpbstr - lpBuffer < length )
397 LPSTR lpend = lpstr;
399 if( *lpstr == '%' )
401 do { lpend++; } while( *lpend && *lpend != '%' );
402 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
404 LPSTR lpKey;
405 *lpend = '\0';
406 lpKey = SHELL_FindString(lpEnv, lpstr+1);
407 if( lpKey ) /* found key value */
409 int l = strlen(lpKey);
411 if( l > length - (lpbstr - lpBuffer) - 1 )
413 WARN("-- Env subst aborted - string too short\n");
414 *lpend = '%';
415 break;
417 strcpy(lpbstr, lpKey);
418 lpbstr += l;
420 else break;
421 *lpend = '%';
422 lpstr = lpend + 1;
424 else break; /* back off and whine */
426 continue;
429 *lpbstr++ = *lpstr++;
432 *lpbstr = '\0';
433 if( lpstr - str == strlen(str) )
435 strncpy(str, lpBuffer, length);
436 length = 1;
438 else
439 length = 0;
441 TRACE("-- return %s\n", str);
443 OemToCharA(str,str);
444 HeapFree( GetProcessHeap(), 0, lpBuffer);
446 /* Return str length in the LOWORD
447 * and 1 in HIWORD if subst was successful.
449 return (DWORD)MAKELONG(strlen(str), length);
452 /*************************************************************************
453 * ShellHookProc [SHELL.103]
454 * System-wide WH_SHELL hook.
456 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
458 TRACE("%i, %04x, %08x\n", code, wParam,
459 (unsigned)lParam );
460 if( SHELL_hHook && SHELL_hWnd )
462 UINT16 uMsg = 0;
463 switch( code )
465 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
466 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
467 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
469 PostMessageA( HWND_32(SHELL_hWnd), uMsg, wParam, 0 );
471 return CallNextHookEx16( SHELL_hHook, code, wParam, lParam );
474 /*************************************************************************
475 * RegisterShellHook [SHELL.102]
477 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
479 TRACE("%04x [%u]\n", hWnd, uAction );
481 switch( uAction )
483 case 2: /* register hWnd as a shell window */
484 if( !SHELL_hHook )
486 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
487 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
488 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
489 if ( SHELL_hHook )
491 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
492 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
493 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
495 else
496 WARN("-- unable to install ShellHookProc()!\n");
499 if ( SHELL_hHook )
500 return ((SHELL_hWnd = hWnd) != 0);
501 break;
503 default:
504 WARN("-- unknown code %i\n", uAction );
505 SHELL_hWnd = 0; /* just in case */
507 return FALSE;
511 /***********************************************************************
512 * DriveType (SHELL.262)
514 UINT16 WINAPI DriveType16( UINT16 drive )
516 UINT ret;
517 char path[] = "A:\\";
518 path[0] += drive;
519 ret = GetDriveTypeA(path);
520 switch(ret) /* some values are not supported in Win16 */
522 case DRIVE_CDROM:
523 ret = DRIVE_REMOTE;
524 break;
525 case DRIVE_NO_ROOT_DIR:
526 ret = DRIVE_UNKNOWN;
527 break;
529 return ret;