advpack: Fix the documentation for AddDelBackupEntry.
[wine] / dlls / advpack / files.c
1 /*
2  * Advpack file functions
3  *
4  * Copyright 2006 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "setupapi.h"
30 #include "advpub.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
34
35 /***********************************************************************
36  *      AddDelBackupEntry (ADVPACK.@)
37  *
38  * Either appends the files in the file list to the backup section of
39  * the specified INI, or deletes the entries from the INI file.
40  *
41  * PARAMS
42  *   lpcszFileList  [I] NULL-separated list of filenames.
43  *   lpcszBackupDir [I] Path of the backup directory.
44  *   lpcszBaseName  [I] Basename of the INI file.
45  *   dwFlags        [I] AADBE_ADD_ENTRY adds the entries in the file list
46  *                      to the INI file, while AADBE_DEL_ENTRY removes
47  *                      the entries from the INI file.
48  *
49  * RETURNS
50  *   Success: S_OK.
51  *   Failure: E_FAIL.
52  *
53  * NOTES
54  *   If the INI file does not exist before adding entries to it, the file
55  *   will be created.
56  * 
57  *   If lpcszBackupDir is NULL, the INI file is assumed to exist in
58  *   c:\windows or created there if it does not exist.
59  *
60  * BUGS
61  *   Unimplemented.
62  */
63 HRESULT WINAPI AddDelBackupEntry(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
64                                  LPCSTR lpcszBaseName, DWORD dwFlags)
65 {
66     FIXME("(%p, %p, %p, %ld) stub\n", lpcszFileList, lpcszBackupDir,
67           lpcszBaseName, dwFlags);
68
69     return E_FAIL;
70 }
71
72 /* FIXME: this is only for the local case, X:\ */
73 #define ROOT_LENGTH 3
74
75 UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
76                                   UINT_PTR Param1, UINT_PTR Param2)
77 {
78     return 1;
79 }
80
81 UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
82                              UINT_PTR Param1, UINT_PTR Param2)
83 {
84     /* only be verbose for error notifications */
85     if (!Notification ||
86         Notification == SPFILENOTIFY_RENAMEERROR ||
87         Notification == SPFILENOTIFY_DELETEERROR ||
88         Notification == SPFILENOTIFY_COPYERROR)
89     {
90         return SetupDefaultQueueCallbackA(Context, Notification,
91                                           Param1, Param2);
92     }
93
94     return 1;
95 }
96
97 /***********************************************************************
98  *      AdvInstallFile (ADVPACK.@)
99  *
100  * Copies a file from the source to a destination.
101  *
102  * PARAMS
103  *   hwnd           [I] Handle to the window used for messages.
104  *   lpszSourceDir  [I] Source directory.
105  *   lpszSourceFile [I] Source filename.
106  *   lpszDestDir    [I] Destination directory.
107  *   lpszDestFile   [I] Optional destination filename.
108  *   dwFlags        [I] See advpub.h.
109  *   dwReserved     [I] Reserved.  Must be 0.
110  *
111  * RETURNS
112  *   Success: S_OK.
113  *   Failure: E_FAIL.
114  *
115  * NOTES
116  *   If lpszDestFile is NULL, the destination filename is the same as
117  *   lpszSourceFIle.
118  */
119 HRESULT WINAPI AdvInstallFile(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
120                               LPCSTR lpszDestDir, LPCSTR lpszDestFile,
121                               DWORD dwFlags, DWORD dwReserved)
122 {
123     PSP_FILE_CALLBACK_A pFileCallback;
124     LPSTR szPath, szDestFilename;
125     char szRootPath[ROOT_LENGTH];
126     DWORD dwLen, dwLastError;
127     HSPFILEQ fileQueue;
128     PVOID pContext;
129
130     TRACE("(%p,%p,%p,%p,%p,%ld,%ld)\n", hwnd, debugstr_a(lpszSourceDir),
131           debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
132           debugstr_a(lpszDestFile), dwFlags, dwReserved);
133
134     if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
135         return E_INVALIDARG;
136         
137     fileQueue = SetupOpenFileQueue();
138     if (fileQueue == INVALID_HANDLE_VALUE)
139         return HRESULT_FROM_WIN32(GetLastError());
140
141     pContext = NULL;
142     dwLastError = ERROR_SUCCESS;
143
144     lstrcpynA(szRootPath, lpszSourceDir, ROOT_LENGTH);
145     szPath = (LPSTR)lpszSourceDir + ROOT_LENGTH;
146
147     /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
148     if (lpszDestFile)
149     {
150         dwLen = lstrlenA(lpszDestFile);
151         szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
152         lstrcpyA(szDestFilename, lpszDestFile);
153     }
154     else
155     {
156         dwLen = lstrlenA(lpszSourceFile);
157         szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
158         lstrcpyA(szDestFilename, lpszSourceFile);
159     }
160
161     /* add the file copy operation to the setup queue */
162     if (!SetupQueueCopyA(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
163                          NULL, lpszDestDir, szDestFilename, dwFlags))
164     {
165         dwLastError = GetLastError();
166         goto done;
167     }
168
169     pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
170                                                0, 0, NULL);
171     if (!pContext)
172     {
173         dwLastError = GetLastError();
174         goto done;
175     }
176
177     /* don't output anything for AIF_QUIET */
178     if (dwFlags & AIF_QUIET)
179         pFileCallback = pQuietQueueCallback;
180     else
181         pFileCallback = pQueueCallback;
182
183     /* perform the file copy */
184     if (!SetupCommitFileQueueA(hwnd, fileQueue, pFileCallback, pContext))
185     {
186         dwLastError = GetLastError();
187         goto done;
188     }
189
190 done:
191     SetupTermDefaultQueueCallback(pContext);
192     SetupCloseFileQueue(fileQueue);
193     
194     HeapFree(GetProcessHeap(), 0, szDestFilename);
195     
196     return HRESULT_FROM_WIN32(dwLastError);
197 }
198
199 static HRESULT DELNODE_recurse_dirtree(LPSTR fname, DWORD flags)
200 {
201     DWORD fattrs = GetFileAttributesA(fname);
202     HRESULT ret = E_FAIL;
203
204     if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
205     {
206         HANDLE hFindFile;
207         WIN32_FIND_DATAA w32fd;
208         BOOL done = TRUE;
209         int fname_len = lstrlenA(fname);
210
211         /* Generate a path with wildcard suitable for iterating */
212         if (CharPrevA(fname, fname + fname_len) != "\\")
213         {
214             lstrcpyA(fname + fname_len, "\\");
215             ++fname_len;
216         }
217         lstrcpyA(fname + fname_len, "*");
218
219         if ((hFindFile = FindFirstFileA(fname, &w32fd)) != INVALID_HANDLE_VALUE)
220         {
221             /* Iterate through the files in the directory */
222             for (done = FALSE; !done; done = !FindNextFileA(hFindFile, &w32fd))
223             {
224                 TRACE("%s\n", w32fd.cFileName);
225                 if (lstrcmpA(".", w32fd.cFileName) != 0 &&
226                     lstrcmpA("..", w32fd.cFileName) != 0)
227                 {
228                     lstrcpyA(fname + fname_len, w32fd.cFileName);
229                     if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
230                     {
231                         break; /* Failure */
232                     }
233                 }
234             }
235             FindClose(hFindFile);
236         }
237
238         /* We're done with this directory, so restore the old path without wildcard */
239         *(fname + fname_len) = '\0';
240
241         if (done)
242         {
243             TRACE("%s: directory\n", fname);
244             if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryA(fname))
245             {
246                 ret = S_OK;
247             }
248         }
249     }
250     else
251     {
252         TRACE("%s: file\n", fname);
253         if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileA(fname))
254         {
255             ret = S_OK;
256         }
257     }
258     
259     return ret;
260 }
261
262 /***********************************************************************
263  *              DelNode    (ADVPACK.@)
264  *
265  * Deletes a file or directory
266  *
267  * PARAMS
268  *   pszFileOrDirName   [I] Name of file or directory to delete
269  *   dwFlags            [I] Flags; see include/advpub.h
270  *
271  * RETURNS 
272  *   Success: S_OK
273  *   Failure: E_FAIL
274  *
275  * BUGS
276  *   - Ignores flags
277  *   - Native version apparently does a lot of checking to make sure
278  *     we're not trying to delete a system directory etc.
279  */
280 HRESULT WINAPI DelNode( LPCSTR pszFileOrDirName, DWORD dwFlags )
281 {
282     CHAR fname[MAX_PATH];
283     HRESULT ret = E_FAIL;
284     
285     TRACE("(%s, 0x%08lx)\n", debugstr_a(pszFileOrDirName), dwFlags);
286     
287     if (dwFlags)
288         FIXME("Flags ignored!\n");
289
290     if (pszFileOrDirName && *pszFileOrDirName)
291     {
292         lstrcpyA(fname, pszFileOrDirName);
293
294         /* TODO: Should check for system directory deletion etc. here */
295
296         ret = DELNODE_recurse_dirtree(fname, dwFlags);
297     }
298
299     return ret;
300 }
301
302 /* returns the parameter at dwIndex in a list of parameters
303  * separated by the cSeparator character
304  */
305 static LPSTR get_parameter(LPSTR szParameters, CHAR cSeparator, DWORD dwIndex)
306 {
307     LPSTR szParam = NULL;
308     DWORD i = 0;
309
310     while (*szParameters && i < dwIndex)
311     {
312         if (*szParameters == cSeparator)
313             i++;
314
315         szParameters++;
316     }
317
318     if (!*szParameters)
319         return NULL;
320
321     szParam = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szParameters));
322     lstrcpyA(szParam, szParameters);
323
324     return szParam;
325 }
326
327 /***********************************************************************
328  *             DelNodeRunDLL32    (ADVPACK.@)
329  *
330  * Deletes a file or directory, WinMain style.
331  *
332  * PARAMS
333  *   hWnd    [I] Handle to the window used for the display.
334  *   hInst   [I] Instance of the process.
335  *   cmdline [I] Contains parameters in the order FileOrDirName,Flags.
336  *   show    [I] How the window should be shown.
337  *
338  * RETURNS
339  *   Success: S_OK.
340  *   Failure: E_FAIL.
341  */
342 HRESULT WINAPI DelNodeRunDLL32( HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show )
343 {
344     LPSTR szFilename, szFlags;
345     DWORD dwFlags;
346     HRESULT res;
347
348     TRACE("(%s)\n", debugstr_a(cmdline));
349
350     /* get the parameters at indexes 0 and 1 respectively */
351     szFilename = get_parameter(cmdline, ',', 0);
352     szFlags = get_parameter(cmdline, ',', 1);
353
354     dwFlags = atol(szFlags);
355
356     res = DelNode(szFilename, dwFlags);
357
358     HeapFree(GetProcessHeap(), 0, szFilename);
359     HeapFree(GetProcessHeap(), 0, szFlags);
360
361     return res;
362 }
363
364 /* The following defintions were copied from dlls/cabinet/cabinet.h */
365
366 /* EXTRACTdest flags */
367 #define EXTRACT_FILLFILELIST  0x00000001
368 #define EXTRACT_EXTRACTFILES  0x00000002
369
370 struct ExtractFileList {
371         LPSTR  filename;
372         struct ExtractFileList *next;
373         BOOL   unknown;  /* always 1L */
374 } ;
375
376 /* the first parameter of the function Extract */
377 typedef struct {
378         long  result1;          /* 0x000 */
379         long  unknown1[3];      /* 0x004 */
380         struct ExtractFileList *filelist; /* 0x010 */
381         long  filecount;        /* 0x014 */
382         DWORD flags;            /* 0x018 */
383         char  directory[0x104]; /* 0x01c */
384         char  lastfile[0x20c];  /* 0x120 */
385 } EXTRACTdest;
386
387 static HRESULT (WINAPI *pExtract)(EXTRACTdest*, LPCSTR);
388
389 /* removes legal characters before and after file list, and
390  * converts the file list to a NULL-separated list
391  */
392 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
393 {
394     DWORD dwLen;
395     char *first = (char *)FileList;
396     char *last = (char *)FileList + strlen(FileList) - 1;
397     LPSTR szConvertedList, temp;
398     
399     /* any number of these chars before the list is OK */
400     while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
401         first++;
402
403     /* any number of these chars after the list is OK */
404     while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
405         last--;
406
407     if (first == last)
408         return NULL;
409
410     dwLen = last - first + 3; /* room for double-null termination */
411     szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
412     lstrcpynA(szConvertedList, first, dwLen - 1);
413
414     szConvertedList[dwLen - 1] = '\0';
415     szConvertedList[dwLen] = '\0';
416
417     /* empty list */
418     if (!lstrlenA(szConvertedList))
419         return NULL;
420         
421     *dwNumFiles = 1;
422
423     /* convert the colons to double-null termination */
424     temp = szConvertedList;
425     while (*temp)
426     {
427         if (*temp == ':')
428         {
429             *temp = '\0';
430             (*dwNumFiles)++;
431         }
432
433         temp++;
434     }
435
436     return szConvertedList;
437 }
438
439 static void free_file_node(struct ExtractFileList *pNode)
440 {
441     HeapFree(GetProcessHeap(), 0, pNode->filename);
442     HeapFree(GetProcessHeap(), 0, pNode);
443 }
444
445 /* determines whether szFile is in the NULL-separated szFileList */
446 static BOOL file_in_list(LPSTR szFile, LPSTR szFileList)
447 {
448     DWORD dwLen = lstrlenA(szFile);
449     DWORD dwTestLen;
450
451     while (*szFileList)
452     {
453         dwTestLen = lstrlenA(szFileList);
454
455         if (dwTestLen == dwLen)
456         {
457             if (!lstrcmpiA(szFile, szFileList))
458                 return TRUE;
459         }
460
461         szFileList += dwTestLen + 1;
462     }
463
464     return FALSE;
465 }
466
467 /* removes nodes from the linked list that aren't specified in szFileList
468  * returns the number of files that are in both the linked list and szFileList
469  */
470 static DWORD fill_file_list(EXTRACTdest *extractDest, LPCSTR szCabName, LPSTR szFileList)
471 {
472     DWORD dwNumFound = 0;
473     struct ExtractFileList *pNode;
474     struct ExtractFileList *prev = NULL;
475
476     extractDest->flags |= EXTRACT_FILLFILELIST;
477     if (pExtract(extractDest, szCabName))
478     {
479         extractDest->flags &= ~EXTRACT_FILLFILELIST;
480         return -1;
481     }
482
483     pNode = extractDest->filelist;
484     while (pNode)
485     {
486         if (file_in_list(pNode->filename, szFileList))
487         {
488             prev = pNode;
489             pNode = pNode->next;
490             dwNumFound++;
491         }
492         else if (prev)
493         {
494             prev->next = pNode->next;
495             free_file_node(pNode);
496             pNode = prev->next;
497         }
498         else
499         {
500             extractDest->filelist = pNode->next;
501             free_file_node(pNode);
502             pNode = extractDest->filelist;
503         }
504     }
505
506     extractDest->flags &= ~EXTRACT_FILLFILELIST;
507     return dwNumFound;
508 }
509
510 /***********************************************************************
511  *             ExtractFiles    (ADVPACK.@)
512  *
513  * Extracts the specified files from a cab archive into
514  * a destination directory.
515  *
516  * PARAMS
517  *   CabName   [I] Filename of the cab archive.
518  *   ExpandDir [I] Destination directory for the extracted files.
519  *   Flags     [I] Reserved.
520  *   FileList  [I] Optional list of files to extract.  See NOTES.
521  *   LReserved [I] Reserved.  Must be NULL.
522  *   Reserved  [I] Reserved.  Must be 0.
523  *
524  * RETURNS
525  *   Success: S_OK.
526  *   Failure: E_FAIL.
527  *
528  * NOTES
529  *   FileList is a colon-separated list of filenames.  If FileList is
530  *   non-NULL, only the files in the list will be extracted from the
531  *   cab file, otherwise all files will be extracted.  Any number of
532  *   spaces, tabs, or colons can be before or after the list, but
533  *   the list itself must only be separated by colons.
534  */
535 HRESULT WINAPI ExtractFiles ( LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
536                               LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
537 {   
538     EXTRACTdest extractDest;
539     HMODULE hCabinet;
540     HRESULT res = S_OK;
541     DWORD dwFileCount = 0;
542     DWORD dwFilesFound = 0;
543     LPSTR szConvertedList = NULL;
544
545     TRACE("(%p %p %ld %p %p %ld)\n", CabName, ExpandDir, Flags,
546           FileList, LReserved, Reserved);
547
548     if (!CabName || !ExpandDir)
549         return E_INVALIDARG;
550
551     if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
552         return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
553
554     hCabinet = LoadLibraryA("cabinet.dll");
555     if (!hCabinet)
556         return E_FAIL;
557
558     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
559     if (!pExtract)
560     {
561         res = E_FAIL;
562         goto done;
563     }
564
565     ZeroMemory(&extractDest, sizeof(EXTRACTdest));
566     lstrcpyA(extractDest.directory, ExpandDir);
567
568     if (FileList)
569     {
570         szConvertedList = convert_file_list(FileList, &dwFileCount);
571         if (!szConvertedList || dwFileCount == -1)
572         {
573             res = E_FAIL;
574             goto done;
575         }
576
577         dwFilesFound = fill_file_list(&extractDest, CabName, szConvertedList);
578         if (dwFilesFound != dwFileCount)
579         {
580             res = E_FAIL;
581             goto done;
582         }
583     }
584     else
585         extractDest.flags |= EXTRACT_FILLFILELIST;
586
587     extractDest.flags |= EXTRACT_EXTRACTFILES;
588     res = pExtract(&extractDest, CabName);
589
590 done:
591     FreeLibrary(hCabinet);
592     HeapFree(GetProcessHeap(), 0, szConvertedList);
593
594     return res;
595 }
596
597 /***********************************************************************
598  *      FileSaveMarkNotExist (ADVPACK.@)
599  *
600  * Marks the files in the file list as not existing so they won't be
601  * backed up during a save.
602  *
603  * PARAMS
604  *   pszFileList [I] NULL-separated list of filenames.
605  *   pszDir      [I] Path of the backup directory.
606  *   pszBaseName [I] Basename of the INI file.
607  *
608  * RETURNS
609  *   Success: S_OK.
610  *   Failure: E_FAIL.
611  */
612 HRESULT WINAPI FileSaveMarkNotExist(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
613 {
614     TRACE("(%p, %p, %p)\n", pszFileList, pszDir, pszBaseName);
615
616     return AddDelBackupEntry(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
617 }
618
619 /***********************************************************************
620  *      FileSaveRestore (ADVPACK.@)
621  *
622  * Saves or restores the files in the specified file list.
623  *
624  * PARAMS
625  *   hDlg        [I] Handle to the dialog used for the display.
626  *   pszFileList [I] NULL-separated list of filenames.
627  *   pszDir      [I] Path of the backup directory.
628  *   pszBaseName [I] Basename of the backup files.
629  *   dwFlags     [I] See advpub.h.
630  *
631  * RETURNS
632  *   Success: S_OK.
633  *   Failure: E_FAIL.
634  *
635  * NOTES
636  *   If pszFileList is NULL on restore, all files will be restored.
637  *
638  * BUGS
639  *   Unimplemented.
640  */
641 HRESULT WINAPI FileSaveRestore(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
642                                LPSTR pszBaseName, DWORD dwFlags)
643 {
644     FIXME("(%p, %p, %p, %p, %ld) stub\n", hDlg, pszFileList, pszDir,
645           pszBaseName, dwFlags);
646
647     return E_FAIL;
648 }
649
650 /***********************************************************************
651  *      FileSaveRestoreOnINF (ADVPACK.@)
652  *
653  *
654  * PARAMS
655  *   hWnd              [I] Handle to the window used for the display.
656  *   pszTitle          [I] Title of the window.
657  *   pszINF            [I] Fully-qualified INF filename.
658  *   pszSection        [I] GenInstall INF section name.
659  *   pszBackupDir      [I] Directory to store the backup file.
660  *   pszBaseBackupFile [I] Basename of the backup files.
661  *   dwFlags           [I] See advpub.h
662  *
663  * RETURNS
664  *   Success: S_OK.
665  *   Failure: E_FAIL.
666  *
667  * NOTES
668  *   If pszSection is NULL, the default section will be used.
669  *
670  * BUGS
671  *   Unimplemented.
672  */
673 HRESULT WINAPI FileSaveRestoreOnINF(HWND hWnd, PCSTR pszTitle, PCSTR pszINF,
674                                     PCSTR pszSection, PCSTR pszBackupDir,
675                                     PCSTR pszBaseBackupFile, DWORD dwFlags)
676 {
677     FIXME("(%p, %p, %p, %p, %p, %p, %ld) stub\n", hWnd, pszTitle, pszINF,
678           pszSection, pszBackupDir, pszBaseBackupFile, dwFlags);
679
680     return E_FAIL;
681 }
682
683 /***********************************************************************
684  *             GetVersionFromFile      (ADVPACK.@)
685  *
686  * See GetVersionFromFileEx.
687  */
688 HRESULT WINAPI GetVersionFromFile( LPSTR Filename, LPDWORD MajorVer,
689                                    LPDWORD MinorVer, BOOL Version )
690 {
691     TRACE("(%s, %p, %p, %d)\n", Filename, MajorVer, MinorVer, Version);
692     return GetVersionFromFileEx(Filename, MajorVer, MinorVer, Version);
693 }
694
695 /* data for GetVersionFromFileEx */
696 typedef struct tagLANGANDCODEPAGE
697 {
698     WORD wLanguage;
699     WORD wCodePage;
700 } LANGANDCODEPAGE;
701
702 /***********************************************************************
703  *             GetVersionFromFileEx    (ADVPACK.@)
704  *
705  * Gets the files version or language information.
706  *
707  * PARAMS
708  *   lpszFilename [I] The file to get the info from.
709  *   pdwMSVer     [O] Major version.
710  *   pdwLSVer     [O] Minor version.
711  *   bVersion     [I] Whether to retrieve version or language info.
712  *
713  * RETURNS
714  *   Always returns S_OK.
715  *
716  * NOTES
717  *   If bVersion is TRUE, version information is retrieved, else
718  *   pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
719  */
720 HRESULT WINAPI GetVersionFromFileEx( LPSTR lpszFilename, LPDWORD pdwMSVer,
721                                      LPDWORD pdwLSVer, BOOL bVersion )
722 {
723     VS_FIXEDFILEINFO *pFixedVersionInfo;
724     LANGANDCODEPAGE *pLangAndCodePage;
725     DWORD dwHandle, dwInfoSize;
726     CHAR szWinDir[MAX_PATH];
727     CHAR szFile[MAX_PATH];
728     LPVOID pVersionInfo = NULL;
729     BOOL bFileCopied = FALSE;
730     UINT uValueLen;
731
732     TRACE("(%s, %p, %p, %d)\n", lpszFilename, pdwMSVer, pdwLSVer, bVersion);
733
734     *pdwLSVer = 0;
735     *pdwMSVer = 0;
736
737     lstrcpynA(szFile, lpszFilename, MAX_PATH);
738
739     dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
740     if (!dwInfoSize)
741     {
742         /* check that the file exists */
743         if (GetFileAttributesA(szFile) == INVALID_FILE_ATTRIBUTES)
744             return S_OK;
745
746         /* file exists, but won't be found by GetFileVersionInfoSize,
747         * so copy it to the temp dir where it will be found.
748         */
749         GetWindowsDirectoryA(szWinDir, MAX_PATH);
750         GetTempFileNameA(szWinDir, NULL, 0, szFile);
751         CopyFileA(lpszFilename, szFile, FALSE);
752         bFileCopied = TRUE;
753
754         dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
755         if (!dwInfoSize)
756             goto done;
757     }
758
759     pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
760     if (!pVersionInfo)
761         goto done;
762
763     if (!GetFileVersionInfoA(szFile, dwHandle, dwInfoSize, pVersionInfo))
764         goto done;
765
766     if (bVersion)
767     {
768         if (!VerQueryValueA(pVersionInfo, "\\",
769             (LPVOID *)&pFixedVersionInfo, &uValueLen))
770             goto done;
771
772         if (!uValueLen)
773             goto done;
774
775         *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
776         *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
777     }
778     else
779     {
780         if (!VerQueryValueA(pVersionInfo, "\\VarFileInfo\\Translation",
781              (LPVOID *)&pLangAndCodePage, &uValueLen))
782             goto done;
783
784         if (!uValueLen)
785             goto done;
786
787         *pdwMSVer = pLangAndCodePage->wLanguage;
788         *pdwLSVer = pLangAndCodePage->wCodePage;
789     }
790
791 done:
792     HeapFree(GetProcessHeap(), 0, pVersionInfo);
793
794     if (bFileCopied)
795         DeleteFileA(szFile);
796
797     return S_OK;
798 }