Onega's profileOnegaBlogListsNetwork Tools Help

Blog


    September 25

    ATL, MFC and scoped_any

    I am glad to found Achieve More Reliable Resource Management with Our Custom C++ Classes, but when using it in a unit test project, I got weird compile error. The error points to line 238 of scoped_any.h

    DECLARE_SMART_ANY_TYPEDEFS(scoped) and according to other error message it claims “close_rpc_binding” is not valid. After struggling it turned out to be confliction between ATL and MFC. In an ATL project the source file compiled successfully, but in its corresponding unit test project I have to use MFC because CPPUNIT MFCTestRunner is used to produce GUI. At last the problem was solved by moving MFC related headers from stdafx.h to where they are actually required, and disable precompiled header.

    There is also a post(Automate Resource Management with shared_ptr) demonstrating resource management via custom deleter and boost::shared_ptr.

    June 18

    Reusing CWinApp derived MFC class in Console application

    Once I have to integrate a MFC GUI application into a console application. The MFC has a global "theApp" object, but I also want to change something before its ctor was called, so I moved "theApp" from global to local scope, but ctor of CMainFrame reproted error. The trick is to call AfxWinInit after declaring theApp object.

    {
        CMyApp::QUEUE_LENGTH = queue_length;
        CMyApp theApp; // don't use global instance so that I can pass options to it
        // Need to init MFC again to set theApp in correct state
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
            std::cout << "Fatal Error: MFC initialization failed" << std::endl;
            return -1;
        }
        theApp.InitInstance();
        return theApp.Run();
    }

    April 03

    Code snippet Using WMI via C++/CLI

    #include "stdafx.h"
    #using < system.management.dll>
    using namespace System;
    using namespace System::Management; 
    int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Query processor count via CLI by Onega");
        ConnectionOptions^ options = gcnew ConnectionOptions();
        //options->Password = L"";
        //options->Username = L"";
        //options->Authority = "NTLMDomain:" ;
        options->Impersonation = ImpersonationLevel::Impersonate;
        ManagementScope^ scope = gcnew ManagementScope("\\\\localhost\\root\\CIMV2",options);
        SelectQuery^ processorquery = gcnew SelectQuery("SELECT * FROM WIN32_Processor ");
        ManagementObjectSearcher^ query = gcnew ManagementObjectSearcher(scope, processorquery);
        ManagementObjectCollection^ resultcollection = query->Get();
        ManagementObjectCollection::ManagementObjectEnumerator^ enu = resultcollection->GetEnumerator();
        int processor_count = 0;
        while(enu->MoveNext())
        {
            Console::WriteLine(enu->Current->ToString());
            processor_count++;
        }
        Console::WriteLine(L"Processor count:{0}", processor_count);
        return 0;
    }

    January 08

    VC++ 9.0 Express edition web setup

    It depends on proxy settings of IE, but does not understand pac (proxy automatic configuration file) settings.

    September 06

    LoadIcon() failed in Ctor of CDialog derived class

    Once I encountered an application that raise runtime error in Ctor of a CDialog derived class at the following line:
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    After revisiting the program, I found that there is a global variable of this dialog class, this seems to cause initialize sequence problem, then the fix is move above line from Ctor to OnInitDialog().