Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Versions 1

Status
Not open for further replies.

SteveD73

Programmer
Jan 5, 2001
109
GB
Hi

Is it possible to get a programs version number from an exe?

All I want to do is display an exes version number.

Thanks
 
read it from registry. If this information is absent in registry, there is no other standard way to know that. John Fill
1c.bmp


ivfmd@mail.md
 
If the version number is stored in a Version Resource you can get it from there. Here's a class I use to extract the current exe's version number from the resource:

First the ver.h header file:
Code:
//	Declarations for Version from Version Resource class
//	Written by Frank Solomon 2000
//
#ifndef __ver_h
#define __ver_h

#include <windows.h>

namespace fss {

	class	VersionResource {
		UINT	VLen;
		LPVOID	VBuf;
		char	VString[1024];
	public:
		VersionResource();
		const char *c_str() const;
	};

}	// namespace fss

#endif	//__ver_h

Then the ver.cpp implementation:

Code:
//	Definitions for Version from Version Resource class
//	Written by Frank Solomon 2000
//

#include &quot;ver.h&quot;

namespace fss {

	VersionResource::VersionResource() {
		HRSRC	hrV = FindResource(NULL,&quot;#1&quot;,RT_VERSION);
		if(hrV == NULL) {
			throw;
		} else {
			HGLOBAL hgrV = LoadResource(NULL,hrV);
			if(hgrV == NULL) {
				throw;
			} else {
				LPVOID pV = LockResource(hgrV);
				if(VerQueryValue(pV,&quot;\\StringFileInfo\\040904b0\\FileVersion&quot;,&VBuf,&VLen)) {
					if(VLen > 0 && VLen < 1024) {
						wsprintf(VString,&quot;%s&quot;,VBuf);
					} else {
						throw;
					}
				}
				FreeResource(hgrV);
			}
		}
	}

	const char *VersionResource::c_str() const {
		return (const char *)VString;
	}

}

Sorry that some of those lines may have wrapped funny. You'll need to add version.lib to your project settings under the linker options.

Also, you'll need a &quot;uses&quot; line in your code:

Code:
#include &quot;ver.h&quot;
using fss::VersionResource;
int main(int argc, const char *argv[]) {
   VersionResource	V;
   cout << &quot;Version is &quot; << V.c_str() << endl;
   return 0;
}

Of course, if your current executable doesn't have a version resource, it's not going to work. So, be sure to add one to your project and link it in.

Good luck,
Frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top