Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


We fill the VS_FIXEDFILEINFO structure using VerQueryValue().

void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;

    DWORD Zero, Size;
    char Text[40];

    Size = GetFileVersionInfoSize(“c:\\avc\\About\\debug\\About.exe”, &Zero);
        .
        .
        .
    VS_FIXEDFILEINFO *InfoStructure;                   ⇐
    VerQueryValue(VersionData, “\\”, (VOID **) &InfoStructure, &TextSize);   ⇐
        .
        .
        .

Now we get the major and minor version numbers from the VS_FIXEDFILEINFO structure’s dwProductVersionMS and dwProductVersionLS, which we decode using the right-shift operator >> and add them to FormattedString.

void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
        .
        .
        .
    VS_FIXEDFILEINFO *InfoStructure;
    VerQueryValue(VersionData, “\\”, (VOID **) &InfoStructure, &TextSize);

    int minor2 = InfoStructure->dwProductVersionLS & 0×f;                 ⇐
    int minor1 = (InfoStructure->dwProductVersionLS >> 16) & 0×f;         ⇐

    int minor = minor1 * 10 + minor2;                                     ⇐

    int major = (InfoStructure->dwProductVersionMS >> 16) & 0×f;          ⇐

    wsprintf(Text, “ version %d.%d”, major, minor);                       ⇐
    FormattedString += Text;                                              ⇐
    FormattedString += “\r\n”;                                            ⇐
        .
        .
        .

Now we have the product name and the product version. Next, we get the company name from the Version resource.

Getting the Company Name

We get the company name by reading the CompanyName resource data and adding it to the FormattedString.

void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
        .
        .
        .
    VerQueryValue(VersionData, “\\StringFileInfo\\040904b0\\CompanyName”,
&TextData, &TextSize);                               ⇐

    FormattedString += “A product of ”;        ⇐
    FormattedString += (char *) TextData;      ⇐
    FormattedString += “\r\n”;                 ⇐
        .
        .
        .

There is one more item we need to fetch from the Version resource before displaying the result in our About box: the operating system for which the product is designed.

Getting the Target Operating System Type

You get the operating system the program was designed for with the dwFileOS member of the VS_FIXEDFILEINFO structure. Possible values are as follows:

VOS__BASE              0×00000000L
VOS__PM16              0×00000002L
VOS__PM32              0×00000003L
VOS__WINDOWS16         0×00000001L
VOS__WINDOWS32         0×00000004L
VOS_DOS                0×00010000L
VOS_DOS_WINDOWS16      0×00010001L
VOS_DOS_WINDOWS32      0×00010004L
VOS_NT                 0×00040000L
VOS_NT_WINDOWS32       0×00040004L
VOS_OS216              0×00020000L
VOS_OS216_PM16         0×00020002L
VOS_OS232              0×00030000L
VOS_OS232_PM32         0×00030003L
VOS_UNKNOWN            0×00000000L

In this case, we check whether the program is designed to be a Win32 application. If so, we add that information to the About box.

void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
        .
        .
        .

    if(InfoStructure->dwFileOS == (UINT) VOS__WINDOWS32){        ⇐

        FormattedString += “Designed for Win32\r\n”;            ⇐
    }
        .
        .
        .
}


No UpdateData() Call Needed

If you wonder why we didn’t call UpdateData() after filling the dialog box’s m_text data member, recall that UpdateData() is automatically called when you display a dialog box or when the user clicks the OK button.



Figure 14.5  The About box retrieves information from the Version resource.

Finally, we place the FormattedString object into the dialog box and display that dialog box.

void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
        .
        .
        .
    if(InfoStructure->dwFileOS == (UINT) VOS__WINDOWS32){
        FormattedString += “Designed for Win32\r\n”;
    }
        .
        .
        .
    aboutDlg.m_text = FormattedString;     ⇐

    aboutDlg.DoModal();                       ⇐

}

That completes the About program. Run it now, as shown in Figure 14.5, and open the About box. As you can see, we’ve been able to retrieve information from the Version resource and display it. The About program is a success—now we’re using the often neglected Version resource.

The code for this example, About.h and About.cpp, appears in Listing 14.3. The code for About.rc appears in Listing 14.4.


Listing 14.3 About.h and About.cpp

// About.h : main header file for the ABOUT application
//

#if !defined(AFX_ABOUT_H__B9F6B2B5_AADA_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_ABOUT_H__B9F6B2B5_AADA_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
    #error include ‘stdafx.h’ before including this file for PCH
