advpack: Forward AddDelBackupEntryA to its unicode counterpart.
[wine/dcerpc.git] / dlls / advpack / files.c
blobdbf6b66cf501b76ce3025c1a597e649751dd5a93
1 /*
2 * Advpack file functions
4 * Copyright 2006 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <stdlib.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "winternl.h"
30 #include "setupapi.h"
31 #include "advpub.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
37 /* converts an ansi double null-terminated list to a unicode list */
38 static LPWSTR ansi_to_unicode_list(LPCSTR ansi_list)
40 DWORD len, wlen = 0;
41 LPWSTR list;
42 LPCSTR ptr = ansi_list;
44 while (*ptr) ptr += lstrlenA(ptr) + 1;
45 len = ptr + 1 - ansi_list;
46 wlen = MultiByteToWideChar(CP_ACP, 0, ansi_list, len, NULL, 0);
47 list = HeapAlloc(GetProcessHeap(), 0, wlen * sizeof(WCHAR));
48 MultiByteToWideChar(CP_ACP, 0, ansi_list, len, list, wlen);
49 return list;
52 /***********************************************************************
53 * AddDelBackupEntryA (ADVPACK.@)
55 * See AddDelBackupEntryW.
57 HRESULT WINAPI AddDelBackupEntryA(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
58 LPCSTR lpcszBaseName, DWORD dwFlags)
60 UNICODE_STRING backupdir, basename;
61 LPWSTR filelist, backup;
62 HRESULT res;
64 TRACE("(%p, %p, %p, %ld)\n", lpcszFileList, lpcszBackupDir,
65 lpcszBaseName, dwFlags);
67 if (lpcszFileList)
68 filelist = ansi_to_unicode_list(lpcszFileList);
69 else
70 filelist = NULL;
72 RtlCreateUnicodeStringFromAsciiz(&backupdir, lpcszBackupDir);
73 RtlCreateUnicodeStringFromAsciiz(&basename, lpcszBaseName);
75 if (lpcszBackupDir)
76 backup = backupdir.Buffer;
77 else
78 backup = NULL;
80 res = AddDelBackupEntryW(filelist, backup, basename.Buffer, dwFlags);
82 HeapFree(GetProcessHeap(), 0, filelist);
84 RtlFreeUnicodeString(&backupdir);
85 RtlFreeUnicodeString(&basename);
87 return res;
90 /***********************************************************************
91 * AddDelBackupEntryW (ADVPACK.@)
93 * Either appends the files in the file list to the backup section of
94 * the specified INI, or deletes the entries from the INI file.
96 * PARAMS
97 * lpcszFileList [I] NULL-separated list of filenames.
98 * lpcszBackupDir [I] Path of the backup directory.
99 * lpcszBaseName [I] Basename of the INI file.
100 * dwFlags [I] AADBE_ADD_ENTRY adds the entries in the file list
101 * to the INI file, while AADBE_DEL_ENTRY removes
102 * the entries from the INI file.
104 * RETURNS
105 * S_OK in all cases.
107 * NOTES
108 * If the INI file does not exist before adding entries to it, the file
109 * will be created.
111 * If lpcszBackupDir is NULL, the INI file is assumed to exist in
112 * c:\windows or created there if it does not exist.
114 HRESULT WINAPI AddDelBackupEntryW(LPCWSTR lpcszFileList, LPCWSTR lpcszBackupDir,
115 LPCWSTR lpcszBaseName, DWORD dwFlags)
117 WCHAR szIniPath[MAX_PATH];
118 LPWSTR szString = NULL;
120 static const WCHAR szBackupEntry[] = {
121 '-','1',',','0',',','0',',','0',',','0',',','0',',','-','1',0
124 static const WCHAR backslash[] = {'\\',0};
125 static const WCHAR ini[] = {'.','i','n','i',0};
126 static const WCHAR backup[] = {'b','a','c','k','u','p',0};
128 TRACE("(%p, %p, %p, %ld)\n", lpcszFileList, lpcszBackupDir,
129 lpcszBaseName, dwFlags);
131 if (!lpcszFileList || !*lpcszFileList)
132 return S_OK;
134 if (lpcszBackupDir)
135 lstrcpyW(szIniPath, lpcszBackupDir);
136 else
137 GetWindowsDirectoryW(szIniPath, MAX_PATH);
139 lstrcatW(szIniPath, backslash);
140 lstrcatW(szIniPath, lpcszBaseName);
141 lstrcatW(szIniPath, ini);
143 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_NORMAL);
145 if (dwFlags & AADBE_ADD_ENTRY)
146 szString = (LPWSTR)szBackupEntry;
147 else if (dwFlags & AADBE_DEL_ENTRY)
148 szString = NULL;
150 /* add or delete the INI entries */
151 while (*lpcszFileList)
153 WritePrivateProfileStringW(backup, lpcszFileList, szString, szIniPath);
154 lpcszFileList += lstrlenW(lpcszFileList) + 1;
157 /* hide the INI file */
158 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
160 return S_OK;
163 /* FIXME: this is only for the local case, X:\ */
164 #define ROOT_LENGTH 3
166 UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
167 UINT_PTR Param1, UINT_PTR Param2)
169 return 1;
172 UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
173 UINT_PTR Param1, UINT_PTR Param2)
175 /* only be verbose for error notifications */
176 if (!Notification ||
177 Notification == SPFILENOTIFY_RENAMEERROR ||
178 Notification == SPFILENOTIFY_DELETEERROR ||
179 Notification == SPFILENOTIFY_COPYERROR)
181 return SetupDefaultQueueCallbackA(Context, Notification,
182 Param1, Param2);
185 return 1;
188 /***********************************************************************
189 * AdvInstallFileA (ADVPACK.@)
191 * Copies a file from the source to a destination.
193 * PARAMS
194 * hwnd [I] Handle to the window used for messages.
195 * lpszSourceDir [I] Source directory.
196 * lpszSourceFile [I] Source filename.
197 * lpszDestDir [I] Destination directory.
198 * lpszDestFile [I] Optional destination filename.
199 * dwFlags [I] See advpub.h.
200 * dwReserved [I] Reserved. Must be 0.
202 * RETURNS
203 * Success: S_OK.
204 * Failure: E_FAIL.
206 * NOTES
207 * If lpszDestFile is NULL, the destination filename is the same as
208 * lpszSourceFIle.
210 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
211 LPCSTR lpszDestDir, LPCSTR lpszDestFile,
212 DWORD dwFlags, DWORD dwReserved)
214 PSP_FILE_CALLBACK_A pFileCallback;
215 LPSTR szPath, szDestFilename;
216 char szRootPath[ROOT_LENGTH];
217 DWORD dwLen, dwLastError;
218 HSPFILEQ fileQueue;
219 PVOID pContext;
221 TRACE("(%p,%p,%p,%p,%p,%ld,%ld)\n", hwnd, debugstr_a(lpszSourceDir),
222 debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
223 debugstr_a(lpszDestFile), dwFlags, dwReserved);
225 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
226 return E_INVALIDARG;
228 fileQueue = SetupOpenFileQueue();
229 if (fileQueue == INVALID_HANDLE_VALUE)
230 return HRESULT_FROM_WIN32(GetLastError());
232 pContext = NULL;
233 dwLastError = ERROR_SUCCESS;
235 lstrcpynA(szRootPath, lpszSourceDir, ROOT_LENGTH);
236 szPath = (LPSTR)lpszSourceDir + ROOT_LENGTH;
238 /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
239 if (lpszDestFile)
241 dwLen = lstrlenA(lpszDestFile);
242 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
243 lstrcpyA(szDestFilename, lpszDestFile);
245 else
247 dwLen = lstrlenA(lpszSourceFile);
248 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
249 lstrcpyA(szDestFilename, lpszSourceFile);
252 /* add the file copy operation to the setup queue */
253 if (!SetupQueueCopyA(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
254 NULL, lpszDestDir, szDestFilename, dwFlags))
256 dwLastError = GetLastError();
257 goto done;
260 pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
261 0, 0, NULL);
262 if (!pContext)
264 dwLastError = GetLastError();
265 goto done;
268 /* don't output anything for AIF_QUIET */
269 if (dwFlags & AIF_QUIET)
270 pFileCallback = pQuietQueueCallback;
271 else
272 pFileCallback = pQueueCallback;
274 /* perform the file copy */
275 if (!SetupCommitFileQueueA(hwnd, fileQueue, pFileCallback, pContext))
277 dwLastError = GetLastError();
278 goto done;
281 done:
282 SetupTermDefaultQueueCallback(pContext);
283 SetupCloseFileQueue(fileQueue);
285 HeapFree(GetProcessHeap(), 0, szDestFilename);
287 return HRESULT_FROM_WIN32(dwLastError);
290 static HRESULT DELNODE_recurse_dirtree(LPSTR fname, DWORD flags)
292 DWORD fattrs = GetFileAttributesA(fname);
293 HRESULT ret = E_FAIL;
295 if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
297 HANDLE hFindFile;
298 WIN32_FIND_DATAA w32fd;
299 BOOL done = TRUE;
300 int fname_len = lstrlenA(fname);
302 /* Generate a path with wildcard suitable for iterating */
303 if (CharPrevA(fname, fname + fname_len) != "\\")
305 lstrcpyA(fname + fname_len, "\\");
306 ++fname_len;
308 lstrcpyA(fname + fname_len, "*");
310 if ((hFindFile = FindFirstFileA(fname, &w32fd)) != INVALID_HANDLE_VALUE)
312 /* Iterate through the files in the directory */
313 for (done = FALSE; !done; done = !FindNextFileA(hFindFile, &w32fd))
315 TRACE("%s\n", w32fd.cFileName);
316 if (lstrcmpA(".", w32fd.cFileName) != 0 &&
317 lstrcmpA("..", w32fd.cFileName) != 0)
319 lstrcpyA(fname + fname_len, w32fd.cFileName);
320 if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
322 break; /* Failure */
326 FindClose(hFindFile);
329 /* We're done with this directory, so restore the old path without wildcard */
330 *(fname + fname_len) = '\0';
332 if (done)
334 TRACE("%s: directory\n", fname);
335 if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryA(fname))
337 ret = S_OK;
341 else
343 TRACE("%s: file\n", fname);
344 if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileA(fname))
346 ret = S_OK;
350 return ret;
353 /***********************************************************************
354 * DelNodeA (ADVPACK.@)
356 * Deletes a file or directory
358 * PARAMS
359 * pszFileOrDirName [I] Name of file or directory to delete
360 * dwFlags [I] Flags; see include/advpub.h
362 * RETURNS
363 * Success: S_OK
364 * Failure: E_FAIL
366 * BUGS
367 * - Ignores flags
368 * - Native version apparently does a lot of checking to make sure
369 * we're not trying to delete a system directory etc.
371 HRESULT WINAPI DelNodeA( LPCSTR pszFileOrDirName, DWORD dwFlags )
373 CHAR fname[MAX_PATH];
374 HRESULT ret = E_FAIL;
376 TRACE("(%s, 0x%08lx)\n", debugstr_a(pszFileOrDirName), dwFlags);
378 if (dwFlags)
379 FIXME("Flags ignored!\n");
381 if (pszFileOrDirName && *pszFileOrDirName)
383 lstrcpyA(fname, pszFileOrDirName);
385 /* TODO: Should check for system directory deletion etc. here */
387 ret = DELNODE_recurse_dirtree(fname, dwFlags);
390 return ret;
393 /* returns the parameter at dwIndex in a list of parameters
394 * separated by the cSeparator character
396 static LPSTR get_parameter(LPSTR szParameters, CHAR cSeparator, DWORD dwIndex)
398 LPSTR szParam = NULL;
399 DWORD i = 0;
401 while (*szParameters && i < dwIndex)
403 if (*szParameters == cSeparator)
404 i++;
406 szParameters++;
409 if (!*szParameters)
410 return NULL;
412 szParam = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szParameters));
413 lstrcpyA(szParam, szParameters);
415 return szParam;
418 /***********************************************************************
419 * DelNodeRunDLL32A (ADVPACK.@)
421 * Deletes a file or directory, WinMain style.
423 * PARAMS
424 * hWnd [I] Handle to the window used for the display.
425 * hInst [I] Instance of the process.
426 * cmdline [I] Contains parameters in the order FileOrDirName,Flags.
427 * show [I] How the window should be shown.
429 * RETURNS
430 * Success: S_OK.
431 * Failure: E_FAIL.
433 HRESULT WINAPI DelNodeRunDLL32A( HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show )
435 LPSTR szFilename, szFlags;
436 DWORD dwFlags;
437 HRESULT res;
439 TRACE("(%s)\n", debugstr_a(cmdline));
441 /* get the parameters at indexes 0 and 1 respectively */
442 szFilename = get_parameter(cmdline, ',', 0);
443 szFlags = get_parameter(cmdline, ',', 1);
445 dwFlags = atol(szFlags);
447 res = DelNodeA(szFilename, dwFlags);
449 HeapFree(GetProcessHeap(), 0, szFilename);
450 HeapFree(GetProcessHeap(), 0, szFlags);
452 return res;
455 /* The following defintions were copied from dlls/cabinet/cabinet.h */
457 /* EXTRACTdest flags */
458 #define EXTRACT_FILLFILELIST 0x00000001
459 #define EXTRACT_EXTRACTFILES 0x00000002
461 struct ExtractFileList {
462 LPSTR filename;
463 struct ExtractFileList *next;
464 BOOL unknown; /* always 1L */
467 /* the first parameter of the function Extract */
468 typedef struct {
469 long result1; /* 0x000 */
470 long unknown1[3]; /* 0x004 */
471 struct ExtractFileList *filelist; /* 0x010 */
472 long filecount; /* 0x014 */
473 DWORD flags; /* 0x018 */
474 char directory[0x104]; /* 0x01c */
475 char lastfile[0x20c]; /* 0x120 */
476 } EXTRACTdest;
478 static HRESULT (WINAPI *pExtract)(EXTRACTdest*, LPCSTR);
480 /* removes legal characters before and after file list, and
481 * converts the file list to a NULL-separated list
483 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
485 DWORD dwLen;
486 char *first = (char *)FileList;
487 char *last = (char *)FileList + strlen(FileList) - 1;
488 LPSTR szConvertedList, temp;
490 /* any number of these chars before the list is OK */
491 while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
492 first++;
494 /* any number of these chars after the list is OK */
495 while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
496 last--;
498 if (first == last)
499 return NULL;
501 dwLen = last - first + 3; /* room for double-null termination */
502 szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
503 lstrcpynA(szConvertedList, first, dwLen - 1);
505 szConvertedList[dwLen - 1] = '\0';
506 szConvertedList[dwLen] = '\0';
508 /* empty list */
509 if (!lstrlenA(szConvertedList))
510 return NULL;
512 *dwNumFiles = 1;
514 /* convert the colons to double-null termination */
515 temp = szConvertedList;
516 while (*temp)
518 if (*temp == ':')
520 *temp = '\0';
521 (*dwNumFiles)++;
524 temp++;
527 return szConvertedList;
530 static void free_file_node(struct ExtractFileList *pNode)
532 HeapFree(GetProcessHeap(), 0, pNode->filename);
533 HeapFree(GetProcessHeap(), 0, pNode);
536 /* determines whether szFile is in the NULL-separated szFileList */
537 static BOOL file_in_list(LPSTR szFile, LPSTR szFileList)
539 DWORD dwLen = lstrlenA(szFile);
540 DWORD dwTestLen;
542 while (*szFileList)
544 dwTestLen = lstrlenA(szFileList);
546 if (dwTestLen == dwLen)
548 if (!lstrcmpiA(szFile, szFileList))
549 return TRUE;
552 szFileList += dwTestLen + 1;
555 return FALSE;
558 /* removes nodes from the linked list that aren't specified in szFileList
559 * returns the number of files that are in both the linked list and szFileList
561 static DWORD fill_file_list(EXTRACTdest *extractDest, LPCSTR szCabName, LPSTR szFileList)
563 DWORD dwNumFound = 0;
564 struct ExtractFileList *pNode;
565 struct ExtractFileList *prev = NULL;
567 extractDest->flags |= EXTRACT_FILLFILELIST;
568 if (pExtract(extractDest, szCabName))
570 extractDest->flags &= ~EXTRACT_FILLFILELIST;
571 return -1;
574 pNode = extractDest->filelist;
575 while (pNode)
577 if (file_in_list(pNode->filename, szFileList))
579 prev = pNode;
580 pNode = pNode->next;
581 dwNumFound++;
583 else if (prev)
585 prev->next = pNode->next;
586 free_file_node(pNode);
587 pNode = prev->next;
589 else
591 extractDest->filelist = pNode->next;
592 free_file_node(pNode);
593 pNode = extractDest->filelist;
597 extractDest->flags &= ~EXTRACT_FILLFILELIST;
598 return dwNumFound;
601 /***********************************************************************
602 * ExtractFilesA (ADVPACK.@)
604 * Extracts the specified files from a cab archive into
605 * a destination directory.
607 * PARAMS
608 * CabName [I] Filename of the cab archive.
609 * ExpandDir [I] Destination directory for the extracted files.
610 * Flags [I] Reserved.
611 * FileList [I] Optional list of files to extract. See NOTES.
612 * LReserved [I] Reserved. Must be NULL.
613 * Reserved [I] Reserved. Must be 0.
615 * RETURNS
616 * Success: S_OK.
617 * Failure: E_FAIL.
619 * NOTES
620 * FileList is a colon-separated list of filenames. If FileList is
621 * non-NULL, only the files in the list will be extracted from the
622 * cab file, otherwise all files will be extracted. Any number of
623 * spaces, tabs, or colons can be before or after the list, but
624 * the list itself must only be separated by colons.
626 HRESULT WINAPI ExtractFilesA( LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
627 LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
629 EXTRACTdest extractDest;
630 HMODULE hCabinet;
631 HRESULT res = S_OK;
632 DWORD dwFileCount = 0;
633 DWORD dwFilesFound = 0;
634 LPSTR szConvertedList = NULL;
636 TRACE("(%p %p %ld %p %p %ld)\n", CabName, ExpandDir, Flags,
637 FileList, LReserved, Reserved);
639 if (!CabName || !ExpandDir)
640 return E_INVALIDARG;
642 if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
643 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
645 hCabinet = LoadLibraryA("cabinet.dll");
646 if (!hCabinet)
647 return E_FAIL;
649 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
650 if (!pExtract)
652 res = E_FAIL;
653 goto done;
656 ZeroMemory(&extractDest, sizeof(EXTRACTdest));
657 lstrcpyA(extractDest.directory, ExpandDir);
659 if (FileList)
661 szConvertedList = convert_file_list(FileList, &dwFileCount);
662 if (!szConvertedList || dwFileCount == -1)
664 res = E_FAIL;
665 goto done;
668 dwFilesFound = fill_file_list(&extractDest, CabName, szConvertedList);
669 if (dwFilesFound != dwFileCount)
671 res = E_FAIL;
672 goto done;
675 else
676 extractDest.flags |= EXTRACT_FILLFILELIST;
678 extractDest.flags |= EXTRACT_EXTRACTFILES;
679 res = pExtract(&extractDest, CabName);
681 done:
682 FreeLibrary(hCabinet);
683 HeapFree(GetProcessHeap(), 0, szConvertedList);
685 return res;
688 /***********************************************************************
689 * FileSaveMarkNotExistA (ADVPACK.@)
691 * Marks the files in the file list as not existing so they won't be
692 * backed up during a save.
694 * PARAMS
695 * pszFileList [I] NULL-separated list of filenames.
696 * pszDir [I] Path of the backup directory.
697 * pszBaseName [I] Basename of the INI file.
699 * RETURNS
700 * Success: S_OK.
701 * Failure: E_FAIL.
703 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
705 TRACE("(%p, %p, %p)\n", pszFileList, pszDir, pszBaseName);
707 return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
710 /***********************************************************************
711 * FileSaveRestoreA (ADVPACK.@)
713 * Saves or restores the files in the specified file list.
715 * PARAMS
716 * hDlg [I] Handle to the dialog used for the display.
717 * pszFileList [I] NULL-separated list of filenames.
718 * pszDir [I] Path of the backup directory.
719 * pszBaseName [I] Basename of the backup files.
720 * dwFlags [I] See advpub.h.
722 * RETURNS
723 * Success: S_OK.
724 * Failure: E_FAIL.
726 * NOTES
727 * If pszFileList is NULL on restore, all files will be restored.
729 * BUGS
730 * Unimplemented.
732 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
733 LPSTR pszBaseName, DWORD dwFlags)
735 FIXME("(%p, %p, %p, %p, %ld) stub\n", hDlg, pszFileList, pszDir,
736 pszBaseName, dwFlags);
738 return E_FAIL;
741 /***********************************************************************
742 * FileSaveRestoreOnINFA (ADVPACK.@)
745 * PARAMS
746 * hWnd [I] Handle to the window used for the display.
747 * pszTitle [I] Title of the window.
748 * pszINF [I] Fully-qualified INF filename.
749 * pszSection [I] GenInstall INF section name.
750 * pszBackupDir [I] Directory to store the backup file.
751 * pszBaseBackupFile [I] Basename of the backup files.
752 * dwFlags [I] See advpub.h
754 * RETURNS
755 * Success: S_OK.
756 * Failure: E_FAIL.
758 * NOTES
759 * If pszSection is NULL, the default section will be used.
761 * BUGS
762 * Unimplemented.
764 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
765 LPCSTR pszSection, LPCSTR pszBackupDir,
766 LPCSTR pszBaseBackupFile, DWORD dwFlags)
768 FIXME("(%p, %p, %p, %p, %p, %p, %ld) stub\n", hWnd, pszTitle, pszINF,
769 pszSection, pszBackupDir, pszBaseBackupFile, dwFlags);
771 return E_FAIL;
774 /***********************************************************************
775 * GetVersionFromFileA (ADVPACK.@)
777 * See GetVersionFromFileEx.
779 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
780 LPDWORD MinorVer, BOOL Version )
782 TRACE("(%s, %p, %p, %d)\n", Filename, MajorVer, MinorVer, Version);
783 return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
786 /* data for GetVersionFromFileEx */
787 typedef struct tagLANGANDCODEPAGE
789 WORD wLanguage;
790 WORD wCodePage;
791 } LANGANDCODEPAGE;
793 /***********************************************************************
794 * GetVersionFromFileExA (ADVPACK.@)
796 * Gets the files version or language information.
798 * PARAMS
799 * lpszFilename [I] The file to get the info from.
800 * pdwMSVer [O] Major version.
801 * pdwLSVer [O] Minor version.
802 * bVersion [I] Whether to retrieve version or language info.
804 * RETURNS
805 * Always returns S_OK.
807 * NOTES
808 * If bVersion is TRUE, version information is retrieved, else
809 * pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
811 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
812 LPDWORD pdwLSVer, BOOL bVersion )
814 VS_FIXEDFILEINFO *pFixedVersionInfo;
815 LANGANDCODEPAGE *pLangAndCodePage;
816 DWORD dwHandle, dwInfoSize;
817 CHAR szWinDir[MAX_PATH];
818 CHAR szFile[MAX_PATH];
819 LPVOID pVersionInfo = NULL;
820 BOOL bFileCopied = FALSE;
821 UINT uValueLen;
823 TRACE("(%s, %p, %p, %d)\n", lpszFilename, pdwMSVer, pdwLSVer, bVersion);
825 *pdwLSVer = 0;
826 *pdwMSVer = 0;
828 lstrcpynA(szFile, lpszFilename, MAX_PATH);
830 dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
831 if (!dwInfoSize)
833 /* check that the file exists */
834 if (GetFileAttributesA(szFile) == INVALID_FILE_ATTRIBUTES)
835 return S_OK;
837 /* file exists, but won't be found by GetFileVersionInfoSize,
838 * so copy it to the temp dir where it will be found.
840 GetWindowsDirectoryA(szWinDir, MAX_PATH);
841 GetTempFileNameA(szWinDir, NULL, 0, szFile);
842 CopyFileA(lpszFilename, szFile, FALSE);
843 bFileCopied = TRUE;
845 dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
846 if (!dwInfoSize)
847 goto done;
850 pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
851 if (!pVersionInfo)
852 goto done;
854 if (!GetFileVersionInfoA(szFile, dwHandle, dwInfoSize, pVersionInfo))
855 goto done;
857 if (bVersion)
859 if (!VerQueryValueA(pVersionInfo, "\\",
860 (LPVOID *)&pFixedVersionInfo, &uValueLen))
861 goto done;
863 if (!uValueLen)
864 goto done;
866 *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
867 *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
869 else
871 if (!VerQueryValueA(pVersionInfo, "\\VarFileInfo\\Translation",
872 (LPVOID *)&pLangAndCodePage, &uValueLen))
873 goto done;
875 if (!uValueLen)
876 goto done;
878 *pdwMSVer = pLangAndCodePage->wLanguage;
879 *pdwLSVer = pLangAndCodePage->wCodePage;
882 done:
883 HeapFree(GetProcessHeap(), 0, pVersionInfo);
885 if (bFileCopied)
886 DeleteFileA(szFile);
888 return S_OK;