Skip to content

Instantly share code, notes, and snippets.

@code-walkers
Created January 29, 2010 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-walkers/290232 to your computer and use it in GitHub Desktop.
Save code-walkers/290232 to your computer and use it in GitHub Desktop.
EasyInstaller Changes for installation on Win7
// CreateInstallerDlg.cpp : implementation file
//
// This is the program that creates the Installer (self-extracting exe) program:
// Run this program to create your install program.
//
#include "stdafx.h"
#include "CreateInstaller.h"
#include "CreateInstallerDlg.h"
#include "zip.h"
#include ".\createinstallerdlg.h"
#include "about.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCreateInstallerDlg dialog
CCreateInstallerDlg::CCreateInstallerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCreateInstallerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCreateInstallerDlg)
m_target = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCreateInstallerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCreateInstallerDlg)
DDX_Control(pDX, IDC_INLIST, m_lstInFiles);
DDX_Control(pDX, IDC_OUTFNAME, m_edtTarget);
DDX_Text(pDX, IDC_OUTFNAME, m_target);
DDX_Text(pDX, IDC_EDIT3, m_license);
DDX_Text(pDX, IDC_EDIT4, m_info);
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_COMBO1, m_cmbProjectList);
DDX_Control(pDX, IDC_CHECK1, m_btn95);
DDX_Control(pDX, IDC_CHECK2, m_btn98);
DDX_Control(pDX, IDC_CHECK3, m_btnME);
DDX_Control(pDX, IDC_CHECK4, m_btnNT35);
DDX_Control(pDX, IDC_CHECK5, m_btnNT4);
DDX_Control(pDX, IDC_CHECK6, m_btn2K);
DDX_Control(pDX, IDC_CHECK7, m_btnXP);
DDX_Control(pDX, IDC_CHECK8, m_btn2K3);
DDX_Control(pDX, IDC_CHECK9, m_btnVista);
DDX_Control(pDX, IDC_CHECK10, m_btnWin7);
DDX_Control(pDX, IDC_EDIT1, m_edtWebCaption);
DDX_Control(pDX, IDC_EDIT2, m_edtWebURL);
DDX_Control(pDX, IDC_EDIT3, m_edtLicense);
DDX_Control(pDX, IDC_EDIT4, m_edtInfo);
DDX_Control(pDX, IDC_PFDIR, m_edtProgramFilesDirectory);
DDX_Control(pDX, IDC_PFNAME, m_edtProjectFilename);
DDX_Control(pDX, IDC_NESTEDREPOSITORY, m_btnNestedRepository);
DDX_Control(pDX, IDC_NEEDREPOSITORY, m_btnNeedRepository);
}
BEGIN_MESSAGE_MAP(CCreateInstallerDlg, CDialog)
//{{AFX_MSG_MAP(CCreateInstallerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_MAKEEXE, OnMakeInstaller)
ON_BN_CLICKED(IDC_INBROWSE, OnInBrowse)
ON_BN_CLICKED(IDC_OUTBROWSE, OnOutBrowse)
ON_BN_CLICKED(IDC_DELINFILES, OnDelinfiles)
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE(IDC_COMBO1, OnCbnSelchangeCombo1)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCreateInstallerDlg message handlers
BOOL CCreateInstallerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
char path[MAX_PATH];
char dir[MAX_PATH];
char drive[MAX_PATH];
char file[MAX_PATH];
char ext[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
_splitpath(path, drive, dir, file, ext);
iniFileName.Format("%s%sCreateSetup.ini", drive, dir);
TCHAR lastProject[100];
int ret;
ret = GetPrivateProfileString("CONFIG", "last", "", lastProject, 99, iniFileName);
if(!ret)
m_cmbProjectList.SetWindowText("Project");
else
GetProject(lastProject);
TCHAR sections[10000];
PTCHAR ss = sections;
GetPrivateProfileSectionNames(sections, sizeof(TCHAR)*10000, iniFileName);
// loop through all section names - skip CONFIG
CString s;
int idx = 0;
int sel = -1;
while(1)
{
s.Format("%s", ss);
if(s.GetLength() == 0)
break;
ss += s.GetLength() + 1;
if(!s.Compare("CONFIG"))
continue;
m_cmbProjectList.AddString(s);
if(!s.Compare(lastProject))
sel = idx;
idx++;
}
m_cmbProjectList.SetCurSel(sel);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCreateInstallerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCreateInstallerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCreateInstallerDlg::SetFileListScroll()
{
// Find the longest string in the list box.
CString str;
CSize sz;
int dx = 0;
TEXTMETRIC tm;
CDC* pDC = m_lstInFiles.GetDC();
CFont* pFont = m_lstInFiles.GetFont();
// Select the listbox font, save the old font
CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
pDC->GetTextMetrics(&tm);
for (int i = 0; i < m_lstInFiles.GetCount(); i++)
{
m_lstInFiles.GetText(i, str);
sz = pDC->GetTextExtent(str);
// Add the avg width to prevent clipping
sz.cx += tm.tmAveCharWidth;
if (sz.cx > dx)
dx = sz.cx;
}
// Select the old font back into the DC
pDC->SelectObject(pOldFont);
m_lstInFiles.ReleaseDC(pDC);
// Set the horizontal extent so every character of all strings
// can be scrolled to.
m_lstInFiles.SetHorizontalExtent(dx);
}
// Shows browse dialog box for addition of new files in list
void CCreateInstallerDlg::OnInBrowse()
{
UpdateData();
static CString strFName;
CFileDialog dlg(TRUE, NULL, strFName, OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, "All Files(*.*)|*.*||", this);
if(dlg.DoModal() == IDOK)
{
// Deselect any selected items in list
for(int i = 0; i < m_lstInFiles.GetCount(); ++i)
m_lstInFiles.SetSel(i,FALSE);
// Add each file name selected to list
for(POSITION pos = dlg.GetStartPosition(); pos != NULL; )
{
strFName = dlg.GetNextPathName(pos);
// If this is the first file name in list create an exe name and set it as output file
if(m_lstInFiles.GetCount() == 0)
{
if(strFName.ReverseFind('.') == -1)
m_target += ".exe";
else
m_target = strFName.Left(strFName.ReverseFind('.')) + ".exe";
}
// Add and Select new file name
m_lstInFiles.SetSel(m_lstInFiles.AddString(strFName));
}
UpdateData(FALSE);
m_lstInFiles.SetFocus();
}
SetFileListScroll();
}
// delete the selected files in list
void CCreateInstallerDlg::OnDelinfiles()
{
// Create an array to store selected items
int nCount = m_lstInFiles.GetSelCount();
CArray<int,int> aryListBoxSel;
aryListBoxSel.SetSize(nCount);
m_lstInFiles.GetSelItems(nCount, aryListBoxSel.GetData());
// Delete the selected items while considering that indexes will be reduced by deletion of an item
for(int i = 0; i < nCount; ++i)
m_lstInFiles.DeleteString(aryListBoxSel[i] - i);
}
// Show browse dialog box for output file name
void CCreateInstallerDlg::OnOutBrowse()
{
UpdateData();
CFileDialog dlg(FALSE, NULL, m_target, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "All Files(*.)|*.*||", this);
if(dlg.DoModal() == IDOK)
{
m_target = dlg.GetPathName();
UpdateData(FALSE);
m_edtTarget.SetSel(0, -1);
m_edtTarget.SetFocus();
}
}
void CCreateInstallerDlg::OnMakeInstaller()
{
UpdateData();
if(m_target.IsEmpty())
{
AfxMessageBox("You need to enter in an Output path (full path name and file name).");
return;
}
CString str;
wrap.Init();
// project name to class
CString prjName;
m_cmbProjectList.GetWindowText(prjName);
wrap.SetProjectName(prjName);
// clear the proj parms so that we can save the new ones
WritePrivateProfileSection(prjName, NULL, iniFileName);
// add files to class
int numFiles = m_lstInFiles.GetCount();
if(numFiles == 0)
{
AfxMessageBox("Please enter at least one input file name");
return;
}
for(int i = 0; i < numFiles; ++i)
{
// add to wrap class
CString str1;
m_lstInFiles.GetText(i, str);
wrap.AddFile(str);
// write to config file
TCHAR addStr[10];
sprintf(addStr, "add%02d", i+1);
WritePrivateProfileString(prjName, addStr, str, iniFileName);
}
// agreement
// web site caption
m_edtWebCaption.GetWindowText(str);
wrap.SetWebSiteCaption(str);
WritePrivateProfileString(prjName, "webcaption", str, iniFileName);
// web site URL
m_edtWebURL.GetWindowText(str);
wrap.SetWebSiteURL(str);
WritePrivateProfileString(prjName, "webURL", str, iniFileName);
// license agreement
m_edtLicense.GetWindowText(str);
wrap.AddAgreement(str);
WritePrivateProfileString(prjName, "agreement", str, iniFileName);
// info file
m_edtInfo.GetWindowText(str);
wrap.AddInfo(str);
WritePrivateProfileString(prjName, "info", str, iniFileName);
// "Program Files" directory
m_edtProgramFilesDirectory.GetWindowText(str);
wrap.SetProgramFilesDirectory(str);
WritePrivateProfileString(prjName, "pfdir", str, iniFileName);
// "Project Filename"
m_edtProjectFilename.GetWindowText(str);
wrap.SetProjectFilename(str);
WritePrivateProfileString(prjName, "pfname", str, iniFileName);
if (m_btnNestedRepository.GetCheck()) {
/* TODO: Store the information that we do not want the installer to ask for the repository location. */
}
if (m_btnNeedRepository.GetCheck()) {
/* TODO: Store the information that we need the installer to process the repository location. */
} else {
/* TODO: Store the information that we need the installer to ignore repository processing. */
}
// which version of Windows
DWORD winVer = 0;
if(m_btn95.GetCheck())
winVer |= 0x01;
if(m_btn98.GetCheck())
winVer |= 0x02;
if(m_btnME.GetCheck())
winVer |= 0x04;
if(m_btnNT35.GetCheck())
winVer |= 0x10;
if(m_btnNT4.GetCheck())
winVer |= 0x20;
if(m_btn2K.GetCheck())
winVer |= 0x40;
if(m_btnXP.GetCheck())
winVer |= 0x80;
if(m_btn2K3.GetCheck())
winVer |= 0x0100;
if(m_btnVista.GetCheck())
winVer |= 0x0200;
if(m_btnWin7.GetCheck())
winVer |= 0x0300;
wrap.SetWinVer(winVer);
str.Format(_T("%d"), winVer);
WritePrivateProfileString(prjName, "winver", str, iniFileName);
DWORD extraFlags = 0x0000;
if (m_btnNestedRepository.GetCheck()) {
extraFlags |= EF_NESTED_REPOSITORY;
}
if (m_btnNeedRepository.GetCheck()) {
extraFlags |= EF_NEED_REPOSITORY;
} else {
extraFlags &= ~EF_NEED_REPOSITORY;
}
wrap.SetExtraFlags(extraFlags);
str.Format(_T("%d"),extraFlags);
WritePrivateProfileString(prjName,"extraflags",str,iniFileName);
wrap.SetTargetName(m_target);
WritePrivateProfileString(prjName, "out", m_target, iniFileName);
// Now do the install
wrap.CreateInstaller();
CString strMsg;
strMsg.Format("%s successfully created from %d files.", m_target, m_lstInFiles.GetCount());
WritePrivateProfileString("CONFIG", "last", prjName, iniFileName);
AfxMessageBox(strMsg, MB_ICONINFORMATION | MB_OK);
UpdateData(FALSE);
}
void CCreateInstallerDlg::OnCbnSelchangeCombo1()
{
int sel = m_cmbProjectList.GetCurSel();
CString selStr;
m_cmbProjectList.GetLBText(sel, selStr);
GetProject(selStr);
}
void CCreateInstallerDlg::GetProject(CString prj)
{
TCHAR str[255];
int rc;
int idx = 1;
TCHAR addStr[10];
m_lstInFiles.ResetContent();
while(1)
{
sprintf(addStr, "add%02d", idx);
rc = GetPrivateProfileString(prj, addStr, "", str, 254, iniFileName);
if(!rc)
break;
m_lstInFiles.AddString(str);
idx++;
}
SetFileListScroll();
rc = GetPrivateProfileString(prj, _T("out"), _T(""), str, 254, iniFileName);
m_edtTarget.SetWindowText(str);
int set;
DWORD extraFlags = GetPrivateProfileInt(prj,"extraflags",0,iniFileName);
set = (extraFlags & EF_NESTED_REPOSITORY) ? 1 : 0;
m_btnNestedRepository.SetCheck(set);
set = (extraFlags & EF_NEED_REPOSITORY) ? 1 : 0;
m_btnNeedRepository.SetCheck(set);
DWORD winVer = GetPrivateProfileInt(prj, "winver", 0, iniFileName);
set = (winVer & 0x01) ? 1 : 0;
m_btn95.SetCheck(set);
set = (winVer & 0x02) ? 1 : 0;
m_btn98.SetCheck(set);
set = (winVer & 0x04) ? 1 : 0;
m_btnME.SetCheck(set);
set = (winVer & 0x10) ? 1 : 0;
m_btnNT35.SetCheck(set);
set = (winVer & 0x20) ? 1 : 0;
m_btnNT4.SetCheck(set);
set = (winVer & 0x40) ? 1 : 0;
m_btn2K.SetCheck(set);
set = (winVer & 0x80) ? 1 : 0;
m_btnXP.SetCheck(set);
set = (winVer & 0x0100) ? 1 : 0;
m_btn2K3.SetCheck(set);
set = (winVer & 0x0200) ? 1 : 0;
m_btnVista.SetCheck(set);
set = (winVer & 0x0300) ? 1 : 0;
m_btnWin7.SetCheck(set);
CString s;
s.Format(_T("%s web site"), prj);
rc = GetPrivateProfileString(prj, _T("webcaption"), s, str, 99, iniFileName);
m_edtWebCaption.SetWindowText(str);
rc = GetPrivateProfileString(prj, _T("webURL"), _T("< web site URL >"), str, 99, iniFileName);
m_edtWebURL.SetWindowText(str);
rc = GetPrivateProfileString(prj, _T("agreement"), _T(""), str, 99, iniFileName);
m_edtLicense.SetWindowText(str);
rc = GetPrivateProfileString(prj, _T("info"), _T(""), str, 99, iniFileName);
m_edtInfo.SetWindowText(str);
rc = GetPrivateProfileString(prj, _T("pfdir"), _T(""), str, 99, iniFileName);
m_edtProgramFilesDirectory.SetWindowText(str);
rc = GetPrivateProfileString(prj, _T("pfname"), _T(""), str, 99, iniFileName);
m_edtProjectFilename.SetWindowText(str);
}
void CCreateInstallerDlg::OnBnClickedButton1()
{
CString sel;
m_cmbProjectList.GetLBText(m_cmbProjectList.GetCurSel(), sel);
CString s;
s.Format(_T("Are you sure you want to delete the %s project?"), sel);
int rc = AfxMessageBox(s, MB_ICONEXCLAMATION | MB_YESNO);
if(rc == IDYES)
{
// delete project
WritePrivateProfileSection(sel, NULL, iniFileName);
m_cmbProjectList.DeleteString(m_cmbProjectList.GetCurSel());
}
}
void CCreateInstallerDlg::OnBnClickedButton2()
{
CAbout ca;
ca.DoModal();
}
void CCreateInstallerDlg::OnBnClickedButton3()
{
CString str;
CString prjName;
m_cmbProjectList.GetWindowText(prjName);
// clear the proj parms so that we can save the new ones
WritePrivateProfileSection(prjName, NULL, iniFileName);
// save project files
int numFiles = m_lstInFiles.GetCount();
if(numFiles == 0)
{
AfxMessageBox("Please enter at least one input file name");
return;
}
for(int i = 0; i < numFiles; ++i)
{
// write to config file
TCHAR addStr[10];
sprintf(addStr, "add%02d", i+1);
WritePrivateProfileString(prjName, addStr, str, iniFileName);
}
// web site caption
m_edtWebCaption.GetWindowText(str);
WritePrivateProfileString(prjName, "webcaption", str, iniFileName);
// web site URL
m_edtWebURL.GetWindowText(str);
WritePrivateProfileString(prjName, "webURL", str, iniFileName);
m_edtProgramFilesDirectory.GetWindowText(str);
WritePrivateProfileString(prjName, "pfdir", str, iniFileName);
// which version of Windows
DWORD winVer = 0;
if(m_btn95.GetCheck())
winVer |= 0x01;
if(m_btn98.GetCheck())
winVer |= 0x02;
if(m_btnME.GetCheck())
winVer |= 0x04;
if(m_btnNT35.GetCheck())
winVer |= 0x10;
if(m_btnNT4.GetCheck())
winVer |= 0x20;
if(m_btn2K.GetCheck())
winVer |= 0x40;
if(m_btnXP.GetCheck())
winVer |= 0x80;
if(m_btn2K3.GetCheck())
winVer |= 0x0100;
if(m_btnVista.GetCheck())
winVer |= 0x0200;
if(m_btnWin7.GetCheck())
winVer |= 0x0300;
str.Format(_T("%d"), winVer);
WritePrivateProfileString(prjName, "winver", str, iniFileName);
DWORD extraFlags = 0;
if (m_btnNestedRepository.GetCheck()) {
extraFlags |= EF_NESTED_REPOSITORY;
}
if (m_btnNeedRepository.GetCheck()) {
extraFlags |= EF_NEED_REPOSITORY;
}
str.Format(_T("%d"),extraFlags);
WritePrivateProfileString(prjName,"extraflags",str,iniFileName);
// installer target name
WritePrivateProfileString(prjName, "out", m_target, iniFileName);
}
void CCreateInstallerDlg::OnBnClickedButton4()
{
// license agreement
UpdateData();
CFileDialog dlg(TRUE, NULL, m_license, OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, "All Files(*.)|*.*||", this);
if(dlg.DoModal() == IDOK)
{
m_license = dlg.GetPathName();
UpdateData(FALSE);
m_edtLicense.SetSel(0, -1);
m_edtLicense.SetFocus();
}
}
void CCreateInstallerDlg::OnBnClickedButton5()
{
// info file
UpdateData();
CFileDialog dlg(TRUE, NULL, m_info, OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, "All Files(*.)|*.*||", this);
if(dlg.DoModal() == IDOK)
{
m_info = dlg.GetPathName();
UpdateData(FALSE);
m_edtInfo.SetSel(0, -1);
m_edtInfo.SetFocus();
}
}
// CreateInstallerDlg.h : header file
//
#include "afxwin.h"
#include "Wrap.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CCreateInstallerDlg dialog
class CCreateInstallerDlg : public CDialog
{
// Construction
public:
CCreateInstallerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CCreateInstallerDlg)
enum { IDD = IDD_CREATEINSTALLER_DIALOG };
CListBox m_lstInFiles;
CEdit m_edtTarget;
CString m_target;
CString m_license;
CString m_info;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCreateInstallerDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CCreateInstallerDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnMakeInstaller();
afx_msg void OnInBrowse();
afx_msg void OnOutBrowse();
afx_msg void OnDelinfiles();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
void GetProject(CString prj);
void SetFileListScroll();
CString iniFileName;
CWrap wrap;
public:
afx_msg void OnCbnSelchangeCombo1();
afx_msg void OnBnClickedButton1();
CComboBox m_cmbProjectList;
CButton m_btn95;
CButton m_btn98;
CButton m_btnME;
CButton m_btnNT35;
CButton m_btnNT4;
CButton m_btn2K;
CButton m_btnXP;
CButton m_btn2K3;
CButton m_btnVista;
CButton m_btnWin7;
CEdit m_edtWebCaption;
CEdit m_edtWebURL;
CEdit m_edtInfo;
CEdit m_edtLicense;
CEdit m_edtProgramFilesDirectory;
CEdit m_edtProjectFilename;
CButton m_btnNestedRepository;
CButton m_btnNeedRepository;
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedCheck10();
afx_msg void OnBnClickedCheck9();
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
// GetWinVer.cpp
//
// Copyright (C) 2001 Hans Dietrich
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GetWinVer.h"
// from winbase.h
#ifndef VER_PLATFORM_WIN32s
#define VER_PLATFORM_WIN32s 0
#endif
#ifndef VER_PLATFORM_WIN32_WINDOWS
#define VER_PLATFORM_WIN32_WINDOWS 1
#endif
#ifndef VER_PLATFORM_WIN32_NT
#define VER_PLATFORM_WIN32_NT 2
#endif
#ifndef VER_PLATFORM_WIN32_CE
#define VER_PLATFORM_WIN32_CE 3
#endif
/*
This table has been assembled from Usenet postings, personal
observations, and reading other people's code. Please feel
free to add to it or correct it.
dwPlatFormID dwMajorVersion dwMinorVersion dwBuildNumber
95 1 4 0 950
95 SP1 1 4 0 >950 && <=1080
95 OSR2 1 4 <10 >1080
98 1 4 10 1998
98 SP1 1 4 10 >1998 && <2183
98 SE 1 4 10 >=2183
ME 1 4 90 3000
NT 3.51 2 3 51
NT 4 2 4 0 1381
2000 2 5 0 2195
XP 2 5 1
CE 3
WIN7 6 1
*/
///////////////////////////////////////////////////////////////////////////////
// GetWinVer
BOOL GetWinVer(CString& strVersion, int *nVersion)
{
strVersion = WUNKNOWNSTR;
*nVersion = WUNKNOWN;
OSVERSIONINFO osinfo;
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osinfo))
return FALSE;
DWORD dwPlatformId = osinfo.dwPlatformId;
DWORD dwMinorVersion = osinfo.dwMinorVersion;
DWORD dwMajorVersion = osinfo.dwMajorVersion;
DWORD dwBuildNumber = osinfo.dwBuildNumber & 0xFFFF; // Win 95 needs this
if ((dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && (dwMajorVersion == 4))
{
if ((dwMinorVersion < 10) && (dwBuildNumber == 950))
{
strVersion = W95STR;
*nVersion = W95;
}
else if ((dwMinorVersion < 10) &&
((dwBuildNumber > 950) && (dwBuildNumber <= 1080)))
{
strVersion = W95SP1STR;
*nVersion = W95SP1;
}
else if ((dwMinorVersion < 10) && (dwBuildNumber > 1080))
{
strVersion = W95OSR2STR;
*nVersion = W95OSR2;
}
else if ((dwMinorVersion == 10) && (dwBuildNumber == 1998))
{
strVersion = W98STR;
*nVersion = W98;
}
else if ((dwMinorVersion == 10) &&
((dwBuildNumber > 1998) && (dwBuildNumber < 2183)))
{
strVersion = W98SP1STR;
*nVersion = W98SP1;
}
else if ((dwMinorVersion == 10) && (dwBuildNumber >= 2183))
{
strVersion = W98SESTR;
*nVersion = W98SE;
}
else if (dwMinorVersion == 90)
{
strVersion = WMESTR;
*nVersion = WME;
}
}
else if (dwPlatformId == VER_PLATFORM_WIN32_NT)
{
if ((dwMajorVersion == 3) && (dwMinorVersion == 51))
{
strVersion = WNT351STR;
*nVersion = WNT351;
}
else if ((dwMajorVersion == 4) && (dwMinorVersion == 0))
{
strVersion = WNT4STR;
*nVersion = WNT4;
}
else if ((dwMajorVersion == 5) && (dwMinorVersion == 0))
{
strVersion = W2KSTR;
*nVersion = W2K;
}
else if ((dwMajorVersion == 5) && (dwMinorVersion == 1))
{
strVersion = WXPSTR;
*nVersion = WXP;
}
else if ((dwMajorVersion == 5) && (dwMinorVersion == 2))
{
strVersion = W2K3STR;
*nVersion = W2K3;
}
else if ((dwMajorVersion == 6) && (dwMinorVersion == 0))
{
strVersion = WVISTASTR;
*nVersion = WVISTA;
}
else if( (dwMajorVersion == 6) && (dwMinorVersion == 1))
{
strVersion = WIN7STR;
*nVersion = WIN7;
}
}
else if (dwPlatformId == VER_PLATFORM_WIN32_CE)
{
strVersion = WCESTR;
*nVersion = WCE;
}
return TRUE;
}
// GetWinVer.h
//
// Copyright (C) 2001 Hans Dietrich
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GETWINVER_H
#define GETWINVER_H
#define WUNKNOWNSTR _T("unknown Windows version")
#define W95STR _T("Windows 95")
#define W95SP1STR _T("Windows 95 SP1")
#define W95OSR2STR _T("Windows 95 OSR2")
#define W98STR _T("Windows 98")
#define W98SP1STR _T("Windows 98 SP1")
#define W98SESTR _T("Windows 98 SE")
#define WMESTR _T("Windows ME")
#define WNT351STR _T("Windows NT 3.51")
#define WNT4STR _T("Windows NT 4")
#define W2KSTR _T("Windows 2000")
#define WXPSTR _T("Windows XP")
#define W2K3STR _T("Windows 2003 Server")
#define WVISTASTR _T("Windows Vista")
#define WIN7STR _T("Windows 7")
#define WCESTR _T("Windows CE")
#define WUNKNOWN 0
#define W9XFIRST 1
#define W95 1
#define W95SP1 2
#define W95OSR2 3
#define W98 4
#define W98SP1 5
#define W98SE 6
#define WME 7
#define W9XLAST 99
#define WNTFIRST 101
#define WNT351 101
#define WNT4 102
#define W2K 103
#define WXP 104
#define W2K3 105
#define WVISTA 106
#define WIN7 107
#define WNTLAST 199
#define WCEFIRST 201
#define WCE 201
#define WCELAST 299
BOOL GetWinVer(CString& strVersion, int *nVersion);
#endif //GETWINVER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment