📄 shell32_main.c
字号:
/*
* Shell basics
*
* Copyright 1998 Marcus Meissner
* Copyright 1998 Juergen Schmied (jsch) * <juergen.schmied@metronet.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#define COBJMACROS
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winreg.h"
#include "dlgs.h"
#include "shellapi.h"
#include "winuser.h"
#include "wingdi.h"
#include "shlobj.h"
#include "shlguid.h"
#include "shlwapi.h"
#include "undocshell.h"
#include "pidl.h"
#include "shell32_main.h"
#include "version.h"
#include "shresdef.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(shell);
extern const char * const SHELL_Authors[];
#define MORE_DEBUG 1
/*************************************************************************
* CommandLineToArgvW [SHELL32.@]
*
* We must interpret the quotes in the command line to rebuild the argv
* array correctly:
* - arguments are separated by spaces or tabs
* - quotes serve as optional argument delimiters
* '"a b"' -> 'a b'
* - escaped quotes must be converted back to '"'
* '\"' -> '"'
* - an odd number of '\'s followed by '"' correspond to half that number
* of '\' followed by a '"' (extension of the above)
* '\\\"' -> '\"'
* '\\\\\"' -> '\\"'
* - an even number of '\'s followed by a '"' correspond to half that number
* of '\', plus a regular quote serving as an argument delimiter (which
* means it does not appear in the result)
* 'a\\"b c"' -> 'a\b c'
* 'a\\\\"b c"' -> 'a\\b c'
* - '\' that are not followed by a '"' are copied literally
* 'a\b' -> 'a\b'
* 'a\\b' -> 'a\\b'
*
* Note:
* '\t' == 0x0009
* ' ' == 0x0020
* '"' == 0x0022
* '\\' == 0x005c
*/
LPWSTR* WINAPI CommandLineToArgvW(LPCWSTR lpCmdline, int* numargs)
{
DWORD argc;
HGLOBAL hargv;
LPWSTR *argv;
LPCWSTR cs;
LPWSTR arg,s,d;
LPWSTR cmdline;
int in_quotes,bcount;
if (*lpCmdline==0)
{
/* Return the path to the executable */
DWORD len, size=16;
hargv=GlobalAlloc(size, 0);
argv=GlobalLock(hargv);
for (;;)
{
len = GetModuleFileNameW(0, (LPWSTR)(argv+1), size-sizeof(LPWSTR));
if (!len)
{
GlobalFree(hargv);
return NULL;
}
if (len < size) break;
size*=2;
hargv=GlobalReAlloc(hargv, size, 0);
argv=GlobalLock(hargv);
}
argv[0]=(LPWSTR)(argv+1);
if (numargs)
*numargs=2;
return argv;
}
/* to get a writeable copy */
argc=0;
bcount=0;
in_quotes=0;
cs=lpCmdline;
while (1)
{
if (*cs==0 || ((*cs==0x0009 || *cs==0x0020) && !in_quotes))
{
/* space */
argc++;
/* skip the remaining spaces */
while (*cs==0x0009 || *cs==0x0020) {
cs++;
}
if (*cs==0)
break;
bcount=0;
continue;
}
else if (*cs==0x005c)
{
/* '\', count them */
bcount++;
}
else if ((*cs==0x0022) && ((bcount & 1)==0))
{
/* unescaped '"' */
in_quotes=!in_quotes;
bcount=0;
}
else
{
/* a regular character */
bcount=0;
}
cs++;
}
/* Allocate in a single lump, the string array, and the strings that go with it.
* This way the caller can make a single GlobalFree call to free both, as per MSDN.
*/
hargv=GlobalAlloc(0, argc*sizeof(LPWSTR)+(strlenW(lpCmdline)+1)*sizeof(WCHAR));
argv=GlobalLock(hargv);
if (!argv)
return NULL;
cmdline=(LPWSTR)(argv+argc);
strcpyW(cmdline, lpCmdline);
argc=0;
bcount=0;
in_quotes=0;
arg=d=s=cmdline;
while (*s)
{
if ((*s==0x0009 || *s==0x0020) && !in_quotes)
{
/* Close the argument and copy it */
*d=0;
argv[argc++]=arg;
/* skip the remaining spaces */
do {
s++;
} while (*s==0x0009 || *s==0x0020);
/* Start with a new argument */
arg=d=s;
bcount=0;
}
else if (*s==0x005c)
{
/* '\\' */
*d++=*s++;
bcount++;
}
else if (*s==0x0022)
{
/* '"' */
if ((bcount & 1)==0)
{
/* Preceded by an even number of '\', this is half that
* number of '\', plus a quote which we erase.
*/
d-=bcount/2;
in_quotes=!in_quotes;
s++;
}
else
{
/* Preceded by an odd number of '\', this is half that
* number of '\' followed by a '"'
*/
d=d-bcount/2-1;
*d++='"';
s++;
}
bcount=0;
}
else
{
/* a regular character */
*d++=*s++;
bcount=0;
}
}
if (*arg)
{
*d='\0';
argv[argc++]=arg;
}
if (numargs)
*numargs=argc;
return argv;
}
static DWORD shgfi_get_exe_type(LPCWSTR szFullPath)
{
BOOL status = FALSE;
HANDLE hfile;
DWORD BinaryType;
IMAGE_DOS_HEADER mz_header;
IMAGE_NT_HEADERS nt;
DWORD len;
char magic[4];
status = GetBinaryTypeW (szFullPath, &BinaryType);
if (!status)
return 0;
if (BinaryType == SCS_DOS_BINARY || BinaryType == SCS_PIF_BINARY)
return 0x4d5a;
hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, 0 );
if ( hfile == INVALID_HANDLE_VALUE )
return 0;
/*
* The next section is adapted from MODULE_GetBinaryType, as we need
* to examine the image header to get OS and version information. We
* know from calling GetBinaryTypeA that the image is valid and either
* an NE or PE, so much error handling can be omitted.
* Seek to the start of the file and read the header information.
*/
SetFilePointer( hfile, 0, NULL, SEEK_SET );
ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
ReadFile( hfile, magic, sizeof(magic), &len, NULL );
if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
{
SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
CloseHandle( hfile );
if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
{
return IMAGE_NT_SIGNATURE |
(nt.OptionalHeader.MajorSubsystemVersion << 24) |
(nt.OptionalHeader.MinorSubsystemVersion << 16);
}
return IMAGE_NT_SIGNATURE;
}
else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
{
IMAGE_OS2_HEADER ne;
SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
CloseHandle( hfile );
if (ne.ne_exetyp == 2)
return IMAGE_OS2_SIGNATURE | (ne.ne_expver << 16);
return 0;
}
CloseHandle( hfile );
return 0;
}
/*************************************************************************
* SHELL_IsShortcut [internal]
*
* Decide if an item id list points to a shell shortcut
*/
BOOL SHELL_IsShortcut(LPCITEMIDLIST pidlLast)
{
char szTemp[MAX_PATH];
HKEY keyCls;
BOOL ret = FALSE;
if (_ILGetExtension(pidlLast, szTemp, MAX_PATH) &&
HCR_MapTypeToValueA(szTemp, szTemp, MAX_PATH, TRUE))
{
if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, KEY_QUERY_VALUE, &keyCls))
{
if (ERROR_SUCCESS == RegQueryValueExA(keyCls, "IsShortcut", NULL, NULL, NULL, NULL))
ret = TRUE;
RegCloseKey(keyCls);
}
}
return ret;
}
#define SHGFI_KNOWN_FLAGS \
(SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
/*************************************************************************
* SHGetFileInfoW [SHELL32.@]
*
*/
DWORD_PTR WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags )
{
WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
int iIndex;
DWORD_PTR ret = TRUE;
DWORD dwAttributes = 0;
IShellFolder * psfParent = NULL;
IExtractIconW * pei = NULL;
LPITEMIDLIST pidlLast = NULL, pidl = NULL;
HRESULT hr = S_OK;
BOOL IconNotYetLoaded=TRUE;
UINT uGilFlags = 0;
TRACE("%s fattr=0x%lx sfi=%p(attr=0x%08lx) size=0x%x flags=0x%x\n",
(flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
psfi, psfi->dwAttributes, sizeofpsfi, flags);
if ( (flags & SHGFI_USEFILEATTRIBUTES) &&
(flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
return FALSE;
/* windows initializes this values regardless of the flags */
if (psfi != NULL)
{
psfi->szDisplayName[0] = '\0';
psfi->szTypeName[0] = '\0';
psfi->iIcon = 0;
}
if (!(flags & SHGFI_PIDL))
{
/* SHGetFileInfo should work with absolute and relative paths */
if (PathIsRelativeW(path))
{
GetCurrentDirectoryW(MAX_PATH, szLocation);
PathCombineW(szFullPath, szLocation, path);
}
else
{
lstrcpynW(szFullPath, path, MAX_PATH);
}
}
if (flags & SHGFI_EXETYPE)
{
if (flags != SHGFI_EXETYPE)
return 0;
return shgfi_get_exe_type(szFullPath);
}
/*
* psfi is NULL normally to query EXE type. If it is NULL, none of the
* below makes sense anyway. Windows allows this and just returns FALSE
*/
if (psfi == NULL)
return FALSE;
/*
* translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
* is not specified.
* The pidl functions fail on not existing file names
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -