info.c 28.9 KB
Newer Older
1 2
/*
 * Copyright (C) 2004 Stefan Leichter
3
 * Copyright (C) 2017 Akihiro Sagawa
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20
 */

#include <stdarg.h>
21
#include <stdio.h>
22
#include <assert.h>
23 24 25 26

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
27
#include "winnls.h"
28
#include "winuser.h"
29
#include "winver.h"
30
#include "verrsrc.h"
31
#include "wine/test.h"
32

33
#define MY_LAST_ERROR ((DWORD)-1)
34 35 36
#define EXPECT_BAD_PATH__NOT_FOUND \
    ok( (ERROR_PATH_NOT_FOUND == GetLastError()) || \
	(ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) || \
37
	(ERROR_FILE_NOT_FOUND == GetLastError()) || \
38 39
	(ERROR_BAD_PATHNAME == GetLastError()) || \
        (ERROR_SUCCESS == GetLastError()), \
40
	"Last error wrong! ERROR_RESOURCE_DATA_NOT_FOUND/ERROR_BAD_PATHNAME (98)/" \
41 42
	"ERROR_PATH_NOT_FOUND (NT4)/ERROR_FILE_NOT_FOUND (2k3) " \
        "ERROR_SUCCESS (2k) expected, got %u\n", GetLastError());
43 44 45
#define EXPECT_INVALID__NOT_FOUND \
    ok( (ERROR_PATH_NOT_FOUND == GetLastError()) || \
	(ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) || \
46
	(ERROR_FILE_NOT_FOUND == GetLastError()) || \
47 48
	(ERROR_INVALID_PARAMETER == GetLastError()) || \
        (ERROR_SUCCESS == GetLastError()), \
49
	"Last error wrong! ERROR_RESOURCE_DATA_NOT_FOUND/ERROR_INVALID_PARAMETER (98)/" \
50 51
	"ERROR_PATH_NOT_FOUND (NT4)/ERROR_FILE_NOT_FOUND (2k3) " \
	"ERROR_SUCCESS (2k) expected, got %u\n", GetLastError());
52

53 54 55 56 57 58 59 60 61 62 63 64
static void create_file(const CHAR *name)
{
    HANDLE file;
    DWORD written;

    file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
    WriteFile(file, name, strlen(name), &written, NULL);
    WriteFile(file, "\n", strlen("\n"), &written, NULL);
    CloseHandle(file);
}

65 66
static void test_info_size(void)
{   DWORD hdl, retval;
67
    char mypath[MAX_PATH] = "";
68

69
    SetLastError(MY_LAST_ERROR);
70 71
    retval = GetFileVersionInfoSizeA( NULL, NULL);
    ok( !retval,
72
	"GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
73
	retval);
74
    EXPECT_INVALID__NOT_FOUND;
75 76

    hdl = 0x55555555;
77
    SetLastError(MY_LAST_ERROR);
78 79
    retval = GetFileVersionInfoSizeA( NULL, &hdl);
    ok( !retval,
80
	"GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
81
	retval);
82
    EXPECT_INVALID__NOT_FOUND;
83
    ok( hdl == 0L,
84
	"Handle wrong! 0L expected, got 0x%08x\n", hdl);
85

86
    SetLastError(MY_LAST_ERROR);
87 88
    retval = GetFileVersionInfoSizeA( "", NULL);
    ok( !retval,
89
	"GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
90
	retval);
91
    EXPECT_BAD_PATH__NOT_FOUND;
92 93

    hdl = 0x55555555;
94
    SetLastError(MY_LAST_ERROR);
95 96
    retval = GetFileVersionInfoSizeA( "", &hdl);
    ok( !retval,
97
	"GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
98
	retval);
99
    EXPECT_BAD_PATH__NOT_FOUND;
100
    ok( hdl == 0L,
101
	"Handle wrong! 0L expected, got 0x%08x\n", hdl);
102

103
    SetLastError(MY_LAST_ERROR);
104 105
    retval = GetFileVersionInfoSizeA( "kernel32.dll", NULL);
    ok( retval,
106
	"GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
107
	retval);
108
    ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
109
	"Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got %u\n",
110
	MY_LAST_ERROR, GetLastError());
111 112

    hdl = 0x55555555;
113
    SetLastError(MY_LAST_ERROR);
114 115
    retval = GetFileVersionInfoSizeA( "kernel32.dll", &hdl);
    ok( retval,
116
	"GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
117
	retval);
