⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desktop.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  ReactOS kernel
 *  Copyright (C) 2004 ReactOS Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/* $Id: desktop.c 21846 2006-05-07 20:23:16Z amunger $
 *
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS system libraries
 * FILE:            lib/userenv/desktop.c
 * PURPOSE:         Desktop and start menu support functions.
 * PROGRAMMER:      Eric Kohl
 */

#include <precomp.h>

#define NDEBUG
#include <debug.h>


/* FUNCTIONS ***************************************************************/

static BOOL
GetDesktopPath (BOOL bCommonPath,
		LPWSTR lpDesktopPath)
{
  WCHAR szPath[MAX_PATH];
  DWORD dwLength;
  DWORD dwType;
  HKEY hKey;
  LONG Error;

  DPRINT ("GetDesktopPath() called\n");

  Error = RegOpenKeyExW (HKEY_CURRENT_USER,
		         L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
		         0,
		         KEY_QUERY_VALUE,
		         &hKey);
  if (Error != ERROR_SUCCESS)
    {
      DPRINT1 ("RegOpenKeyExW() failed\n");
      SetLastError((DWORD)Error);
      return FALSE;
    }

  dwLength = MAX_PATH * sizeof(WCHAR);
  Error = RegQueryValueExW (hKey,
			    bCommonPath ? L"Common Desktop" : L"Desktop",
			    0,
			    &dwType,
			    (LPBYTE)szPath,
			   &dwLength);
  if (Error != ERROR_SUCCESS)
    {
      DPRINT1 ("RegQueryValueExW() failed\n");
      RegCloseKey (hKey);
      SetLastError((DWORD)Error);
      return FALSE;
    }

  RegCloseKey (hKey);

  if (dwType == REG_EXPAND_SZ)
    {
      ExpandEnvironmentStringsW (szPath,
				 lpDesktopPath,
				 MAX_PATH);
    }
  else
    {
      wcscpy (lpDesktopPath, szPath);
    }

  DPRINT ("GetDesktopPath() done\n");

  return TRUE;
}


static BOOL
GetProgramsPath (BOOL bCommonPath,
		 LPWSTR lpProgramsPath)
{
  WCHAR szPath[MAX_PATH];
  DWORD dwLength;
  DWORD dwType;
  HKEY hKey;
  LONG Error;

  DPRINT ("GetProgramsPath() called\n");

  Error = RegOpenKeyExW (HKEY_CURRENT_USER,
		         L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
		         0,
		         KEY_QUERY_VALUE,
		         &hKey);
  if (Error != ERROR_SUCCESS)
    {
      DPRINT1 ("RegOpenKeyExW() failed\n");
      SetLastError((DWORD)Error);
      return FALSE;
    }

  dwLength = MAX_PATH * sizeof(WCHAR);
  Error = RegQueryValueExW (hKey,
			    bCommonPath ? L"Common Programs" : L"Programs",
			    0,
			    &dwType,
			    (LPBYTE)szPath,
			    &dwLength);
  if (Error != ERROR_SUCCESS)
    {
      DPRINT1 ("RegQueryValueExW() failed\n");
      RegCloseKey (hKey);
      SetLastError((DWORD)Error);
      return FALSE;
    }

  RegCloseKey (hKey);

  if (dwType == REG_EXPAND_SZ)
    {
      ExpandEnvironmentStringsW (szPath,
				 lpProgramsPath,
				 MAX_PATH);
    }
  else
    {
      wcscpy (lpProgramsPath,
	      szPath);
    }

  DPRINT ("GetProgramsPath() done\n");

  return TRUE;
}


BOOL WINAPI
AddDesktopItemA (BOOL bCommonItem,
		 LPCSTR lpItemName,
		 LPCSTR lpArguments,
		 LPCSTR lpIconLocation,
		 INT iIcon,
		 LPCSTR lpWorkingDirectory, /* Optional */
		 WORD wHotKey,
		 INT iShowCmd)
{
  UNICODE_STRING ItemName;
  UNICODE_STRING Arguments;
  UNICODE_STRING IconLocation;
  UNICODE_STRING WorkingDirectory;
  BOOL bResult;
  NTSTATUS Status;

  Status = RtlCreateUnicodeStringFromAsciiz(&ItemName,
					    (LPSTR)lpItemName);
  if (!NT_SUCCESS(Status))
    {
      SetLastError (RtlNtStatusToDosError (Status));
      return FALSE;
    }

  Status = RtlCreateUnicodeStringFromAsciiz(&Arguments,
					    (LPSTR)lpArguments);
  if (!NT_SUCCESS(Status))
    {
      RtlFreeUnicodeString(&ItemName);
      SetLastError (RtlNtStatusToDosError (Status));
      return FALSE;
    }

  Status = RtlCreateUnicodeStringFromAsciiz(&IconLocation,
					    (LPSTR)lpIconLocation);
  if (!NT_SUCCESS(Status))
    {
      RtlFreeUnicodeString(&Arguments);
      RtlFreeUnicodeString(&ItemName);
      SetLastError (RtlNtStatusToDosError (Status));
      return FALSE;
    }

  if (lpWorkingDirectory != NULL)
    {
      Status = RtlCreateUnicodeStringFromAsciiz(&WorkingDirectory,
						(LPSTR)lpWorkingDirectory);
      if (!NT_SUCCESS(Status))
	{
	  RtlFreeUnicodeString(&IconLocation);
	  RtlFreeUnicodeString(&Arguments);
	  RtlFreeUnicodeString(&ItemName);
	  SetLastError (RtlNtStatusToDosError (Status));
	  return FALSE;
	}
    }

  bResult = AddDesktopItemW(bCommonItem,
			    ItemName.Buffer,
			    Arguments.Buffer,
			    IconLocation.Buffer,
			    iIcon,
			    (lpWorkingDirectory != NULL) ? WorkingDirectory.Buffer : NULL,
			    wHotKey,
			    iShowCmd);

  if (lpWorkingDirectory != NULL)
    {
      RtlFreeUnicodeString(&WorkingDirectory);
    }

  RtlFreeUnicodeString(&IconLocation);
  RtlFreeUnicodeString(&Arguments);
  RtlFreeUnicodeString(&ItemName);

  return bResult;
}


BOOL WINAPI
AddDesktopItemW (BOOL bCommonDesktop,
		 LPCWSTR lpItemName,
		 LPCWSTR lpArguments,
		 LPCWSTR lpIconLocation,
		 INT iIcon,
		 LPCWSTR lpWorkingDirectory,  /* Optional */
		 WORD wHotKey,
		 INT iShowCmd)
{
  DYN_FUNCS Ole32;
  WCHAR szLinkPath[MAX_PATH];
  WCHAR szArguments[MAX_PATH];
  WCHAR szCommand[MAX_PATH];
  WIN32_FIND_DATAW FindData;
  HANDLE hFind;
  LPWSTR Ptr;
  DWORD dwLength;
  IShellLinkW* psl;
  IPersistFile* ppf;
  HRESULT hr;
  BOOL bResult;

  DPRINT ("AddDesktopItemW() called\n");

  bResult = FALSE;

  if (!GetDesktopPath (bCommonDesktop, szLinkPath))
    {
      DPRINT1 ("GetDesktopPath() failed\n");
      return FALSE;
    }
  DPRINT ("Desktop path: '%S'\n", szLinkPath);

  /* Make sure the path exists */
  hFind = FindFirstFileW (szLinkPath,
			  &FindData);
  if (hFind == INVALID_HANDLE_VALUE)
    {
      DPRINT ("'%S' does not exist\n", szLinkPath);

      /* Create directory path */
      if (!CreateDirectoryPath (szLinkPath, NULL))
        return FALSE;
    }
  else
    {
      DPRINT ("'%S' exists\n", szLinkPath);
      FindClose (hFind);
    }

  /* Append backslash, item name and ".lnk" extension */
  wcscat (szLinkPath, L"\\");
  wcscat (szLinkPath, lpItemName);
  wcscat (szLinkPath, L".lnk");
  DPRINT ("Link path: '%S'\n", szLinkPath);

  /* Split 'lpArguments' string into command and arguments */
  Ptr = wcschr (lpArguments, L' ');
  DPRINT ("Ptr %p  lpArguments %p\n", Ptr, lpArguments);
  if (Ptr != NULL)
    {
      dwLength = (DWORD)(Ptr - lpArguments);
      DPRINT ("dwLength %lu\n", dwLength);
      memcpy (szCommand, lpArguments, dwLength * sizeof(WCHAR));
      szCommand[dwLength] = 0;
      Ptr++;
      wcscpy (szArguments, Ptr);
    }
  else
    {
      wcscpy (szCommand, lpArguments);
      szArguments[0] = 0;
    }
  DPRINT ("szCommand: '%S'\n", szCommand);
  DPRINT ("szArguments: '%S'\n", szArguments);

  /* Dynamically load ole32.dll */
  if (!LoadDynamicImports(&DynOle32, &Ole32))
    {
      DPRINT1("USERENV: Unable to load OLE32.DLL\n");
      return FALSE;
    }

  Ole32.fn.CoInitialize(NULL);

  hr = Ole32.fn.CoCreateInstance(&CLSID_ShellLink,
                                 NULL,
                                 CLSCTX_INPROC_SERVER,
                                 &IID_IShellLinkW,
                                 (LPVOID*)&psl);
  if (!SUCCEEDED(hr))
    {
      Ole32.fn.CoUninitialize();
      UnloadDynamicImports(&Ole32);
      return FALSE;
    }

  hr = psl->lpVtbl->QueryInterface(psl,
                                   &IID_IPersistFile,
                                   (LPVOID*)&ppf);
  if (SUCCEEDED(hr))
    {
      psl->lpVtbl->SetDescription(psl,
                                  lpItemName);

      psl->lpVtbl->SetPath(psl,
                           szCommand);

      psl->lpVtbl->SetArguments(psl,
                                szArguments);

      psl->lpVtbl->SetIconLocation(psl,
                                   lpIconLocation,
                                   iIcon);

      if (lpWorkingDirectory != NULL)
        {
          psl->lpVtbl->SetWorkingDirectory(psl,
                                           lpWorkingDirectory);
        }
      else
        {
          psl->lpVtbl->SetWorkingDirectory(psl,
                                           L"%HOMEDRIVE%%HOMEPATH%");
        }

      psl->lpVtbl->SetHotkey(psl,
                             wHotKey);

      psl->lpVtbl->SetShowCmd(psl,
                              iShowCmd);

      hr = ppf->lpVtbl->Save(ppf,
                             szLinkPath,
                             TRUE);
      if (SUCCEEDED(hr))
        bResult = TRUE;

      ppf->lpVtbl->Release(ppf);
    }

  psl->lpVtbl->Release(psl);

  Ole32.fn.CoUninitialize();

  UnloadDynamicImports(&Ole32);

  DPRINT ("AddDesktopItemW() done\n");

  return bResult;
}


BOOL WINAPI
DeleteDesktopItemA (BOOL bCommonItem,
		    LPCSTR lpItemName)
{
  UNICODE_STRING ItemName;
  BOOL bResult;
  NTSTATUS Status;

  Status = RtlCreateUnicodeStringFromAsciiz(&ItemName,
					    (LPSTR)lpItemName);
  if (!NT_SUCCESS(Status))
    {
      SetLastError (RtlNtStatusToDosError (Status));
      return FALSE;
    }

  bResult = DeleteDesktopItemW(bCommonItem,
			       ItemName.Buffer);

  RtlFreeUnicodeString(&ItemName);

  return bResult;
}


BOOL WINAPI
DeleteDesktopItemW (BOOL bCommonItem,
		    LPCWSTR lpItemName)
{
  WCHAR szLinkPath[MAX_PATH];

  DPRINT ("DeleteDesktopItemW() called\n");

  if (!GetDesktopPath (bCommonItem, szLinkPath))
    {
      DPRINT1 ("GetDesktopPath() failed\n");
      return FALSE;
    }

  wcscat (szLinkPath, L"\\");
  wcscat (szLinkPath, lpItemName);
  wcscat (szLinkPath, L".lnk");
  DPRINT ("Link path: '%S'\n", szLinkPath);

  return DeleteFileW (szLinkPath);
}


BOOL WINAPI
CreateGroupA (LPCSTR lpGroupName,
	      BOOL bCommonGroup)
{
  UNICODE_STRING GroupName;
  BOOL bResult;
  NTSTATUS Status;

  Status = RtlCreateUnicodeStringFromAsciiz(&GroupName,
					    (LPSTR)lpGroupName);
  if (!NT_SUCCESS(Status))
    {
      SetLastError (RtlNtStatusToDosError (Status));
      return FALSE;
    }

  bResult = CreateGroupW(GroupName.Buffer, bCommonGroup);

  RtlFreeUnicodeString(&GroupName);

  return bResult;
}


BOOL WINAPI
CreateGroupW (LPCWSTR lpGroupName,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -