• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Determine JVM version using C++

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
stitch2802
Asked:
stitch2802
  • 2
  • 2
1 Solution
 
jkrCommented:
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
 
stitch2802Author Commented:
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
 
jkrCommented:
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
 
stitch2802Author Commented:
Ok, thanks jkr! =)

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now