118
    ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
119
	"Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got %u\n",
120
	MY_LAST_ERROR, GetLastError());
121
    ok( hdl == 0L,
122
	"Handle wrong! 0L expected, got 0x%08x\n", hdl);
123

124
    SetLastError(MY_LAST_ERROR);
125 126
    retval = GetFileVersionInfoSizeA( "notexist.dll", NULL);
    ok( !retval,
127
	"GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
128
	retval);
129
    ok( (ERROR_FILE_NOT_FOUND == GetLastError()) ||
130
	(ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) ||
131 132
	(MY_LAST_ERROR == GetLastError()) ||
	(ERROR_SUCCESS == GetLastError()), /* win2k */
133
	"Last error wrong! ERROR_FILE_NOT_FOUND/ERROR_RESOURCE_DATA_NOT_FOUND "
134
	"(XP)/0x%08x (NT4) expected, got %u\n", MY_LAST_ERROR, GetLastError());
135 136 137 138 139 140 141

    /* test a currently loaded executable */
    if(GetModuleFileNameA(NULL, mypath, MAX_PATH)) {
	hdl = 0x55555555;
	SetLastError(MY_LAST_ERROR);
	retval = GetFileVersionInfoSizeA( mypath, &hdl);
	ok( retval,
142
            "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
143 144
	    retval);
	ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
145
            "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got %u\n",
146 147
	    MY_LAST_ERROR, GetLastError());
	ok( hdl == 0L,
148
            "Handle wrong! 0L expected, got 0x%08x\n", hdl);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    }
    else
	trace("skipping GetModuleFileNameA(NULL,..) failed\n");

    /* test a not loaded executable */
    if(GetSystemDirectoryA(mypath, MAX_PATH)) {
	lstrcatA(mypath, "\\regsvr32.exe");

	if(INVALID_FILE_ATTRIBUTES == GetFileAttributesA(mypath))
	    trace("GetFileAttributesA(%s) failed\n", mypath);
	else {
	    hdl = 0x55555555;
	    SetLastError(MY_LAST_ERROR);
	    retval = GetFileVersionInfoSizeA( mypath, &hdl);
	    ok( retval,
164
		"GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
165 166
		retval);
	    ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
167
		"Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got %u\n",
168 169
		MY_LAST_ERROR, GetLastError());
	    ok( hdl == 0L,
170
		"Handle wrong! 0L expected, got 0x%08x\n", hdl);
171 172 173
	}
    }
    else
174
	trace("skipping GetSystemDirectoryA(mypath,..) failed\n");
175 176 177 178 179 180 181 182 183

    create_file("test.txt");

    /* no version info */
    SetLastError(0xdeadbeef);
    hdl = 0xcafe;
    retval = GetFileVersionInfoSizeA("test.txt", &hdl);
    ok(retval == 0, "Expected 0, got %d\n", retval);
    ok(hdl == 0, "Expected 0, got %d\n", hdl);
184 185
    ok(GetLastError() == ERROR_RESOURCE_DATA_NOT_FOUND ||
       GetLastError() == ERROR_SUCCESS, /* win2k */
186 187 188
       "Expected ERROR_RESOURCE_DATA_NOT_FOUND, got %d\n", GetLastError());

    DeleteFileA("test.txt");
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static void VersionDwordLong2String(DWORDLONG Version, LPSTR lpszVerString)
{
    WORD a, b, c, d;

    a = (WORD)(Version >> 48);
    b = (WORD)((Version >> 32) & 0xffff);
    c = (WORD)((Version >> 16) & 0xffff);
    d = (WORD)(Version & 0xffff);

    sprintf(lpszVerString, "%d.%d.%d.%d", a, b, c, d);
}

static void test_info(void)
{
    DWORD hdl, retval;
    PVOID pVersionInfo = NULL;
    BOOL boolret;
    VS_FIXEDFILEINFO *pFixedVersionInfo;
    UINT uiLength;
    char VersionString[MAX_PATH];
211
    static const char backslash[] = "\\";
212 213 214 215 216 217
    DWORDLONG dwlVersion;

    hdl = 0x55555555;
    SetLastError(MY_LAST_ERROR);
    retval = GetFileVersionInfoSizeA( "kernel32.dll", &hdl);
    ok( retval,
218
	"GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
219 220
	retval);
    ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
221
	"Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got %u\n",
222 223
	MY_LAST_ERROR, GetLastError());
    ok( hdl == 0L,
224
	"Handle wrong! 0L expected, got 0x%08x\n", hdl);
225 226 227 228 229 230 231 232 233

    if ( retval == 0 || hdl != 0)
        return;

    pVersionInfo = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retval );
    ok(pVersionInfo != 0, "HeapAlloc failed\n" );
    if (pVersionInfo == 0)
        return;

234 235
    if (0)
    {
236 237
    /* this test crashes on WinNT4
     */
238
    boolret = GetFileVersionInfoA( "kernel32.dll", 0, retval, 0);
239
    ok (!boolret, "GetFileVersionInfoA should have failed: GetLastError = %u\n", GetLastError());
240 241 242
    ok ((GetLastError() == ERROR_INVALID_DATA) || (GetLastError() == ERROR_BAD_PATHNAME) ||
	(GetLastError() == NO_ERROR),
        "Last error wrong! ERROR_INVALID_DATA/ERROR_BAD_PATHNAME (ME)/"
243
	"NO_ERROR (95) expected, got %u\n",
244
        GetLastError());
245
    }
246 247

    boolret = GetFileVersionInfoA( "kernel32.dll", 0, retval, pVersionInfo );
248
    ok (boolret, "GetFileVersionInfoA failed: GetLastError = %u\n", GetLastError());
249
    if (!boolret)
250
        goto cleanup;
251

252 253 254 255 256 257 258
    boolret = VerQueryValueA( pVersionInfo, NULL, (LPVOID *)&pFixedVersionInfo, &uiLength );
    ok (boolret || GetLastError() == NO_ERROR /* Win98 */,
       "VerQueryValueA failed: GetLastError = %u\n", GetLastError());

    boolret = VerQueryValueA( pVersionInfo, "", (LPVOID *)&pFixedVersionInfo, &uiLength );
    ok (boolret, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());

259
    boolret = VerQueryValueA( pVersionInfo, backslash, (LPVOID *)&pFixedVersionInfo, &uiLength );
260
    ok (boolret, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());
261
    if (!boolret)
262
        goto cleanup;
263 264 265 266 267 268 269 270

    dwlVersion = (((DWORDLONG)pFixedVersionInfo->dwFileVersionMS) << 32) +
        pFixedVersionInfo->dwFileVersionLS;

    VersionDwordLong2String(dwlVersion, VersionString);

    trace("kernel32.dll version: %s\n", VersionString);

271 272
    if (0)
    {
273 274
    /* this test crashes on WinNT4
     */
275
    boolret = VerQueryValueA( pVersionInfo, backslash, (LPVOID *)&pFixedVersionInfo, 0);
276
    ok (boolret, "VerQueryValue failed: GetLastError = %u\n", GetLastError());
277
    }
278 279 280

cleanup:
    HeapFree( GetProcessHeap(), 0, pVersionInfo);
281 282
}

283
static void test_32bit_win(void)
284 285
{
    DWORD hdlA, retvalA;
286
    DWORD hdlW, retvalW = 0;
287 288 289
    BOOL retA,retW;
    PVOID pVersionInfoA = NULL;
    PVOID pVersionInfoW = NULL;
290 291
    char *pBufA;
    WCHAR *pBufW;
292
    UINT uiLengthA, uiLengthW;
293
    char mypathA[MAX_PATH];
294
    WCHAR mypathW[MAX_PATH];
295 296
    char rootA[] = "\\";
    WCHAR rootW[] = { '\\', 0 };
297
    WCHAR emptyW[] = { 0 };
298 299 300 301 302 303 304 305
    char varfileinfoA[] = "\\VarFileInfo\\Translation";
    WCHAR varfileinfoW[]    = { '\\','V','a','r','F','i','l','e','I','n','f','o',
                                '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
    char WineVarFileInfoA[] = { 0x09, 0x04, 0xE4, 0x04 };
    char FileDescriptionA[] = "\\StringFileInfo\\040904E4\\FileDescription";
    WCHAR FileDescriptionW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
                                '\\','0','4','0','9','0','4','E','4',
                                '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n', 0 };
306 307
    char WineFileDescriptionA[] = "FileDescription";
    WCHAR WineFileDescriptionW[] = { 'F','i','l','e','D','e','s','c','r','i','p','t','i','o','n', 0 };
308
    BOOL is_unicode_enabled = TRUE;
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    /* A copy from dlls/version/info.c */
    typedef struct
    {
        WORD  wLength;
        WORD  wValueLength;
        WORD  wType;
        WCHAR szKey[1];
#if 0   /* variable length structure */
        /* DWORD aligned */
        BYTE  Value[];
        /* DWORD aligned */
        VS_VERSION_INFO_STRUCT32 Children[];
#endif
    } VS_VERSION_INFO_STRUCT32;

325 326
    /* If we call GetFileVersionInfoA on a system that supports Unicode, NT/W2K/XP/W2K3 (by default) and Wine,
     * the versioninfo will contain Unicode strings.
327 328
     * Part of the test is to call both the A and W versions, which should have the same Version Information
     * for some requests, on systems that support both calls.
329 330 331
     */

    /* First get the versioninfo via the W versions */
332
    SetLastError(0xdeadbeef);
333 334 335
    GetModuleFileNameW(NULL, mypathW, MAX_PATH);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
336
        win_skip("GetModuleFileNameW not existing on this platform, skipping comparison between A- and W-calls\n");
337
        is_unicode_enabled = FALSE;
338 339
    }

