delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/01/19/08:03:29

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Wed, 19 Jan 2005 08:03:00 -0500
From: Jason Tishler <jason AT tishler DOT net>
To: cygwin AT cygwin DOT com
Subject: Re: looking for shell program to retrieve property info from windows files
Message-ID: <20050119130300.GA3452@tishler.net>
Mail-Followup-To: cygwin AT cygwin DOT com
References: <200501181915090630 DOT 3FC77633 AT smtp DOT atl DOT yahoo DOT com>
Mime-Version: 1.0
In-Reply-To: <200501181915090630.3FC77633@smtp.atl.yahoo.com>
User-Agent: Mutt/1.4.1i
X-IsSubscribed: yes

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Jaye,

On Tue, Jan 18, 2005 at 07:15:09PM -0500, Jaye Speaks wrote:
> does anyone know of a shell program to retrieve the property info from
> windows files.  I need info like the fileversion or prodversion.

No, so I wrote my own:

    $ version 'C:\Program Files\Microsoft Office\OFFICE11\WINPROJ.EXE'
    Required Information:
        ProductVersion = 11.0.2003.816
        FileVersion = 11.0.2003.816
    Optional Information:

    $ cygversion /mnt/c/Program\ Files/Microsoft\ Office/OFFICE11/WINPROJ.EXE 
    Required Information:
        ProductVersion = 11.0.2003.816
        FileVersion = 11.0.2003.816
    Optional Information:

See attached for the source and Makefile.

Note the Makefile builds two executables:

    version.exe:    Mingw executable
    cygversion.exe: Cygwin executable

Jason

-- 
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D  8784 1AFD E4CC ECF4 8EF6

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="version.cc"

/*
 * Copyright (c) 2005 Jason Tishler
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * A copy of the GNU General Public License can be found at
 * http://www.gnu.org/
 *
 * Written by Jason Tishler <jason AT tishler DOT net>
 *
 * $Id: version.cc,v 1.6 2005/01/19 14:49:52 jt Exp $
 */

#include <windows.h>
#include <stdio.h>
#ifdef __CYGWIN__
#include <sys/cygwin.h>
#endif

const char* GetVersionString(const void* aVersionInfo,
	const char* aVersionName);
const VS_FIXEDFILEINFO* GetFixedFileInfo(const void* aVersionInfo);
void PrintRequiredVersionInfo(const VS_FIXEDFILEINFO* aFixedFileInfo);
void PrintOptionalVersionInfo(char* aVersionInfo);
bool GetFullPathName(const char* aFileName, char* aPathName,
	unsigned int aSize);
void PrintUsage();

const char* aVersionNames[] =
{
	"Comments",
	"CompanyName",
	"FileDescription",
	"FileVersion",
	"InternalName",
	"LegalCopyright",
	"LegalTrademarks",
	"OriginalFilename",
	"PrivateBuild",
	"ProductName",
	"ProductVersion",
	"SpecialBuild"
};

int
main(int argc, const char* argv[])
{
	if (argc < 2)
	{
		PrintUsage();
		exit(1);
	}

	for (int i = 1; i < argc; i++)
	{
		char aPathName[MAX_PATH + 1];
		const char* aFileName = argv[i];
		GetFullPathName(aFileName, aPathName, sizeof(aPathName));

		if (argc > 2)
		{
			printf("%s%s:\n", (i != 1) ? "\n" : "", aFileName);
		}

		DWORD aHandle = 0;
		DWORD aSize = GetFileVersionInfoSize(aPathName, &aHandle);
		if (aSize == 0)
		{
			exit(2);
		}

		char* aVersionInfo = new char[aSize];
		BOOL aStatus = GetFileVersionInfo(aPathName, aHandle, aSize, aVersionInfo);
		if (!aStatus)
		{
			delete [] aVersionInfo;
			exit(3);
		}

		const VS_FIXEDFILEINFO* aFixedFileInfo = GetFixedFileInfo(aVersionInfo);
		PrintRequiredVersionInfo(aFixedFileInfo);

		PrintOptionalVersionInfo(aVersionInfo);

		delete [] aVersionInfo;
	}

	exit(0);
}

