Release 970120
[wine/multimedia.git] / misc / shell.c
blobad492c0f64df4c7e1261926bcdf7f948c8d6c178
1 /*
2 * Shell Library Functions
3 */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <ctype.h>
9 #include "windows.h"
10 #include "file.h"
11 #include "shell.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "neexe.h"
15 #include "resource.h"
16 #include "dlgs.h"
17 #include "win.h"
18 #include "cursoricon.h"
19 #include "stddebug.h"
20 #include "debug.h"
21 #include "xmalloc.h"
22 #include "winreg.h"
24 /* .ICO file ICONDIR definitions */
26 #pragma pack(1)
28 typedef struct
30 BYTE bWidth; /* Width, in pixels, of the image */
31 BYTE bHeight; /* Height, in pixels, of the image */
32 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
33 BYTE bReserved; /* Reserved ( must be 0) */
34 WORD wPlanes; /* Color Planes */
35 WORD wBitCount; /* Bits per pixel */
36 DWORD dwBytesInRes; /* How many bytes in this resource? */
37 DWORD dwImageOffset; /* Where in the file is this image? */
38 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
40 typedef struct
42 WORD idReserved; /* Reserved (must be 0) */
43 WORD idType; /* Resource Type (1 for icons) */
44 WORD idCount; /* How many images? */
45 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
46 } icoICONDIR, *LPicoICONDIR;
48 #pragma pack(4)
50 extern HGLOBAL16 CURSORICON_LoadHandler( HGLOBAL16, HINSTANCE16, BOOL);
51 extern WORD GetIconID( HGLOBAL16 hResource, DWORD resType );
53 /*************************************************************************
54 * DragAcceptFiles [SHELL.9]
56 void DragAcceptFiles(HWND hWnd, BOOL b)
58 WND* wnd = WIN_FindWndPtr(hWnd);
60 if( wnd )
61 wnd->dwExStyle = b? wnd->dwExStyle | WS_EX_ACCEPTFILES
62 : wnd->dwExStyle & ~WS_EX_ACCEPTFILES;
66 /*************************************************************************
67 * DragQueryFile [SHELL.11]
69 UINT DragQueryFile(HDROP16 hDrop, WORD wFile, LPSTR lpszFile, WORD wLength)
71 /* hDrop is a global memory block allocated with GMEM_SHARE
72 * with DROPFILESTRUCT as a header and filenames following
73 * it, zero length filename is in the end */
75 LPDROPFILESTRUCT lpDropFileStruct;
76 LPSTR lpCurrent;
77 WORD i;
79 dprintf_reg(stddeb,"DragQueryFile(%04x, %i, %p, %u)\n",
80 hDrop,wFile,lpszFile,wLength);
82 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock16(hDrop);
83 if(!lpDropFileStruct) return 0;
85 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
87 i = 0;
88 while (i++ < wFile)
90 while (*lpCurrent++); /* skip filename */
91 if (!*lpCurrent)
92 return (wFile == 0xFFFF) ? i : 0;
95 i = strlen(lpCurrent);
96 if (!lpszFile) return i+1; /* needed buffer size */
98 i = (wLength > i) ? i : wLength-1;
99 strncpy(lpszFile, lpCurrent, i);
100 lpszFile[i] = '\0';
102 GlobalUnlock16(hDrop);
103 return i;
107 /*************************************************************************
108 * DragFinish [SHELL.12]
110 void DragFinish(HDROP16 h)
112 GlobalFree16((HGLOBAL16)h);
116 /*************************************************************************
117 * DragQueryPoint [SHELL.13]
119 BOOL DragQueryPoint(HDROP16 hDrop, POINT16 *p)
121 LPDROPFILESTRUCT lpDropFileStruct;
122 BOOL16 bRet;
124 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock16(hDrop);
126 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
127 bRet = lpDropFileStruct->fInNonClientArea;
129 GlobalUnlock16(hDrop);
130 return bRet;
133 /*************************************************************************
134 * SHELL_FindExecutable
135 * Utility for code sharing between FindExecutable and ShellExecute
137 static HINSTANCE16 SHELL_FindExecutable( LPCSTR lpFile,
138 LPCSTR lpDirectory,
139 LPCSTR lpOperation,
140 LPSTR lpResult)
142 char *extension = NULL; /* pointer to file extension */
143 char tmpext[5]; /* local copy to mung as we please */
144 char filetype[256]; /* registry name for this filetype */
145 LONG filetypelen=256; /* length of above */
146 char command[256]; /* command from registry */
147 LONG commandlen=256; /* This is the most DOS can handle :) */
148 char buffer[256]; /* Used to GetProfileString */
149 HINSTANCE16 retval=31; /* default - 'No association was found' */
150 char *tok; /* token pointer */
151 int i; /* random counter */
152 char xlpFile[256]; /* result of SearchPath */
154 dprintf_exec(stddeb, "SHELL_FindExecutable: File %s, Dir %s\n",
155 (lpFile != NULL?lpFile:"-"),
156 (lpDirectory != NULL?lpDirectory:"-"));
158 lpResult[0]='\0'; /* Start off with an empty return string */
160 /* trap NULL parameters on entry */
161 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
163 /* FIXME - should throw a warning, perhaps! */
164 return 2; /* File not found. Close enough, I guess. */
166 if (SearchPath32A(lpDirectory,lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
167 lpFile = xlpFile;
169 /* First thing we need is the file's extension */
170 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
171 /* File->Run in progman uses */
172 /* .\FILE.EXE :( */
173 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
175 return 31; /* no association */
178 /* Make local copy & lowercase it for reg & 'programs=' lookup */
179 lstrcpyn32A( tmpext, extension, 5 );
180 CharLower32A( tmpext );
181 dprintf_exec(stddeb, "SHELL_FindExecutable: %s file\n", tmpext);
183 /* Three places to check: */
184 /* 1. win.ini, [windows], programs (NB no leading '.') */
185 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
186 /* 3. win.ini, [extensions], extension (NB no leading '.' */
187 /* All I know of the order is that registry is checked before */
188 /* extensions; however, it'd make sense to check the programs */
189 /* section first, so that's what happens here. */
191 /* See if it's a program - if GetProfileString fails, we skip this
192 * section. Actually, if GetProfileString fails, we've probably
193 * got a lot more to worry about than running a program... */
194 if ( GetProfileString32A("windows", "programs", "exe pif bat com",
195 buffer, sizeof(buffer)) > 0 )
197 for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
199 tok = strtok(buffer, " \t"); /* ? */
200 while( tok!= NULL)
202 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
204 strcpy(lpResult, xlpFile);
205 /* Need to perhaps check that the file has a path
206 * attached */
207 dprintf_exec(stddeb, "SHELL_FindExecutable: found %s\n",
208 lpResult);
209 return 33;
211 /* Greater than 32 to indicate success FIXME According to the
212 * docs, I should be returning a handle for the
213 * executable. Does this mean I'm supposed to open the
214 * executable file or something? More RTFM, I guess... */
216 tok=strtok(NULL, " \t");
220 /* Check registry */
221 if (RegQueryValue16( (HKEY)HKEY_CLASSES_ROOT, tmpext, filetype,
222 &filetypelen ) == SHELL_ERROR_SUCCESS )
224 filetype[filetypelen]='\0';
225 dprintf_exec(stddeb, "SHELL_FindExecutable: File type: %s\n",
226 filetype);
228 /* Looking for ...buffer\shell\lpOperation\command */
229 strcat( filetype, "\\shell\\" );
230 strcat( filetype, lpOperation );
231 strcat( filetype, "\\command" );
233 if (RegQueryValue16( (HKEY)HKEY_CLASSES_ROOT, filetype, command,
234 &commandlen ) == SHELL_ERROR_SUCCESS )
236 /* Is there a replace() function anywhere? */
237 command[commandlen]='\0';
238 strcpy( lpResult, command );
239 tok=strstr( lpResult, "%1" );
240 if (tok != NULL)
242 tok[0]='\0'; /* truncate string at the percent */
243 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
244 tok=strstr( command, "%1" );
245 if ((tok!=NULL) && (strlen(tok)>2))
247 strcat( lpResult, &tok[2] );
250 retval=33; /* FIXME see above */
253 else /* Check win.ini */
255 /* Toss the leading dot */
256 extension++;
257 if ( GetProfileString32A( "extensions", extension, "", command,
258 sizeof(command)) > 0)
260 if (strlen(command)!=0)
262 strcpy( lpResult, command );
263 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
264 if (tok != NULL)
266 tok[0]='\0';
267 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
268 tok=strstr( command, "^" ); /* see above */
269 if ((tok != NULL) && (strlen(tok)>5))
271 strcat( lpResult, &tok[5]);
274 retval=33; /* FIXME - see above */
279 dprintf_exec(stddeb, "SHELL_FindExecutable: returning %s\n", lpResult);
280 return retval;
283 /*************************************************************************
284 * ShellExecute [SHELL.20]
286 HINSTANCE16 ShellExecute(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
287 LPSTR lpParameters, LPCSTR lpDirectory,
288 INT iShowCmd)
290 HINSTANCE16 retval=31;
291 char cmd[256];
293 dprintf_exec(stddeb, "ShellExecute(%04x,'%s','%s','%s','%s',%x)\n",
294 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
295 lpParameters ? lpParameters : "<null>",
296 lpDirectory ? lpDirectory : "<null>", iShowCmd);
298 if (lpFile==NULL) return 0; /* should not happen */
299 if (lpOperation==NULL) /* default is open */
300 lpOperation="open";
302 retval = SHELL_FindExecutable( lpFile, lpDirectory, lpOperation, cmd );
304 if ( retval <= 32 )
306 return retval;
309 if (lpParameters)
311 strcat(cmd," ");
312 strcat(cmd,lpParameters);
315 dprintf_exec(stddeb,"ShellExecute:starting %s\n",cmd);
316 return WinExec32(cmd,iShowCmd);
319 /*************************************************************************
320 * FindExecutable [SHELL.21]
322 HINSTANCE16 FindExecutable(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
324 HINSTANCE16 retval=31; /* default - 'No association was found' */
326 dprintf_exec(stddeb, "FindExecutable: File %s, Dir %s\n",
327 (lpFile != NULL?lpFile:"-"),
328 (lpDirectory != NULL?lpDirectory:"-"));
330 lpResult[0]='\0'; /* Start off with an empty return string */
332 /* trap NULL parameters on entry */
333 if (( lpFile == NULL ) || ( lpResult == NULL ))
335 /* FIXME - should throw a warning, perhaps! */
336 return 2; /* File not found. Close enough, I guess. */
339 retval = SHELL_FindExecutable( lpFile, lpDirectory, "open",
340 lpResult );
342 dprintf_exec(stddeb, "FindExecutable: returning %s\n", lpResult);
343 return retval;
346 typedef struct
348 LPCSTR szApp;
349 LPCSTR szOtherStuff;
350 HICON32 hIcon;
351 } ABOUT_INFO;
354 /*************************************************************************
355 * AboutDlgProc32 (not an exported API function)
357 LRESULT AboutDlgProc32( HWND32 hWnd, UINT32 msg, WPARAM32 wParam,
358 LPARAM lParam )
360 char Template[512], AppTitle[512];
362 switch(msg)
364 case WM_INITDIALOG:
366 ABOUT_INFO *info = (ABOUT_INFO *)lParam;
367 if (info)
369 SendDlgItemMessage32A(hWnd, stc1, STM_SETICON, info->hIcon, 0);
370 GetWindowText32A( hWnd, Template, sizeof(Template) );
371 sprintf( AppTitle, Template, info->szApp );
372 SetWindowText32A( hWnd, AppTitle );
373 SetWindowText32A( GetDlgItem32(hWnd,100), info->szOtherStuff );
376 return 1;
378 case WM_COMMAND:
379 if (wParam == IDOK)
381 EndDialog(hWnd, TRUE);
382 return TRUE;
384 break;
386 return 0;
390 /*************************************************************************
391 * AboutDlgProc16 (SHELL.33)
393 LRESULT AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
394 LPARAM lParam )
396 return AboutDlgProc32( hWnd, msg, wParam, lParam );
400 /*************************************************************************
401 * ShellAbout16 (SHELL.22)
403 BOOL16 ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
404 HICON16 hIcon )
406 return ShellAbout32A( hWnd, szApp, szOtherStuff, hIcon );
409 /*************************************************************************
410 * ShellAbout32A (SHELL32.82)
412 BOOL32 ShellAbout32A( HWND32 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
413 HICON32 hIcon )
415 ABOUT_INFO info;
416 info.szApp = szApp;
417 info.szOtherStuff = szOtherStuff;
418 info.hIcon = hIcon;
419 if (!hIcon) info.hIcon = LoadIcon16( 0, MAKEINTRESOURCE(OIC_WINEICON) );
420 return DialogBoxIndirectParam32A( WIN_GetWindowInstance( hWnd ),
421 SYSRES_GetResPtr( SYSRES_DIALOG_SHELL_ABOUT_MSGBOX ),
422 hWnd, AboutDlgProc32, (LPARAM)&info );
426 /*************************************************************************
427 * ShellAbout32W (SHELL32.83)
429 BOOL32 ShellAbout32W( HWND32 hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
430 HICON32 hIcon )
432 BOOL32 ret;
433 ABOUT_INFO info;
435 info.szApp = HEAP_strdupWtoA( GetProcessHeap(), 0, szApp );
436 info.szOtherStuff = HEAP_strdupWtoA( GetProcessHeap(), 0, szOtherStuff );
437 info.hIcon = hIcon;
438 if (!hIcon) info.hIcon = LoadIcon16( 0, MAKEINTRESOURCE(OIC_WINEICON) );
439 ret = DialogBoxIndirectParam32A( WIN_GetWindowInstance( hWnd ),
440 SYSRES_GetResPtr( SYSRES_DIALOG_SHELL_ABOUT_MSGBOX ),
441 hWnd, AboutDlgProc32, (LPARAM)&info );
442 HeapFree( GetProcessHeap(), 0, (LPSTR)info.szApp );
443 HeapFree( GetProcessHeap(), 0, (LPSTR)info.szOtherStuff );
444 return ret;
448 /*************************************************************************
449 * SHELL_GetResourceTable
451 * FIXME: Implement GetPEResourceTable in w32sys.c and call it here.
453 static BYTE* SHELL_GetResourceTable(HFILE32 hFile)
455 struct mz_header_s mz_header;
456 struct ne_header_s ne_header;
457 int size;
459 _llseek32( hFile, 0, SEEK_SET );
460 if ((_lread32(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
461 (mz_header.mz_magic != MZ_SIGNATURE)) return (BYTE*)-1;
463 _llseek32( hFile, mz_header.ne_offset, SEEK_SET );
464 if (_lread32( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
465 return NULL;
467 if (ne_header.ne_magic == PE_SIGNATURE)
468 { fprintf(stdnimp,"Win32s FIXME: file %s line %i\n", __FILE__, __LINE__ );
469 return NULL; }
471 if (ne_header.ne_magic != NE_SIGNATURE) return NULL;
473 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
475 if( size > sizeof(NE_TYPEINFO) )
477 BYTE* pTypeInfo = (BYTE*)xmalloc(size);
479 if( !pTypeInfo ) return NULL;
481 _llseek32(hFile, mz_header.ne_offset+ne_header.resource_tab_offset, SEEK_SET);
482 if( _lread32( hFile, (char*)pTypeInfo, size) != size )
483 { free(pTypeInfo); return NULL; }
484 return pTypeInfo;
486 /* no resources */
488 return NULL;
491 /*************************************************************************
492 * SHELL_LoadResource
494 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE32 hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
496 BYTE* ptr;
497 HGLOBAL16 handle = DirectResAlloc( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
499 if( (ptr = (BYTE*)GlobalLock16( handle )) )
501 _llseek32( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
502 _lread32( hFile, (char*)ptr, pNInfo->length << sizeShift);
503 return handle;
505 return 0;
508 /*************************************************************************
509 * ICO_LoadIcon
511 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE32 hFile, LPicoICONDIRENTRY lpiIDE)
513 BYTE* ptr;
514 HGLOBAL16 handle = DirectResAlloc( hInst, 0x10, lpiIDE->dwBytesInRes);
516 if( (ptr = (BYTE*)GlobalLock16( handle )) )
518 _llseek32( hFile, lpiIDE->dwImageOffset, SEEK_SET);
519 _lread32( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
520 return handle;
522 return 0;
525 /*************************************************************************
526 * ICO_GetIconDirectory
528 * Read .ico file and build phony ICONDIR struct for GetIconID
530 static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE32 hFile, LPicoICONDIR* lplpiID )
532 WORD id[3]; /* idReserved, idType, idCount */
533 LPicoICONDIR lpiID;
534 int i;
536 _llseek32( hFile, 0, SEEK_SET );
537 if( _lread32(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
539 /* check .ICO header
541 * - see http://www.microsoft.com/win32dev/ui/icons.htm
544 if( id[0] || id[1] != 1 || !id[2] ) return 0;
546 i = id[2]*sizeof(icoICONDIRENTRY) + sizeof(id);
548 lpiID = (LPicoICONDIR)xmalloc(i);
550 if( _lread32(hFile,(char*)lpiID->idEntries,i) == i )
552 HGLOBAL16 handle = DirectResAlloc( hInst, 0x10,
553 id[2]*sizeof(ICONDIRENTRY) + sizeof(id) );
554 if( handle )
556 CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
557 lpID->idReserved = lpiID->idReserved = id[0];
558 lpID->idType = lpiID->idType = id[1];
559 lpID->idCount = lpiID->idCount = id[2];
560 for( i=0; i < lpiID->idCount; i++ )
562 memcpy((void*)(lpID->idEntries + i),
563 (void*)(lpiID->idEntries + i), sizeof(ICONDIRENTRY) - 2);
564 lpID->idEntries[i].icon.wResId = i;
566 *lplpiID = lpiID;
567 return handle;
570 /* fail */
572 free(lpiID);
573 return 0;
576 /*************************************************************************
577 * InternalExtractIcon [SHELL.39]
579 * This abortion is called directly by Progman
581 HGLOBAL16 InternalExtractIcon(HINSTANCE16 hInstance, LPCSTR lpszExeFileName, UINT nIconIndex, WORD n )
583 HGLOBAL16 hRet = 0;
584 HGLOBAL16* RetPtr = NULL;
585 BYTE* pData;
586 OFSTRUCT ofs;
587 HFILE32 hFile = OpenFile32( lpszExeFileName, &ofs, OF_READ );
589 dprintf_reg(stddeb, "InternalExtractIcon(%04x, file '%s', start from %d, extract %d\n",
590 hInstance, lpszExeFileName, nIconIndex, n);
592 if( hFile == HFILE_ERROR32 || !n ) return 0;
594 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
595 RetPtr = (HICON16*)GlobalLock16(hRet);
597 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
599 pData = SHELL_GetResourceTable(hFile);
600 if( pData )
602 HICON16 hIcon = 0;
603 BOOL icoFile = FALSE;
604 UINT iconDirCount = 0;
605 UINT iconCount = 0;
606 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
607 NE_NAMEINFO* pIconStorage = NULL;
608 NE_NAMEINFO* pIconDir = NULL;
609 LPicoICONDIR lpiID = NULL;
611 if( pData == (BYTE*)-1 )
613 /* check for .ICO file */
615 hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID);
616 if( hIcon )
617 { icoFile = TRUE; iconDirCount = 1; iconCount = lpiID->idCount; }
619 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
621 /* find icon directory and icon repository */
623 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON )
625 iconDirCount = pTInfo->count;
626 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
627 dprintf_reg(stddeb,"\tfound directory - %i icon families\n", iconDirCount);
629 if( pTInfo->type_id == NE_RSCTYPE_ICON )
631 iconCount = pTInfo->count;
632 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
633 dprintf_reg(stddeb,"\ttotal icons - %i\n", iconCount);
635 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
638 /* load resources and create icons */
640 if( (pIconStorage && pIconDir) || icoFile )
641 if( nIconIndex == (UINT)-1 ) RetPtr[0] = iconDirCount;
642 else if( nIconIndex < iconDirCount )
644 UINT i, icon;
646 if( n > iconDirCount - nIconIndex ) n = iconDirCount - nIconIndex;
648 for( i = nIconIndex; i < nIconIndex + n; i++ )
650 /* .ICO files have only one icon directory */
652 if( !icoFile )
653 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i,
654 *(WORD*)pData );
655 RetPtr[i-nIconIndex] = GetIconID( hIcon, 3 );
656 GlobalFree16(hIcon);
659 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
661 hIcon = 0;
662 if( icoFile )
663 hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
664 else
665 for( i = 0; i < iconCount; i++ )
666 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
667 hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,
668 *(WORD*)pData );
669 RetPtr[icon-nIconIndex] = (hIcon)?CURSORICON_LoadHandler( hIcon, hInstance, FALSE ):0;
672 if( icoFile ) free(lpiID);
673 else free(pData);
675 _lclose32( hFile );
677 /* return array with icon handles */
679 return hRet;
682 /*************************************************************************
683 * ExtractIcon [SHELL.34]
685 HICON16 ExtractIcon(HINSTANCE16 hInstance, LPCSTR lpszExeFileName, WORD nIconIndex)
687 HGLOBAL16 handle = InternalExtractIcon(hInstance,lpszExeFileName,nIconIndex, 1);
689 if( handle )
691 HICON16* ptr = (HICON16*)GlobalLock16(handle);
692 HICON16 hIcon = *ptr;
694 GlobalFree16(handle);
695 return hIcon;
697 return 0;
700 /*************************************************************************
701 * ExtractAssociatedIcon [SHELL.36]
703 * Return icon for given file (either from file itself or from associated
704 * executable) and patch parameters if needed.
706 HICON16 ExtractAssociatedIcon(HINSTANCE16 hInst,LPSTR lpIconPath,LPWORD lpiIcon)
708 HICON16 hIcon = ExtractIcon(hInst, lpIconPath, *lpiIcon);
710 if( hIcon < 2 )
713 if( hIcon == 1 ) /* no icons found in given file */
715 char tempPath[0x80];
716 UINT uRet = FindExecutable(lpIconPath,NULL,tempPath);
718 if( uRet > 32 && tempPath[0] )
720 strcpy(lpIconPath,tempPath);
721 hIcon = ExtractIcon(hInst, lpIconPath, *lpiIcon);
723 if( hIcon > 2 ) return hIcon;
725 else hIcon = 0;
728 if( hIcon == 1 )
729 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
730 else
731 *lpiIcon = 6; /* generic icon - found nothing */
733 GetModuleFileName16(hInst, lpIconPath, 0x80);
734 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE(*lpiIcon));
737 return hIcon;
740 /*************************************************************************
741 * FindEnvironmentString [SHELL.38]
743 * Returns a pointer into the DOS environment... Ugh.
745 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
747 UINT l = strlen(entry);
748 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
750 if( lstrncmpi32A(lpEnv, entry, l) ) continue;
752 if( !*(lpEnv+l) )
753 return (lpEnv + l); /* empty entry */
754 else if ( *(lpEnv+l)== '=' )
755 return (lpEnv + l + 1);
757 return NULL;
760 SEGPTR FindEnvironmentString(LPSTR str)
762 SEGPTR spEnv = GetDOSEnvironment();
763 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
765 LPSTR lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
767 if( lpString ) /* offset should be small enough */
768 return spEnv + (lpString - lpEnv);
770 return (SEGPTR)NULL;
773 /*************************************************************************
774 * DoEnvironmentSubst [SHELL.37]
776 * Replace %KEYWORD% in the str with the value of variable KEYWORD
777 * from "DOS" environment.
779 DWORD DoEnvironmentSubst(LPSTR str,WORD length)
781 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment());
782 LPSTR lpBuffer = (LPSTR)xmalloc(length);
783 LPSTR lpstr = str;
784 LPSTR lpbstr = lpBuffer;
786 CharToOem32A(str,str);
788 dprintf_reg(stddeb,"DoEnvSubst: accept %s", str);
790 while( *lpstr && lpbstr - lpBuffer < length )
792 LPSTR lpend = lpstr;
794 if( *lpstr == '%' )
796 do { lpend++; } while( *lpend && *lpend != '%' );
797 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
799 LPSTR lpKey;
800 *lpend = '\0';
801 lpKey = SHELL_FindString(lpEnv, lpstr+1);
802 if( lpKey ) /* found key value */
804 int l = strlen(lpKey);
806 if( l > length - (lpbstr - lpBuffer) - 1 )
808 fprintf(stdnimp,"File %s, line %i: Env subst aborted - string too short\n",
809 __FILE__, __LINE__);
810 *lpend = '%';
811 break;
813 strcpy(lpbstr, lpKey);
814 lpbstr += l;
816 else break;
817 *lpend = '%';
818 lpstr = lpend + 1;
820 else break; /* back off and whine */
822 continue;
825 *lpbstr++ = *lpstr++;
828 *lpbstr = '\0';
829 if( lpstr - str == strlen(str) )
831 strncpy(str, lpBuffer, length);
832 length = 1;
834 else
835 length = 0;
837 dprintf_reg(stddeb," return %s\n", str);
839 OemToChar32A(str,str);
840 free(lpBuffer);
842 /* Return str length in the LOWORD
843 * and 1 in HIWORD if subst was successful.
845 return (DWORD)MAKELONG(strlen(str), length);
848 /*************************************************************************
849 * RegisterShellHook [SHELL.102]
851 int RegisterShellHook(void *ptr)
853 fprintf(stdnimp, "RegisterShellHook( %p ) : Empty Stub !!!\n", ptr);
854 return 0;
858 /*************************************************************************
859 * ShellHookProc [SHELL.103]
861 int ShellHookProc(void)
863 fprintf(stdnimp, "ShellHookProc : Empty Stub !!!\n");
864 return 0;
867 /*************************************************************************
868 * SHGetFileInfoA [SHELL32.54]
870 DWORD
871 SHGetFileInfo32A(LPCSTR path,DWORD dwFileAttributes,SHFILEINFO32A *psfi,
872 UINT32 sizeofpsfi,UINT32 flags
874 fprintf(stdnimp,"SHGetFileInfo32A(%s,0x%08lx,%p,%d,0x%08x)\n",
875 path,dwFileAttributes,psfi,sizeofpsfi,flags
877 return TRUE;
880 /*************************************************************************
881 * CommandLineToArgvW [SHELL32.2]
883 LPWSTR*
884 CommandLineToArgvW(LPWSTR cmdline,LPDWORD numargs) {
885 LPWSTR *argv,s,t;
886 int i;
888 /* to get writeable copy */
889 cmdline = HEAP_strdupW( GetProcessHeap(), 0, cmdline);
890 s=cmdline;i=0;
891 while (*s) {
892 /* space */
893 if (*s==0x0020) {
894 i++;
895 s++;
896 while (*s && *s==0x0020)
897 s++;
898 continue;
900 s++;
902 argv=(LPWSTR*)HeapAlloc( GetProcessHeap(), 0, sizeof(LPWSTR)*(i+1) );
903 s=t=cmdline;
904 i=0;
905 while (*s) {
906 if (*s==0x0020) {
907 *s=0;
908 argv[i++]=HEAP_strdupW( GetProcessHeap(), 0, t );
909 *s=0x0020;
910 while (*s && *s==0x0020)
911 s++;
912 if (*s)
913 t=s+1;
914 else
915 t=s;
916 continue;
918 s++;
920 if (*t)
921 argv[i++]=(LPWSTR)HEAP_strdupW( GetProcessHeap(), 0, t );
922 HeapFree( GetProcessHeap(), 0, cmdline );
923 argv[i]=NULL;
924 *numargs=i;
925 return argv;