340 341 342 343 344
    if (is_unicode_enabled)
    { 
        retvalW = GetFileVersionInfoSizeW( mypathW, &hdlW);
        pVersionInfoW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retvalW );
        retW = GetFileVersionInfoW( mypathW, 0, retvalW, pVersionInfoW );
345
        ok(retW, "GetFileVersionInfo failed: GetLastError = %u\n", GetLastError());
346
    }
347 348 349 350 351

    GetModuleFileNameA(NULL, mypathA, MAX_PATH);
    retvalA = GetFileVersionInfoSizeA( mypathA, &hdlA);
    pVersionInfoA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retvalA );
    retA = GetFileVersionInfoA( mypathA, 0, retvalA, pVersionInfoA );
352
    ok(retA, "GetFileVersionInfo failed: GetLastError = %u\n", GetLastError());
353

354 355
    if (is_unicode_enabled)
    { 
356
        ok( retvalA == retvalW, "The size of the struct should be the same for both A/W calls, it is (%d) vs. (%d)\n",
357 358 359
                                retvalA, retvalW);
        ok( !memcmp(pVersionInfoA, pVersionInfoW, retvalA), "Both structs should be the same, they aren't\n");
    }
360

361
    /* The structs on Windows are bigger than just the struct for the basic information. The total struct
362 363 364 365 366 367
     * contains also an empty part, which is used for converted strings. The converted strings are a result
     * of calling VerQueryValueA on a 32bit resource and calling VerQueryValueW on a 16bit resource.
     * The first WORD of the structure (wLength) shows the size of the base struct. The total struct size depends
     * on the Windows version:
     *
     * 16bits resource (numbers are from a sample app):
368
     *
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
     * Windows Version    Retrieved with A/W    wLength        StructSize
     * ====================================================================================
     * Win98              A                     0x01B4 (436)   436
     * NT4                A/W                   0x01B4 (436)   2048 ???
     * W2K/XP/W2K3        A/W                   0x01B4 (436)   1536 which is (436 - sizeof(VS_FIXEDFILEINFO)) * 4
     *
     * 32bits resource (numbers are from this test executable version_crosstest.exe):
     * Windows Version    Retrieved with A/W    wLength        StructSize
     * =============================================================
     * Win98              A                     0x01E0 (480)   848 (structure data doesn't seem correct)
     * NT4                A/W                   0x0350 (848)   1272 (848 * 1.5)
     * W2K/XP/W2K3        A/W                   0x0350 (848)   1700 which is (848 * 2) + 4 
     *
     * Wine will follow the implementation (eventually) of W2K/XP/W2K3
     */

    /* Now some tests for the above (only if we are unicode enabled) */

    if (is_unicode_enabled)
    { 
389
        VS_VERSION_INFO_STRUCT32 *vvis = pVersionInfoW;
390 391 392 393 394
        ok ( retvalW == ((vvis->wLength * 2) + 4) || retvalW == (vvis->wLength * 1.5),
             "Structure is not of the correct size\n");
    }

    /* Although the 32bit resource structures contain Unicode strings, VerQueryValueA will always return normal strings,
395
     * VerQueryValueW will always return Unicode ones. (That means everything returned for StringFileInfo requests).
396 397
     */

398 399 400
    /* Get the VS_FIXEDFILEINFO information, this must be the same for both A- and W-Calls */ 

    retA = VerQueryValueA( pVersionInfoA, rootA, (LPVOID *)&pBufA, &uiLengthA );
401
    ok (retA, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());
402
    ok ( uiLengthA == sizeof(VS_FIXEDFILEINFO), "Size (%d) doesn't match the size of the VS_FIXEDFILEINFO struct\n", uiLengthA);
403 404 405

    if (is_unicode_enabled)
    { 
406 407 408 409 410
        if(0)
        {   /* This causes Vista and w2k8 to crash */
            retW = VerQueryValueW( pVersionInfoW, NULL, (LPVOID *)&pBufW, &uiLengthW );
            ok (retW, "VerQueryValueW failed: GetLastError = %u\n", GetLastError());
        }
411 412 413 414

        retW = VerQueryValueW( pVersionInfoW, emptyW, (LPVOID *)&pBufW, &uiLengthW );
        ok (retW, "VerQueryValueW failed: GetLastError = %u\n", GetLastError());

415
        retW = VerQueryValueW( pVersionInfoW, rootW, (LPVOID *)&pBufW, &uiLengthW );
416
        ok (retW, "VerQueryValueW failed: GetLastError = %u\n", GetLastError());
417
        ok ( uiLengthW == sizeof(VS_FIXEDFILEINFO), "Size (%d) doesn't match the size of the VS_FIXEDFILEINFO struct\n", uiLengthW );
418 419 420 421 422

        ok( uiLengthA == uiLengthW, "The size of VS_FIXEDFILEINFO should be the same for both A/W calls, it is (%d) vs. (%d)\n",
                                    uiLengthA, uiLengthW);
        ok( !memcmp(pBufA, pBufW, uiLengthA), "Both values should be the same, they aren't\n");
    }
423

424 425 426
    /* Get some VarFileInfo information, this must be the same for both A- and W-Calls */

    retA = VerQueryValueA( pVersionInfoA, varfileinfoA, (LPVOID *)&pBufA, &uiLengthA );
427
    ok (retA, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());
428 429 430 431 432
    ok( !memcmp(pBufA, WineVarFileInfoA, uiLengthA), "The VarFileInfo should have matched 0904e404 (non case sensitive)\n");

    if (is_unicode_enabled)
    { 
        retW = VerQueryValueW( pVersionInfoW, varfileinfoW, (LPVOID *)&pBufW, &uiLengthW );
433
        ok (retW, "VerQueryValueW failed: GetLastError = %u\n", GetLastError());
434 435 436 437
        ok( uiLengthA == uiLengthW, "The size of the VarFileInfo information should be the same for both A/W calls, it is (%d) vs. (%d)\n",
                                    uiLengthA, uiLengthW);
        ok( !memcmp(pBufA, pBufW, uiLengthA), "Both values should be the same, they aren't\n");
    }
438

439
    /* Get some StringFileInfo information, this will be ANSI for A-Calls and Unicode for W-Calls */
440

441
    retA = VerQueryValueA( pVersionInfoA, FileDescriptionA, (LPVOID *)&pBufA, &uiLengthA );
442
    ok (retA, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());
443 444
    ok( !lstrcmpA(WineFileDescriptionA, pBufA), "expected '%s' got '%s'\n",
        WineFileDescriptionA, pBufA);
445

446 447
    /* Test a second time */
    retA = VerQueryValueA( pVersionInfoA, FileDescriptionA, (LPVOID *)&pBufA, &uiLengthA );
448
    ok (retA, "VerQueryValueA failed: GetLastError = %u\n", GetLastError());
449 450
    ok( !lstrcmpA(WineFileDescriptionA, pBufA), "expected '%s' got '%s'\n",
        WineFileDescriptionA, pBufA);
451

452 453 454
    if (is_unicode_enabled)
    { 
        retW = VerQueryValueW( pVersionInfoW, FileDescriptionW, (LPVOID *)&pBufW, &uiLengthW );
455
        ok (retW, "VerQueryValueW failed: GetLastError = %u\n", GetLastError());
456
        ok( !lstrcmpW(WineFileDescriptionW, pBufW), "FileDescription should have been '%s'\n", WineFileDescriptionA);
457
    }
458 459

    HeapFree( GetProcessHeap(), 0, pVersionInfoA);
460 461
    if (is_unicode_enabled)
        HeapFree( GetProcessHeap(), 0, pVersionInfoW);
462 463
}