const char*
GetVersionString(const void* aVersionInfo, const char* aVersionName)
{
	const char* aSeparator = "\\";
	const char* aPrefix = "StringFileInfo";
	const char* aLangCodePage = "040904b0";
	char aVersionPath[MAX_PATH + 1];

	strcpy(aVersionPath, aSeparator);
	strcat(aVersionPath, aPrefix);
	strcat(aVersionPath, aSeparator);
	strcat(aVersionPath, aLangCodePage);
	strcat(aVersionPath, aSeparator);
	strcat(aVersionPath, aVersionName);

	void* aBuffer = 0;
	UINT aSize = 0;
	VerQueryValue((void*) aVersionInfo, aVersionPath, &aBuffer, &aSize);
	return (const char*) aBuffer;
}

const VS_FIXEDFILEINFO*
GetFixedFileInfo(const void* aVersionInfo)
{
	void* aBuffer = 0;
	UINT aSize = 0;
	const char* aRootBlock = "\\";
	VerQueryValue((void*) aVersionInfo, (char*) aRootBlock, &aBuffer, &aSize);
	return (const VS_FIXEDFILEINFO*) aBuffer;
}

void
PrintRequiredVersionInfo(const VS_FIXEDFILEINFO* aFixedFileInfo)
{
	if (!aFixedFileInfo)
		return;

	printf("Required Information:\n");
	printf("    ProductVersion = %d.%d.%d.%d\n",
		aFixedFileInfo->dwProductVersionMS >> 16,
		aFixedFileInfo->dwProductVersionMS & 0xffff,
		aFixedFileInfo->dwProductVersionLS >> 16,
		aFixedFileInfo->dwProductVersionLS & 0xffff);
	printf("    FileVersion = %d.%d.%d.%d\n",
		aFixedFileInfo->dwFileVersionMS >> 16,
		aFixedFileInfo->dwFileVersionMS & 0xffff,
		aFixedFileInfo->dwFileVersionLS >> 16,
		aFixedFileInfo->dwFileVersionLS & 0xffff);
}

void
PrintOptionalVersionInfo(char* aVersionInfo)
{
	printf("Optional Information:\n");
	int aNumVersionNames = sizeof(aVersionNames) / sizeof(const char*);
	for (int i = 0; i < aNumVersionNames; i++)
	{
		const char* aVersionName = aVersionNames[i];
		const char* aVersionString =
			GetVersionString(aVersionInfo, aVersionName);
		if (aVersionString)
			printf("    %s: %s\n", aVersionName, aVersionString);
	}
}

bool
GetFullPathName(const char* aFileName, char* aPathName, unsigned int aSize)
{
#ifdef __CYGWIN__
	cygwin_conv_to_full_win32_path(aFileName, aPathName);
	return true;
#else
	char *aDummy;
	DWORD aStatus = GetFullPathName (aFileName, aSize, aPathName, &aDummy);
	return (aStatus > 0);
#endif // __CYGWIN__
}

void
PrintUsage()
{
	printf("usage: version files...\n");
}

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=Makefile

#
# Copyright (c) 2005 Jason Tishler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# A copy of the GNU General Public License can be found at
# http://www.gnu.org/
#
# Written by Jason Tishler <jason AT tishler DOT net>
#
# $Id: Makefile,v 1.3 2005/01/19 14:50:10 jt Exp $
#

all: version cygversion

CC = g++
CFLAGS = -O2 -s
Files = version.cc
Libs = -lversion

version: $(Files)
	$(CC) $(CFLAGS) -mno-cygwin -o $@ $(Files) $(Libs)

cygversion: $(Files)
	$(CC) $(CFLAGS) -o $@ $(Files) $(Libs)

clean:
	$(RM) *.exe


--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
--cNdxnHkX5QqsyA0e--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019