FunctionX - Practical Learning Logo

Visual C++ Spoiled in .NET

 

Introduction

From the first release of Microsoft Visual Studio .NET, Visual C++ programmers appeared to be the most spoiled of the whole environment. While some folks at Microsoft decided to discontinue publishing classic Visual Basic, creating a lot of orphans, the Visual C++ compiler developers chose to use a different approach: continue the existing offerings and add more.

Five languages

What sets the new Visual C++ apart from the others in the .NET family is that this programming environment provides essentially at least 6 independent programming languages in one package. No other Visual-related object from the .NET family can make that claim.

When Microsoft decided to stop publishing classic Visual Basic, they changed the language completely to come up with Visual Basic .NET, needless to state that this created a lot of bitter programmers because this resulted in code to maintain and convert so much that many Visual Basic programmers are still considering continuing with classic Visual Basic on their own. On the other hand, the compiler developers who publish Visual C++ viewed the issue otherwise. They decided not only to continue supporting classic Visual C++, but they also added .Net support C++ and brought high improvements to ATL. As a result, Visual C++ programmers got, at a minimum, 6 independent programming languages for the price of one:

  • C/C++: You can continue writing C/C++ ANSI compliant applications using only the Standards without worrying about the internal rules of the Microsoft Windows operating system. This means that you can create applications that syntactically would fully run on Linux/Unix and other operating systems.

    Here is what a (simple) C++ application looks like:
     
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "C++ is fun!!!\n\n";
    	return 0;
    }
  • MFC: This has always been the primary library used by Visual C++ programmers. Continuing to publish and support this library is the best of the news for Visual C++ programmers because this is what determines its most difference with Visual Basic programmers. Microsoft has decided, somewhat definitely, to stop publishing classic Visual Basic. Many of us believe that Visual Basic .NET is not (classic) Visual Basic. The changes are numerous and "visually" detectable (and probably detestable). Instead, those in charge of Visual C++ chose to not only publish a new version for the library (MFC 7) but also highly improved its documentation. Although MFC is not the easiest library to use (honestly no commercial library is), the number of its articles on the MSDN web site is ever growing.

    Here is what a simple MFC application looks like:
     
    #include <afxwin.h>
    
    class CMainFrame : public CFrameWnd
    {
    public:
    	CMainFrame()
    	{
    		Create(NULL, "Simple Windows Application");
    	}
    };
    
    class CExerciseApplication: public CWinApp
    {
    public:
    	BOOL InitInstance()
    	{
    		m_pMainWnd = new CMainFrame;
    		m_pMainWnd->ShowWindow(SW_SHOW);
    		m_pMainWnd->UpdateWindow();
    
    		return TRUE;
    	}
    };
    
    CExerciseApplication theApp;
  • Win32: This is used to create graphical applications for the Microsoft Windows family of operating systems using C or C++. You don't need to know anything about MFC. This makes this type of application independent from all the others as Win32 applications are not necessarily consoles as those created in the C/C++ category above. Because these applications don't abide by MFC rules, you don't have to know the MFC library. Win32 is highly documented also on the MSDN web site.

    Here is what a Win32 application looks like:
     
    //---------------------------------------------------------------------------
    #include <windows.h>
    //---------------------------------------------------------------------------
    const char ClsName[] = "WndMsg";
    const char WindowCaption[] = "Windows Fundamentals";
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        MSG         Msg;
    	HWND		hWnd;
        WNDCLASSEX  WndClsEx;
    
        WndClsEx.cbSize        = sizeof(WNDCLASSEX);
        WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
        WndClsEx.lpfnWndProc   = WndProc;
        WndClsEx.cbClsExtra    = NULL;
        WndClsEx.cbWndExtra    = NULL;
        WndClsEx.hInstance     = hInstance;
        WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndClsEx.lpszMenuName  = NULL;
        WndClsEx.lpszClassName = ClsName;
        WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        RegisterClassEx(&WndClsEx);
    
        hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, ClsName, WindowCaption,
                                                  WS_OVERLAPPEDWINDOW,
    	                              CW_USEDEFAULT, CW_USEDEFAULT,
    		              CW_USEDEFAULT, CW_USEDEFAULT,
                                                  NULL, NULL, hInstance, NULL);
    
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    
        return Msg.wParam;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            break;
        default:
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }
    //---------------------------------------------------------------------------
  • Managed C++: This is the new addition to the C/C++ family of languages. It is not strictly C++, it is a new language that follows C++ rules but adds some of its own concepts. The good news is that it is as close to C++ as a finger nail is to a finger, but they are distinct. Therefore, as you would do with C++, you can write completely functional console applications, using its own compiler.

    Here is what a simple Managed C++ applications looks like:
     
    #using <mscorlib.dll>
    using namespace System;
    
    int main()
    {
    	Console::WriteLine("Managed C++ is wonderful!!!\n");
    	return 0;
    }
  • Visual C++ .NET: At this time, we can just call it Visual C++ .NET. We could also call it Managed Visual C++ or anything like that. This is not classic Visual C++ (we could also call the other, Visual C++ With MFC). Because of the most definite differences with classic previous programming environments and the high degree of work involved, this is probably what made the Visual Basic compiler developers say, "the hell with it, this would be too much work". So they decided to discontinue Visual Basic. The Visual C++ compiler developers decided to ship both Visual C++ and Visual C++ .NET. Essentially, they decided to ship both the MFC library and the .Net framework. From the information we have so far, they will continue publishing and supporting the MFC library. On the other hand, for the first time since Visual C++ 2.1, there is true improvement of rapid application development (RAD) in Visual C++. For the first time, and using Visual C++ .NET 2003, you can "visually" pick up a control from the Controls toolbox, position it on a form or dialog box, use an impressive Properties window that gives you access to virtually all properties of the control, and change their values easily. In MFC, not only were some properties of the controls changeable only programmatically but also you had to use CWnd methods that were more closely related to Win32 than implementing MFC's own interpretation or simplification. With Visual C++ .NET, most properties of the controls are changed by value instead of methods. This highly simplifies programming and makes your job faster.

    Here is what a simple Visual C++ .NET program looks like:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Basic .NET Windows Application";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  • ATL: This allows you to create an Active Template Library project using COM objects. This library also has gone various improvements since 3.0. It has also received a lot of documentation updates

So, although we who love C++ are not using the easiest language in the world, we believe that complete changes are not likely to happen in our world all of a sudden. This is also because the C++ community is very large and strong. If you were thinking of switching to Visual Basic .Net because it appears to be easy, you may; but you would be abandoning the strongest and most aggressive language on the market. C++ is simply the best and its documentation and only growing.

 

Copyright © 2003-2015, FunctionX, Inc.