Introduction to Visual C++
Overview
The following are a list of ideas and features of Visual C++.
- It is a development environment rather than a compiler. There is no reason to ever leave this environment while you are working on a program. The following are some of the services it provides in addition to being a C++ compiler.
- It is an editor.
- It provides an excellent debugger.
- You can run your programs from it.
- You can search files and directories.
- You can buy add-ons, like bounds checker.
- It is a C compiler.
- There is on line help
- If you type a class object followed by "." or "->" it will tell you
all the possible completions. (IntelliSense) If you type a function name, it will tell you the prototype.
If you start typing a variable name and type
^<space> the name will be completed.
- It had a built-in profiler. It is
not available in our edition.
- Visual Studio supports other languages.
These are VB, C#, and J#.
- It is the standard PC software development system in industry. For
virtually all companies, there is no second choice for windows development.
(At least in this country. Borland products are used in
Europe.)
- A complete install, including documentation is
almost 3 gig.
- When you want to work on a programming task, you create a
solution. Solutions are subdivided into project. A
project may be a library or the code in which the main resides. In functionality, this is a
approximately equivalent to the UNIX make file. It is
easier to use.
-
Builds are the process of creating executables or
libraries from source code files.
- For
us, we will be creating console applications.
There are other choices.
- This is a Microsoft product.
- The help pages are tied to internet explorer
(IE). You need a recent version of IE if you are to read the
help pages. They are in HTML format. This implies that they do not copy well into a text document.
Word is cool with them.
- At the college we have visual studio visual studio .net
2005. This is also referred to as visual studio 8.0.
If you have an older version at home, you will still be able to do
the assignments. The only problem is that the project files
are different. So moving stuff from home to college and
back can be an issue.
- We are not developing
"true" .NET application. I will say a few words about .NET
in class.
General Remarks About Windows Operating Systems
(Skip for Software Design and data structures courses.)
The following are some general remarks about the Windows operating systems.
- DOS, Windows 3.1, 95, 98, 98 second edition, Millennium, NT, 2000,
XP and CE are the list of the windows operating systems. Windows 95, 98, 98 second edition, and
Millennium are collectively referred to as Windows 9x. Windows CE is the MS operating system designed for small systems like
personal organizer.
- Win32 is the API use to access features of these operating systems. As a rule the API is upwards compatible. That is a program that will run on an earlier system will run on a more recent one. This creates a situation where there are a lot of obsolete functions in the API. There are exceptions to upwards
compatible!
- Windows 9x and CE does not have any security features.
- Windows OSs only run on Intel boxes. There was a time when NT ran on DEC Alpha machines.
- Windows 95 does not support Unicode. What is Unicode? These are 16 bit characters which may be used to support languages with larger alphabets.
Pure .NET only uses 16 bit characters.
- A number of Win32 functions on Windows 9x have restricted implementations. Some Win32 functions are useless on Windows 9x. For example, if you attempt to specify a security option on an API function on Windows 95, you will get an error return.
- The API is C oriented. There is a C++ API called MFC. (Microsoft Foundation Class.) This is used to created windows programs.
-
The .NET framework brings use another class library.
It has 3500+ classes. More of this will be presented in class.
-
System resources are organized as kernel objects. What are system resources? Files, threads, processes, critical sections, etc. are system resources. Kernel objects are defined as basic
elements of the operating system. System resources are identified and referenced by handles. What is a handle? An address of name. Under the surface it is currently a long integer.
Analogous to handles are the files pointers used in the C file system or the file
descriptors used in UNIX or the I/O objects used by C++.
- The only way to manipulate Kernel objects is through the Win32 API. There are no "back doors". In Windows 3.11 for
example, one could access screen memory directly.
Technically, .NET and MFC provide class libraries to manipulate
Kernel objects. But, these are implemented using the WIN32
API.
- In windows XP/NT/2000 kernel objects have security attributes. For example, a memory mapping can be set up so that some users can read and write to it. Others can read only. And still others can have no access at all.
- Win32 is a "rich and flexible" interface. There are many different ways to perform the same task. Question whether this is good?
There is something very pleasing about the simplicity of the UNIX
API.
- Thread is the basic unit of execution on Win32. This contrast with many systems, including UNIX, that see the process as the basic unit of execution. Remember that WIN32 is s relatively recent API.
- Function names in Win32 have long descriptive names. Example: FindFirstFile. This is good for the sake of readability.
- The API uses predefined data types. These are all uppercase and attempt to be very descriptive. Example BOOL, HANDLE,
DWORD, LPSECURITY_ATTRIBUTES. They can also be a pain in the neck to type.
- The predefined data types do not use pointer notation. This is more confusing to me, but that is how they do it. So, instead of writing DWORD *, they write LPDWORD.
- Variables in the prototypes use Hungarian notation. Thus, they would name a pointer to a zero ('\0') terminated string that holds the date as: lpszDate. I don't like this, but it is not my choice. I will not use this notation in my code and I will not expect you to
use it.
- The include files will be found in the directory: \ProgramFiles\Microsoft Visual Studio\VC98\include. Discuss how to locate.
- Most important include file is windows.h. This will pull in all the other needed include files for using the WIN32 API. You still have to specify the C, and C++ include files that you want to use.
- Win32 was designed to be backwards compatible with the Windows 3.1 API. What does this mean? There are a lot of residue functions that should not be used. (OpenFile is such a function.) These are 16-bit functions. Also names like LPVOID occur because of compatibility issues. In the help pages you will be told when a function is obsolete. When you see this, do not use the function for new code.
Creating and Debugging a Visual C++ Program
The following are the steps that must be followed in creating a C++ program using Visual C++.
Writing Programs Using Visual C++
It is the goal of this section to get you ready to write programs using visual C++. The following is a list of steps that should get you started in writing programs using visual C++.
I will describe Visual Studio 6.0 and Visual Studio .NET.
Visual Studio 6.0
( I will not talk about this in any class.)
- Create a directory on your zip or hard drive to hold your projects. Most people call this directory "projects". You can do this using windows explorer or "my computer".
Since zip drives are much slower than hard drives, you will use
the zip drive to make a copy of your project.
- Start the visual C++ environment. Click on: <Start>-<Programs><Microsoft-visual studio 6.0>-<Microsoft Visual C++ 6.0>
- Once visual C++ has come up, you can create a new project. Each project will be in its own directory. Click on: <file> - <new> - <Win32 Console Application>. Then enter the name of the directory that will hold your project and the project name in the appropriate places. The project name will become a subdirectory in the directory that you specified.
The temp director is a good place to put you project. When
you are done for the day, you can move the project to your zip
drive.
- You will then be prompted for the kind of console application that you wish to create. Choose an empty project. Click <finish>. Then reassure Microsoft that you know what you are doing on the next prompt. You can choose something simpler than an empty project, but you need a project for the more complex stuff you will do in the future.
- Create a source file. This will be used to hold your program. This is done by clicking: <file>-<new>-<C++ Source File> and then type in the file name. For C++ source files, .cpp is the best choice the extension. This is currently the most universally used.
- Now you are ready to type your program. You can play with the options later, but at least there is an auto indent feature.
- To compile and link your program, click on: <build>-<build project.exe> Note: pressing f7 does the same job.
- Error messages, warnings, and information will appear in the bottom window. Press f4 to view each error and the window above will have the cursor at the error. Correct errors and go to step 7.
- To run a program, click on <build>-<execute project.exe>. Pressing cntrl-f5 works too. You will see the console window appear and the output of the program on the screen. If the program needs command line arguments, they may be set by <project>-<settings>-<debug>. In this window you will see a place to type the command line arguments.
- If this is going to be a multithreaded application, must indicate so by <project>-<settings>-<C/C++>-<Category code generation>-<Use Run-time Library debug multithreaded>. If you don't do this, the wrong libraries will be used.
To debug a successfully compiled program do the following:
- The default compilation sets up for debugging. If you want optimization, you do it with the appropriate <project>-<settings>-<C/C++> option.
- Then set a break points) where you want execution to stop by putting the cursor at this point and pressing the hand icon or f9.
- Start the debugger by pressing f5.
- f10 performs single steps through instructions. f11 steps into functions.
- Put cursor on any variable whose value you want to see.
Visual Studio .NET
- Create a directory on your flash card, or hard drive to hold your projects. Most people call this directory "projects". You can do this using windows explorer or "my computer".
Since zip drives and flash cards are much slower than hard drives, you
should use these media only to make copies of your lab. You
will waste a lot of time to compile your programs on these drives.
Of course, by using them, you will not leave your latest version on
a college computer.
- Start the visual C++ environment. Click on: <Start>-<programs>-<Microsoft-Visual
Studio .NET>-<Microsoft-Visual Studio .NET>
- Once .NET comes up, select New Project. (At
the college this is under the file menu selection and then
under new.) If
you are working on a project, select its name from the file-open
menus selection.
- If you are creating a new project you must do the
following:
- In the New Project dialog box, select
the following:
- Under Project types: select Win32.
- Under Templates: select Win32
Console Application.
- Use Browse.. to pick the directory in
which you would like your Win32 project.
- In the Name: text box, type the name
of your project.
- Press the OK button.
- A dialog box: Win32 Application Wizard
will then come up. Click on Application Settings
and then select Empty project. Then click on the
Finish button.
- You should see the new project come up on the
screen. There will be folders for header files (.h files)
and for source files. (.cpp files)
- Create a source file. This is done by
highlighting the project name and pressing <project>-<Add New Item>.
Select code in the dialog box under the Categories:
text box. In the text box labeled by name: type the
name of the source file. In the Templates: window comes
up select "C++ file" for a .cpp file and "Header file" for a .h
file. Enter the name for
the file (no extension necessary) and the file will be added to
your project. Whatever name you enter will be ready
for editing.
- If you want to edit an existing file, double
clock on the source file name in the Solution Explorer window
and the file will be ready to edit. Note
that there will be tabs on the top of the edit window for files that
you have edited. you may select a file to edit from these.
- To compile and link your program, click on: <build>-<build
solution> Note: ctr-shift-b will do the same job.
- Error messages, warnings, and information will appear in the bottom window.
Double on each error and the window above will display the code
that contains the error. The cursor will be positioned at the error.
After you correct the error, repeat step 7.
- To run a program, click on <debug>-<start
without debugging>. Pressing cntrl-f5 works too. You will see the console window appear and the output of the program on the screen. If the program needs command line arguments, they may be set by
right clicking on your project and selecting Properties. In this window
select Configuration Properties - Debugging. In the
window on your right, you will see a place to type the command line arguments.
To debug a successfully compiled program do the following:
- Set a break points) where you want execution to stop by putting the cursor at this point
and left clicking in the margin or pressing f9.
- Start the debugger by pressing f5.
- f10 performs single steps through instructions. f11 steps into functions.
We will talk more about this in class.
- Put cursor on any variable whose value you want to see.
For structures and objects, you must right click the variable and
select quickwatch. More in class.
Discuss the help options. Press f1 while the cursor is on a function and you will get help on it. You can also search for key words or on an index.
They also have help in terms of function completion.
FTP (Not relevant for data structures class.)
Discuss ftp. Mention that there are multiple types of ftp clients. Make sure that everyone knows the difference between an ASCII and a binary transfer. The following instructions are instructions to upload a file to your
phobos account using ftp using to the most simple client interface. This interface is available on almost all operating systems.
- Get an ms-dos prompt window.
- Using the cd command, get in the directory containing the files to be uploaded. It should be a directory with the same name as your lab.
- Type "ftp phobos.ramapo.edu"
- Sign into your account in the usual way. Make sure that you use
your account name and password to sign in. You will be in your home directory.
- Type status to make sure that you are doing an ASCII transfer. If not, type
ASCII to get into an ASCII mode.
- Use cd to move to the directory from which you will upload.
- Use mput with wild cards (The "*" character) to upload your data.
- You are finished. Type "quit" to exit ftp. Note: if you are logged on for a while without activity, ftp will log you off.
Note: The college has a much better ftp utility.
You can down load it to your system.