📄 advpack.c
字号:
/*
* Advpack main
*
* Copyright 2004 Huw D M Davies
* Copyright 2005 Sami Aario
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winreg.h"
#include "winternl.h"
#include "winnls.h"
#include "setupapi.h"
#include "advpub.h"
#include "wine/unicode.h"
#include "wine/debug.h"
#include "advpack_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(advpack);
typedef HRESULT (WINAPI *DLLREGISTER) (void);
#define MAX_FIELD_LENGTH 512
#define PREFIX_LEN 5
/* registry path of the Installed Components key for per-user stubs */
static const WCHAR setup_key[] = {
'S','O','F','T','W','A','R','E','\\',
'M','i','c','r','o','s','o','f','t','\\',
'A','c','t','i','v','e',' ','S','e','t','u','p','\\',
'I','n','s','t','a','l','l','e','d',' ',
'C','o','m','p','o','n','e','n','t','s',0
};
/* parses the destination directory parameters from pszSection
* the parameters are of the form: root,key,value,unknown,fallback
* we first read the reg value root\\key\\value and if that fails,
* use fallback as the destination directory
*/
static void get_dest_dir(HINF hInf, PCWSTR pszSection, PWSTR pszBuffer, DWORD dwSize)
{
INFCONTEXT context;
WCHAR key[MAX_PATH], value[MAX_PATH];
WCHAR prefix[PREFIX_LEN];
HKEY root, subkey;
DWORD size;
static const WCHAR hklm[] = {'H','K','L','M',0};
static const WCHAR hkcu[] = {'H','K','C','U',0};
/* load the destination parameters */
SetupFindFirstLineW(hInf, pszSection, NULL, &context);
SetupGetStringFieldW(&context, 1, prefix, PREFIX_LEN, &size);
SetupGetStringFieldW(&context, 2, key, MAX_PATH, &size);
SetupGetStringFieldW(&context, 3, value, MAX_PATH, &size);
if (!lstrcmpW(prefix, hklm))
root = HKEY_LOCAL_MACHINE;
else if (!lstrcmpW(prefix, hkcu))
root = HKEY_CURRENT_USER;
else
root = NULL;
size = dwSize * sizeof(WCHAR);
/* fallback to the default destination dir if reg fails */
if (RegOpenKeyW(root, key, &subkey) ||
RegQueryValueExW(subkey, value, NULL, NULL, (LPBYTE)pszBuffer, &size))
{
SetupGetStringFieldW(&context, 5, pszBuffer, dwSize, NULL);
}
RegCloseKey(subkey);
}
/* loads the LDIDs specified in the install section of an INF */
void set_ldids(HINF hInf, LPCWSTR pszInstallSection, LPCWSTR pszWorkingDir)
{
WCHAR field[MAX_FIELD_LENGTH];
WCHAR line[MAX_FIELD_LENGTH];
WCHAR dest[MAX_PATH];
INFCONTEXT context;
DWORD size;
int ldid;
static const WCHAR source_dir[] = {'S','o','u','r','c','e','D','i','r',0};
static const WCHAR custDestW[] = {
'C','u','s','t','o','m','D','e','s','t','i','n','a','t','i','o','n',0
};
if (!SetupGetLineTextW(NULL, hInf, pszInstallSection, custDestW,
field, MAX_FIELD_LENGTH, &size))
return;
if (!SetupFindFirstLineW(hInf, field, NULL, &context))
return;
do
{
LPWSTR value, ptr, key, key_copy = NULL;
SetupGetLineTextW(&context, NULL, NULL, NULL,
line, MAX_FIELD_LENGTH, &size);
/* SetupGetLineTextW returns the value if there is only one key, but
* returns the whole line if there is more than one key
*/
if (!(value = strchrW(line, '=')))
{
SetupGetStringFieldW(&context, 0, NULL, 0, &size);
key = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
key_copy = key;
SetupGetStringFieldW(&context, 0, key, size, &size);
value = line;
}
else
{
key = line;
*(value++) = '\0';
}
/* remove leading whitespace from the value */
while (*value == ' ')
value++;
/* FIXME: need to check the query option */
ptr = strchrW(value, ',');
if (ptr)
*ptr = '\0';
/* set dest to pszWorkingDir if key is SourceDir */
if (pszWorkingDir && !lstrcmpiW(value, source_dir))
lstrcpynW(dest, pszWorkingDir, MAX_PATH);
else
get_dest_dir(hInf, value, dest, MAX_PATH);
/* set all ldids to dest */
while ((ptr = get_parameter(&key, ',')))
{
ldid = atolW(ptr);
SetupSetDirectoryIdW(hInf, ldid, dest);
}
HeapFree(GetProcessHeap(), 0, key_copy);
} while (SetupFindNextLine(&context, &context));
}
/***********************************************************************
* CloseINFEngine (ADVPACK.@)
*
* Closes a handle to an INF file opened with OpenINFEngine.
*
* PARAMS
* hInf [I] Handle to the INF file to close.
*
* RETURNS
* Success: S_OK.
* Failure: E_FAIL.
*/
HRESULT WINAPI CloseINFEngine(HINF hInf)
{
TRACE("(%p)\n", hInf);
if (!hInf)
return E_INVALIDARG;
SetupCloseInfFile(hInf);
return S_OK;
}
/***********************************************************************
* DllMain (ADVPACK.@)
*/
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
if (fdwReason == DLL_PROCESS_ATTACH)
DisableThreadLibraryCalls(hinstDLL);
return TRUE;
}
/***********************************************************************
* IsNTAdmin (ADVPACK.@)
*
* Checks if the user has admin privileges.
*
* PARAMS
* reserved [I] Reserved. Must be 0.
* pReserved [I] Reserved. Must be NULL.
*
* RETURNS
* TRUE if user has admin rights, FALSE otherwise.
*/
BOOL WINAPI IsNTAdmin(DWORD reserved, LPDWORD pReserved)
{
SID_IDENTIFIER_AUTHORITY SidAuthority = {SECURITY_NT_AUTHORITY};
PTOKEN_GROUPS pTokenGroups;
BOOL bSidFound = FALSE;
DWORD dwSize, i;
HANDLE hToken;
PSID pSid;
TRACE("(%d, %p)\n", reserved, pReserved);
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
if (!GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwSize))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
CloseHandle(hToken);
return FALSE;
}
}
pTokenGroups = HeapAlloc(GetProcessHeap(), 0, dwSize);
if (!pTokenGroups)
{
CloseHandle(hToken);
return FALSE;
}
if (!GetTokenInformation(hToken, TokenGroups, pTokenGroups, dwSize, &dwSize))
{
HeapFree(GetProcessHeap(), 0, pTokenGroups);
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
if (!AllocateAndInitializeSid(&SidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSid))
{
HeapFree(GetProcessHeap(), 0, pTokenGroups);
return FALSE;
}
for (i = 0; i < pTokenGroups->GroupCount; i++)
{
if (EqualSid(pSid, pTokenGroups->Groups[i].Sid))
{
bSidFound = TRUE;
break;
}
}
HeapFree(GetProcessHeap(), 0, pTokenGroups);
FreeSid(pSid);
return bSidFound;
}
/***********************************************************************
* NeedRebootInit (ADVPACK.@)
*
* Sets up conditions for reboot checking.
*
* RETURNS
* Value required by NeedReboot.
*/
DWORD WINAPI NeedRebootInit(VOID)
{
FIXME("(VOID): stub\n");
return 0;
}
/***********************************************************************
* NeedReboot (ADVPACK.@)
*
* Determines whether a reboot is required.
*
* PARAMS
* dwRebootCheck [I] Value from NeedRebootInit.
*
* RETURNS
* TRUE if a reboot is needed, FALSE otherwise.
*
* BUGS
* Unimplemented.
*/
BOOL WINAPI NeedReboot(DWORD dwRebootCheck)
{
FIXME("(%d): stub\n", dwRebootCheck);
return FALSE;
}
/***********************************************************************
* OpenINFEngineA (ADVPACK.@)
*
* See OpenINFEngineW.
*/
HRESULT WINAPI OpenINFEngineA(LPCSTR pszInfFilename, LPCSTR pszInstallSection,
DWORD dwFlags, HINF *phInf, PVOID pvReserved)
{
UNICODE_STRING filenameW, installW;
HRESULT res;
TRACE("(%s, %s, %d, %p, %p)\n", debugstr_a(pszInfFilename),
debugstr_a(pszInstallSection), dwFlags, phInf, pvReserved);
if (!pszInfFilename || !phInf)
return E_INVALIDARG;
RtlCreateUnicodeStringFromAsciiz(&filenameW, pszInfFilename);
RtlCreateUnicodeStringFromAsciiz(&installW, pszInstallSection);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -