Notes On Using Visual C++

Version 0.5
Copyright (C) 2008 by Zack Smith
All rights reserved.

Is there a free version of Visual C++?

Yes, there is a free version of Visual C++, and indeed all of the other Microsoft languages. They are packaged together in the Visual Studio Express package that you can download from Microsoft's website.

Visual C++ Express lacks a resource editor and does not include MFC, which are vital for creating even basic applications. However Express can be used for creating OpenGL applications and SDL-based programs. Note that you can do widgets using OpenGL (albeit basic ones) using the GLUI library.

Static libraries

To link a library as static, right click on the project and go into Properties, click on Configuration Properties, General, and set Configuration Type to Static Library (.lib).

Statically linked executable

Doing a static link is a good idea since it protects against missing DLLs and it avoids the hassles of manifest files. To link an executable statically, you will need to ensure that all components of it are also statically linked, including libraries. And, for all projects in your solution, you need to go into Project Properties, Configuration Properties, C/C++, Code Generation, and set Runtime Library to /MT. For good measure, go into Linker, Command Line, and add the command line option /NODEFAULTLIB:library.

Setting include and link paths

Select the Tools pulldown menu, then Options, then Projects and Solutions, then VC++ Directories, then under "Show Directories For:" select either Library files or Include files.

How to create a simple dialog box?

This is all you need:
MessageBoxA(NULL, "message", "window title", MB_OK);

How to create a simple file-open dialog box?

Like so:
OPENFILENAME ofn;
short szFile[260];
ZeroMemory (&ofn, sizeof(ofn));
szFile[0]=0;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
ofn.hwndOwner = NULL; // hwnd;
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = (LPWSTR) szFile;
ofn.lpstrFileTitle = NULL;
ofn.lpstrFilter = _T("All\0*.*\0Text\0*.txt\0");
ofn.lpstrInitialDir = NULL;
ofn.lpstrInitialDir = _T("");
ofn.nFilterIndex = 1;
ofn.nMaxFile = sizeof(szFile);
ofn.nMaxFileTitle = 0;

if (TRUE == GetOpenFileName(&ofn)) {
    // Use szFile here.
}			

How to create a color-wheel popup to let users choose a color?

Thus:
unsigned long chooser () 
{
    CHOOSECOLOR cc;
    static COLORREF custom[16];

    memset (custom, 0, sizeof(COLORREF)*16);
    custom[0] = 0xff0000;   

    memset (&cc, 0, sizeof(cc));
    cc.lStructSize = sizeof(cc);
    cc.lpCustColors = (LPDWORD) custom;
    cc.rgbResult = current_color;
    cc.Flags = CC_FULLOPEN | CC_RGBINIT;

    if (ChooseColor (&cc)) 
    	return cc.rgbResult;
    else
    	return 0xff000000;
}

What is a manifest?

It's an XML file that describes what versions of DLLs that application uses, including C runtime libraries. Like it or not, you have to include it with your program, either inside or separate. The default behavior of Visual C++ is to include it inside the .exe file.

Note, when you make a Release to give to the general public, you will need to include the version of the C runtime libraries that are provided with Visual C++, e.g. Microsoft.VC90.CRT. There are four files total and they are either these or something similar that came with your VC++.

  • c:/progra~1/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/Microsoft.VC90.CRT.manifest
  • c:/progra~1/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/msvcr90.dll
  • c:/progra~1/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/msvcp90.dll
  • c:/progra~1/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/msvcm90.dll

If you fail to provide the needed manifest file and DLLs, your program will not start but will produce an error:

The application configuration is incorrect. Reinstalling this application may fix this problem. Upon inspection of the Event Log (run eventvwr.exe) you will find a SxS or side-by-side event error.

Links