#endif

#include “resource.h”       // main symbols

/////////////////////////////////////////////////////////////////////////////
// CAboutApp:
// See About.cpp for the implementation of this class
//

class CAboutApp : public CWinApp
{
public:
    CAboutApp();

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutApp)
    public:
    virtual BOOL InitInstance();
    //}}AFX_VIRTUAL

// Implementation

    //{{AFX_MSG(CAboutApp)

    afx_msg void OnAppAbout();
        // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional
declarations immediately before the previous line.

#endif // !defined(AFX_ABOUT_H__B9F6B2B5_AADA_11D1_887F_D42B07C10710__INCLUDED_)



// About.cpp : Defines the class behaviors for the application.
//

#include “stdafx.h”
#include “About.h”

#include “MainFrm.h”
#include “ChildFrm.h”
#include “AboutDoc.h”
#include “AboutView.h”
#include “winver.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutApp

BEGIN_MESSAGE_MAP(CAboutApp, CWinApp)

    //{{AFX_MSG_MAP(CAboutApp)
    ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
        // NOTE - the ClassWizard will add and remove mapping macros here.
        //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    // Standard file based document commands
    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
    // Standard print setup command
    ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAboutApp construction

CAboutApp::CAboutApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CAboutApp object

CAboutApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CAboutApp initialization

BOOL CAboutApp::InitInstance()
{
    AfxEnableControlContainer();

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.

#ifdef _AFXDLL
    Enable3dControls();            // Call this when using MFC in a shared
DLL

#else
    Enable3dControlsStatic();    // Call this when linking to MFC statically
#endif

    // Change the registry key under which our settings are stored.
    // You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T(“Local AppWizard-Generated Applications”));

    LoadStdProfileSettings();  // Load standard INI file options (including
MRU)

    // Register the application’s document templates.  Document templates
    //  serve as the connection between documents, frame windows and views.

    CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
        IDR_ABOUTTYPE,
        RUNTIME_CLASS(CAboutDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CAboutView));
    AddDocTemplate(pDocTemplate);

    // create main MDI Frame window
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
        return FALSE;
    m_pMainWnd = pMainFrame;

    // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);

    // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
        return FALSE;

    // The main window has been initialized, so show and update it.
    pMainFrame->ShowWindow(m_nCmdShow);
    pMainFrame->UpdateWindow();


    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    CString    m_text;
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
        // No message handlers
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    m_text = _T(“”);
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    DDX_Text(pDX, IDC_STATIC1, m_text);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
        // No message handlers
    //}}AFX_MSG_MAPEND_MESSAGE_MAP()

// App command to run the dialog
void CAboutApp::OnAppAbout()
{
    CAboutDlg aboutDlg;

    DWORD Zero, Size;
    char Text[40];

    Size = GetFileVersionInfoSize(“c:\\avc\\About\\debug\\About.exe”, &Zero);

    LPVOID VersionData = new char[Size];
    GetFileVersionInfo(“c:\\avc\\About\\debug\\About.exe”, Zero, Size,
VersionData);

    LPVOID TextData;
    UINT TextSize = 0;

    VerQueryValue(VersionData, “\\StringFileInfo\\040904b0\\ProductName”,
&TextData, &TextSize);

    CString FormattedString = (char *) TextData;

    VS_FIXEDFILEINFO *InfoStructure;
    VerQueryValue(VersionData, “\\”, (VOID **) &InfoStructure, &TextSize);

    int minor2 = InfoStructure->dwProductVersionLS & 0×f;
    int minor1 = (InfoStructure->dwProductVersionLS >> 16) & 0×f;


    int minor = minor1 * 10 + minor2;

    int major = (InfoStructure->dwProductVersionMS >> 16) & 0×f;
    wsprintf(Text, “ version %d.%d”, major, minor);
    FormattedString += Text;
    FormattedString += “\r\n”;

    VerQueryValue(VersionData, “\\StringFileInfo\\040904b0\\CompanyName”,
&TextData, &TextSize);

    FormattedString += “A product of ”;
    FormattedString += (char *) TextData;
    FormattedString += “\r\n”;

    if(InfoStructure->dwFileOS == (UINT) VOS__WINDOWS32){
        FormattedString += “Designed for Win32\r\n”;
    }

    aboutDlg.m_text = FormattedString;

    aboutDlg.DoModal();

}


/////////////////////////////////////////////////////////////////////////////
// CAboutApp commands


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.