wininet: If necessary, add a slash between the hostname and path in
[wine/testsucceed.git] / dlls / advpack / files.c
blob482b9ed2dc43c7c2225afee1b44b421b2dc3bdec
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 "setupapi.h"
30 #include "advpub.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
35 /***********************************************************************
36 * AddDelBackupEntryA (ADVPACK.@)
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.
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.
49 * RETURNS
50 * S_OK in all cases.
52 * NOTES
53 * If the INI file does not exist before adding entries to it, the file
54 * will be created.
56 * If lpcszBackupDir is NULL, the INI file is assumed to exist in
57 * c:\windows or created there if it does not exist.
59 HRESULT WINAPI AddDelBackupEntryA(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
60 LPCSTR lpcszBaseName, DWORD dwFlags)
62 CHAR szIniPath[MAX_PATH];
63 LPSTR szString = NULL;
65 const char szBackupEntry[] = "-1,0,0,0,0,0,-1";
67 TRACE("(%p, %p, %p, %ld)\n", lpcszFileList, lpcszBackupDir,
68 lpcszBaseName, dwFlags);
70 if (!lpcszFileList || !*lpcszFileList)
71 return S_OK;
73 if (lpcszBackupDir)
74 lstrcpyA(szIniPath, lpcszBackupDir);
75 else
76 GetWindowsDirectoryA(szIniPath, MAX_PATH);
78 lstrcatA(szIniPath, "\\");
79 lstrcatA(szIniPath, lpcszBaseName);
80 lstrcatA(szIniPath, ".ini");
82 SetFileAttributesA(szIniPath, FILE_ATTRIBUTE_NORMAL);
84 if (dwFlags & AADBE_ADD_ENTRY)
85 szString = (LPSTR)szBackupEntry;
86 else if (dwFlags & AADBE_DEL_ENTRY)
87 szString = NULL;
89 /* add or delete the INI entries */
90 while (*lpcszFileList)
92 WritePrivateProfileStringA("backup", lpcszFileList, szString, szIniPath);
93 lpcszFileList += lstrlenA(lpcszFileList) + 1;
96 /* hide the INI file */
97 SetFileAttributesA(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
99 return S_OK;
102 /* FIXME: this is only for the local case, X:\ */
103 #define ROOT_LENGTH 3
105 UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
106 UINT_PTR Param1, UINT_PTR Param2)
108 return 1;
111 UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
112 UINT_PTR Param1, UINT_PTR Param2)
114 /* only be verbose for error notifications */
115 if (!Notification ||
116 Notification == SPFILENOTIFY_RENAMEERROR ||
117 Notification == SPFILENOTIFY_DELETEERROR ||
118 Notification == SPFILENOTIFY_COPYERROR)
120 return SetupDefaultQueueCallbackA(Context, Notification,
121 Param1, Param2);
124 return 1;
127 /***********************************************************************
128 * AdvInstallFileA (ADVPACK.@)
130 * Copies a file from the source to a destination.
132 * PARAMS
133 * hwnd [I] Handle to the window used for messages.
134 * lpszSourceDir [I] Source directory.
135 * lpszSourceFile [I] Source filename.
136 * lpszDestDir [I] Destination directory.
137 * lpszDestFile [I] Optional destination filename.
138 * dwFlags [I] See advpub.h.
139 * dwReserved [I] Reserved. Must be 0.
141 * RETURNS
142 * Success: S_OK.
143 * Failure: E_FAIL.
145 * NOTES
146 * If lpszDestFile is NULL, the destination filename is the same as
147 * lpszSourceFIle.
149 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
150 LPCSTR lpszDestDir, LPCSTR lpszDestFile,
151 DWORD dwFlags, DWORD dwReserved)
153 PSP_FILE_CALLBACK_A pFileCallback;
154 LPSTR szPath, szDestFilename;
155 char szRootPath[ROOT_LENGTH];
156 DWORD dwLen, dwLastError;
157 HSPFILEQ fileQueue;
158 PVOID pContext;
160 TRACE("(%p,%p,%p,%p,%p,%ld,%ld)\n", hwnd, debugstr_a(lpszSourceDir),
161 debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
162 debugstr_a(lpszDestFile), dwFlags, dwReserved);
164 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
165 return E_INVALIDARG;
167 fileQueue = SetupOpenFileQueue();
168 if (fileQueue == INVALID_HANDLE_VALUE)
169 return HRESULT_FROM_WIN32(GetLastError());
171 pContext = NULL;
172 dwLastError = ERROR_SUCCESS;
174 lstrcpynA(szRootPath, lpszSourceDir, ROOT_LENGTH);
175 szPath = (LPSTR)lpszSourceDir + ROOT_LENGTH;
177 /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
178 if (lpszDestFile)
180 dwLen = lstrlenA(lpszDestFile);
181 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
182 lstrcpyA(szDestFilename, lpszDestFile);
184 else
186 dwLen = lstrlenA(lpszSourceFile);
187 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen);
188 lstrcpyA(szDestFilename, lpszSourceFile);
191 /* add the file copy operation to the setup queue */
192 if (!SetupQueueCopyA(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
193 NULL, lpszDestDir, szDestFilename, dwFlags))
195 dwLastError = GetLastError();
196 goto done;
199 pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
200 0, 0, NULL);
201 if (!pContext)
203 dwLastError = GetLastError();
204 goto done;
207 /* don't output anything for AIF_QUIET */
208 if (dwFlags & AIF_QUIET)
209 pFileCallback = pQuietQueueCallback;
210 else
211 pFileCallback = pQueueCallback;
213 /* perform the file copy */
214 if (!SetupCommitFileQueueA(hwnd, fileQueue, pFileCallback, pContext))
216 dwLastError = GetLastError();
217 goto done;
220 done:
221 SetupTermDefaultQueueCallback(pContext);
222 SetupCloseFileQueue(fileQueue);
224 HeapFree(GetProcessHeap(), 0, szDestFilename);
226 return HRESULT_FROM_WIN32(dwLastError);
229 static HRESULT DELNODE_recurse_dirtree(LPSTR fname, DWORD flags)
231 DWORD fattrs = GetFileAttributesA(fname);
232 HRESULT ret = E_FAIL;
234 if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
236 HANDLE hFindFile;
237 WIN32_FIND_DATAA w32fd;
238 BOOL done = TRUE;
239 int fname_len = lstrlenA(fname);
241 /* Generate a path with wildcard suitable for iterating */
242 if (CharPrevA(fname, fname + fname_len) != "\\")
244 lstrcpyA(fname + fname_len, "\\");
245 ++fname_len;
247 lstrcpyA(fname + fname_len, "*");
249 if ((hFindFile = FindFirstFileA(fname, &w32fd)) != INVALID_HANDLE_VALUE)
251 /* Iterate through the files in the directory */
252 for (done = FALSE; !done; done = !FindNextFileA(hFindFile, &w32fd))
254 TRACE("%s\n", w32fd.cFileName);
255 if (lstrcmpA(".", w32fd.cFileName) != 0 &&
256 lstrcmpA("..", w32fd.cFileName) != 0)
258 lstrcpyA(fname + fname_len, w32fd.cFileName);
259 if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
261 break; /* Failure */
265 FindClose(hFindFile);
268 /* We're done with this directory, so restore the old path without wildcard */
269 *(fname + fname_len) = '\0';
271 if (done)
273 TRACE("%s: directory\n", fname);
274 if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryA(fname))
276 ret = S_OK;
280 else
282 TRACE("%s: file\n", fname);
283 if (SetFileAttributesA(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileA(fname))
285 ret = S_OK;
289 return ret;
292 /***********************************************************************
293 * DelNodeA (ADVPACK.@)
295 * Deletes a file or directory
297 * PARAMS
298 * pszFileOrDirName [I] Name of file or directory to delete
299 * dwFlags [I] Flags; see include/advpub.h
301 * RETURNS
302 * Success: S_OK
303 * Failure: E_FAIL
305 * BUGS
306 * - Ignores flags
307 * - Native version apparently does a lot of checking to make sure
308 * we're not trying to delete a system directory etc.
310 HRESULT WINAPI DelNodeA( LPCSTR pszFileOrDirName, DWORD dwFlags )
312 CHAR fname[MAX_PATH];
313 HRESULT ret = E_FAIL;
315 TRACE("(%s, 0x%08lx)\n", debugstr_a(pszFileOrDirName), dwFlags);
317 if (dwFlags)
318 FIXME("Flags ignored!\n");
320 if (pszFileOrDirName && *pszFileOrDirName)
322 lstrcpyA(fname, pszFileOrDirName);
324 /* TODO: Should check for system directory deletion etc. here */
326 ret = DELNODE_recurse_dirtree(fname, dwFlags);
329 return ret;
332 /* returns the parameter at dwIndex in a list of parameters
333 * separated by the cSeparator character
335 static LPSTR get_parameter(LPSTR szParameters, CHAR cSeparator, DWORD dwIndex)
337 LPSTR szParam = NULL;
338 DWORD i = 0;
340 while (*szParameters && i < dwIndex)
342 if (*szParameters == cSeparator)
343 i++;
345 szParameters++;
348 if (!*szParameters)
349 return NULL;
351 szParam = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szParameters));
352 lstrcpyA(szParam, szParameters);
354 return szParam;
357 /***********************************************************************
358 * DelNodeRunDLL32A (ADVPACK.@)
360 * Deletes a file or directory, WinMain style.
362 * PARAMS
363 * hWnd [I] Handle to the window used for the display.
364 * hInst [I] Instance of the process.
365 * cmdline [I] Contains parameters in the order FileOrDirName,Flags.
366 * show [I] How the window should be shown.
368 * RETURNS
369 * Success: S_OK.
370 * Failure: E_FAIL.
372 HRESULT WINAPI DelNodeRunDLL32A( HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show )
374 LPSTR szFilename, szFlags;
375 DWORD dwFlags;
376 HRESULT res;
378 TRACE("(%s)\n", debugstr_a(cmdline));
380 /* get the parameters at indexes 0 and 1 respectively */
381 szFilename = get_parameter(cmdline, ',', 0);
382 szFlags = get_parameter(cmdline, ',', 1);
384 dwFlags = atol(szFlags);
386 res = DelNodeA(szFilename, dwFlags);
388 HeapFree(GetProcessHeap(), 0, szFilename);
389 HeapFree(GetProcessHeap(), 0, szFlags);
391 return res;
394 /* The following defintions were copied from dlls/cabinet/cabinet.h */
396 /* EXTRACTdest flags */
397 #define EXTRACT_FILLFILELIST 0x00000001
398 #define EXTRACT_EXTRACTFILES 0x00000002
400 struct ExtractFileList {
401 LPSTR filename;
402 struct ExtractFileList *next;
403 BOOL unknown; /* always 1L */
406 /* the first parameter of the function Extract */
407 typedef struct {
408 long result1; /* 0x000 */
409 long unknown1[3]; /* 0x004 */
410 struct ExtractFileList *filelist; /* 0x010 */
411 long filecount; /* 0x014 */
412 DWORD flags; /* 0x018 */
413 char directory[0x104]; /* 0x01c */
414 char lastfile[0x20c]; /* 0x120 */
415 } EXTRACTdest;
417 static HRESULT (WINAPI *pExtract)(EXTRACTdest*, LPCSTR);
419 /* removes legal characters before and after file list, and
420 * converts the file list to a NULL-separated list
422 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
424 DWORD dwLen;
425 char *first = (char *)FileList;
426 char *last = (char *)FileList + strlen(FileList) - 1;
427 LPSTR szConvertedList, temp;
429 /* any number of these chars before the list is OK */
430 while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
431 first++;
433 /* any number of these chars after the list is OK */
434 while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
435 last--;
437 if (first == last)
438 return NULL;
440 dwLen = last - first + 3; /* room for double-null termination */
441 szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
442 lstrcpynA(szConvertedList, first, dwLen - 1);
444 szConvertedList[dwLen - 1] = '\0';
445 szConvertedList[dwLen] = '\0';
447 /* empty list */
448 if (!lstrlenA(szConvertedList))
449 return NULL;
451 *dwNumFiles = 1;
453 /* convert the colons to double-null termination */
454 temp = szConvertedList;
455 while (*temp)
457 if (*temp == ':')
459 *temp = '\0';
460 (*dwNumFiles)++;
463 temp++;
466 return szConvertedList;
469 static void free_file_node(struct ExtractFileList *pNode)
471 HeapFree(GetProcessHeap(), 0, pNode->filename);
472 HeapFree(GetProcessHeap(), 0, pNode);
475 /* determines whether szFile is in the NULL-separated szFileList */
476 static BOOL file_in_list(LPSTR szFile, LPSTR szFileList)
478 DWORD dwLen = lstrlenA(szFile);
479 DWORD dwTestLen;
481 while (*szFileList)
483 dwTestLen = lstrlenA(szFileList);
485 if (dwTestLen == dwLen)
487 if (!lstrcmpiA(szFile, szFileList))
488 return TRUE;
491 szFileList += dwTestLen + 1;
494 return FALSE;
497 /* removes nodes from the linked list that aren't specified in szFileList
498 * returns the number of files that are in both the linked list and szFileList
500 static DWORD fill_file_list(EXTRACTdest *extractDest, LPCSTR szCabName, LPSTR szFileList)
502 DWORD dwNumFound = 0;
503 struct ExtractFileList *pNode;
504 struct ExtractFileList *prev = NULL;
506 extractDest->flags |= EXTRACT_FILLFILELIST;
507 if (pExtract(extractDest, szCabName))
509 extractDest->flags &= ~EXTRACT_FILLFILELIST;
510 return -1;
513 pNode = extractDest->filelist;
514 while (pNode)
516 if (file_in_list(pNode->filename, szFileList))
518 prev = pNode;
519 pNode = pNode->next;
520 dwNumFound++;
522 else if (prev)
524 prev->next = pNode->next;
525 free_file_node(pNode);
526 pNode = prev->next;
528 else
530 extractDest->filelist = pNode->next;
531 free_file_node(pNode);
532 pNode = extractDest->filelist;
536 extractDest->flags &= ~EXTRACT_FILLFILELIST;
537 return dwNumFound;
540 /***********************************************************************
541 * ExtractFilesA (ADVPACK.@)
543 * Extracts the specified files from a cab archive into
544 * a destination directory.
546 * PARAMS
547 * CabName [I] Filename of the cab archive.
548 * ExpandDir [I] Destination directory for the extracted files.
549 * Flags [I] Reserved.
550 * FileList [I] Optional list of files to extract. See NOTES.
551 * LReserved [I] Reserved. Must be NULL.
552 * Reserved [I] Reserved. Must be 0.
554 * RETURNS
555 * Success: S_OK.
556 * Failure: E_FAIL.
558 * NOTES
559 * FileList is a colon-separated list of filenames. If FileList is
560 * non-NULL, only the files in the list will be extracted from the
561 * cab file, otherwise all files will be extracted. Any number of
562 * spaces, tabs, or colons can be before or after the list, but
563 * the list itself must only be separated by colons.
565 HRESULT WINAPI ExtractFilesA( LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
566 LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
568 EXTRACTdest extractDest;
569 HMODULE hCabinet;
570 HRESULT res = S_OK;
571 DWORD dwFileCount = 0;
572 DWORD dwFilesFound = 0;
573 LPSTR szConvertedList = NULL;
575 TRACE("(%p %p %ld %p %p %ld)\n", CabName, ExpandDir, Flags,
576 FileList, LReserved, Reserved);
578 if (!CabName || !ExpandDir)
579 return E_INVALIDARG;
581 if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
582 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
584 hCabinet = LoadLibraryA("cabinet.dll");
585 if (!hCabinet)
586 return E_FAIL;
588 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
589 if (!pExtract)
591 res = E_FAIL;
592 goto done;
595 ZeroMemory(&extractDest, sizeof(EXTRACTdest));
596 lstrcpyA(extractDest.directory, ExpandDir);
598 if (FileList)
600 szConvertedList = convert_file_list(FileList, &dwFileCount);
601 if (!szConvertedList || dwFileCount == -1)
603 res = E_FAIL;
604 goto done;
607 dwFilesFound = fill_file_list(&extractDest, CabName, szConvertedList);
608 if (dwFilesFound != dwFileCount)
610 res = E_FAIL;
611 goto done;
614 else
615 extractDest.flags |= EXTRACT_FILLFILELIST;
617 extractDest.flags |= EXTRACT_EXTRACTFILES;
618 res = pExtract(&extractDest, CabName);
620 done:
621 FreeLibrary(hCabinet);
622 HeapFree(GetProcessHeap(), 0, szConvertedList);
624 return res;
627 /***********************************************************************
628 * FileSaveMarkNotExistA (ADVPACK.@)
630 * Marks the files in the file list as not existing so they won't be
631 * backed up during a save.
633 * PARAMS
634 * pszFileList [I] NULL-separated list of filenames.
635 * pszDir [I] Path of the backup directory.
636 * pszBaseName [I] Basename of the INI file.
638 * RETURNS
639 * Success: S_OK.
640 * Failure: E_FAIL.
642 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
644 TRACE("(%p, %p, %p)\n", pszFileList, pszDir, pszBaseName);
646 return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
649 /***********************************************************************
650 * FileSaveRestoreA (ADVPACK.@)
652 * Saves or restores the files in the specified file list.
654 * PARAMS
655 * hDlg [I] Handle to the dialog used for the display.
656 * pszFileList [I] NULL-separated list of filenames.
657 * pszDir [I] Path of the backup directory.
658 * pszBaseName [I] Basename of the backup files.
659 * dwFlags [I] See advpub.h.
661 * RETURNS
662 * Success: S_OK.
663 * Failure: E_FAIL.
665 * NOTES
666 * If pszFileList is NULL on restore, all files will be restored.
668 * BUGS
669 * Unimplemented.
671 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
672 LPSTR pszBaseName, DWORD dwFlags)
674 FIXME("(%p, %p, %p, %p, %ld) stub\n", hDlg, pszFileList, pszDir,
675 pszBaseName, dwFlags);
677 return E_FAIL;
680 /***********************************************************************
681 * FileSaveRestoreOnINFA (ADVPACK.@)
684 * PARAMS
685 * hWnd [I] Handle to the window used for the display.
686 * pszTitle [I] Title of the window.
687 * pszINF [I] Fully-qualified INF filename.
688 * pszSection [I] GenInstall INF section name.
689 * pszBackupDir [I] Directory to store the backup file.
690 * pszBaseBackupFile [I] Basename of the backup files.
691 * dwFlags [I] See advpub.h
693 * RETURNS
694 * Success: S_OK.
695 * Failure: E_FAIL.
697 * NOTES
698 * If pszSection is NULL, the default section will be used.
700 * BUGS
701 * Unimplemented.
703 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
704 LPCSTR pszSection, LPCSTR pszBackupDir,
705 LPCSTR pszBaseBackupFile, DWORD dwFlags)
707 FIXME("(%p, %p, %p, %p, %p, %p, %ld) stub\n", hWnd, pszTitle, pszINF,
708 pszSection, pszBackupDir, pszBaseBackupFile, dwFlags);
710 return E_FAIL;
713 /***********************************************************************
714 * GetVersionFromFileA (ADVPACK.@)
716 * See GetVersionFromFileEx.
718 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
719 LPDWORD MinorVer, BOOL Version )
721 TRACE("(%s, %p, %p, %d)\n", Filename, MajorVer, MinorVer, Version);
722 return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
725 /* data for GetVersionFromFileEx */
726 typedef struct tagLANGANDCODEPAGE
728 WORD wLanguage;
729 WORD wCodePage;
730 } LANGANDCODEPAGE;
732 /***********************************************************************
733 * GetVersionFromFileExA (ADVPACK.@)
735 * Gets the files version or language information.
737 * PARAMS
738 * lpszFilename [I] The file to get the info from.
739 * pdwMSVer [O] Major version.
740 * pdwLSVer [O] Minor version.
741 * bVersion [I] Whether to retrieve version or language info.
743 * RETURNS
744 * Always returns S_OK.
746 * NOTES
747 * If bVersion is TRUE, version information is retrieved, else
748 * pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
750 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
751 LPDWORD pdwLSVer, BOOL bVersion )
753 VS_FIXEDFILEINFO *pFixedVersionInfo;
754 LANGANDCODEPAGE *pLangAndCodePage;
755 DWORD dwHandle, dwInfoSize;
756 CHAR szWinDir[MAX_PATH];
757 CHAR szFile[MAX_PATH];
758 LPVOID pVersionInfo = NULL;
759 BOOL bFileCopied = FALSE;
760 UINT uValueLen;
762 TRACE("(%s, %p, %p, %d)\n", lpszFilename, pdwMSVer, pdwLSVer, bVersion);
764 *pdwLSVer = 0;
765 *pdwMSVer = 0;
767 lstrcpynA(szFile, lpszFilename, MAX_PATH);
769 dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
770 if (!dwInfoSize)
772 /* check that the file exists */
773 if (GetFileAttributesA(szFile) == INVALID_FILE_ATTRIBUTES)
774 return S_OK;
776 /* file exists, but won't be found by GetFileVersionInfoSize,
777 * so copy it to the temp dir where it will be found.
779 GetWindowsDirectoryA(szWinDir, MAX_PATH);
780 GetTempFileNameA(szWinDir, NULL, 0, szFile);
781 CopyFileA(lpszFilename, szFile, FALSE);
782 bFileCopied = TRUE;
784 dwInfoSize = GetFileVersionInfoSizeA(szFile, &dwHandle);
785 if (!dwInfoSize)
786 goto done;
789 pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
790 if (!pVersionInfo)
791 goto done;
793 if (!GetFileVersionInfoA(szFile, dwHandle, dwInfoSize, pVersionInfo))
794 goto done;
796 if (bVersion)
798 if (!VerQueryValueA(pVersionInfo, "\\",
799 (LPVOID *)&pFixedVersionInfo, &uValueLen))
800 goto done;
802 if (!uValueLen)
803 goto done;
805 *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
806 *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
808 else
810 if (!VerQueryValueA(pVersionInfo, "\\VarFileInfo\\Translation",
811 (LPVOID *)&pLangAndCodePage, &uValueLen))
812 goto done;
814 if (!uValueLen)
815 goto done;
817 *pdwMSVer = pLangAndCodePage->wLanguage;
818 *pdwLSVer = pLangAndCodePage->wCodePage;
821 done:
822 HeapFree(GetProcessHeap(), 0, pVersionInfo);
824 if (bFileCopied)
825 DeleteFileA(szFile);
827 return S_OK;