[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get OCX version

Posted on 2004-04-10
5
Medium Priority
?
845 Views
Last Modified: 2007-12-19
Using .NET, how can I determine the the version when given some file "XYZ.ocx"?
0
Comment
Question by:farsight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
caner_elci earned 500 total points
ID: 10796497
Here, I converted this function from one of my old MFC applications. It works well.. but you should do some error handling if you want.. I just skipped that by a try-catch and didn't check the return values of API calls... Anyway, it's an unsafe code in .NET, so you should compile with /unsafe option (you can set it in project properties)

[DllImport("version.dll")]
public static extern bool GetFileVersionInfo (string sFileName, int handle, int size, byte[] infoBuffer);
[DllImport("version.dll")]
public static extern int GetFileVersionInfoSize (string sFileName, out int handle);
[DllImport("version.dll")]
unsafe public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len);
[DllImport("version.dll")]
unsafe public static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out short *pValue, out uint len);

unsafe public string GetFileVersion( string fileName )
{
    try
    {
        int handle = 0;
        int size = GetFileVersionInfoSize( fileName, out handle );
        if( size == 0 )
            return "Cannot get version info size";
        byte[] buff = new byte[size];
        GetFileVersionInfo( fileName, handle, size, buff );
        short *pSubBlock = null;
        uint len = 0;
        VerQueryValue( buff, @"\VarFileInfo\Translation", out pSubBlock, out len );
        string prodVer = @"\StringFileInfo\" + pSubBlock[0].ToString( "X4" ) + pSubBlock[1].ToString( "X4" ) + @"\ProductVersion";
        byte *pVersion = null;
        string versionInfo;
        VerQueryValue( buff, prodVer, out versionInfo, out len );
        return versionInfo;
    }
    catch( Exception ex )
    {
        return ex.ToString();
    }
}


Caner ELCI
0
 
LVL 12

Author Comment

by:farsight
ID: 10796648
I found this on my own.  I'm checking to see if the results are the same.

---

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsFileVersionInfoClassGetVersionInfoTopic.asp

Here's VB.NET Sample code. I tried to pick OCXes I thought would be on your system.  It shows the "Product Version", not the "File Version", at least in cases like comdlg32.ocx.

comdlg32.ocx
  File    Version: 6.00.88.77
  Product Version: 6.00.8877

--- VB.NET ---

    Public Shared Sub Test_GetFileVersion()
        Dim filenames As String() = New String() { _
            "%systemroot%\Notepad.exe", _
            "%systemroot%\comctl32.ocx", _
            "%systemroot%\comdlg32.ocx", _
            "%systemroot%\mci32.ocx", _
            "%systemroot%\MSCOMM32.ocx", _
            "%systemroot%\mswinsck.ocx", _
            "%systemroot%\richtx32.ocx", _
            "%systemroot%\sysinfo.ocx", _
            "%systemroot%\sysmon.ocx", _
            "noSuchFile.ocx" _
            }

        For Each filename As String In filenames
            Try
                ' Get the file version for the notepad.
                Dim myFileVersionInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo( _
                    filename.Replace("%systemroot%", System.Environment.SystemDirectory))
                ' Print the file name and version number.
                Debug.WriteLine("Filename: " & filename & System.Environment.NewLine & _
                    "  File: " & myFileVersionInfo.FileDescription & System.Environment.NewLine & _
                    "  Version number: " & myFileVersionInfo.FileVersion)
            Catch
                Debug.WriteLine("File: " & filename & " -- not found.")
            End Try
        Next
    End Sub

--- Output ---

Filename: %systemroot%\Notepad.exe
  File: Notepad
  Version number: 5.00.2140.1
Filename: %systemroot%\comctl32.ocx
  File: Windows Common Controls ActiveX Control DLL
  Version number: 6.00.8022
Filename: %systemroot%\comdlg32.ocx
  File: CMDialog ActiveX Control DLL
  Version number: 6.00.8877
Filename: %systemroot%\mci32.ocx
  File: MCI OLE Control DLL
  Version number: 6.00.8418
Filename: %systemroot%\MSCOMM32.ocx
  File: MSComm
  Version number: 6.00.8169
Filename: %systemroot%\mswinsck.ocx
  File: Microsoft Winsock Control DLL
  Version number: 6.00.8877
Filename: %systemroot%\richtx32.ocx
  File: RichTx32.OCX
  Version number: 6.00.8877
Filename: %systemroot%\sysinfo.ocx
  File: SysInfo
  Version number: 6.00.8877
Filename: %systemroot%\sysmon.ocx
  File: System Monitor Control
  Version number: 5.00.2195.6660
File: noSuchFile.ocx -- not found.

0
 
LVL 12

Author Comment

by:farsight
ID: 10796668
I added a little driver to your code, and tried it.
It does indeed produce the same results.
Thanks for the effort.

--- driver --- C# ---

            unsafe public void Test_GetFileVersion()
            {
                  String[] filenames = {
                        @"%systemroot%\Notepad.exe",
                        @"%systemroot%\comctl32.ocx",
                        @"%systemroot%\comdlg32.ocx",
                        @"%systemroot%\mci32.ocx",
                        @"%systemroot%\MSCOMM32.ocx",
                        @"%systemroot%\mswinsck.ocx",
                        @"%systemroot%\richtx32.ocx",
                        @"%systemroot%\sysinfo.ocx",
                        @"%systemroot%\sysmon.ocx",
                        @"noSuchFile.ocx"
                  };

                  foreach (String filename in filenames)
                  {
                        try
                        {
                              // Get the file version for the notepad.
                              string version = GetFileVersion(filename.Replace("%systemroot%", System.Environment.SystemDirectory));
                              // Print the file name and version number.
                              Debug.WriteLine("Filename: " + filename + System.Environment.NewLine +
                                    "  Version number: " + version);
                        }
                        catch (Exception ex)
                        {
                              Debug.WriteLine("File: " + filename + " -- not found.");
                        }
                  } // foreach
            } // Test_GetFileVersion

--- Output ---

Filename: %systemroot%\Notepad.exe
  Version number: 5.00.2140.1
Filename: %systemroot%\comctl32.ocx
  Version number: 6.00.8022
Filename: %systemroot%\comdlg32.ocx
  Version number: 6.00.8877
Filename: %systemroot%\mci32.ocx
  Version number: 6.00.8418
Filename: %systemroot%\MSCOMM32.ocx
  Version number: 6.00.8169
Filename: %systemroot%\mswinsck.ocx
  Version number: 6.00.8877
Filename: %systemroot%\richtx32.ocx
  Version number: 6.00.8877
Filename: %systemroot%\sysinfo.ocx
  Version number: 6.00.8877
Filename: %systemroot%\sysmon.ocx
  Version number: 5.00.2195.6660
Filename: noSuchFile.ocx
  Version number: Cannot get version info size
0
 
LVL 12

Author Comment

by:farsight
ID: 10796696
caner_elci,
Please post a tiny "I answered that question." message at this link, so I can give you those points, too.
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20949611.html
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10796897
Ok, done.. Have a nice coding..

Caner
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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