Solved

Determine JVM version using C++

Posted on 2004-08-07
4
252 Views
Last Modified: 2010-04-01
Hi,

How do I determine the version number of the currently running JVM using C++ on a Window OS machine?

I have considered accessing jview (either as a dll or as a ShellExecute command to get the version number of the file), but I'm curious to see if there are better solutions.

Any comments, advice or suggestions will be handsomely rewarded. :)

TIA,
S/.
0
Comment
Question by:stitch2802
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 11744004
Spawning "jview.exe /?" gives the version number of the installed JVM. This info can also be obtained using

#include <windows.h>
#include <iostream>
using namespace std;

#pragma comment(lib,"version.lib")

   void reportError()
   {
       LPVOID lpMsgBuf;

       FormatMessage(
           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
           NULL,
           GetLastError(),
           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
           (LPTSTR) &lpMsgBuf,
           0,
           NULL );

       cout << (char*)lpMsgBuf << "\n";

       // Free the buffer.
       LocalFree( lpMsgBuf );
   }

   void main( int argc, char *argv[ ], char *envp[ ] )
   {
       cout << "\n";

       if(2 != argc)
       {
           cout << "Syntax: GetVer <File Name>\n";
           return;
       }

       DWORD dwArg;
       DWORD dwInfoSize = GetFileVersionInfoSize(argv[1], &dwArg);

       if(0 == dwInfoSize)
       {
           cout << "No version info available\n";
           reportError();
           return;
       }

       BYTE* lpBuff = new BYTE[dwInfoSize];

       if(!lpBuff)
       {
           cout << "Out of Memory\n";
           return;
       }

       if(0 == GetFileVersionInfo(argv[1], 0, dwInfoSize, lpBuff))
       {
           cout << "Error retrieving version info\n";
           reportError();
           return;
       }

       VS_FIXEDFILEINFO *vInfo;

       UINT uInfoSize;

       if(0 == VerQueryValue(lpBuff, TEXT("\\"),
               (LPVOID*)&vInfo,
               &uInfoSize))
       {
           cout << "Version information not available\n";
           delete lpBuff;
           return;
       }

       if(0 == uInfoSize)
       {
           cout << "Version information not available\n";
           delete lpBuff;
           return;
       }

       cout << argv[1]
           << " Version: "
           << HIWORD(vInfo->dwFileVersionMS) << ","
           << LOWORD(vInfo->dwFileVersionMS) << ","
           << HIWORD(vInfo->dwFileVersionLS) << ","
           << LOWORD(vInfo->dwFileVersionLS) << "\n";

       delete lpBuff;
   }

(original code taken from http://support.microsoft.com/default.aspx?scid=kb;en-us;167597)
0
 

Author Comment

by:stitch2802
ID: 11744925
Hi jkr,

Thanks for answering, but supppose the user has both JVM versions on his/her computer - as in both the Microsoft JVM and the Java VM - won't jview just give me information about the Microsoft JVM and none whatsoever for the Java VM?

I'm trying to figure which version is running as well.

Any advice? :)

TIA,
S/.

0
 
LVL 86

Expert Comment

by:jkr
ID: 11744994
Well, then just grab the version info from all files in queston :o)

(I'm not too familiar with Java and it's VMs, but obtaining the version information is the same for all)
0
 

Author Comment

by:stitch2802
ID: 11744996
Ok, thanks jkr! =)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now