2 * Setupapi miscellaneous functions
4 * Copyright 2005 Eric Kohl
5 * Copyright 2007 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "setupapi_private.h"
27 /* Unicode constants */
28 static const WCHAR BackSlash
[] = {'\\',0};
29 static const WCHAR TranslationRegKey
[] = {'\\','V','e','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n',0};
31 /* Handles and critical sections for the SetupLog API */
32 static HANDLE setupact
= INVALID_HANDLE_VALUE
;
33 static HANDLE setuperr
= INVALID_HANDLE_VALUE
;
34 static CRITICAL_SECTION setupapi_cs
;
35 static CRITICAL_SECTION_DEBUG critsect_debug
=
38 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
39 0, 0, { (DWORD_PTR
)(__FILE__
": setupapi_cs") }
41 static CRITICAL_SECTION setupapi_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
45 IN PWSTR InstallerName
,
46 OUT HMODULE
* ModulePointer
,
47 OUT PVOID
* FunctionPointer
)
49 HMODULE hModule
= NULL
;
50 LPSTR FunctionNameA
= NULL
;
54 *ModulePointer
= NULL
;
55 *FunctionPointer
= NULL
;
57 Comma
= strchrW(InstallerName
, ',');
60 rc
= ERROR_INVALID_PARAMETER
;
66 hModule
= LoadLibraryW(InstallerName
);
74 /* Skip comma spaces */
75 while (*Comma
== ',' || isspaceW(*Comma
))
78 /* W->A conversion for function name */
79 FunctionNameA
= pSetupUnicodeToMultiByte(Comma
, CP_ACP
);
87 *FunctionPointer
= GetProcAddress(hModule
, FunctionNameA
);
88 if (!*FunctionPointer
)
94 *ModulePointer
= hModule
;
98 if (rc
!= ERROR_SUCCESS
&& hModule
)
100 MyFree(FunctionNameA
);
106 IN HMODULE ModulePointer
,
107 IN PVOID FunctionPointer
)
109 if (ModulePointer
== NULL
)
110 return ERROR_SUCCESS
;
111 if (FreeLibrary(ModulePointer
))
112 return ERROR_SUCCESS
;
114 return GetLastError();
117 /**************************************************************************
118 * MyFree [SETUPAPI.@]
120 * Frees an allocated memory block from the process heap.
123 * lpMem [I] pointer to memory block which will be freed
128 VOID WINAPI
MyFree(LPVOID lpMem
)
130 TRACE("%p\n", lpMem
);
131 HeapFree(GetProcessHeap(), 0, lpMem
);
135 /**************************************************************************
136 * MyMalloc [SETUPAPI.@]
138 * Allocates memory block from the process heap.
141 * dwSize [I] size of the allocated memory block
144 * Success: pointer to allocated memory block
147 LPVOID WINAPI
MyMalloc(DWORD dwSize
)
149 TRACE("%lu\n", dwSize
);
150 return HeapAlloc(GetProcessHeap(), 0, dwSize
);
154 /**************************************************************************
155 * MyRealloc [SETUPAPI.@]
157 * Changes the size of an allocated memory block or allocates a memory
158 * block from the process heap.
161 * lpSrc [I] pointer to memory block which will be resized
162 * dwSize [I] new size of the memory block
165 * Success: pointer to the resized memory block
169 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
170 * block like MyMalloc.
172 LPVOID WINAPI
MyRealloc(LPVOID lpSrc
, DWORD dwSize
)
174 TRACE("%p %lu\n", lpSrc
, dwSize
);
177 return HeapAlloc(GetProcessHeap(), 0, dwSize
);
179 return HeapReAlloc(GetProcessHeap(), 0, lpSrc
, dwSize
);
183 /**************************************************************************
184 * pSetupDuplicateString [SETUPAPI.@]
186 * Duplicates a unicode string.
189 * lpSrc [I] pointer to the unicode string that will be duplicated
192 * Success: pointer to the duplicated unicode string
196 * Call MyFree() to release the duplicated string.
198 LPWSTR WINAPI
pSetupDuplicateString(LPCWSTR lpSrc
)
202 TRACE("%s\n", debugstr_w(lpSrc
));
204 lpDst
= MyMalloc((lstrlenW(lpSrc
) + 1) * sizeof(WCHAR
));
208 strcpyW(lpDst
, lpSrc
);
214 /**************************************************************************
215 * QueryRegistryValue [SETUPAPI.@]
217 * Retrieves value data from the registry and allocates memory for the
221 * hKey [I] Handle of the key to query
222 * lpValueName [I] Name of value under hkey to query
223 * lpData [O] Destination for the values contents,
224 * lpType [O] Destination for the value type
225 * lpcbData [O] Destination for the size of data
228 * Success: ERROR_SUCCESS
232 * Use MyFree to release the lpData buffer.
234 LONG WINAPI
QueryRegistryValue(HKEY hKey
,
242 TRACE("%p %s %p %p %p\n",
243 hKey
, debugstr_w(lpValueName
), lpData
, lpType
, lpcbData
);
245 /* Get required buffer size */
247 lError
= RegQueryValueExW(hKey
, lpValueName
, 0, lpType
, NULL
, lpcbData
);
248 if (lError
!= ERROR_SUCCESS
)
251 /* Allocate buffer */
252 *lpData
= MyMalloc(*lpcbData
);
254 return ERROR_NOT_ENOUGH_MEMORY
;
256 /* Query registry value */
257 lError
= RegQueryValueExW(hKey
, lpValueName
, 0, lpType
, *lpData
, lpcbData
);
258 if (lError
!= ERROR_SUCCESS
)
265 /**************************************************************************
266 * pSetupMultiByteToUnicode [SETUPAPI.@]
268 * Converts a multi-byte string to a Unicode string.
271 * lpMultiByteStr [I] Multi-byte string to be converted
272 * uCodePage [I] Code page
275 * Success: pointer to the converted Unicode string
279 * Use MyFree to release the returned Unicode string.
281 LPWSTR WINAPI
pSetupMultiByteToUnicode(LPCSTR lpMultiByteStr
, UINT uCodePage
)
286 TRACE("%s %d\n", debugstr_a(lpMultiByteStr
), uCodePage
);
288 nLength
= MultiByteToWideChar(uCodePage
, 0, lpMultiByteStr
,
293 lpUnicodeStr
= MyMalloc(nLength
* sizeof(WCHAR
));
294 if (lpUnicodeStr
== NULL
)
296 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
300 if (!MultiByteToWideChar(uCodePage
, 0, lpMultiByteStr
,
301 nLength
, lpUnicodeStr
, nLength
))
303 MyFree(lpUnicodeStr
);
311 /**************************************************************************
312 * pSetupUnicodeToMultiByte [SETUPAPI.@]
314 * Converts a Unicode string to a multi-byte string.
317 * lpUnicodeStr [I] Unicode string to be converted
318 * uCodePage [I] Code page
321 * Success: pointer to the converted multi-byte string
325 * Use MyFree to release the returned multi-byte string.
327 LPSTR WINAPI
pSetupUnicodeToMultiByte(LPCWSTR lpUnicodeStr
, UINT uCodePage
)
329 LPSTR lpMultiByteStr
;
332 TRACE("%s %d\n", debugstr_w(lpUnicodeStr
), uCodePage
);
334 nLength
= WideCharToMultiByte(uCodePage
, 0, lpUnicodeStr
, -1,
335 NULL
, 0, NULL
, NULL
);
339 lpMultiByteStr
= MyMalloc(nLength
);
340 if (lpMultiByteStr
== NULL
)
343 if (!WideCharToMultiByte(uCodePage
, 0, lpUnicodeStr
, -1,
344 lpMultiByteStr
, nLength
, NULL
, NULL
))
346 MyFree(lpMultiByteStr
);
350 return lpMultiByteStr
;
354 /**************************************************************************
355 * DoesUserHavePrivilege [SETUPAPI.@]
357 * Check whether the current user has got a given privilege.
360 * lpPrivilegeName [I] Name of the privilege to be checked
366 BOOL WINAPI
DoesUserHavePrivilege(LPCWSTR lpPrivilegeName
)
370 PTOKEN_PRIVILEGES lpPrivileges
;
373 BOOL bResult
= FALSE
;
375 TRACE("%s\n", debugstr_w(lpPrivilegeName
));
377 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
380 if (!GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &dwSize
))
382 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
389 lpPrivileges
= MyMalloc(dwSize
);
390 if (lpPrivileges
== NULL
)
396 if (!GetTokenInformation(hToken
, TokenPrivileges
, lpPrivileges
, dwSize
, &dwSize
))
398 MyFree(lpPrivileges
);
405 if (!LookupPrivilegeValueW(NULL
, lpPrivilegeName
, &PrivilegeLuid
))
407 MyFree(lpPrivileges
);
411 for (i
= 0; i
< lpPrivileges
->PrivilegeCount
; i
++)
413 if (lpPrivileges
->Privileges
[i
].Luid
.HighPart
== PrivilegeLuid
.HighPart
&&
414 lpPrivileges
->Privileges
[i
].Luid
.LowPart
== PrivilegeLuid
.LowPart
)
420 MyFree(lpPrivileges
);
426 /**************************************************************************
427 * pSetupEnablePrivilege [SETUPAPI.@]
429 * Enables or disables one of the current users privileges.
432 * lpPrivilegeName [I] Name of the privilege to be changed
433 * bEnable [I] TRUE: Enables the privilege
434 * FALSE: Disables the privilege
440 BOOL WINAPI
pSetupEnablePrivilege(LPCWSTR lpPrivilegeName
, BOOL bEnable
)
442 TOKEN_PRIVILEGES Privileges
;
446 TRACE("%s %s\n", debugstr_w(lpPrivilegeName
), bEnable
? "TRUE" : "FALSE");
448 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
451 Privileges
.PrivilegeCount
= 1;
452 Privileges
.Privileges
[0].Attributes
= (bEnable
) ? SE_PRIVILEGE_ENABLED
: 0;
454 if (!LookupPrivilegeValueW(NULL
, lpPrivilegeName
,
455 &Privileges
.Privileges
[0].Luid
))
461 bResult
= AdjustTokenPrivileges(hToken
, FALSE
, &Privileges
, 0, NULL
, NULL
);
469 /**************************************************************************
470 * DelayedMove [SETUPAPI.@]
472 * Moves a file upon the next reboot.
475 * lpExistingFileName [I] Current file name
476 * lpNewFileName [I] New file name
482 BOOL WINAPI
DelayedMove(LPCWSTR lpExistingFileName
, LPCWSTR lpNewFileName
)
484 return MoveFileExW(lpExistingFileName
, lpNewFileName
,
485 MOVEFILE_REPLACE_EXISTING
| MOVEFILE_DELAY_UNTIL_REBOOT
);
489 /**************************************************************************
490 * FileExists [SETUPAPI.@]
492 * Checks whether a file exists.
495 * lpFileName [I] Name of the file to check
496 * lpNewFileName [O] Optional information about the existing file
502 BOOL WINAPI
FileExists(LPCWSTR lpFileName
, LPWIN32_FIND_DATAW lpFileFindData
)
504 WIN32_FIND_DATAW FindData
;
509 uErrorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
511 hFind
= FindFirstFileW(lpFileName
, &FindData
);
512 if (hFind
== INVALID_HANDLE_VALUE
)
514 dwError
= GetLastError();
515 SetErrorMode(uErrorMode
);
516 SetLastError(dwError
);
523 memcpy(lpFileFindData
, &FindData
, sizeof(WIN32_FIND_DATAW
));
525 SetErrorMode(uErrorMode
);
531 /**************************************************************************
532 * CaptureStringArg [SETUPAPI.@]
534 * Captures a UNICODE string.
537 * lpSrc [I] UNICODE string to be captured
538 * lpDst [O] Pointer to the captured UNICODE string
541 * Success: ERROR_SUCCESS
542 * Failure: ERROR_INVALID_PARAMETER
545 * Call MyFree to release the captured UNICODE string.
547 DWORD WINAPI
CaptureStringArg(LPCWSTR pSrc
, LPWSTR
*pDst
)
550 return ERROR_INVALID_PARAMETER
;
552 *pDst
= pSetupDuplicateString(pSrc
);
554 return ERROR_SUCCESS
;
558 /**************************************************************************
559 * pSetupCaptureAndConvertAnsiArg [SETUPAPI.@]
561 * Captures an ANSI string and converts it to a UNICODE string.
564 * lpSrc [I] ANSI string to be captured
565 * lpDst [O] Pointer to the captured UNICODE string
568 * Success: ERROR_SUCCESS
569 * Failure: ERROR_INVALID_PARAMETER
572 * Call MyFree to release the captured UNICODE string.
574 DWORD WINAPI
pSetupCaptureAndConvertAnsiArg(LPCSTR pSrc
, LPWSTR
*pDst
)
577 return ERROR_INVALID_PARAMETER
;
579 *pDst
= pSetupMultiByteToUnicode(pSrc
, CP_ACP
);
581 return ERROR_SUCCESS
;
585 /**************************************************************************
586 * pSetupOpenAndMapFileForRead [SETUPAPI.@]
588 * Open and map a file to a buffer.
591 * lpFileName [I] Name of the file to be opened
592 * lpSize [O] Pointer to the file size
593 * lpFile [0] Pointer to the file handle
594 * lpMapping [0] Pointer to the mapping handle
595 * lpBuffer [0] Pointer to the file buffer
598 * Success: ERROR_SUCCESS
602 * Call UnmapAndCloseFile to release the file.
604 DWORD WINAPI
pSetupOpenAndMapFileForRead(LPCWSTR lpFileName
,
612 TRACE("%s %p %p %p %p\n",
613 debugstr_w(lpFileName
), lpSize
, lpFile
, lpMapping
, lpBuffer
);
615 *lpFile
= CreateFileW(lpFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
616 OPEN_EXISTING
, 0, NULL
);
617 if (*lpFile
== INVALID_HANDLE_VALUE
)
618 return GetLastError();
620 *lpSize
= GetFileSize(*lpFile
, NULL
);
621 if (*lpSize
== INVALID_FILE_SIZE
)
623 dwError
= GetLastError();
624 CloseHandle(*lpFile
);
628 *lpMapping
= CreateFileMappingW(*lpFile
, NULL
, PAGE_READONLY
, 0,
630 if (*lpMapping
== NULL
)
632 dwError
= GetLastError();
633 CloseHandle(*lpFile
);
637 *lpBuffer
= MapViewOfFile(*lpMapping
, FILE_MAP_READ
, 0, 0, *lpSize
);
638 if (*lpBuffer
== NULL
)
640 dwError
= GetLastError();
641 CloseHandle(*lpMapping
);
642 CloseHandle(*lpFile
);
646 return ERROR_SUCCESS
;
650 /**************************************************************************
651 * pSetupUnmapAndCloseFile [SETUPAPI.@]
653 * Unmap and close a mapped file.
656 * hFile [I] Handle to the file
657 * hMapping [I] Handle to the file mapping
658 * lpBuffer [I] Pointer to the file buffer
664 BOOL WINAPI
pSetupUnmapAndCloseFile(HANDLE hFile
, HANDLE hMapping
, LPVOID lpBuffer
)
667 hFile
, hMapping
, lpBuffer
);
669 if (!UnmapViewOfFile(lpBuffer
))
672 if (!CloseHandle(hMapping
))
675 if (!CloseHandle(hFile
))
682 /**************************************************************************
683 * StampFileSecurity [SETUPAPI.@]
685 * Assign a new security descriptor to the given file.
688 * lpFileName [I] Name of the file
689 * pSecurityDescriptor [I] New security descriptor
692 * Success: ERROR_SUCCESS
695 DWORD WINAPI
StampFileSecurity(LPCWSTR lpFileName
, PSECURITY_DESCRIPTOR pSecurityDescriptor
)
697 TRACE("%s %p\n", debugstr_w(lpFileName
), pSecurityDescriptor
);
699 if (!SetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
700 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
701 pSecurityDescriptor
))
702 return GetLastError();
704 return ERROR_SUCCESS
;
708 /**************************************************************************
709 * TakeOwnershipOfFile [SETUPAPI.@]
711 * Takes the ownership of the given file.
714 * lpFileName [I] Name of the file
717 * Success: ERROR_SUCCESS
720 DWORD WINAPI
TakeOwnershipOfFile(LPCWSTR lpFileName
)
722 SECURITY_DESCRIPTOR SecDesc
;
723 HANDLE hToken
= NULL
;
724 PTOKEN_OWNER pOwner
= NULL
;
728 TRACE("%s\n", debugstr_w(lpFileName
));
730 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
731 return GetLastError();
733 if (!GetTokenInformation(hToken
, TokenOwner
, NULL
, 0, &dwSize
))
738 pOwner
= (PTOKEN_OWNER
)MyMalloc(dwSize
);
742 return ERROR_NOT_ENOUGH_MEMORY
;
745 if (!GetTokenInformation(hToken
, TokenOwner
, pOwner
, dwSize
, &dwSize
))
750 if (!InitializeSecurityDescriptor(&SecDesc
, SECURITY_DESCRIPTOR_REVISION
))
755 if (!SetSecurityDescriptorOwner(&SecDesc
, pOwner
->Owner
, FALSE
))
760 if (!SetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
, &SecDesc
))
768 return ERROR_SUCCESS
;
771 dwError
= GetLastError();
782 /**************************************************************************
783 * RetreiveFileSecurity [SETUPAPI.@]
785 * Retrieve the security descriptor that is associated with the given file.
788 * lpFileName [I] Name of the file
791 * Success: ERROR_SUCCESS
794 DWORD WINAPI
RetreiveFileSecurity(LPCWSTR lpFileName
,
795 PSECURITY_DESCRIPTOR
*pSecurityDescriptor
)
797 PSECURITY_DESCRIPTOR SecDesc
;
798 DWORD dwSize
= 0x100;
801 TRACE("%s %p\n", debugstr_w(lpFileName
), pSecurityDescriptor
);
803 SecDesc
= MyMalloc(dwSize
);
805 return ERROR_NOT_ENOUGH_MEMORY
;
807 if (GetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
808 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
809 SecDesc
, dwSize
, &dwSize
))
811 *pSecurityDescriptor
= SecDesc
;
812 return ERROR_SUCCESS
;
815 dwError
= GetLastError();
816 if (dwError
!= ERROR_INSUFFICIENT_BUFFER
)
822 SecDesc
= MyRealloc(SecDesc
, dwSize
);
824 return ERROR_NOT_ENOUGH_MEMORY
;
826 if (GetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
827 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
828 SecDesc
, dwSize
, &dwSize
))
830 *pSecurityDescriptor
= SecDesc
;
831 return ERROR_SUCCESS
;
834 dwError
= GetLastError();
842 * See: https://msdn.microsoft.com/en-us/library/bb432397(v=vs.85).aspx
843 * for more information.
845 DWORD GlobalSetupFlags
= 0;
847 /***********************************************************************
848 * pSetupGetGlobalFlags (SETUPAPI.@)
850 DWORD WINAPI
pSetupGetGlobalFlags(void)
852 return GlobalSetupFlags
;
855 /***********************************************************************
856 * pSetupModifyGlobalFlags (SETUPAPI.@)
858 void WINAPI
pSetupModifyGlobalFlags( DWORD mask
, DWORD flags
)
861 GlobalSetupFlags
= (GlobalSetupFlags
& ~mask
) | (flags
& mask
);
864 /***********************************************************************
865 * pSetupSetGlobalFlags (SETUPAPI.@)
867 void WINAPI
pSetupSetGlobalFlags( DWORD flags
)
869 pSetupModifyGlobalFlags(0xFFFFFFFF, flags
);
873 /***********************************************************************
874 * AssertFail (SETUPAPI.@)
876 * Shows an assert fail error messagebox
879 * lpFile [I] file where assert failed
880 * uLine [I] line number in file
881 * lpMessage [I] assert message
884 VOID WINAPI
AssertFail(LPSTR lpFile
, UINT uLine
, LPSTR lpMessage
)
886 CHAR szModule
[MAX_PATH
];
891 TRACE("%s %u %s\n", lpFile
, uLine
, lpMessage
);
893 GetModuleFileNameA(hInstance
, szModule
, MAX_PATH
);
894 lpName
= strrchr(szModule
, '\\');
901 "Assertion failure at line %u in file %s: %s\n\nCall DebugBreak()?",
902 uLine
, lpFile
, lpMessage
);
904 if (MessageBoxA(NULL
, szBuffer
, lpName
, MB_SETFOREGROUND
|
905 MB_TASKMODAL
| MB_ICONERROR
| MB_YESNO
) == IDYES
)
910 /**************************************************************************
911 * GetSetFileTimestamp [SETUPAPI.@]
913 * Gets or sets a files timestamp.
916 * lpFileName [I] File name
917 * lpCreationTime [I/O] Creation time
918 * lpLastAccessTime [I/O] Last access time
919 * lpLastWriteTime [I/O] Last write time
920 * bSetFileTime [I] TRUE: Set file times
921 * FALSE: Get file times
924 * Success: ERROR_SUCCESS
927 DWORD WINAPI
GetSetFileTimestamp(LPCWSTR lpFileName
,
928 LPFILETIME lpCreationTime
,
929 LPFILETIME lpLastAccessTime
,
930 LPFILETIME lpLastWriteTime
,
931 BOOLEAN bSetFileTime
)
935 DWORD dwError
= ERROR_SUCCESS
;
937 TRACE("%s %p %p %p %x\n", debugstr_w(lpFileName
), lpCreationTime
,
938 lpLastAccessTime
, lpLastWriteTime
, bSetFileTime
);
940 hFile
= CreateFileW(lpFileName
,
941 bSetFileTime
? GENERIC_WRITE
: GENERIC_READ
,
942 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
948 if (hFile
== INVALID_HANDLE_VALUE
)
949 return GetLastError();
952 bRet
= SetFileTime(hFile
, lpCreationTime
, lpLastAccessTime
, lpLastWriteTime
);
954 bRet
= GetFileTime(hFile
, lpCreationTime
, lpLastAccessTime
, lpLastWriteTime
);
957 dwError
= GetLastError();
965 /**************************************************************************
966 * pSetupGetFileTitle [SETUPAPI.@]
968 * Returns a pointer to the last part of a fully qualified file name.
971 * lpFileName [I] File name
974 * Pointer to a files name.
977 pSetupGetFileTitle(LPCWSTR lpFileName
)
983 TRACE("%s\n", debugstr_w(lpFileName
));
985 ptr
= (LPWSTR
)lpFileName
;
995 if (c
== (WCHAR
)'\\' || c
== (WCHAR
)'/' || c
== (WCHAR
)':')
1003 /**************************************************************************
1004 * pSetupConcatenatePaths [SETUPAPI.@]
1006 * Concatenates two paths.
1009 * lpPath [I/O] Path to append path to
1010 * lpAppend [I] Path to append
1011 * dwBufferSize [I] Size of the path buffer
1012 * lpRequiredSize [O] Required size for the concatenated path. Optional
1019 pSetupConcatenatePaths(LPWSTR lpPath
,
1022 LPDWORD lpRequiredSize
)
1027 BOOL bBackslash
= FALSE
;
1029 TRACE("%s %s %lu %p\n", debugstr_w(lpPath
), debugstr_w(lpAppend
),
1030 dwBufferSize
, lpRequiredSize
);
1032 dwPathSize
= lstrlenW(lpPath
);
1034 /* Ignore trailing backslash */
1035 if (lpPath
[dwPathSize
- 1] == (WCHAR
)'\\')
1038 dwAppendSize
= lstrlenW(lpAppend
);
1040 /* Does the source string have a leading backslash? */
1041 if (lpAppend
[0] == (WCHAR
)'\\')
1047 dwTotalSize
= dwPathSize
+ dwAppendSize
+ 2;
1048 if (lpRequiredSize
!= NULL
)
1049 *lpRequiredSize
= dwTotalSize
;
1051 /* Append a backslash to the destination string */
1052 if (bBackslash
== FALSE
)
1054 if (dwPathSize
< dwBufferSize
)
1056 lpPath
[dwPathSize
- 1] = (WCHAR
)'\\';
1061 if (dwPathSize
+ dwAppendSize
< dwBufferSize
)
1063 lstrcpynW(&lpPath
[dwPathSize
],
1068 if (dwBufferSize
>= dwTotalSize
)
1069 lpPath
[dwTotalSize
- 1] = 0;
1071 return (dwBufferSize
>= dwTotalSize
);
1075 /**************************************************************************
1076 * pSetupCenterWindowRelativeToParent [SETUPAPI.@]
1078 * Centers a window relative to its parent.
1081 * hwnd [I] Window to center.
1087 pSetupCenterWindowRelativeToParent(HWND hwnd
)
1093 INT nWindowWidth
, nWindowHeight
;
1094 INT nOwnerWidth
, nOwnerHeight
;
1097 hwndOwner
= GetWindow(hwnd
, GW_OWNER
);
1098 if (hwndOwner
== NULL
)
1103 ClientToScreen(hwndOwner
, &ptOrigin
);
1105 GetWindowRect(hwnd
, &rcWindow
);
1106 GetClientRect(hwndOwner
, &rcOwner
);
1108 nWindowWidth
= rcWindow
.right
- rcWindow
.left
;
1109 nWindowHeight
= rcWindow
.bottom
- rcWindow
.top
;
1111 nOwnerWidth
= rcOwner
.right
- rcOwner
.left
;
1112 nOwnerHeight
= rcOwner
.bottom
- rcOwner
.top
;
1114 posX
= ((nOwnerWidth
- nWindowWidth
) / 2) + ptOrigin
.x
;
1115 posY
= ((nOwnerHeight
- nWindowHeight
) / 2) + ptOrigin
.y
;
1117 MoveWindow(hwnd
, posX
, posY
, nWindowWidth
, nWindowHeight
, 0);
1121 /**************************************************************************
1122 * pSetupGetVersionInfoFromImage [SETUPAPI.@]
1124 * Retrieves version information for a given file.
1127 * lpFileName [I] File name
1128 * lpFileVersion [O] Pointer to the full file version
1129 * lpVersionVarSize [O] Pointer to the size of the variable version
1137 pSetupGetVersionInfoFromImage(LPWSTR lpFileName
,
1138 PULARGE_INTEGER lpFileVersion
,
1139 LPWORD lpVersionVarSize
)
1145 VS_FIXEDFILEINFO
*lpFixedInfo
;
1148 dwSize
= GetFileVersionInfoSizeW(lpFileName
, &dwHandle
);
1152 lpInfo
= MyMalloc(dwSize
);
1156 if (!GetFileVersionInfoW(lpFileName
, 0, dwSize
, lpInfo
))
1162 if (!VerQueryValueW(lpInfo
, BackSlash
,
1163 (LPVOID
*)&lpFixedInfo
, &uSize
))
1169 lpFileVersion
->LowPart
= lpFixedInfo
->dwFileVersionLS
;
1170 lpFileVersion
->HighPart
= lpFixedInfo
->dwFileVersionMS
;
1172 *lpVersionVarSize
= 0;
1173 if (!VerQueryValueW(lpInfo
, TranslationRegKey
,
1174 (LPVOID
*)&lpVarSize
, &uSize
))
1182 *lpVersionVarSize
= *lpVarSize
;
1190 /***********************************************************************
1191 * SetupUninstallOEMInfW (SETUPAPI.@)
1193 BOOL WINAPI
SetupUninstallOEMInfW( PCWSTR inf_file
, DWORD flags
, PVOID reserved
)
1195 static const WCHAR infW
[] = {'\\','i','n','f','\\',0};
1196 WCHAR target
[MAX_PATH
];
1198 TRACE("%s, 0x%08x, %p\n", debugstr_w(inf_file
), flags
, reserved
);
1202 SetLastError(ERROR_INVALID_PARAMETER
);
1206 if (!GetWindowsDirectoryW( target
, sizeof(target
)/sizeof(WCHAR
) )) return FALSE
;
1208 strcatW( target
, infW
);
1209 strcatW( target
, inf_file
);
1211 if (flags
& SUOI_FORCEDELETE
)
1212 return DeleteFileW(target
);
1214 FIXME("not deleting %s\n", debugstr_w(target
));
1219 /***********************************************************************
1220 * SetupUninstallOEMInfA (SETUPAPI.@)
1222 BOOL WINAPI
SetupUninstallOEMInfA( PCSTR inf_file
, DWORD flags
, PVOID reserved
)
1225 WCHAR
*inf_fileW
= NULL
;
1227 TRACE("%s, 0x%08x, %p\n", debugstr_a(inf_file
), flags
, reserved
);
1229 if (inf_file
&& !(inf_fileW
= strdupAtoW( inf_file
))) return FALSE
;
1230 ret
= SetupUninstallOEMInfW( inf_fileW
, flags
, reserved
);
1231 HeapFree( GetProcessHeap(), 0, inf_fileW
);
1235 /***********************************************************************
1236 * InstallCatalog (SETUPAPI.@)
1238 DWORD WINAPI
InstallCatalog( LPCSTR catalog
, LPCSTR basename
, LPSTR fullname
)
1240 FIXME("%s, %s, %p\n", debugstr_a(catalog
), debugstr_a(basename
), fullname
);
1244 /***********************************************************************
1245 * pSetupInstallCatalog (SETUPAPI.@)
1247 DWORD WINAPI
pSetupInstallCatalog( LPCWSTR catalog
, LPCWSTR basename
, LPWSTR fullname
)
1252 TRACE ("%s, %s, %p\n", debugstr_w(catalog
), debugstr_w(basename
), fullname
);
1254 if (!CryptCATAdminAcquireContext(&admin
,NULL
,0))
1255 return GetLastError();
1257 if (!(cat
= CryptCATAdminAddCatalog( admin
, (PWSTR
)catalog
, (PWSTR
)basename
, 0 )))
1259 DWORD rc
= GetLastError();
1260 CryptCATAdminReleaseContext(admin
, 0);
1263 CryptCATAdminReleaseCatalogContext(admin
, cat
, 0);
1264 CryptCATAdminReleaseContext(admin
,0);
1267 FIXME("not returning full installed catalog path\n");
1272 static UINT
detect_compression_type( LPCWSTR file
)
1276 UINT type
= FILE_COMPRESSION_NONE
;
1277 static const BYTE LZ_MAGIC
[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
1278 static const BYTE MSZIP_MAGIC
[] = { 0x4b, 0x57, 0x41, 0x4a };
1279 static const BYTE NTCAB_MAGIC
[] = { 0x4d, 0x53, 0x43, 0x46 };
1282 handle
= CreateFileW( file
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1283 if (handle
== INVALID_HANDLE_VALUE
)
1285 ERR("cannot open file %s\n", debugstr_w(file
));
1286 return FILE_COMPRESSION_NONE
;
1288 if (!ReadFile( handle
, buffer
, sizeof(buffer
), &size
, NULL
) || size
!= sizeof(buffer
))
1290 CloseHandle( handle
);
1291 return FILE_COMPRESSION_NONE
;
1293 if (!memcmp( buffer
, LZ_MAGIC
, sizeof(LZ_MAGIC
) )) type
= FILE_COMPRESSION_WINLZA
;
1294 else if (!memcmp( buffer
, MSZIP_MAGIC
, sizeof(MSZIP_MAGIC
) )) type
= FILE_COMPRESSION_MSZIP
;
1295 else if (!memcmp( buffer
, NTCAB_MAGIC
, sizeof(NTCAB_MAGIC
) )) type
= FILE_COMPRESSION_MSZIP
; /* not a typo */
1297 CloseHandle( handle
);
1301 static BOOL
get_file_size( LPCWSTR file
, DWORD
*size
)
1305 handle
= CreateFileW( file
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1306 if (handle
== INVALID_HANDLE_VALUE
)
1308 ERR("cannot open file %s\n", debugstr_w(file
));
1311 *size
= GetFileSize( handle
, NULL
);
1312 CloseHandle( handle
);
1316 static BOOL
get_file_sizes_none( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1320 if (!get_file_size( source
, &size
)) return FALSE
;
1321 if (source_size
) *source_size
= size
;
1322 if (target_size
) *target_size
= size
;
1326 static BOOL
get_file_sizes_lz( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1333 if (!get_file_size( source
, &size
)) ret
= FALSE
;
1334 else *source_size
= size
;
1341 if ((file
= LZOpenFileW( (LPWSTR
)source
, &of
, OF_READ
)) < 0)
1343 ERR("cannot open source file for reading\n");
1346 *target_size
= LZSeek( file
, 0, 2 );
1352 static UINT CALLBACK
file_compression_info_callback( PVOID context
, UINT notification
, UINT_PTR param1
, UINT_PTR param2
)
1354 DWORD
*size
= context
;
1355 FILE_IN_CABINET_INFO_W
*info
= (FILE_IN_CABINET_INFO_W
*)param1
;
1357 switch (notification
)
1359 case SPFILENOTIFY_FILEINCABINET
:
1361 *size
= info
->FileSize
;
1364 default: return NO_ERROR
;
1368 static BOOL
get_file_sizes_cab( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1375 if (!get_file_size( source
, &size
)) ret
= FALSE
;
1376 else *source_size
= size
;
1380 ret
= SetupIterateCabinetW( source
, 0, file_compression_info_callback
, target_size
);
1385 /***********************************************************************
1386 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1388 * See SetupGetFileCompressionInfoExW.
1390 BOOL WINAPI
SetupGetFileCompressionInfoExA( PCSTR source
, PSTR name
, DWORD len
, PDWORD required
,
1391 PDWORD source_size
, PDWORD target_size
, PUINT type
)
1394 WCHAR
*nameW
= NULL
, *sourceW
= NULL
;
1398 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source
), name
, len
, required
,
1399 source_size
, target_size
, type
);
1401 if (!source
|| !(sourceW
= pSetupMultiByteToUnicode( source
, CP_ACP
))) return FALSE
;
1405 ret
= SetupGetFileCompressionInfoExW( sourceW
, NULL
, 0, &nb_chars
, NULL
, NULL
, NULL
);
1406 if (!(nameW
= HeapAlloc( GetProcessHeap(), 0, nb_chars
* sizeof(WCHAR
) )))
1412 ret
= SetupGetFileCompressionInfoExW( sourceW
, nameW
, nb_chars
, &nb_chars
, source_size
, target_size
, type
);
1415 if ((nameA
= pSetupUnicodeToMultiByte( nameW
, CP_ACP
)))
1417 if (name
&& len
>= nb_chars
) lstrcpyA( name
, nameA
);
1420 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1426 if (required
) *required
= nb_chars
;
1427 HeapFree( GetProcessHeap(), 0, nameW
);
1433 /***********************************************************************
1434 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1436 * Get compression type and compressed/uncompressed sizes of a given file.
1439 * source [I] File to examine.
1440 * name [O] Actual filename used.
1441 * len [I] Length in characters of 'name' buffer.
1442 * required [O] Number of characters written to 'name'.
1443 * source_size [O] Size of compressed file.
1444 * target_size [O] Size of uncompressed file.
1445 * type [O] Compression type.
1451 BOOL WINAPI
SetupGetFileCompressionInfoExW( PCWSTR source
, PWSTR name
, DWORD len
, PDWORD required
,
1452 PDWORD source_size
, PDWORD target_size
, PUINT type
)
1458 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source
), name
, len
, required
,
1459 source_size
, target_size
, type
);
1461 if (!source
) return FALSE
;
1463 source_len
= lstrlenW( source
) + 1;
1464 if (required
) *required
= source_len
;
1465 if (name
&& len
>= source_len
)
1467 lstrcpyW( name
, source
);
1472 comp
= detect_compression_type( source
);
1473 if (type
) *type
= comp
;
1477 case FILE_COMPRESSION_MSZIP
:
1478 case FILE_COMPRESSION_NTCAB
: ret
= get_file_sizes_cab( source
, source_size
, target_size
); break;
1479 case FILE_COMPRESSION_NONE
: ret
= get_file_sizes_none( source
, source_size
, target_size
); break;
1480 case FILE_COMPRESSION_WINLZA
: ret
= get_file_sizes_lz( source
, source_size
, target_size
); break;
1486 /***********************************************************************
1487 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1489 * See SetupGetFileCompressionInfoW.
1491 DWORD WINAPI
SetupGetFileCompressionInfoA( PCSTR source
, PSTR
*name
, PDWORD source_size
,
1492 PDWORD target_size
, PUINT type
)
1495 DWORD error
, required
;
1498 TRACE("%s, %p, %p, %p, %p\n", debugstr_a(source
), name
, source_size
, target_size
, type
);
1500 if (!source
|| !name
|| !source_size
|| !target_size
|| !type
)
1501 return ERROR_INVALID_PARAMETER
;
1503 ret
= SetupGetFileCompressionInfoExA( source
, NULL
, 0, &required
, NULL
, NULL
, NULL
);
1504 if (!(actual_name
= MyMalloc( required
))) return ERROR_NOT_ENOUGH_MEMORY
;
1506 ret
= SetupGetFileCompressionInfoExA( source
, actual_name
, required
, &required
,
1507 source_size
, target_size
, type
);
1510 error
= GetLastError();
1511 MyFree( actual_name
);
1514 *name
= actual_name
;
1515 return ERROR_SUCCESS
;
1518 /***********************************************************************
1519 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1521 * Get compression type and compressed/uncompressed sizes of a given file.
1524 * source [I] File to examine.
1525 * name [O] Actual filename used.
1526 * source_size [O] Size of compressed file.
1527 * target_size [O] Size of uncompressed file.
1528 * type [O] Compression type.
1531 * Success: ERROR_SUCCESS
1532 * Failure: Win32 error code.
1534 DWORD WINAPI
SetupGetFileCompressionInfoW( PCWSTR source
, PWSTR
*name
, PDWORD source_size
,
1535 PDWORD target_size
, PUINT type
)
1538 DWORD error
, required
;
1541 TRACE("%s, %p, %p, %p, %p\n", debugstr_w(source
), name
, source_size
, target_size
, type
);
1543 if (!source
|| !name
|| !source_size
|| !target_size
|| !type
)
1544 return ERROR_INVALID_PARAMETER
;
1546 ret
= SetupGetFileCompressionInfoExW( source
, NULL
, 0, &required
, NULL
, NULL
, NULL
);
1547 if (!(actual_name
= MyMalloc( required
*sizeof(WCHAR
) ))) return ERROR_NOT_ENOUGH_MEMORY
;
1549 ret
= SetupGetFileCompressionInfoExW( source
, actual_name
, required
, &required
,
1550 source_size
, target_size
, type
);
1553 error
= GetLastError();
1554 MyFree( actual_name
);
1557 *name
= actual_name
;
1558 return ERROR_SUCCESS
;
1561 static DWORD
decompress_file_lz( LPCWSTR source
, LPCWSTR target
)
1568 if ((src
= LZOpenFileW( (LPWSTR
)source
, &sof
, OF_READ
)) < 0)
1570 ERR("cannot open source file for reading\n");
1571 return ERROR_FILE_NOT_FOUND
;
1573 if ((dst
= LZOpenFileW( (LPWSTR
)target
, &dof
, OF_CREATE
)) < 0)
1575 ERR("cannot open target file for writing\n");
1577 return ERROR_FILE_NOT_FOUND
;
1579 if ((error
= LZCopy( src
, dst
)) >= 0) ret
= ERROR_SUCCESS
;
1582 WARN("failed to decompress file %d\n", error
);
1583 ret
= ERROR_INVALID_DATA
;
1591 struct callback_context
1597 static UINT CALLBACK
decompress_or_copy_callback( PVOID context
, UINT notification
, UINT_PTR param1
, UINT_PTR param2
)
1599 struct callback_context
*context_info
= context
;
1600 FILE_IN_CABINET_INFO_W
*info
= (FILE_IN_CABINET_INFO_W
*)param1
;
1602 switch (notification
)
1604 case SPFILENOTIFY_FILEINCABINET
:
1606 if (context_info
->has_extracted
)
1607 return FILEOP_ABORT
;
1609 TRACE("Requesting extraction of cabinet file %s\n",
1610 wine_dbgstr_w(info
->NameInCabinet
));
1611 strcpyW( info
->FullTargetName
, context_info
->target
);
1612 context_info
->has_extracted
= TRUE
;
1615 default: return NO_ERROR
;
1619 static DWORD
decompress_file_cab( LPCWSTR source
, LPCWSTR target
)
1621 struct callback_context context
= {0, target
};
1624 ret
= SetupIterateCabinetW( source
, 0, decompress_or_copy_callback
, &context
);
1626 if (ret
) return ERROR_SUCCESS
;
1627 else return GetLastError();
1630 /***********************************************************************
1631 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1633 * See SetupDecompressOrCopyFileW.
1635 DWORD WINAPI
SetupDecompressOrCopyFileA( PCSTR source
, PCSTR target
, PUINT type
)
1638 WCHAR
*sourceW
= NULL
, *targetW
= NULL
;
1640 if (source
&& !(sourceW
= pSetupMultiByteToUnicode( source
, CP_ACP
))) return FALSE
;
1641 if (target
&& !(targetW
= pSetupMultiByteToUnicode( target
, CP_ACP
)))
1644 return ERROR_NOT_ENOUGH_MEMORY
;
1647 ret
= SetupDecompressOrCopyFileW( sourceW
, targetW
, type
);
1655 /***********************************************************************
1656 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1658 * Copy a file and decompress it if needed.
1661 * source [I] File to copy.
1662 * target [I] Filename of the copy.
1663 * type [I] Compression type.
1666 * Success: ERROR_SUCCESS
1667 * Failure: Win32 error code.
1669 DWORD WINAPI
SetupDecompressOrCopyFileW( PCWSTR source
, PCWSTR target
, PUINT type
)
1672 DWORD ret
= ERROR_INVALID_PARAMETER
;
1674 if (!source
|| !target
) return ERROR_INVALID_PARAMETER
;
1676 if (!type
) comp
= detect_compression_type( source
);
1681 case FILE_COMPRESSION_NONE
:
1682 if (CopyFileW( source
, target
, FALSE
)) ret
= ERROR_SUCCESS
;
1683 else ret
= GetLastError();
1685 case FILE_COMPRESSION_WINLZA
:
1686 ret
= decompress_file_lz( source
, target
);
1688 case FILE_COMPRESSION_NTCAB
:
1689 case FILE_COMPRESSION_MSZIP
:
1690 ret
= decompress_file_cab( source
, target
);
1693 WARN("unknown compression type %d\n", comp
);
1697 TRACE("%s -> %s %d\n", debugstr_w(source
), debugstr_w(target
), comp
);
1702 * implemented (used by pSetupGuidFromString)
1704 static BOOL
TrimGuidString(PCWSTR szString
, LPWSTR szNewString
)
1709 if (wcslen(szString
) == 38)
1711 if ((szString
[0] == L
'{') && (szString
[37] == L
'}'))
1713 for (Index
= 0; Index
< wcslen(szString
); Index
++)
1714 szBuffer
[Index
] = szString
[Index
+ 1];
1716 szBuffer
[36] = L
'\0';
1717 wcscpy(szNewString
, szBuffer
);
1721 szNewString
[0] = L
'\0';
1730 pSetupGuidFromString(PCWSTR pString
, LPGUID lpGUID
)
1735 if (!TrimGuidString(pString
, szBuffer
))
1737 return RPC_S_INVALID_STRING_UUID
;
1740 Status
= UuidFromStringW(szBuffer
, lpGUID
);
1741 if (Status
!= RPC_S_OK
)
1743 return RPC_S_INVALID_STRING_UUID
;
1754 pSetupStringFromGuid(LPGUID lpGUID
, PWSTR pString
, DWORD dwStringLen
)
1760 if (dwStringLen
< 39)
1762 return ERROR_INSUFFICIENT_BUFFER
;
1765 Status
= UuidToStringW(lpGUID
, &rpcBuffer
);
1766 if (Status
!= RPC_S_OK
)
1771 wcscpy(szBuffer
, L
"{");
1772 wcscat(szBuffer
, rpcBuffer
);
1773 wcscat(szBuffer
, L
"}");
1775 wcscpy(pString
, szBuffer
);
1777 RpcStringFreeW(&rpcBuffer
);
1786 pSetupIsGuidNull(LPGUID lpGUID
)
1788 return IsEqualGUID(lpGUID
, &GUID_NULL
);
1796 pSetupIsUserAdmin(VOID
)
1798 SID_IDENTIFIER_AUTHORITY Authority
= {SECURITY_NT_AUTHORITY
};
1799 BOOL bResult
= FALSE
;
1802 if (!AllocateAndInitializeSid(&Authority
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1803 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0,
1809 if (!CheckTokenMembership(NULL
, lpSid
, &bResult
))
1819 /***********************************************************************
1820 * SetupInitializeFileLogW(SETUPAPI.@)
1822 HSPFILELOG WINAPI
SetupInitializeFileLogW(LPCWSTR LogFileName
, DWORD Flags
)
1824 struct FileLog
* Log
;
1826 WCHAR Windir
[MAX_PATH
];
1829 TRACE("%s, 0x%x\n",debugstr_w(LogFileName
),Flags
);
1831 if (Flags
& SPFILELOG_SYSTEMLOG
)
1833 if (!pSetupIsUserAdmin() && !(Flags
& SPFILELOG_QUERYONLY
))
1835 /* insufficient privileges */
1836 SetLastError(ERROR_ACCESS_DENIED
);
1837 return INVALID_HANDLE_VALUE
;
1840 if (LogFileName
|| (Flags
& SPFILELOG_FORCENEW
))
1842 /* invalid parameter */
1843 SetLastError(ERROR_INVALID_PARAMETER
);
1844 return INVALID_HANDLE_VALUE
;
1847 ret
= GetSystemWindowsDirectoryW(Windir
, MAX_PATH
);
1848 if (!ret
|| ret
>= MAX_PATH
)
1850 /* generic failure */
1851 return INVALID_HANDLE_VALUE
;
1855 wcscat(Windir
, L
"repair\\setup.log");
1861 /* invalid parameter */
1862 SetLastError(ERROR_INVALID_PARAMETER
);
1863 return INVALID_HANDLE_VALUE
;
1866 wcsncpy(Windir
, LogFileName
, MAX_PATH
);
1869 if (FileExists(Windir
, NULL
))
1871 /* take ownership */
1872 ret
= TakeOwnershipOfFile(Windir
);
1874 if (ret
!= ERROR_SUCCESS
)
1878 return INVALID_HANDLE_VALUE
;
1881 if (!SetFileAttributesW(Windir
, FILE_ATTRIBUTE_NORMAL
))
1884 return INVALID_HANDLE_VALUE
;
1887 if ((Flags
& SPFILELOG_FORCENEW
))
1889 if (!DeleteFileW(Windir
))
1892 return INVALID_HANDLE_VALUE
;
1898 hLog
= CreateFileW(Windir
,
1899 (Flags
& SPFILELOG_QUERYONLY
) ? GENERIC_READ
: GENERIC_WRITE
,
1900 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1903 FILE_ATTRIBUTE_NORMAL
,
1906 if (hLog
== INVALID_HANDLE_VALUE
)
1909 return INVALID_HANDLE_VALUE
;
1912 /* close log handle */
1915 /* allocate file log struct */
1916 Log
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct FileLog
));
1919 /* not enough memory */
1920 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1921 return INVALID_HANDLE_VALUE
;
1924 /* initialize log */
1925 Log
->LogName
= HeapAlloc(GetProcessHeap(), 0, (wcslen(Windir
)+1) * sizeof(WCHAR
));
1928 /* not enough memory */
1929 HeapFree(GetProcessHeap(), 0, Log
);
1930 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1931 return INVALID_HANDLE_VALUE
;
1934 wcscpy(Log
->LogName
, Windir
);
1935 Log
->ReadOnly
= (Flags
& SPFILELOG_QUERYONLY
);
1936 Log
->SystemLog
= (Flags
& SPFILELOG_SYSTEMLOG
);
1938 return (HSPFILELOG
)Log
;
1941 /***********************************************************************
1942 * SetupInitializeFileLogA(SETUPAPI.@)
1944 HSPFILELOG WINAPI
SetupInitializeFileLogA(LPCSTR LogFileName
, DWORD Flags
)
1947 LPWSTR LogFileNameW
= NULL
;
1949 TRACE("%s, 0x%x\n",debugstr_a(LogFileName
),Flags
);
1953 LogFileNameW
= strdupAtoW(LogFileName
);
1957 /* not enough memory */
1958 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1959 return INVALID_HANDLE_VALUE
;
1962 hLog
= SetupInitializeFileLogW(LogFileNameW
, Flags
);
1963 HeapFree(GetProcessHeap(), 0, LogFileNameW
);
1967 hLog
= SetupInitializeFileLogW(NULL
, Flags
);
1973 /***********************************************************************
1974 * SetupTerminateFileLog(SETUPAPI.@)
1976 BOOL WINAPI
SetupTerminateFileLog(HANDLE FileLogHandle
)
1978 struct FileLog
* Log
;
1980 TRACE ("%p\n",FileLogHandle
);
1982 Log
= (struct FileLog
*)FileLogHandle
;
1984 /* free file log handle */
1985 HeapFree(GetProcessHeap(), 0, Log
->LogName
);
1986 HeapFree(GetProcessHeap(), 0, Log
);
1988 SetLastError(ERROR_SUCCESS
);
1993 /***********************************************************************
1994 * SetupCloseLog(SETUPAPI.@)
1996 void WINAPI
SetupCloseLog(void)
1998 EnterCriticalSection(&setupapi_cs
);
2000 CloseHandle(setupact
);
2001 setupact
= INVALID_HANDLE_VALUE
;
2003 CloseHandle(setuperr
);
2004 setuperr
= INVALID_HANDLE_VALUE
;
2006 LeaveCriticalSection(&setupapi_cs
);
2009 /***********************************************************************
2010 * SetupOpenLog(SETUPAPI.@)
2012 BOOL WINAPI
SetupOpenLog(BOOL reserved
)
2014 WCHAR path
[MAX_PATH
];
2016 static const WCHAR setupactlog
[] = {'\\','s','e','t','u','p','a','c','t','.','l','o','g',0};
2017 static const WCHAR setuperrlog
[] = {'\\','s','e','t','u','p','e','r','r','.','l','o','g',0};
2019 EnterCriticalSection(&setupapi_cs
);
2021 if (setupact
!= INVALID_HANDLE_VALUE
&& setuperr
!= INVALID_HANDLE_VALUE
)
2023 LeaveCriticalSection(&setupapi_cs
);
2027 GetWindowsDirectoryW(path
, MAX_PATH
);
2028 lstrcatW(path
, setupactlog
);
2030 setupact
= CreateFileW(path
, FILE_GENERIC_WRITE
, FILE_SHARE_WRITE
| FILE_SHARE_READ
,
2031 NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2032 if (setupact
== INVALID_HANDLE_VALUE
)
2034 LeaveCriticalSection(&setupapi_cs
);
2038 SetFilePointer(setupact
, 0, NULL
, FILE_END
);
2040 GetWindowsDirectoryW(path
, MAX_PATH
);
2041 lstrcatW(path
, setuperrlog
);
2043 setuperr
= CreateFileW(path
, FILE_GENERIC_WRITE
, FILE_SHARE_WRITE
| FILE_SHARE_READ
,
2044 NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2045 if (setuperr
== INVALID_HANDLE_VALUE
)
2047 CloseHandle(setupact
);
2048 setupact
= INVALID_HANDLE_VALUE
;
2049 LeaveCriticalSection(&setupapi_cs
);
2053 SetFilePointer(setuperr
, 0, NULL
, FILE_END
);
2055 LeaveCriticalSection(&setupapi_cs
);
2060 /***********************************************************************
2061 * SetupLogErrorA(SETUPAPI.@)
2063 BOOL WINAPI
SetupLogErrorA(LPCSTR message
, LogSeverity severity
)
2065 static const char null
[] = "(null)";
2070 EnterCriticalSection(&setupapi_cs
);
2072 if (setupact
== INVALID_HANDLE_VALUE
|| setuperr
== INVALID_HANDLE_VALUE
)
2074 SetLastError(ERROR_FILE_INVALID
);
2079 if (message
== NULL
)
2082 len
= lstrlenA(message
);
2084 ret
= WriteFile(setupact
, message
, len
, &written
, NULL
);
2088 if (severity
>= LogSevMaximum
)
2094 if (severity
> LogSevInformation
)
2095 ret
= WriteFile(setuperr
, message
, len
, &written
, NULL
);
2098 LeaveCriticalSection(&setupapi_cs
);
2102 /***********************************************************************
2103 * SetupLogErrorW(SETUPAPI.@)
2105 BOOL WINAPI
SetupLogErrorW(LPCWSTR message
, LogSeverity severity
)
2113 len
= WideCharToMultiByte(CP_ACP
, 0, message
, -1, NULL
, 0, NULL
, NULL
);
2114 msg
= HeapAlloc(GetProcessHeap(), 0, len
);
2117 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2120 WideCharToMultiByte(CP_ACP
, 0, message
, -1, msg
, len
, NULL
, NULL
);
2123 /* This is the normal way to proceed. The log files are ASCII files
2124 * and W is to be converted.
2126 ret
= SetupLogErrorA(msg
, severity
);
2128 HeapFree(GetProcessHeap(), 0, msg
);