📄 misc.c
字号:
}
return lpMultiByteStr;
}
/**************************************************************************
* DoesUserHavePrivilege [SETUPAPI.@]
*
* Check whether the current user has got a given privilege.
*
* PARAMS
* lpPrivilegeName [I] Name of the privilege to be checked
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
{
HANDLE hToken;
DWORD dwSize;
PTOKEN_PRIVILEGES lpPrivileges;
LUID PrivilegeLuid;
DWORD i;
BOOL bResult = FALSE;
TRACE("%s\n", debugstr_w(lpPrivilegeName));
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
CloseHandle(hToken);
return FALSE;
}
}
lpPrivileges = MyMalloc(dwSize);
if (lpPrivileges == NULL)
{
CloseHandle(hToken);
return FALSE;
}
if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
{
MyFree(lpPrivileges);
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
{
MyFree(lpPrivileges);
return FALSE;
}
for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
{
if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
{
bResult = TRUE;
}
}
MyFree(lpPrivileges);
return bResult;
}
/**************************************************************************
* EnablePrivilege [SETUPAPI.@]
*
* Enables or disables one of the current users privileges.
*
* PARAMS
* lpPrivilegeName [I] Name of the privilege to be changed
* bEnable [I] TRUE: Enables the privilege
* FALSE: Disables the privilege
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
{
TOKEN_PRIVILEGES Privileges;
HANDLE hToken;
BOOL bResult;
TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
Privileges.PrivilegeCount = 1;
Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
&Privileges.Privileges[0].Luid))
{
CloseHandle(hToken);
return FALSE;
}
bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
CloseHandle(hToken);
return bResult;
}
/**************************************************************************
* DelayedMove [SETUPAPI.@]
*
* Moves a file upon the next reboot.
*
* PARAMS
* lpExistingFileName [I] Current file name
* lpNewFileName [I] New file name
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
{
return MoveFileExW(lpExistingFileName, lpNewFileName,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
}
/**************************************************************************
* FileExists [SETUPAPI.@]
*
* Checks whether a file exists.
*
* PARAMS
* lpFileName [I] Name of the file to check
* lpNewFileName [O] Optional information about the existing file
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
{
WIN32_FIND_DATAW FindData;
HANDLE hFind;
UINT uErrorMode;
DWORD dwError;
uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
hFind = FindFirstFileW(lpFileName, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
SetErrorMode(uErrorMode);
SetLastError(dwError);
return FALSE;
}
FindClose(hFind);
if (lpFileFindData)
memcpy(lpFileFindData, &FindData, sizeof(WIN32_FIND_DATAW));
SetErrorMode(uErrorMode);
return TRUE;
}
/**************************************************************************
* CaptureStringArg [SETUPAPI.@]
*
* Captures a UNICODE string.
*
* PARAMS
* lpSrc [I] UNICODE string to be captured
* lpDst [O] Pointer to the captured UNICODE string
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: ERROR_INVALID_PARAMETER
*
* NOTE
* Call MyFree to release the captured UNICODE string.
*/
DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
{
if (pDst == NULL)
return ERROR_INVALID_PARAMETER;
*pDst = DuplicateString(pSrc);
return ERROR_SUCCESS;
}
/**************************************************************************
* CaptureAndConvertAnsiArg [SETUPAPI.@]
*
* Captures an ANSI string and converts it to a UNICODE string.
*
* PARAMS
* lpSrc [I] ANSI string to be captured
* lpDst [O] Pointer to the captured UNICODE string
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: ERROR_INVALID_PARAMETER
*
* NOTE
* Call MyFree to release the captured UNICODE string.
*/
DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
{
if (pDst == NULL)
return ERROR_INVALID_PARAMETER;
*pDst = MultiByteToUnicode(pSrc, CP_ACP);
return ERROR_SUCCESS;
}
/**************************************************************************
* OpenAndMapFileForRead [SETUPAPI.@]
*
* Open and map a file to a buffer.
*
* PARAMS
* lpFileName [I] Name of the file to be opened
* lpSize [O] Pointer to the file size
* lpFile [0] Pointer to the file handle
* lpMapping [0] Pointer to the mapping handle
* lpBuffer [0] Pointer to the file buffer
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: Other
*
* NOTE
* Call UnmapAndCloseFile to release the file.
*/
DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
LPDWORD lpSize,
LPHANDLE lpFile,
LPHANDLE lpMapping,
LPVOID *lpBuffer)
{
DWORD dwError;
TRACE("%s %p %p %p %p\n",
debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
*lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (*lpFile == INVALID_HANDLE_VALUE)
return GetLastError();
*lpSize = GetFileSize(*lpFile, NULL);
if (*lpSize == INVALID_FILE_SIZE)
{
dwError = GetLastError();
CloseHandle(*lpFile);
return dwError;
}
*lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
*lpSize, NULL);
if (*lpMapping == NULL)
{
dwError = GetLastError();
CloseHandle(*lpFile);
return dwError;
}
*lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
if (*lpBuffer == NULL)
{
dwError = GetLastError();
CloseHandle(*lpMapping);
CloseHandle(*lpFile);
return dwError;
}
return ERROR_SUCCESS;
}
/**************************************************************************
* UnmapAndCloseFile [SETUPAPI.@]
*
* Unmap and close a mapped file.
*
* PARAMS
* hFile [I] Handle to the file
* hMapping [I] Handle to the file mapping
* lpBuffer [I] Pointer to the file buffer
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
{
TRACE("%p %p %p\n",
hFile, hMapping, lpBuffer);
if (!UnmapViewOfFile(lpBuffer))
return FALSE;
if (!CloseHandle(hMapping))
return FALSE;
if (!CloseHandle(hFile))
return FALSE;
return TRUE;
}
/**************************************************************************
* StampFileSecurity [SETUPAPI.@]
*
* Assign a new security descriptor to the given file.
*
* PARAMS
* lpFileName [I] Name of the file
* pSecurityDescriptor [I] New security descriptor
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: other
*/
DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
{
TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
pSecurityDescriptor))
return GetLastError();
return ERROR_SUCCESS;
}
/**************************************************************************
* TakeOwnershipOfFile [SETUPAPI.@]
*
* Takes the ownership of the given file.
*
* PARAMS
* lpFileName [I] Name of the file
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: other
*/
DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
{
SECURITY_DESCRIPTOR SecDesc;
HANDLE hToken = NULL;
PTOKEN_OWNER pOwner = NULL;
DWORD dwError;
DWORD dwSize;
TRACE("%s\n", debugstr_w(lpFileName));
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return GetLastError();
if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
{
goto fail;
}
pOwner = (PTOKEN_OWNER)MyMalloc(dwSize);
if (pOwner == NULL)
{
CloseHandle(hToken);
return ERROR_NOT_ENOUGH_MEMORY;
}
if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
{
goto fail;
}
if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
{
goto fail;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -