Category Archives: Windows

openSSL vs. vcpkg – some strange experiences

One of the projects I was working in the last period uses CURL that uses OpenSSL version 1.1.1.1 in foreground. At some moment in time, for some technical reasons I was tempted to try using the CPPRESTSDK library. But because I had some local NuGet miss-configuration issues in order to have the CPPRESTSDK library build I realized I need to build it myself. But in order to achieve this, I needed the vcpkg tool.

And, because with a quick search I haven’t found the download and the Azure DevOps of this tool for built was down and no Artifacts available, I said: what if I build it myself?

So, I did it… It lasted some time on my laptop but at the end was fine. I had a brand new vcpkg package and I built the cpprestsdk.

Later, returning to my project and integrating the new cpprestsdk library I realized I was started getting new linking errors.

2>Generating   Code...   
2>libcrypto.lib(comp_lib.obj)   : error LNK2005: COMP_CTX_free already defined in libeay32.lib(LIBEAY32.dll)  
2>libcrypto.lib(comp_lib.obj)   : error LNK2005: COMP_CTX_new already defined in libeay32.lib(LIBEAY32.dll)
2>libcrypto.lib(comp_lib.obj)   : error LNK2005: COMP_compress_block already defined in   libeay32.lib(LIBEAY32.dll)  
2>libcrypto.lib(comp_lib.obj)   : error LNK2005: COMP_expand_block already defined in   libeay32.lib(LIBEAY32.dll)   
2>Creating library C:\dev\ my-project\Build\x64\Debug\myproject.lib and object   C:\dev\my-project\Build\x64\Debug\myproject.exp   
2>C:\dev\my-project\Build\x64\Debug\   myproject.dll : fatal error LNK1169: one or more multiply defined symbols   found

Within the sample solution, the situation is even worth. I was able to find out even more compile errors while the vcpkg is installed.

Vcpkg uses OpenSSL to insure secure connection and googling I found out that starting with the OpenSSL 1.1.x Windows version the static LIB and DLL filenames have changed, so depending on local files, it may affect, the former DLL’s libeay32.dll and ssleay32.dll are no longer available.

I double-checked the environment variables, trying to identify paths containing the old libeay32.dll (that was renamed to openssl.dll) and to remove it. I did such investigation, but I found nothing “suspicions”.
The goal was to avoid the names’ collision in case the linker was looking to link “our” openSSL together with LIBEAY32.dll.

The salvation came from GauthamBanasandra’s suggestion. I just ran the next command within the vcpkg repository local folder.

.\vcpkg.exe integrate remove

After the execution of this command, I was able to build back the project as before. And it was fine for me… I don’t need the vcpkg for other proposes right now.
By the way, this command has to be executed within the vcpkg cloning repository folder. Otherwise, you’ll get an error: Could not detect vcpkg-root in case you don’t have an environment variable VCPKG_ROOT pointing to your vcpkg binary repository.

Off-course, in case you still need vcpkg.exe you can reinstall it temporary with vcpkg.exe integrate install. But then, until there is a fix for this situation or a better workaround, you might face the initial problem I had within OpenSSL based projects. So, most probably you will have to call again vcpkg.exe integrate remove.

The issues are created by the fact the actual vcpkg.exe manager is still using the old openSSL library, libeay32.dll.

Until the Vcpkg team didn’t upgrade to a newer OpenSSL library (1.1.x+), we will have to find such workarounds in order to compile different projects. If someone knows other workaround and is open to share, I would not mind mentioning here her/his idea.

The good news is that an upgrade to the OpenSSL 1.1.x is ongoing. The upgrade issue is already open and the pull request #8566 seems advanced in progress. Let’s stay tuned!

psa.exe partial tree sample

Process Status Analysis – the first steps

I am pleased to announce my first cross-platform and open source project, the Process Status Analysis tool, available on GitHub.

The Process Status Analysis (psa) version 0.2 is available for Windows and Linux (Debian derived / Ubuntu tested) Operating Systems.
Download: psa for Ubuntu Linux x64 (3582 downloads)
Download: psa for Ubuntu Linux x86 (2833 downloads)
Download: psa.exe for Windows x64 (2811 downloads)
Download: psa.exe for Win32 (2829 downloads)

You may wonder why I did it or what it brings new. Well, I did it for fun, in my spare time and I will continue improving it when I’ll find a time to do it.

The project is written in modern C++ using idioms from the C++ 1x standards. Even if initially was done as a C++ for Windows only, during the past days I managed the port of it for Linux using Visual Studio 2017’s project templates and a connection via SSH.
In general, the source code base is similar, differing just by OS specific stuff.

In case you want to find out more about how to develop C++ Linux projects from the best development tool (imho), Visual Studio, you can find more information on Visual Studio development team blog.

Related to this psa project, the Linux version requires libprocps4-dev library in order to build.

The main reason for starting this project was that I wanted to know what’s the total memory amount used by my Chrome browser. I know it uses a lot of resources, but not that much… 🙂

Even if my preferred processes analysis tool, the Process Hacker offers a lot of processes administration possibilities, but it didn’t provide what I want, so I decided to enjoy a bit.

Chrome processes in Process Hacker

Sample – Google Chrome processes in Process Hacker tool

So, what I achieved by psa.exe was something like:

C:\Windows\system32> psa -o chrome
PID [896] chrome.exe 237.5234 MB
PID [1496] chrome.exe 87.6875 MB
PID [2388] chrome.exe 166.5000 MB
PID [5860] chrome.exe 3.1211 MB
PID [7336] chrome.exe 273.2188 MB
PID [8444] chrome.exe 68.0508 MB
PID [8624] chrome.exe 63.3945 MB
PID [9180] chrome.exe 296.9766 MB
PID [10600] chrome.exe 292.6289 MB
PID [12352] chrome.exe 182.5977 MB
PID [13688] chrome.exe 2.3555 MB
PID [14052] chrome.exe 73.1875 MB
PID [16200] chrome.exe 211.9805 MB
PID [17284] chrome.exe 55.7148 MB
PID [18036] chrome.exe 208.6680 MB
PID [19012] chrome.exe 457.2305 MB
PID [19312] chrome.exe 143.4766 MB
-----------------------------------
Total used memory: 2824.31 MB

A bit too much in my humble opinion…
The features this tool offers includes:

Get all processes loaded in memory information

I case you want to have a snapshot of all the processes loaded in the OS’s memory you can have it with.

psa -a

Get process only used memory

With psa.exe you can reach the used memory by a specific parameter -o after the process name or at least a part of its name.

psa -o chrome             // find how much memory uses your Chrome!   o_O

Currently, there is no string replace ‘*’ but it’s ongoing.


Print processes tree

Storing the processes’ data within a generic tree done by me, I took the decision to print the processes’ tree output, similarly there is in Windows with tree.exe tool or on Linux in pstree or even htop.

./psa -t
./psa -t 1034

psa.exe partial tree sample

Process Status Analysis partial tree of Windows process

Top most “expensive” processes

In case you want to see what are the most expensive processes within your operating system, you can achieve it with:

psa -e 20

or simpler psa -e in case you’re sure you want top 10 expensive processes (the default value).

silviu@ubuntu-dev-server:~/projects/psa-lin/bin/x64/Release$ ./psa -e
Top 10 consuming memory processes
-------------------------------------------
PID        Process Name         RAM Usage
-------------------------------------------
[924]    /usr/lib/policykit-1/polkitd   270.68 MB
[906]    /usr/lib/accountsservice/accounts-daemon       269.43 MB
[842]    /usr/sbin/rsyslogd     250.39 MB
[878]    /usr/lib/snapd/snapd   197.29 MB
[531]    /lib/systemd/systemd-timesyncd         97.97 MB
[1614]    sshd: silviu@pts/0    93.16 MB
[1576]    sshd: silviu [priv]   93.16 MB
[863]    /usr/bin/lxcfs         93.13 MB
[427]    /sbin/lvmetad          92.55 MB
[1034]    /usr/sbin/sshd        63.98 MB
-------------------------------------------


C:\Windows\system32> psa -e 10
Top 10 consuming memory processes
-------------------------------------------
PID        Process Name         RAM Usage
-------------------------------------------
[19012]    chrome.exe           435.67 MB
[17684]    chrome.exe           329.51 MB
[7336]    chrome.exe            259.61 MB
[15576]    devenv.exe           248.20 MB
[896]    chrome.exe             222.85 MB
[2388]    chrome.exe            188.21 MB
[18428]    chrome.exe           184.48 MB
[15760]    chrome.exe           173.82 MB
[488]    Dropbox.exe            162.15 MB
[5488]    googledrivesync.exe   161.11 MB
-------------------------------------------
Total used memory: 2365.61 MB

Redirect output to a file

From the standard output the information can be easily redirected to a file.

c:\> psa -t > windows_processes_tree.txt
# ./psa -t > linux_processes_tree.txt        

Kill process feature

This feature was not implemented yet but in case we need it we can be done it easily with the existing tools on the target OS (ex. Task Manager, Process Exporer/Hacker, pskill.exe for Windows or the combination ps + kill on LInux).

Feedbacks and improvements
Any constructive feedback, suggestions, contributions to improvements are appreciated.
Feel free to add any issue you find, wish or suggestion you have in the GitHub repository, the 
Issues section or here as a comment.

Finally, I got Windows 10

Finally, I got the Windows 10 for my laptop… Even if this task is trivial, it was a surprising experience this time. But let me tell you the story of Windows 10 installation on my Samsung RC 710 laptop.

Back in August, I tweeted “That’s all I have within #Windows10 X64 Ent setup on Samsung RC710 with SSD”. By that, I meant that my setup process was stuck in the boot phase using the installation of this Windows brand new version. That was reproduced within Windows 8.1 x64 upgrade tentative or with clean Windows 10 installation.
I tried few Windows 10 .ISOs and a friend’s DVD but no chance. The setup has started and stuck within few seconds.

win10_setup_stockes

Because the laptop got some hardware upgrades from the original configuration, I tried to restore to the initial configuration but nothing new. Reading this article where is specifying “For 64-bit installations, a small number of older PCs may be blocked from installation because they do not support CMPXCHG16b, PrefetchW, and LAHF/SAHF“, I tried an x86 .ISO but I got the same situation. Also, I tried some BIOS changes without any improvements.

Getting online contact with Microsoft it offered me no new things. I got only typical support trivial answers.

During the time on the same laptop, I was running fine Win XP, Win 7 and Win 8.1 OSs without any such bad experiences. To me it was clear that Windows 10 has some backward compatibilities issues.

So, I took it on my own googling for my situation. Reading different forums I realized this is a common issue for Samsung old laptops and it is generated by the WiFi card.

The solution in my case was buying a brand new Atheros AR5B22 WiFi card and replacing the old WiFi card with that.

If you’re in a similar situation and you’re looking for instructions how to disassemble your laptop here it is a brief presentation.

By the way, you’re doing on your own risk. If you’re not confident, please contact a specialist.samsung_laptop_to_win10_3samsung_laptop_to_win10_4samsung_laptop_to_win10_6samsung_laptop_to_win10_5upgrading_windows_on_samsung_laptop

With this new WiFi card plugged-in, the upgrade from Window 8.1 x64 to Windows 10 x64 became a trivial task.

It would be nice if Microsoft would get more in count such behaviors and would improve the Windows 10’s backward compatibility, especially because the old computers are included within their OS target.

Update 02.06.2016: Samsung admits they are lame: “Don’t Install Windows 10 Because We Suck At Making Drivers”. Sad…

You can find additional information here. That’s why, most probably I will never buy a Samsung phone or any other gadget made by them, anymore.

Getting Table’s indexes experiences – workaround

Trying to get table indexes information in SQL Server 2012 I identified a strange situation within a specific method that I was using so long but it was not acting as expected in one situation.

The way of getting indexes information using the ODBC C API into that old and inherited method looks like:

nRetCode = ::SQLStatistics(hstmtAux,
                                    NULL,
                                    0,
                                    NULL,
                                    0,
                                    (TCHAR*)(LPCTSTR)strTempTable,
                                    SQL_NTS,
                                    SQL_INDEX_ALL,
                                    SQL_ENSURE);
if (nRetCode == SQL_SUCCESS || nRetCode == SQL_SUCCESS_WITH_INFO) {
  nRetCode = ::SQLBindCol(hstmtAux, 4, SQL_C_SHORT, &swNonUnique, sizeof(SWORD),
                          &cbNonUnique);
  nRetCode = ::SQLBindCol(hstmtAux, 5, SQL_CHAR, szIdxQualif,
                          sizeof(CHAR) * 130, &cbIdxQualif);
  nRetCode = ::SQLBindCol(hstmtAux, 6, SQL_C_CHAR, szIdxName,
                          sizeof(CHAR) * 130, &cbIdxName);
  nRetCode =
      ::SQLBindCol(hstmtAux, 7, SQL_C_SHORT, &swType, sizeof(SWORD), &cbType);
  nRetCode = ::SQLBindCol(hstmtAux, 8, SQL_C_SHORT, &swSeqInIdx, sizeof(SWORD),
                          &cbSeqInIdx);
  nRetCode = ::SQLBindCol(hstmtAux, 9, SQL_C_CHAR, szIdxColName,
                          sizeof(CHAR) * 130, &cbIdxColName);

  while (bNoFetch || nRetCode == SQL_SUCCESS_WITH_INFO ||
         (nRetCode = ::SQLExtendedFetch(hstmtAux, SQL_FETCH_NEXT, 1, &crow,
                                        &rgfRowStatus)) == SQL_SUCCESS) {
    if (cbIdxName != SQL_NULL_DATA &&
        _tcslen((TCHAR)szIdxName) < 0) {  
        // rest of the code 
    } 
  } // rest of the code 
}

Usually, I got the right information about indexes but in one situation I encounter a strange behavior. It’s about having a clustered index into a scenario. I have a table that contains two indexes referenced to some fields: IndexField_1 and IndexField_3 mapped over int, NULL fields. When IndexField_1 is Non-Unique, Non-Clustered and IndexField_3 is Clustered index I get the right information.
But if the index IndexField_1 is Clustered and the IndexField_3 is Non-Unique, Non-Clustered I get no information about IndexField_1 index (eg. szIdxName and szIdxColName are “” and their length is -1 that means SQL_NULL_DATA). Within while loop, with the next iteration, I get correct information about the second index IndexField_3.

Because SQLExtendedFetch() is deprecated I tried using SQLFetchScroll() but the behavior is the same from my interest point of view.

I was not sure whether the problem is with SQLStatistics, the bindings or SQLFetchScroll (they all always return SQL_SUCCESS). It looks such a problem with the driver when the first index is clustered.
According to SQLStatistics documentation if my swType parameter is SQL_TABLE_STAT I have no information for index or field. But for this scenario, I had no indexes of combined fields.
For the good scenario I observed that my while loop had 3 iterations including one of having swType = SQL_TABLE_STAT without information in szIdxName. But for the bad scenario, the loop had only 2 iterations. So it looks like SQLExtendedFetch() is not getting the last one index.

After some googling and research without very significant solutions, I decided to apply a workaround by avoiding the old API and I rewrite my method.

So, in order to get table indexes information, I have chosen a direct SQL query into SYS tables: sys.tables, sys.indexes, sys.schema.

SELECT DISTINCT I.[name] AS IndexName, I.is_unique AS IsUnique,
    I.is_primary_key AS IsPrimaryKey, I.type AS IndexType,
    I.type_desc AS IndexDesc,
    T.[object_id] AS ObjectID FROM sys.tables AS T INNER JOIN
        sys.indexes AS I ON T.[object_id] = I.[object_id]and T.name =
        'myTABLE' and
        I.type_desc<> 'HEAP' INNER JOIN sys.schemas AS S ON T.schema_id =
            S.schema_id and s.name = 'myTABLE'

Because I preferred getting also information about the index’s composed fields, I applied a second additional SQL query:

SELECT AC.Name as ColumnName FROM sys.tables as T INNER JOIN
    sys.indexes as I on T.[object_id] =
    I.[object_id] INNER JOIN sys.index_columns as IC on IC.[object_id] =
        I.[object_id] and IC.[index_id] =
            I.[index_id] INNER JOIN sys.all_columns as AC on IC.[object_id] =
                AC.[object_id] and IC.[column_id] =
                    AC.[column_id] WHERE I.object_id =
                        'NumericObjectID' and T.name = 'TableName' and I.name =
                            'IndexName' order by T.name,
                        I.name

and I have collected data into a container of defined structure according to my SQL Indexes interest information:

struct SQL_INDEX {
  CString index_name;
  bool is_unique;
  long object_id;
  bool is_primary;
  short index_type;
  CString index_desc;
  std::vector vectColumns;
};

The last member vectColumns stores information about the columns that are used for a specific index.

Finally, the new method that collects table indexes information looks like:

// collect table indexes information
void CFoo::GetIndexesInformation(const CString& strSchema,
                                 const CString& strTable,
                                 std::vector& vectKeys,
                                 CDatabase* pDBdata) {
  HSTMT hstmt = SQL_NULL_HSTMT;
  SQLRETURN nRetCode = 0;
  CString sqlQuery;

  sqlQuery.Format(
      _T("SELECT DISTINCT I.[name] as IndexName, I.is_unique as IsUnique, I.is_primary_key as IsPrimaryKey, I.type as IndexType, I.type_desc as IndexDesc, T.[object_id] as ObjectID \
FROM sys.tables as T \
INNER JOIN sys.indexes as I on T.[object_id] = I.[object_id] and T.name = '%s' and I.type_desc <> 'HEAP' \
INNER JOIN sys.schemas as S on T.schema_id = S.schema_id and s.name = '%s'"),
      strTable, strSchema);

  if ((nRetCode = ::SQLAllocStmt(pDBdata->m_hdbc, &hstmt)) == SQL_SUCCESS) {
    if (SQL_NULL_HSTMT != hstmt) {
      pDBdata->OnSetOptions(hstmt);

      nRetCode = ::SQLExecDirect(hstmt, (CHAR*)(LPCTSTR)sqlQuery,
                                 sqlQuery.GetLength());
      if (SQL_SUCCESS == nRetCode) {
        CHAR buffer[128] = {0};
        SQLLEN iLen = 0;
        short int val = 0;
        long obj_id = 0;

        while (::SQLFetch(hstmt) == SQL_SUCCESS) {
          SQL_INDEX ob;

          if (::SQLGetData(hstmt, 1, SQL_C_CHAR, buffer, 128, &iLen) ==
              SQL_SUCCESS)
            ob.index_name = buffer;

          if (::SQLGetData(hstmt, 2, SQL_C_SHORT, &val, sizeof(short int),
                           &iLen) == SQL_SUCCESS)
            ob.is_unique = (1 == val);

          if (::SQLGetData(hstmt, 3, SQL_C_SHORT, &val, sizeof(short int),
                           &iLen) == SQL_SUCCESS)
            ob.is_primary = (1 == val);

          if (::SQLGetData(hstmt, 4, SQL_C_SHORT, &val, sizeof(short int),
                           &iLen) == SQL_SUCCESS)
            ob.index_type = val;

          if (::SQLGetData(hstmt, 5, SQL_C_CHAR, buffer, 128, &iLen) ==
              SQL_SUCCESS)
            ob.index_desc = buffer;

          if (::SQLGetData(hstmt, 6, SQL_C_LONG, &obj_id, sizeof(long),
                           &iLen) == SQL_SUCCESS)
            ob.object_id = obj_id;

          vectKeys.push_back(ob);
        }
      }
    }
  }

  // collect index’s columns/fields information
  for (auto it = vectKeys.begin(); it != vectKeys.end(); ++it) {
    HSTMT hstmt2 = SQL_NULL_HSTMT;
    if ((nRetCode = ::SQLAllocStmt(pDBdata->m_hdbc, &hstmt2)) == SQL_SUCCESS) {
      if (hstmt2 != SQL_NULL_HSTMT) {
        pDBdata->OnSetOptions(hstmt2);

        CString sSQL;
        SQLLEN iLen = 0;

        sSQL.Format(_T("SELECT AC.Name as ColumnName \
FROM sys.tables as T inner join sys.indexes as I on T.[object_id] = I.[object_id] \
INNER JOIN sys.index_columns as IC on IC.[object_id] = I.[object_id] and IC.[index_id] = I.[index_id] \
INNER JOIN sys.all_columns as AC on IC.[object_id] = AC.[object_id] and IC.[column_id] = AC.[column_id] \
WHERE I.object_id = %ld and T.name = '%s' and I.name = '%s' order by T.name, I.name"),
                    it->object_id, strTable, it->index_name);

        nRetCode =
            ::SQLExecDirect(hstmt2, (UCHAR*)(LPCTSTR)sSQL, sSQL.GetLength());
        if (SQL_SUCCESS == nRetCode) {
          CHAR buffer[128] = {0};
          while (::SQLFetch(hstmt2) == SQL_SUCCESS) {
            if (::SQLGetData(hstmt2, 1, SQL_C_CHAR, buffer, 128, &iLen) ==
                SQL_SUCCESS) {
              it->vectColumns.push_back(buffer);
            }
          }
        }
      }
    }
  }
}

In this way I have complete information about the indexes of my tables.

std::vector vectIndexesSQL;
pFoo->GetIndexesInformation(strSchema, pDBTable->strTblName, vectIndexesSQL, pDataDB);

Conclusion: When the C/C++ API doesn’t give you any hopes don’t forget that SQL saves you.

SubclassWindow() method issues in projects base on MFC Feature Pack

The Problem
Trying to paint a background image into client area of a MDI application build in VC++ 6.0 to VC++ 2005 IDE it’s not a difficult task.
In case you need, you can find easily good references. For instance, there are two references from Microsoft (KB129471 and KB103786) and one I prefer: a FAQ wrote by a friend of mine.

Unfortunately things are changing radically in case you’re following the same steps in a Visual C++ IDE that has MFC Feature Pack support. If you’re building from the scratch a VC++ 2008/VC++ 2010 a MDI project that has MFC Feature Pack support and you’re trying to apply sub-classing steps, you will have a big surprise in the moment you’re starting your application in debug mode. Effectively your application will crash in the moment you are trying to call SubclassWindow() in CMainFrame::OnCreate().

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
  if (CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1)
    return -1;

  // ---- code ---

  // BANG! IN VS 2010 or VS 2008 with MFC Feature Pack
  // m_wndMDIClient.SubclassWindow(m_hWndMDIClient);
  // where m_wndMDIClient is an instance of CMDIClientWnd
  // (http://support.microsoft.com/kb/129471)

  // ------ code ----

  return 0;
}

Problem details
Starting with MFC Feature Pack CMDIFrameWndEx is the new CMainFrame’s parent class instead of CMDIFrameWnd and the problem acts inside of Attach() method:
because CWnd::FromHandlePermanent(HWND hWnd) looks up into a permanent handle map and in returns existing CWnd pointer.

Wnd* PASCAL CWnd::FromHandlePermanent(HWND hWnd) {
  CHandleMap* pMap = afxMapHWND();
  CWnd* pWnd = NULL;
  if (pMap != NULL) {
    // only look in the permanent map - does no allocations
    pWnd = (CWnd*)pMap->LookupPermanent(hWnd);
    ASSERT(pWnd == NULL || pWnd->m_hWnd == hWnd);
  }
  return pWnd;
}

CHandleMap is the wrapper that implements the mapping mechanism between the pointers of MFC wrapped classes and the Windows object handles. Internally, this class has to dictionaries (m_permanentMap and m_temporaryMap) implemented as CMapPtrToPtr, m_nHandles – the number of handles, m_nOffset – the offset of handles in the object and it has a m_pClass pointer of CRuntimeClass (a run time class associated with all MFC classes).
In case you’re interest in more details, you can find more information here.

We have a pointer to a CHandleMap instance that is assigned with the returned pointer of a handle map returned by afxMapHWND(). The returned pointer pWnd it’s assigned with the result returned by pMap->LookupPermanent(hWnd). LookupPermanet() effectively search into a the permanent hash map for exiting HANDLEs and in our case it find it.

inline CObject* CHandleMap::LookupPermanent(HANDLE h) {
  return (CObject*)m_permanentMap.GetValueAt((LPVOID)h);
}

where

void* CMapPtrToPtr::GetValueAt(void* key) const {
  // find value (or return NULL -- NULL values not different as a result)

  ENSURE(this);

  if (m_pHashTable == NULL)
    return NULL;

  UINT nHash = HashKey(key) % m_nHashTableSize;

  // see if it exists
  CAssoc* pAssoc;
  for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) {
    if (pAssoc->key == key)
      return pAssoc->value;
  }
  return NULL;
}

If the item having nHash key was found into m_pHashTable then the condition if (pAssoc->key == key) is TRUE because the attribute m_hWndMDIClient of CMDIFrameWnd is used yet.
So, effectively what LookupPermanent() has found in m_permanentMap map is m_hWndMDIClient. And because pMap->SetPermanent(m_hWnd = hWndNew, this) is one of the next call into Attach() method those ASSERTs are a must.
Even if those ASSERT() calls from Attach() are available only in debug mode (because of ASSERT() macro behavior) a release build would not save the situation. Soon or later you’ll get conflicts and the application will crash.

Trying to find where this has happened is not so complicated as long as we take in consider our CMainFrame class it’s derived from CMDIFrameWndEx a class that extends CMDIFrameWnd. If we are looking into CMDIFrameWndEx class implementation (AfxMDIClientAreaWnd.cpp) we will see that into this class SubclassWindow() method it’s called jet:

BOOL CMDIFrameWndEx::OnCreateClient(LPCREATESTRUCT lpcs,
                                    CCreateContext* pContext) {
  if (!CMDIFrameWnd::OnCreateClient(lpcs, pContext)) {
    return FALSE;
  }

  if (m_bDoSubclass) {
    m_wndClientArea.SubclassWindow(m_hWndMDIClient);  // this is it!
  }

  return TRUE;
}

Subclassing a CWnd derived instance that has already a mapped HWND item is an error and these ASSERTs try to avoid this from development moment. Having two different CWnd-derived objects with the same HWND is not possible – the only exception is CDC instances that have 2 HWNDs (m_hDC and m_hAttribDC).
Related to my issue, according to Steve Horne from Microsoft, “anything that uses the MFC Feature Pack will be using CMDIFrameWndEx which is a very different beast. It has this feature built it as you’ve found out”.
The worst part is that “If you were able to subclass the Ex client area, you’d probably end up breaking a lot of the FluentUI features.”
The VS 2008 / VS 2010 wizard generates and use a lot of Feature Pack FluendUI items.

A bad solution
An approach might be trying to adapt sub-classing idea directly into CMainFrame class. So, the steps might be:

  • No CMDIClientWnd instance is needed (as in existing tutorials). So no more SubclassWindow() call in CMainFrame::OnCreate().
  • Handle WM_ERASEBKGND, WM_SIZE and WM_PAINT on CMainFrame.
void CMainFrame::OnPaint() {
  CWnd* pWnd =
      CWnd::FromHandle(m_hWndMDIClient);  // returns a MFC object pointer
  // for the given handle if it is valid
  CPaintDC dc(pWnd);

  // the rest of the code
}

CWnd::FromHandle() acquires a pointer to an MFC object pointer from CHandleMap via afxMapHWND().

BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) {
  return FALSE;
}

void CMainFrame::OnSize(UINT nType, int cx, int cy) {
  CMDIFrameWndEx::OnSize(nType, cx, cy);

  Invalidate();
}

At the very first time everything looked nice. But unfortunately I have to admit Steve Horne’s observations. In different situations (most on resizing or moving messages) some of the FluentUI items were not correctly painted (some Ribbon items painting issues – different cases).

So, a better solution is needed.

A good but not perfect solution
In my research, for projects base on MFC Feature Pack, there is no perfect solution for this issue. I mean something similarly with the good solutions that I mentioned in the beginning of this article but acts fine until the first IDE that use MFC Feature Pack.
As we have seen on top trying to subclass a window with an already mapped is not a good idea.
The solution is based on Joseph M. Newcomery’s idea, a well-known book writer and Microsoft Visual C++ MVP. Joe proposes “temporary” remapping only for the case we need – in my case painting actions. For the rest of the action the mapping process inside of framework continues in the classic way. It’s a “gross and ugly” solution but until having a better solution from Microsoft or others I consider it fine for my needs.

  • First step is to define a class CMDIClientWnd derived from CWnd and add WM_PAINT and WM_ERASEBKGND handle methods.
BOOL CMDIClientWnd::OnEraseBkgnd(CDC* pDC) {
  return FALSE;  // let OnPaint() to paint, only
}

void CMDIClientWnd::OnPaint() {
  CPaintDC dc(this);

  // effective painting stuff
}
  • Catch the WM_PAINT message in CMainFrame via PreTranslateMessage() before the message is dispatched for execution and calling our redraw method.
BOOL CMDIClientWnd::OnEraseBkgnd(CDC* pDC) {
  return FALSE;  // let OnPaint() to paint, only
}

void CMDIClientWnd::OnPaint() {
  CPaintDC dc(this);

  // effective painting stuff
}

Here is the RedrawClientArea() public method.

void CMainFrame::RedrawClientArea() {
  CMDIClientWnd wnd_cl;

  wnd_cl.Attach(m_wndClientArea.Detach());
  wnd_cl.Invalidate();
  wnd_cl.UpdateWindow();
  m_wndClientArea.Attach(wnd_cl.Detach());
}

So we create locally an instance of CMDIClientWnd and we attach it internally to ChandleMap::m_permanetMap via Attach(), not before detaching m_wndClientArea (an CMDIClientAreaWnd instance, attribute in CMDIFrameWndEx and as we have seen before it subclass the CMDIFrameWndEx in CMDIFrameWndEx::OnCreateClient()).

The idea is that our CMDIClientWnd instance temporary replace m_wndClientArea instance of CMDIClientAreaWnd right before effective WM_PAINT message is dispatched via PreTranslateMessage().

  • Include your new class header (ex. MDIClientWnd.h) in MainFrm.cpp and call RedrawClientArea() in CMainFrame::OnSize().
void CMainFrame::OnSize(UINT nType, int cx, int cy) {
  CMDIFrameWndEx::OnSize(nType, cx, cy);

  RedrawClientArea();  // repaint on WM_SIZE
}
  • If the child frames window is not tabbed style (when all client area is hidden) and the client area is still visible than we have to call RedrawClientArea() method from WM_MOVE and WM_SIZE handler of CChildFrame and we have to include MainFrm.h into ChildFrame.cpp.
void CChildFrame::OnMove(int x, int y) {
  CMDIChildWndEx::OnMove(x, y);

  CMainFrame* pMainFrame = (CMainFrame*)GetParentFrame();
  ASSERT(pMainFrame);

  pMainFrame->RedrawClientArea();
}

void CChildFrame::OnSize(UINT nType, int cx, int cy) {
  CMDIChildWndEx::OnSize(nType, cx, cy);

  CMainFrame* pMainFrame = (CMainFrame*)GetParentFrame();
  ASSERT(pMainFrame);

  pMainFrame->RedrawClientArea();
}
  • Additionally, in order to make sure the painting message is received by main frame at application’s starting moment and your image is correctly painted from the beginning, please call pMainFrame->Invalidate() after pMainFrame->UpdateWindow() in InitInstance() method of your application class. Otherwise, if your application it’s starting with no opened document (for instance new document), your picture will appear only in the moment a WM_PAINT message is generated in CMainFrame (for instance when you resize your application, select the menu, etc).

A disadvantage of this approach is that the interest message (WM_PAINT) is not handled inside the class of m_wndClientArea, but the good point is that the rest of the messages are left at the correct class of the framework and will work correctly.
Demo application (3862 downloads)

Flexible changes for product version properties – Visual C++ binaries

Manually editing of binary files version in the resource editor of Visual Studio IDE is not a viable solution. If we have dozens of projects in our solution, then for each kit building we should need manual resources file edit. Otherwise, we can use a special tool that does this thing for us.
Unfortunately this approach is not the most flexible and could fail.

For our flexible binaries properties changes and in order to avoid manual edit for each rebuild we can create and include a header file (version.h) that contains some constants of product version and file version of our project (.rc files).

#define PRODUCT_VERSION 4.3.2.198
#define PRODUCT_VERSION_STR "4.3.2.198"

Then, for each .rc file wherever we have FileVersion and ProductVersion we have to use this constants.
When we will build a new kit, we have to change only these constants and then to start the kit building process. Everything is fine until we add new controls in our projects resource files. Then, because of Visual Studio IDE automation we can get an unlikely surprise: the FileVersion and the ProductVersion properties could be reset to 1.0.0.0.

In order to avoid this issue and edit the version only in a single place I propose the following workaround:

  • Create a version.h header file that have to contain these constants only (as on top). I should recommend to create it into the root solution in order of being easy included in all the projects.
  • Include this file in the project you need to use.
  • Use a text editor (ex. notepad.exe) and include next code section at the end of .rc2 resource file of your project (res\your_project.rc2) – this section contains the include version.h file section, too.
// Neutral resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NEU)
#ifdef _WIN32
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
// Versio
//
#include "..\version.h"
VS_VERSION_INFO VERSIONINFO
FILEVERSION PRODUCT_VERSION
PRODUCTVERSION PRODUCT_VERSION
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904e4"
BEGIN
VALUE "CompanyName", "TODO: "
VALUE "FileDescription", "TODO: "
VALUE "FileVersion", PRODUCT_VERSION_STR
VALUE "InternalName", "testResources.exe"
VALUE "LegalCopyright", "TODO: (c) . All rights reserved."
VALUE "OriginalFilename", "your_project.exe"
VALUE "ProductName", "TODO: "
VALUE "ProductVersion", PRODUCT_VERSION_STR
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END
#endif // Neutral resources
/////////////////////////////////////////////////////////////////////////////
  • Edit “040904e4” block code with same data as if we should edit in a resources editor and use the version.h‘s file defined constants. As you can see in my example, for the FileVersion and ProductVersion properties I use my version.h constants. These properties will not be edited anymore.
  • Delete “// Version” section from default resource file your_project.rc (including comments – recommended).
  • Insert next lines into your_project.rc file after “3 TEXTINCLUDE BEGIN” and before “#define _AFX_NO_SPLITTER_RESOURCES\r\n“:
\r\n""#include ""res\\your_project.rc2""\r\n""\0"
3 TEXTINCLUDE
BEGIN
"\r\n"
"#include ""res\\your_project.rc2""\r\n"
"\0"
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#include ""res\\your_project.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#endif\r\n"
"\0"
END

Don’t forget to edit .rc2 file name with the right file name of your project.

  • In your_project.rc file the section “// Generated from the TEXTINCLUDE 3 resource.” have to contain only next declaration:

#include "res\your_project.rc2"

The rest of the section’s lines have to be deleted.

  • We save both resources files: your_project.rc and your_project.rc2.
  • Rebuild the project and check the new generated binary properties. In the FileVersion we will have the major version (in my case 4.0.0.0) and in ProductVersion we have the current build version (4.3.2.198).

Demo application - AutoProductVersion (2147 downloads)

Simple Pictures Unifier – first version

Have you just returned from vacation and you have a lot of pictures? You just downloaded your taken pictures and you got your friends pictures, too? Then, probably, the pictures are scattered, with a random order in your computer.
Would you like seeing pictures from all sources in order of events happening?
If so, try to sort your photos using Simple Pictures Unifier tool! 🙂

Simple Pictures Unifier application

Please, feel free to add comments, suggestions or bugs reports to this application.
Any constructive feedback is sincerely appreciated. Thanks you!

Release space on a Windows partition – SpaceMonger

Few time ago I observed that my laptop’s first partition (48 GB size) free space reduced to 4 GB.
There are a lot of file managers application (Windows Explorer, Total Commander, Far Manager, etc). All of them provide folder and file lists, but have not intuitive display of most important detail: the size.
Of course, there are some known paths that store not useful data (see bottom folders list) and with each file manager you can delete major not useful data. But, you can miss some old huge file (some forgotten video files, etc).

Fortunately there is a tool witch display folders and files size graphically called SpaceMonger. I used it and now my laptop free space is approximately 20 GB. 🙂 At the end of this process I applied the same story on my old workstation, too.
Using this visual tool is easy to observer big files, folders and is easy to clean Windows temporary files, Windows updates install files, easy to observe hibernate and virtual memory files (if you have enough RAM memory you can disable this feature), etc.
Unfortunately SpaceMonger project development was stopped. Fortunately the free version 1.4 runs fine on Windows 7, even if it was written for Windows 9x OSs.
On my workstation (Windows XP SP3 station with 1792 MB RAM) I observed some troubles when I selected a huge folder in order to delete. SpaceMonger application crushed few times. I recommend individual file delete actions.

If you decide to follow this tutorial for your station is your responsibility. I am not responsible for any damage of your operating system or if you’re loosing your personal data. Do it only if you know what you do. You need a user with administrator rights.
You can download and unzip last freeware SpaceMonger application, 1.4.0 version from:
SpaceMonger v1.4.0 -> Free Software tab -> SpaceMonger v1.4.0 -> Download it! button.

Start running SpaceMonger application. Press Open button, select partition driver and wait partition scanning process. Your scanning results should look like this:

You can see how much free space you have, and all big files and folders. With this tool you can find forgotten huge files, too.
On each Windows partition there are two huge files that can be deleted in some circumstances: hiberfil.sys and pagefile.sys.

Disable hibernate feature – hiberfil.sys
Normally this file is located at C:\hiberfil.sys and file size can vary from 250MB to over 2GB. Trying to delete it it’s not a good idea if you don’t understand what means this file.
This file is used by Windows hibernate. This feature is used instead of shutting down when we need a faster Windows start. Windows takes a snapshot of current operation Windows state, copy everything on this file and turns off most of your hardware. To wake up from hibernation you normally move the mouse or hit the spacebar on the keyboard.
If you are using Windows 7 or Windows Vista, you can easy disable hibernate feature in command prompt window typing powercfg -h off on and press Enter.
If you are sure that you don’t need this feature you can disable it in Windows XP following next steps:
– Go to Control Panel (Start -> Control Panel) and select Power Options feature.
– Click the Hibernate tab, uncheck the ‘Enable hibernate support‘ check box, and then click Apply.
– You need to restart your computer and hiberfil.sys should be automatically deleted.
Later, if you need hibernation feature, you need to go back to Hibernate tab from Power Options and check Enable hibernate support.