464
static void test_VerQueryValueA(void)
465 466 467 468 469 470 471 472 473 474
{
    static const char * const value_name[] = {
        "Product", "CompanyName", "FileDescription", "Internal",
        "ProductVersion", "InternalName", "File", "LegalCopyright",
        "FileVersion", "Legal", "OriginalFilename", "ProductName",
        "Company", "Original" };
    char *ver, *p;
    UINT len, ret, translation, i;
    char buf[MAX_PATH];

475
    ret = GetModuleFileNameA(NULL, buf, sizeof(buf));
476 477 478
    assert(ret);

    SetLastError(0xdeadbeef);
479 480
    len = GetFileVersionInfoSizeA(buf, NULL);
    ok(len, "GetFileVersionInfoSizeA(%s) error %u\n", buf, GetLastError());
481 482 483 484 485

    ver = HeapAlloc(GetProcessHeap(), 0, len);
    assert(ver);

    SetLastError(0xdeadbeef);
486 487
    ret = GetFileVersionInfoA(buf, 0, len, ver);
    ok(ret, "GetFileVersionInfoA error %u\n", GetLastError());
488 489 490 491

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
492
    ret = VerQueryValueA(ver, "\\VarFileInfo\\Translation", (LPVOID*)&p, &len);
493 494 495 496 497 498 499 500 501
    ok(ret, "VerQueryValue error %u\n", GetLastError());
    ok(len == 4, "VerQueryValue returned %u, expected 4\n", len);

    translation = *(UINT *)p;
    translation = MAKELONG(HIWORD(translation), LOWORD(translation));

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
502
    ret = VerQueryValueA(ver, "String", (LPVOID*)&p, &len);
503
    ok(!ret, "VerQueryValue should fail\n");
504
    ok(GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND ||
505
       GetLastError() == 0xdeadbeef /* NT4, W2K */,
506 507
       "VerQueryValue returned %u\n", GetLastError());
    ok(p == (char *)0xdeadbeef, "expected 0xdeadbeef got %p\n", p);
508
    ok(len == 0, "expected 0 got %x\n", len);
509 510 511 512

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
513
    ret = VerQueryValueA(ver, "StringFileInfo", (LPVOID*)&p, &len);
514
    ok(ret, "VerQueryValue error %u\n", GetLastError());
515
    ok(len == 0, "VerQueryValue returned %u, expected 0\n", len);
516 517 518 519 520
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
521
    ret = VerQueryValueA(ver, "\\StringFileInfo", (LPVOID*)&p, &len);
522
    ok(ret, "VerQueryValue error %u\n", GetLastError());
523
    ok(len == 0, "VerQueryValue returned %u, expected 0\n", len);
524 525 526 527 528
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
529
    ret = VerQueryValueA(ver, "\\\\StringFileInfo", (LPVOID*)&p, &len);
530
    ok(ret, "VerQueryValue error %u\n", GetLastError());
531
    ok(len == 0, "VerQueryValue returned %u, expected 0\n", len);
532 533 534 535 536
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
537
    ret = VerQueryValueA(ver, "\\StringFileInfo\\\\", (LPVOID*)&p, &len);
538
    ok(ret, "VerQueryValue error %u\n", GetLastError());
539
    ok(len == 0, "VerQueryValue returned %u, expected 0\n", len);
540 541 542 543 544 545
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");

    sprintf(buf, "\\StringFileInfo\\%08x", translation);
    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    SetLastError(0xdeadbeef);
546
    ret = VerQueryValueA(ver, buf, (LPVOID*)&p, &len);
547
    ok(ret, "VerQueryValue error %u\n", GetLastError());
548
    ok(len == 0, "VerQueryValue returned %u, expected 0\n", len);
549 550 551 552 553 554 555 556
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");

    for (i = 0; i < sizeof(value_name)/sizeof(value_name[0]); i++)
    {
	sprintf(buf, "\\StringFileInfo\\%08x\\%s", translation, value_name[i]);
        p = (char *)0xdeadbeef;
        len = 0xdeadbeef;
        SetLastError(0xdeadbeef);
557 558
        ret = VerQueryValueA(ver, buf, (LPVOID*)&p, &len);
        ok(ret, "VerQueryValueA(%s) error %u\n", buf, GetLastError());
559
        ok(len == strlen(value_name[i]) + 1, "VerQueryValue returned %u\n", len);
560 561 562 563
        ok(!strcmp(value_name[i], p), "expected \"%s\", got \"%s\"\n",
           value_name[i], p);

        /* test partial value names */
564
        len = lstrlenA(buf);
565 566 567 568
        buf[len - 2] = 0;
        p = (char *)0xdeadbeef;
        len = 0xdeadbeef;
        SetLastError(0xdeadbeef);
569 570
        ret = VerQueryValueA(ver, buf, (LPVOID*)&p, &len);
        ok(!ret, "VerQueryValueA(%s) succeeded\n", buf);
571
        ok(GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND ||
572
           GetLastError() == 0xdeadbeef /* NT4, W2K */,
573 574
           "VerQueryValue returned %u\n", GetLastError());
        ok(p == (char *)0xdeadbeef, "expected 0xdeadbeef got %p\n", p);
575
        ok(len == 0, "expected 0 or 0xbeef, got %x\n", len);
576 577 578 579 580
    }

    HeapFree(GetProcessHeap(), 0, ver);
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
static void test_extra_block(void)
{
    WORD extra_block[] = {
        72, 0, 0, 'W', 'i', 'n', 'e', 'T', 'e', 's', 't', '\0',
        24, 4, 0, 'B', 'i', 'n', 'a', 'r', 'y', '\0', 0xbeef, 0xdead,
        24, 4, 1, 'T', 'e', 'x', 't', '\0', 'B', '-', ')', '\0',
    };
    char buf[MAX_PATH];
    UINT len, ret;
    ULONG w;
    char *ver, *p;
    WORD *length;

    ret = GetModuleFileNameA(NULL, buf, sizeof(buf));
    ok(ret, "GetModuleFileNameA failed\n");

    len = GetFileVersionInfoSizeA(buf, NULL);
    ok(len, "GetFileVersionInfoSizeA(%s) error %u\n", buf, GetLastError());

    ver = HeapAlloc(GetProcessHeap(), 0, len + sizeof(extra_block) * 2);
    ok(ver != NULL, "Can't allocate memory\n");

    ret = GetFileVersionInfoA(buf, 0, len, ver);
    ok(ret, "GetFileVersionInfoA error %u\n", GetLastError());

    /* forge the string table, as windres dislike an extra block */
    length = (WORD *)ver; /* see VS_VERSION_INFO_STRUCT32 for details */
    memcpy(ver + *length, extra_block, sizeof(extra_block));
    *length += sizeof(extra_block);

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    w = 0xdeadbeef;
    ret = VerQueryValueA(ver, "WineTest\\Binary", (LPVOID*)&p, &len);
    ok(ret, "VerQueryValue error %u\n", GetLastError());
616
    ok(len == 4, "VerQueryValue returned %u, expected 4\n", len);
617
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");
618
    ok(memcmp(p, &w, sizeof(w)) == 0, "got 0x%08x, expected 0x%08x\n", *(PULONG)p, w);
619 620 621 622 623 624 625 626 627 628 629 630

    p = (char *)0xdeadbeef;
    len = 0xdeadbeef;
    ret = VerQueryValueA(ver, "WineTest\\Text", (LPVOID*)&p, &len);
    ok(ret, "VerQueryValue error %u\n", GetLastError());
    ok(len == 4, "VerQueryValue returned %u, expected 4\n", len);
    ok(p != (char *)0xdeadbeef, "not expected 0xdeadbeef\n");
    ok(strcmp(p, "B-)") == 0, "got '%s', expected '%s'\n", p, "B-)");

    HeapFree(GetProcessHeap(), 0, ver);
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
static void test_GetFileVersionInfoEx(void)
{
    char *ver, *p;
    BOOL ret;
    UINT size, translation, i;
    HMODULE mod;
    BOOL (WINAPI *pGetFileVersionInfoExW)(DWORD, LPCWSTR, DWORD, DWORD, LPVOID);
    DWORD (WINAPI *pGetFileVersionInfoSizeExW)(DWORD, LPCWSTR, LPDWORD);
    const LANGID lang = GetUserDefaultUILanguage();
    const LANGID english = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
    const WORD unicode = 1200; /* = UNICODE */
    const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
    const DWORD test_flags[] = {
        0, FILE_VER_GET_LOCALISED, FILE_VER_GET_NEUTRAL,
        FILE_VER_GET_LOCALISED | FILE_VER_GET_NEUTRAL,
        0xdeadbeef, /* invalid value (ignored) */
    };
    char desc[MAX_PATH];

650 651 652 653 654 655 656 657 658 659 660
    mod = GetModuleHandleA("kernel32.dll");
    assert(mod);

    if (!FindResourceExA(mod, (LPCSTR)RT_VERSION, (LPCSTR)VS_VERSION_INFO, lang) &&
        !FindResourceExA(mod, (LPCSTR)RT_VERSION, (LPCSTR)VS_VERSION_INFO,
                         MAKELANGID(PRIMARYLANGID(lang),SUBLANG_NEUTRAL)))
    {
        skip("Translation is not available\n");
        return;
    }

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    size = GetFileVersionInfoSizeW(kernel32W, NULL);
    ok(size, "GetFileVersionInfoSize(kernel32) error %u\n", GetLastError());

    ver = HeapAlloc(GetProcessHeap(), 0, size);
    assert(ver);

    ret = GetFileVersionInfoW(kernel32W, 0, size, ver);
    ok(ret, "GetFileVersionInfo error %u\n", GetLastError());

    ret = VerQueryValueA(ver, "\\VarFileInfo\\Translation", (void **)&p, &size);
    translation = *(UINT *)p;
    ok(ret, "VerQueryValue error %u\n", GetLastError());
    ok(size == 4, "VerQueryValue returned %u, expected 4\n", size);

    /* test default version resource */
    ok(LOWORD(translation) == lang, "got %u, expected lang is %u\n",
       LOWORD(translation), lang);
    ok(HIWORD(translation) == unicode, "got %u, expected codepage is %u\n",
       HIWORD(translation), unicode);

    HeapFree(GetProcessHeap(), 0, ver);

    mod = GetModuleHandleA("version.dll");
    assert(mod);

    /* prefer W-version as A-version is not available on Windows 7 */
    pGetFileVersionInfoExW = (void *)GetProcAddress(mod, "GetFileVersionInfoExW");
    pGetFileVersionInfoSizeExW = (void *)GetProcAddress(mod, "GetFileVersionInfoSizeExW");
    if (!pGetFileVersionInfoExW && !pGetFileVersionInfoSizeExW)
    {
        win_skip("GetFileVersionInfoEx family is not available\n");
        return;
    }

    for (i = 0; i < sizeof(test_flags)/sizeof(test_flags[0]); i++)
    {
        size = pGetFileVersionInfoSizeExW(test_flags[i], kernel32W, NULL);
        ok(size, "[%u] GetFileVersionInfoSizeEx(kernel32) error %u\n", i, GetLastError());

        ver = HeapAlloc(GetProcessHeap(), 0, size);
        assert(ver);

        ret = pGetFileVersionInfoExW(test_flags[i], kernel32W, 0, size, ver);
        ok(ret, "[%u] GetFileVersionInfoEx error %u\n", i, GetLastError());

        ret = VerQueryValueA(ver, "\\VarFileInfo\\Translation", (void **)&p, &size);
        ok(ret, "[%u] VerQueryValue error %u\n", i, GetLastError());
        ok(size == 4, "[%u] VerQueryValue returned %u, expected 4\n", i, size);
        translation = *(UINT *)p;

        /* test MUI version resource */
        if (test_flags[i] & FILE_VER_GET_LOCALISED)
            ok(LOWORD(translation) == lang, "[%u] got %u, expected lang is %u\n",
               i, LOWORD(translation), lang);
        else
            ok(LOWORD(translation) == english, "[%u] got %u, expected lang is %u\n",
               i, LOWORD(translation), english);
        ok(HIWORD(translation) == unicode, "[%u] got %u, expected codepage is %u\n",
           i, HIWORD(translation), unicode);

        /* test string info using translation info */
        size = 0;
        sprintf(desc, "\\StringFileInfo\\%04x%04x\\FileDescription",
                LOWORD(translation), HIWORD(translation));
        ret = VerQueryValueA(ver, desc, (void **)&p, &size);
        ok(ret, "[%u] VerQueryValue error %u\n", i, GetLastError());
        ok(size == strlen(p) + 1, "[%u] VerQueryValue returned %u\n", i, size);

        HeapFree(GetProcessHeap(), 0, ver);
    }

    return;
}

735 736 737
START_TEST(info)
{
    test_info_size();
738
    test_info();
739
    test_32bit_win();
740
    test_VerQueryValueA();
741
    test_extra_block();
742
    test_GetFileVersionInfoEx();
743
}