Disable virtual memory – pagefile.sys
This file is used by Windows virtual memory extension. Virtual memory is allowing your computer’s operating system pretend that you have more RAM than you actually do and run the Windows application smoother.
Usually this file size is 1.5 times than your actual physical memory size and might consume a huge disk space of your computer hard drive space.
Be carefully if you want to delete disable virtual memory feature. You need to have enough RAM memory.
In order disable virtual memory follow next steps:
– Go to Control Panel and run System.
– Click on Advanced tab and click the Settings button on Performance.
– Click Advanced button and Virtual Memory window appears. Select “No paging file” item and click the Set button if you want to remove pagefile.sys.
– In order to apply these changes you need to restart Windows.

Delete individual files
Using SpaceMonger application you can easy delete temporary file, updates installer files and other huge files. But, BE CARFEFULL WHAT YOU DELETE. Don’t delete Windows files, Program Files files, or other important files.
This application provides zoom in/zoom out features in order to have a complete overview of what you intend to delete.
Some paths that could be deleted are:

  • \Windows\Temp
  • \Documents and Settings\user_name\Local Settings\Temp
  • \Documents and Settings\user_name\Application Data\Microsoft\Office\Recent
  • \Documents and Settings\user_name\Local Settings\Temporary Internet Files
  • \WINNT\TEMP
  • If you are using Windows Vista or Windows 7 and you are tempted to remove \Windows\Winsxs, forget this idea. The Winsxs folder, stores multiple copies of dll’s in order to let multiple applications run in Windows without any compatibility problem. These are actually, different versions of the same files which are being stored; as different programs may require different versions. Winsxs is Win 7 and Vista’s native assembly cache. Libraries which are being by multiple applications are stored there. So, don’t delete WinSxS folder!

    Because many application creates own temporary files, I should recommend to you the running of another nice freeware tool, CCleaner. It’s easy to clean all other temporary data and registry, too.
    You can use SpaceMonger tool on other non-system partition, too.

    Dynamic popup and drop down menus for custom representations

    Many applications allow dynamic customization for visual objects or data views. For instance, well known Internet Explorer application provides toolbars customization using a popup menu that appears when the user execute right click mouse action in toolbar zone area.

    Internet Explorer sample menu

    Other sample where this kind of menu is very useful is when it’s used in order to customize database data representation in Windows controls like List control or grid control. These kind of applications allow data filtering and show/hide columns using this kind of menu. The user just right click on control header and gets what he need.

    Starting from this idea, I implemented a class CDynamicPopupMenu. This class allows an easy building of this kind of menus. I used if in a demo dialog base application over a list control.

    my demo application

    Internally, this class uses a STL container (std::map) with a data structure used in order to embed items menu properties. When the menu is built, menu’s behavior is implemented using these properties.

    Add new menu item
    The new item add menu method has next definition:

    void AddItemData(const int item_id, const int parent_id,
    bool is_visible, bool check_flag, bool has_child,
    const std::wstring item_name, bool enable_flag);

    where:

    • item_id – represents internal item ID; the ID is used for menu customization, too;
    • parent_id – parent item ID used when we define a new items sub-group (a drop-down menu); the attribute value is 0 if menu item is a part of initial menu;
    • is_visible – this flag is used a item is checked / unchecked. In my demo application this flag is set true for all list control’s columns that we want to display. For “Select All” and “Check All” items this flag is false because we want to create new subgroup that contains new columns, but we don’t have “Select All” or “Check All” columns.
    • check_flag – this flag allow check/uncheck menu property;
    • has_child – if is true allows a subgroup definition (a new drop-down menu);
    • item_name – unicode menu item name;
    • enable_flag – defines if the item is enable or disable.

    Add separator item
    Add separator item method definition looks like this:

    void AddItemSeparator(const int item_id, const int parent_id);
    

    where:

    • item_id – menu item ID;
    • parent_id – parent item ID from the subgroup has started; the attribute value is 0 if menu item is a part of initial menu.

    Menu add items sample
    In my demo application, in CtestPopupMenuDlg::SetDefaultMapValues(void) method, among other things, you can find next calls:

    m_pCustPPMenu->AddItemData(MI_MAINITEM_1, 0, true, true, false,
                               _T("Item 1"), true);
    m_pCustPPMenu->AddItemSeparator(MI_MAIN_SEP1,0);
    m_pCustPPMenu->AddItemData(MI_MAINITEM_4_GROUP_1, 0, true, true, true, 
                               _T("Group 1"), true);
    m_pCustPPMenu->AddItemData(MI_GROUP_1_SUBITEM_1, MI_MAINITEM_4_GROUP_1,
    false, false, 
                               false, _T("G1-Select All"), true);
    m_pCustPPMenu->AddItemData(MI_GROUP_1_SUBITEM_2, MI_MAINITEM_4_GROUP_1,
    true, false, 
                               false, _T("G1-Item 12"), true);

    Get menu internal data
    In order to access the internal data container (std::map) that stores all dynamic menu items you just can use next method:

    DynamicMenuData* GetMenuData();

    followed by:

    DynamicMenuData* pItemsMap = m_pCustPPMenu->GetMenuData();

    Create and display menu
    Menu creation must be done just after we add all menu items. The menu is displayed only after TrackPopupCustomMenu() call. The definition of this method looks like this:

    DWORD TrackPopupCustomMenu(POINT point, HWND hWnd);

    where:

    • point – mouse coordinates where the menu start building;
    • hWnd – parrent window handle where the menu is created.

    Function’s return value is the menu IDs that was selected. If no item was selected the function returns 0.
    In my demo application, menus creation is called on list control right-click method (NM_RCLICK).

    void CtestPopupMenuDlg::OnNMRclickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
      POINT point;
      ::GetCursorPos(&point);
    
      int nSelItem = m_pCustPPMenu->TrackPopupCustomMenu(point, m_ctrlList.m_hWnd);
    
      if (0 < nSelItem) {
       pNMHDR->hwndFrom = m_ctrlList.m_hWnd;
       pNMHDR->idFrom = nSelItem;
       pNMHDR->code = WM_NOTIFY;
    
       OnNotify( 0, (LPARAM)pNMHDR, pResult);
      }
    
      *pResult = 0;
    }

    As you can see, I’m calling TrackPopupCostumMenu(), using mouse point property when the user right-click over list control.
    I am saving list control handler, selected item ID and WM_NOTIFY value into a pointer to message notification structure NMHDR. Then I’m passing this pointer to OnNotify() method.
    Using WM_NOTIFY message and OnNotify() method, I inform parent control window that a new event was generated.

    BOOL CtestPopupMenuDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
      NMHDR *p = (NMHDR*) lParam;
      _ASSERT(p);
      _ASSERT(m_pCustPPMenu);
      bool bFlag = false;
    
      switch (p->idFrom)
      {
        case MI_MAINITEM_1:
        m_pCustPPMenu->GetItemCheckedFlag(MI_MAINITEM_1, bFlag);
        m_pCustPPMenu->SetCheckedItemFlag(MI_MAINITEM_1, !bFlag);
        FillData();
        break;
        
        case MI_MAINITEM_2:
        m_pCustPPMenu->GetItemCheckedFlag(MI_MAINITEM_2, bFlag);
        m_pCustPPMenu->SetCheckedItemFlag(MI_MAINITEM_2, !bFlag);
        FillData();
        break;
    
       // =======================
       // Silviu: this method store other menu items handlers, too
       // =======================
    
       default:
       break;
      }
    
      return CDialog::OnNotify(wParam, lParam, pResult);
    }

    I am calling GetItemCheckedFlag() if order to detect selected item check status (check / uncheck). Then, if item state means check I apply negation over this bool flag and I’m calling SetCheckedItemFlag() method. Finnaly this method produce changes in my control list, depending on my menu command (FillData() method).

    Menu interaction with parent window (list control)
    In my demo application, the interaction between dynamic menu and list control to be treated by FillData() method.
    In order to use CDynamicPopupMenu’s internal container data is need to initialize a DynamicMenuData pointer with GetDynamicMenuData()’s returned value.

    void CtestPopupMenuDlg::FillData()
    {
      _ASSERT(m_pCustPPMenu);
    
      DynamicMenuData *pItemsMap = m_pCustPPMenu->GetDynamicMenuData();
    
      int nCol = 0;
      if ((NULL != pItemsMap) && (!pItemsMap->empty()))
      {
        // reset columns
        int nColumnCount = m_ctrlList.GetHeaderCtrl()->GetItemCount();
        for (int i=0;i < nColumnCount;i++) 
          m_ctrlList.DeleteColumn(0);
    
        for (iterDynMenu itm = pItemsMap->begin(); itm != pItemsMap->end(); ++itm)
        {
          if (m_pCustPPMenu->GetIsVisible(itm) && m_pCustPPMenu->GetIsChecked(itm))
          {
             m_ctrlList.InsertColumn(nCol++, itm->second.sItemName.c_str(), LVCFMT_LEFT, 70);
          }
        }
      }
    }

    Using that pointer to internal menu data, I iterate over internal container, and for those items that are visible and selected set on true I insert columns in my list control.
    Similarly, when using such menus, the application can apply filters on real data.
    CDynamicPopupMenu class contains other useful methods. This kind of menu can be used in different situations in order to change application’s behavior.

    Download demo application: testPopupMenu (Visual C++ 2005 project)

    Versionable Object’s Serialization in MDI applications

    This article represents a follow-up of the last article, “Versionable Object’s Serialization using MFC in non Document View applications”. In that article I presented to you a way to solve incompatibility issues between different file versions of the same application, based on MFC serialization into a dialog base application.
    But, the dialog base applications are not the best way to use and apply MFC serialization.
    Applications base on document view architecture (MDI or SDI) are the best solution when we want to develop MFC application with serialization support.
    Document View MFC architecture offers support for automatic save and load document to/from a file on a storage area, using a serialization mechanism. MDI (Multiple Document Interface) and SDI (Single Document Interface) application offers default serialization basic mechanism.

    SerAddressBookMDI Main Window

    The serialization is customizable. It’s important to define the right binary elements format, file version and element count. Finally, we have to complete the serialize method.
    Into a document view application some document class’s methods are mapped over New, Open, Save and Save As items, available in File menu. The application’s user can use these commands in order to create or open files, tracking document status changes and serialize data to/from a file.
    MDI applications create a CDocument derived class instance for each open document. SDI applications reuse the same single CDocument derived class instance for each open file.
    In a MDI application CDocument class and the classes derived from this are responsible with internal objects serialization control. This class tracks each change that appears in our document. In this way, our application knows that some changes have been made when we accidentally want to close the application, without saving last changed data.
    When a document is loaded, a CArchive instance is created for reading file internal data. When we create a new document, a CArchive store instance is created and this instance is used for store to a file process.
    CArchive routines are strongly optimized in order to provide a viable store/load mechanism, even if we are serializing a huge number of small items.

    In my demo application, I used the same idea as in my last article: an address book with two versions.

    In current application the serialization process it’s very different then the old application. The serialization process is realized by a CDocument class instance that interaction with the rest application classes. CAddressBook class place was taken by the document class CSerAddressBookMDIDoc.

    In a real application it’s recommended to use unique identifier (UID) in order to “detect” the right object. For simplicity, in my demo application this unique identifier was defined “name” attribute. For instance, I’m using it for a contact update process.

    Document class – CSerAddressBookMDIDoc

    The interface of document class looks like this:

    class CSerAddressBookMDIDoc : public CDocument
    {
      protected: // create from serialization only
      CSerAddressBookMDIDoc();
      DECLARE_DYNCREATE(CSerAddressBookMDIDoc)
    
      // Attributes
      public:
    
      // Operations
      public:
    
      // Overrides
      public:
      virtual BOOL OnNewDocument();
      virtual void Serialize(CArchive& ar);
    
      void SetFileVersion(UINT nFV) { m_nFileVersionSchema = nFV; }
      UINT GetFileVersion() const { return m_nFileVersionSchema; }
    
      const ContactList& GetContacts() const {return m_cContactsList;}
      bool AddContact(const Contact& contact);
      bool RemoveContact(const CString& firstname, const CString& lastname);
      POSITION FindContact(const CString& firstname) const;
      bool FindContact(const CString& firstname, Contact& contact) const;
      bool UpdateContact(const CString& firstname, Contact& contact);
    
      // Implementation
      public:
      virtual ~CSerAddressBookMDIDoc();
      BOOL DoSave(LPCTSTR lpszPathName, BOOL bReplace); // need to override this
      virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
    
      #ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;
      #endif
    
      ContactList m_cContactsList;
    
      // Generated message map functions
      protected:
      DECLARE_MESSAGE_MAP()
    
      private:
      INT m_nFileVersionSchema;
    };

    As you can see, this time I’m using DECLARE_DYNCREATE() macro. This macro allows dynamic document objects runtime creation (a MDI application’s requirement).
    In this class I reused some CAddressBook’s methods. These methods handle objects from m_cContactsList list.
    ContactList is an alias for our Contacts MFC list:
    [cpp]typedef CList ContactList;[/cpp]

    The serialization data method of this document class is listed down:

    void CSerAddressBookMDIDoc::Serialize(CArchive& ar)
    {
      if (ar.IsStoring()) // storing
      {
        ar << m_nFileVersionSchema; // write the number of contacts
        ar << (int)m_cContactsList.GetCount(); 
        Contact::SetCurrentContactVersion(m_nFileVersionSchema); // write all the contacts 
        POSITION pos = m_cContactsList.GetHeadPosition(); 
        while(pos != NULL)
        { 
          Contact contact = m_cContactsList.GetNext(pos);
          contact.Serialize(ar); 
        } 
      } else { // loading
        ar >> m_nFileVersionSchema;
    
        m_cContactsList.RemoveAll();
    
        int count = 0;
        ar >> count;
    
        // read the number of contacts
        while (count-- > 0)
        {
          Contact contact;
          contact.Serialize(ar);
    
          m_cContactsList.AddTail(contact);
        }
      }
      
      UpdateAllViews(NULL);
    }

    This method read (load) or write (store) serialized Contact class’s object using a CArchive object at runtime. If the code flow runs over true branch all information is saving from our list to our file. If code flow choose else branch it means that we are loading an existing file and all file data is loaded in our list.

    In order to store data in a file, initially I save file version (m_nFileVersionSchema) and items count. Then I iterate over all m_cContactsList items (Contact type item) and I serialize this data in order to store in my new file.

    If I want to load data from a file, I am reading file version, I clean my list, I get Contact stored items count and as long as this count variable value is positive I serialized with load flag all Contact file data.
    All serialized Contact entities go to m_cContactsList list. Each time we want to display our files data we have to iterate over this list.

    Internal serialized class – Contact

    As you have seen, in CSerAddressBookMDIDoc::Serialize() method, for both situations (store/load) a new Contact instance is created and this object is passed to Contact::Serialize() for load/store operation.
    The serializing Contact items method looks like this:

    void Contact::Serialize( CArchive& ar )
    {
      if (ar.IsStoring())
      {
        CRuntimeClass* pruntime = Contact::GetRuntimeClass();
        int oldnr = pruntime->m_wSchema;
        pruntime->m_wSchema = CURRENT_VERSION;
    
        ar.SerializeClass(pruntime);
    
        switch (CURRENT_VERSION)
        {
         case 1:
           ar << m_strFirstName << m_strLastName << m_strAddress << m_strPhone; 
         break; 
         case 2:
           ar << m_strFirstName << m_strLastName << m_strAddress << m_strPhone << m_strMobilePhone << m_strEmail; break;
         default: // unknown version for this object 
           AfxMessageBox(_T("Unknown file version."), MB_ICONSTOP);
          break; 
        }
        
        pruntime->m_wSchema = oldnr;
      } else // loading code
        {
        ar.SerializeClass(RUNTIME_CLASS(Contact));
    
        UINT nVersion = ar.GetObjectSchema();
    
        switch (nVersion)
        {
        case 1:
          ar >> m_strFirstName >> m_strLastName >> m_strAddress >> m_strPhone;
          m_strMobilePhone = _T("");
          m_strEmail = _T("");
        break;
    
        case 2:
          ar >> m_strFirstName >> m_strLastName >> m_strAddress >> m_strPhone >> m_strMobilePhone >> m_strEmail;
          break;
    
        default:
         // unknown version for this object
         AfxThrowArchiveException(CArchiveException::badSchema);
         break;
        }
      }
    }

    If I want to store my data to a file, I obtain a runtime pointer to my serialized class, in order to set my file version schema. Then, depending on file version I serialize right object data and finally I reassign initial version schema value of my runtime class.

    If I am loading a file, I call Contact::Serialize() method, I get file version schema and depending on schema value, I add right data to my document class.

    View class – CSerAddressBookMDIView
    This class is responsible with document class content (loaded file’s data) graphical representation. In my demo application, the view class is derived from CListView and has REPORT flag set in order to display data into a grid like.

    Main responsible method with client window’s list control population is CSerAddressBookMDIView::PopulateList() and is listed down:

    void CSerAddressBookMDIView::PopulateList()
    {
      CSerAddressBookMDIDoc* pDoc = GetDocument();
      ASSERT_VALID(pDoc);
    
      CreateViews(pDoc->GetFileVersion());
    
      CListCtrl *pListCtrl = &GetListCtrl();
      ASSERT_VALID(pListCtrl);
    
      // get a reference to the contacts list
      const ContactList& contacts = pDoc->GetContacts();
    
      // iterate over all contacts add add them to the list
      int nCurrentItem = 0;
      POSITION pos = contacts.GetHeadPosition();
      while(pos != NULL)
      {
        const Contact contact = contacts.GetNext(pos);
    
        nCurrentItem = pListCtrl->InsertItem(nCurrentItem, contact.GetFirstName());
        pListCtrl->SetItemText(nCurrentItem, 1, contact.GetLastName());
        pListCtrl->SetItemText(nCurrentItem, 2, contact.GetAddress());
        pListCtrl->SetItemText(nCurrentItem, 3, contact.GetPhoneNumber());
    
        switch(pDoc->GetFileVersion())
        {
          case 1:
          break;
          case 2:
          pListCtrl->SetItemText(nCurrentItem, 4, contact.GetMobileNumber());
          pListCtrl->SetItemText(nCurrentItem, 5, contact.GetEmail());
          break;
          default:
          break;
        }
      }
    }

    First, we obtain a pointer to our current document. Then, we call the method that inserts right list control columns, depending on file version (CreateViews() method).
    We obtain a CListView pointer and an object reference to fist contact from contacts list. Then, as long as we have elements, we iterate over list’s elements (in a while() loop) and insert data to our list control.

    PopulateList() method is called from overwrite CSerAddressBookMDIView::OnUpdate() method. OnUpdate() method is called by MFC framework as long as a document is changed.
    The original OnUpdate() method is called by CDocument::UpdateAllViews() and is implemented in CView class.

    In order to add/remove/update records from our documents I created a special dialog, launched from my Menu menu.
    Display modal dialog method is listed down:

    void CSerAddressBookMDIView::OnMymenuChangedata()
    {
      CSerAddressBookMDIDoc *pDoc = GetDocument();
      ASSERT_VALID(pDoc);
    
      CManipulateDataDlg dlg;
    
      dlg.SetAddressDocument(pDoc);
    
      if (IDOK == dlg.DoModal())
      {
        PopulateList();
      }
    }

    Because I have to interact from my dialog window with contacts list of current document, I have to pass the pointer of my document class ( dlg.SetAddressDocument(pDoc) ) to my dialog window class. If the dialog is closed using Exit button (IDOK id) then the view is refilled, using PopulateList() call.

    CManipulateDataDlg class

    This class is responsible with the management of document contact list items. The difference between this dialog class and the dialog class of last article is that this class is not responsible with load/store process. This role was taken by document view architecture.

    Dialog’s control list population method looks like this:

    void CManipulateDataDlg::PopulateList()
    {
      // delete all current members
      m_cList.DeleteAllItems();
    
      // get a reference to the contacts list
      const ContactList& contacts = m_pAddressDoc->GetContacts();
    
      // iterate over all contacts add add them to the list
      int nCurrentItem = 0;
      POSITION pos = contacts.GetHeadPosition();
      while(pos != NULL)
      {
        const Contact contact = contacts.GetNext(pos);
    
        nCurrentItem = m_cList.InsertItem(nCurrentItem, contact.GetFirstName());
        m_cList.SetItemText(nCurrentItem, 1, contact.GetLastName());
        m_cList.SetItemText(nCurrentItem, 2, contact.GetAddress());
        m_cList.SetItemText(nCurrentItem, 3, contact.GetPhoneNumber());
    
        switch(m_pAddressDoc->GetFileVersion())
        {
          case 1:
          break;
          case 2:
          m_cList.SetItemText(nCurrentItem, 4, contact.GetMobileNumber());
          m_cList.SetItemText(nCurrentItem, 5, contact.GetEmail());
          break;
        }
      }
    }

    Each time we clean the contact list we obtain a reference to the beginning of document contacts list. Depending on file version schema (1 or 2), dialog’s controls are customized. Then, we iterate over contact list elements (ContactList) and I insert data into my control list.

    MDI support for many file extension

    Default MDI applications come with only one file support and only one file extension file format.
    Sometimes, our applications need to support different file format and more file extensions. In my demo application is necessary to support two file format and two file version (version 1 (*.sab1) and version 2 (*.sab2)).
    Same time, the application must support old file format conversion to new file format and vice versa.
    You can find multi file support detailed information for document view MFC application to Microsoft KB 141921. Other useful reference you can find here.
    Starting from these references my application support two file format. I figure some important changes that I made into my initialize method, CSerAddressBookMDIApp::InitInstance.

    BOOL CSerAddressBookMDIApp::InitInstance()
    {
      // ----------
      // MFC’s wizard generated code…
      // ----------
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));
      LoadStdProfileSettings(4); // 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
    
      m_pDocManager = new CMultiDocManager; // Silviu
    
      CMultiDocTemplate* pDocTemplate;
      pDocTemplate = new CMultiDocTemplate(IDR_SerAddressBookTYPE,
      RUNTIME_CLASS(CSerAddressBookMDIDoc),
      RUNTIME_CLASS(CChildFrame), // custom MDI child frame
      RUNTIME_CLASS(CSerAddressBookMDIView));
      if (!pDocTemplate)
      return FALSE;
      AddDocTemplate(pDocTemplate);
    
      //Silviu
      pDocTemplate = new CMultiDocTemplate(
      IDR_SerAddressBook2TYPE,
      RUNTIME_CLASS(CSerAddressBookMDIDoc),
      RUNTIME_CLASS(CChildFrame), // custom MDI child frame
      RUNTIME_CLASS(CSerAddressBookMDIView));
      if (!pDocTemplate)
      return FALSE;
      AddDocTemplate(pDocTemplate);
    
      //Silviu
      pDocTemplate = new CMultiDocTemplate(
      IDR_SerAddressBook3TYPE,
      RUNTIME_CLASS(CSerAddressBookMDIDoc),
      RUNTIME_CLASS(CChildFrame), // custom MDI child frame
      RUNTIME_CLASS(CSerAddressBookMDIView));
      if (!pDocTemplate)
      return FALSE;
      AddDocTemplate(pDocTemplate);
    
      // create main MDI Frame window
      CMainFrame* pMainFrame = new CMainFrame;
      if (!pMainFrame || !pMainFrame->LoadFrame(IDR_SerAddressBookTYPE))
      {
        delete pMainFrame;
        return FALSE;
      }
    
      // ----------
      // MFC’s wizard generated code…
      // ----------
    
      return TRUE;
    }

    First point that I should mention, after LoadStdProfileSettings() (function written by MFC wizard) call, is the initialization of m_pDocManager attribute (pointer to CDocManager class, used for document template management) with a new object pointer to CMultiDocManager (class defined be me according with Microsoft Knowledge Base 141921). CMultiDocManager class overwrites some methods from CDocManager: CreateNewDocument(), DoPromptFileName(), OnFileNew().

    Then, besides default document application template (with resource ID IDR_SerAddressBookTYPE), I create two new templates for my two different files format.
    All templates are added into my document template list (AddDocTemplate()). Last significant change from InitInstance() means the right frame window (IDR_SerAddressBookTYPE – contains Save and Save As options).

    Conclusion:
    Multiple Document Interface (MDI) architecture is the best for this kind of data container application. MFC framework offers stable and complete support for objects serialization: storing and loading process.
    Many of Microsoft Office applications are based on this architecture.

    Download demo application: SerAddressBookMDI (Visual C++ 2005 project)

    Versionable Object’s Serialization using MFC in non Document View applications

    Most existing applications operate with data that must be stored and loaded in different times and different locations. The data is stored in text or binary files with a well defined format.
    The Problem
    Initially, in the first version 1.0, an application operates with data structures that can be stored and loaded. But, next version (2.0) these data structures suffers changes. Some structure’s attributes are added and other could be removed. These things change files format and structure when a new file version is saved.
    Question: What happens when you are using an application version 2.0 and you are trying to load old files format (version 1.0)?
    Answer: Most cases should cause incompatibility troubles between the current new application and files in old format. This could throw exceptions and the application could have undefined behavior.
    That’s why the application must be written in order to be able to open both file versions.
    Solution
    In order to solve the compatibility issue exist many solutions, more or less professional. The recommended solution is the serialization.
    The serialization is a write / read object process to/from a persistent storage. Serialization it’s a good choice in order to maintain a good data structure. Many different frameworks offer serialization support. One of these is Microsoft Foundation Classes – MFC.

    If we want to use MFC serialization support, we can use a CArchive instance. This object, combined with a CFile instance provides a strong mechanism for objects serialization.

    Because, in different applications version, the file suffers significant structure changes we have to use MFC’s serialization concept called Versionable Schema.
    Versionable Schema means the using of CArchive class methods GetObjectSchema() and SetObjectSchema() and a constant VERSIONABLE_SCHEMA (that you can find it in afx.h file and has 0x80000000 value) combined with a OR LOGIC operator and the last application version number as a parameter of IMPLEMENT_SERIAL macro.
    The GetObjectSchema() method is used in order to detect stored objects version from a file that is loaded in our application. The complement of this method, SetObjectSchema() method, allow us to save the objects version.
    Different by the C++ I/O standard streams, the CArchive class is special designed only for objects serialization in binary files.

    In order to serialize a class’s objects we have to follow next steps:
    1. The class that we want to serialize has to be derived from the abstract class CObject (or other classes derived from CObject).
    2. Overwrite CObject’s Serialize() method.
    3. Use DECLARE_SERIAL macro in your class declaration.
    4. The serializable class has to have a default constructor, without arguments.
    5. Use IMPLEMENT_SERIAL macro in the implementation file of serializable class.

    More information you can find it here and in links of this page.

    But, from these steps until to a complete serialization and versionable application there are few significant steps to follow.
    Next, I will present to you a Dialog base sample application that supports serialization and is versionable.

    Sample application – SerAddressBook

    Next, I will present to you how you can create an address book application (based on a MFC Dialog application architecture).

    Suppose that initially our client requested us an address book that contaions: name, prename, address and phone number. But, once with the mobile phone and Internet area extensions our client needs two new fields for mobile phone number and for email address.
    File versions structure

    Our application with a file version 2 looks like this:
    Application window

    Because this is a demo application I kept on my window the possibility to save both version, using two radio buttons.
    A good application design helps us if we have new requirements and we have to change the application structure. The code changes have to be done without too many code interactions. Ideally, with add code only.
    That’s why, my application classes design looks like this:
    Classes Hierarchy

    Although Contact class and CAddressBook class are serializable, the objects serialization is implemented into Contact class.

    Contact class

    From Contact’s interface class you can observe:
    • I derived this class from the abstract CObject;
    DECLARE_SERIAL, macro calling;
    Serialize()‘s method declaration in order to overwrite the parent class;
    • Our class attributes.

    class Contact : public CObject
    {
    public:
      DECLARE_SERIAL(Contact);
    
      Contact();
      Contact(const Contact& rhs);
      Contact(const CString& strFirstName, const CString& strLastName, const CString& strAddress, const CString& strPhone,
      // these two must have a default value because they are not used in the first version
      const CString& strMobilePhone = _T(""), const CString& strEmail = _T(""));
      Contact& operator=(const Contact& rhs);
    
      virtual ~Contact();
    
      void Serialize( CArchive& ar );
    
      CString GetFirstName() const {return m_strFirstName;}
      CString GetLastName() const {return m_strLastName;}
      CString GetAddress() const {return m_strAddress;}
      CString GetPhoneNumber() const {return m_strPhone;}
      CString GetMobileNumber() const {return m_strMobilePhone;}
      CString GetEmail() const {return m_strEmail;}
    
      void SetFirstName(const CString& name) {m_strFirstName = name;}
      void SetLastName(const CString& name) {m_strLastName = name;}
      void SetAddress(const CString& addr) {m_strAddress = addr;}
      void SetPhoneNumber(const CString& phone) {m_strPhone = phone;}
      void SetMobileNumber(const CString& mobile) {m_strMobilePhone = mobile;}
      void SetEmail(const CString& email) {m_strEmail = email;}
    
      static void SetCurrentFileVersion(int nCV) { CURRENT_VERSION = nCV; }
      static int GetCurrentFileVersion() { return CURRENT_VERSION; }
    
    private:
      static int CURRENT_VERSION;
      CString m_strFirstName;
      CString m_strLastName;
      CString m_strAddress;
      CString m_strPhone;
      CString m_strMobilePhone;
      CString m_strEmail;
    };
    
    typedef CList<contact, contact=""> ContactList;

    The last line represents an “alias” definition for a MFC list definition, used in order to store displayed data. This list is using for Contact object administration.
    In the implementation file we declare the IMPLEMENT_SERIAL macro and we are initializing the static variable with our current application version.

    IMPLEMENT_SERIAL( Contact, CObject, VERSIONABLE_SCHEMA | 2);
    int Contact::CURRENT_VERSION = 2;

    Into this declaration you can observe the VERSIOABLE_SCHEMA constant combined on OR logic with 2 (my demo application last version). This, the third macro argument is essential for objects versioning, combined with CArchive::GetObjectSchema() and CArchive::SetObjectSchema().
    More details about this constant and it using process or about these methods you can find here.
    The implementation of Contacte::Serialize() looks like this:

    void Contact::Serialize( CArchive& ar )
    {
      if (ar.IsStoring())
      {
        CRuntimeClass* pruntime = Contact::GetRuntimeClass();
        int oldnr = pruntime->m_wSchema;
        pruntime->m_wSchema = CURRENT_VERSION;
    
        ar.SerializeClass(pruntime);
    
        switch (CURRENT_VERSION)
        {
          case 1:
            ar << m_strFirstName << m_strLastName << m_strAddress << m_strPhone;
          break; 
          
          case 2: 
            ar << m_strFirstName << m_strLastName << m_strAddress << m_strPhone << m_strMobilePhone << m_strEmail;
          break;
          
          default: // unknown version for this object 
            AfxMessageBox(_T("Unknown file version."), MB_ICONSTOP); 
            break; 
        } pruntime->m_wSchema = oldnr;
      } else // loading code
        {
          ar.SerializeClass(RUNTIME_CLASS(Contact));
    
          UINT nVersion = ar.GetObjectSchema();
    
          switch (nVersion)
          {
            case 1:
            ar >> m_strFirstName >> m_strLastName >> m_strAddress >> m_strPhone;
            m_strMobilePhone = _T("");
            m_strEmail = _T("");
            break;
    
            case 2:
            ar >> m_strFirstName >> m_strLastName >> m_strAddress >> m_strPhone >> m_strMobilePhone >> m_strEmail;
            break;
    
            default:
            // unknown version for this object
            AfxThrowArchiveException(CArchiveException::badSchema);
            break;
         }
       }
    }

    If CArchive constructor sets store-load flag on CArchive::store (save to file) then the code flow will follow true if’s block and objects data is sending to archive and stored in the file (including file version, too).
    When we want to open an existing file, our CArchive’s constructor receives CArcuhive::load flag and enter to else if’s block of Serialize(). Is extracting file version, and after that is loading all Contact objects.

    CAddressBook class

    CAddressBook class make the link between interface dialog class (CSerAddressBookDlg) and the serialized class Contact. This class contains a Contact objects list. CAddressBook class is administrating this contact list and is realizing load/store object.

    The interface of this class is looking like this:

    class CAddressBook : public CObject
    {
      DECLARE_SERIAL(CAddressBook);
      ContactList m_cContactsList;
    
      public:
        CAddressBook();
        virtual ~CAddressBook();
    
        void Serialize( CArchive& ar );
    
        bool AddContact(const Contact& contact);
        bool RemoveContact(const CString& firstname,
        const CString& lastname);
        POSITION FindContact(const CString& firstname) const;
        bool FindContact(const CString& firstname, Contact& contact) const;
    
        const ContactList& GetContacts() const {return m_cContactsList;}
    
        void SetFileVersion(int nFV) { m_uiFileVersion = nFV; }
        int GetFileVersion() { return m_uiFileVersion; }
    
      private:
        int m_uiFileVersion;
    };

    Into this declaration you can see the existence of a contact list instance (m_cContactsList). This class contains add, update or remove contact methods.

    Because our class has to be a serialized method we have to overwrite Serialize() method, the method has to me used by the client class ( in our case the interface class – CSerAddressBookDlg).

    void CAddressBook::Serialize(CArchive& ar)
    {
      ar.SerializeClass(RUNTIME_CLASS(CAddressBook));
    
      // storing
      if (ar.IsStoring())
      {
        ar << m_uiFileVersion; // write the number of contacts 
        ar << (int)m_cContactsList.GetCount(); 
        Contact::SetCurrentFileVersion(m_uiFileVersion);
    
        // write all the contacts 
        POSITION pos = m_cContactsList.GetHeadPosition();
        while(pos != NULL) { 
          Contact contact = m_cContactsList.GetNext(pos); 
          contact.Serialize(ar); 
        } 
      } else // loading 
      { ar >> m_uiFileVersion;
        m_cContactsList.RemoveAll();
        int count = 0;
        ar >> count;
        
        // read the number of contacts
        for(INT_PTR i = 0; i < count; ++i) 
        { 
          Contact contact;
          contact.Serialize(ar);
          m_cContactsList.AddTail(contact);
        } 
      } 
    }

    Because CArchive class doesn’t provides any method or attribute in order to obtain the objects (I’m counting when I’m loading or storing), I decided to save the objects count into my files. That’s why, if I’m storing, I call next line:

    ar << m_cContactsList.GetCount();

    Same story, for file version, before starting Contact objects serialization:

    ar << m_uiFileVersion;

    Then, into a while loop I’m iterating over the contact list. I am serializing the data and I’m storing to my new file. If I load a file from disk (else branch) then I follow next steps:
    • I’m cleaning contact list;
    • Get the objects count;
    • Get file version and serialize all objects for load;
    • Add all data to my Contact object list;

    CSerAddressBookDlg

    – The application interface class

    Once we have implemented this serialization mechanism the using of this one into our application became very easy.

    For instance, when the user wants to save into a file all he’s new data, he will call next method:

    void CSerAddressBookDlg::SaveDataContentToFile(CString strSaveFile)
    {
      CFile wFile(strSaveFile, CFile::modeCreate | CFile::modeWrite);
    
      // Create a storing archive
      CArchive arStore(&wFile, CArchive::store);
      m_c_AddressBook.Serialize(arStore);
    
      // Close the storing archive
      arStore.Close();
    
      PopulateList();
    }

    As you can see, I have a CFile object that I’m using it, combined with a CArchive instance, for data storing to a file. Although my local CArchive instance receive as a first parameter the address of the file handler and the store flag CArchive::store.
    Next I call CAddressBook::Serialize() method and I’m closing the store operation.

    Loading file method, based on my Contact serialization mechanism looks like this:

    void CSerAddressBookDlg::LoadDataContentFromFile(CString strLoadedFile)
    {
      CFile rFile(strLoadedFile, CFile::modeRead);
    
      // Create a loading archive
      CArchive arLoad(&rFile, CArchive::load);
      m_c_AddressBook.Serialize(arLoad);
    
      // Close the loading archive
      arLoad.Close();
    
      switch (m_c_AddressBook.GetFileVersion())
      {
        case 1:
        ((CButton*)GetDlgItem(IDC_RADIO_VERSION_1))->SetCheck(BST_CHECKED);
        ((CButton*)GetDlgItem(IDC_RADIO_VERSION_2))->SetCheck(BST_UNCHECKED);
        OnBnClickedRadioVersion1();
        break;
        case 2:
        ((CButton*)GetDlgItem(IDC_RADIO_VERSION_1))->SetCheck(BST_UNCHECKED);
        ((CButton*)GetDlgItem(IDC_RADIO_VERSION_2))->SetCheck(BST_CHECKED);
        OnBnClickedRadioVersion2();
        break;
        default:
        break;
      }
    
      // repopulate the list
      PopulateList();
    }

    As you can see, I am creating a local CFile object, needed for reading operation. Although, I’m creating a local CArchive instance that received as constructor parameter the file handler address with CArchive::load flag.
    Then, I’m calling CAddressBook::Serialize() method. Is entering on else branch and finally we are disconnected the object from file.
    The last line contains PopulateList() call and is my populate list method. It populates my list control (a CListCtrl instance) with the file loaded data in order to display it into our dialog.

    void CSerAddressBookDlg::PopulateList()
    {
      // delete all current members
      m_cList.DeleteAllItems();
    
      // get a reference to the contacts list
      const ContactList& contacts = m_c_AddressBook.GetContacts();
    
      // iterate over all contacts add add them to the list
      int nCurrentItem = 0;
      POSITION pos = contacts.GetHeadPosition();
      while(pos != NULL)
      {
        const Contact contact = contacts.GetNext(pos);
    
        nCurrentItem = m_cList.InsertItem(nCurrentItem, contact.GetFirstName());
        m_cList.SetItemText(nCurrentItem, 1, contact.GetLastName());
        m_cList.SetItemText(nCurrentItem, 2, contact.GetAddress());
        m_cList.SetItemText(nCurrentItem, 3, contact.GetPhoneNumber());
    
        switch(m_c_AddressBook.GetFileVersion())
        {
          case 1:
          break;
          case 2:
          m_cList.SetItemText(nCurrentItem, 4, contact.GetMobileNumber());
          m_cList.SetItemText(nCurrentItem, 5, contact.GetEmail());
          break;
        }
      }
    }

    Conclusions:
    The MFC’s Document View architecture offers complete serialization support. Each MDI/SDI application contains default serialization support. My demo solution presented is an adapted serialization version for dialog base applications.

    Download demo application: SerAddressBook (Visual C++ 2005 project)