?? ch28.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 28 -- Future Explorations</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch27/ch27.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apa/apa.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 28 -</H1>
</CENTER>
<CENTER>
<H1>Future Explorations</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Creating Console Applications</A>
<UL>
<LI><A HREF="#Heading2">Creating a Console Executable</A>
<LI><A HREF="#Heading3">Writing an Object-Oriented Console Application</A>
<LI><A HREF="#Heading4">Scaffolding Discrete Algorithms</A>
</UL>
<LI><A HREF="#Heading5">Creating and Using a 32-Bit Dynamic Link Library</A>
<UL>
<LI><A HREF="#Heading6">Making a 32-Bit DLL</A>
<LI><A HREF="#Heading7">Using 32-Bit DLLs</A>
</UL>
<LI><A HREF="#Heading8">Sending Messages and Commands</A>
<LI><A HREF="#Heading9">Considering International Software Development Issues</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>There are a number of topics that have not been covered elsewhere in this book,
but that are well known to experienced Visual C++ programmers. They are best explored
after you have experience with Developer Studio, MFC, and C++ programming. This chapter
has just enough to show you how interesting these topics are, and to encourage you
to explore them yourself in the months and years to come.</P>
<P>
<H2><A NAME="Heading1"></A>Creating Console Applications</H2>
<P>A console application looks very much like a DOS application, though it runs in
a resizable window. It has a strictly character-based interface with cursor keys
rather than mouse movement. You use the Console API and character-based I/O functions
such as printf() and scanf() to interact with the user.</P>
<P>
<H3><A NAME="Heading2"></A>Creating a Console Executable</H3>
<P>A console application is executed from the DOS command line or by choosing Start,
Run and typing the full name (including the path) of the application. Console applications
are probably still among the easiest programs to create, and this version of the
compiler supports them directly.</P>
<P>Let's walk together through the few steps necessary to create a basic console
application, and then we'll explore some beneficial uses of creating these applications.
The first console application we'll create is a spin on the classic "Hello,
World!" that Kernighan and Ritchie (the creators of C++'s ancestor C) created
in the 1970s.</P>
<P>Open the Microsoft Developer Studio and follow these steps to create a console
application:</P>
<DL>
<DT></DT>
<DD><B>1. </B>In the Microsoft Developer Studio, select File, New.
<P>
<DT></DT>
<DD><B>2. </B>In the New dialog box, click the Projects tab to bring up the now familiar
New project dialog box. (If it isn't familiar, go back to Chapter 1, "Building
Your First Windows Application.")
<P>
<DT></DT>
<DD><B>3.</B> Name the project HelloWorld, set an appropriate folder for the project,
and choose Win32 Console Application from the list on the left.
<P>
<DT></DT>
<DD><B>4. </B>Click OK.
<P>
<DT></DT>
<DD><B>5. </B>AppWizard asks whether you want to create An Empty Project, A Simple
Application, A "Hello World" Application, or An Application that uses MFC.
Select An Empty Project so that you can create our slightly simpler HelloWorld by
yourself.
<P>
<DT></DT>
<DD><B>6. </B>Click Finish.
<P>
</DL>
<P>The project is created immediately but has no file added to it. You create source
and header files and add them to the project. This sample will all fit in one file.
Follow these steps:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Select File, New from the File menu and click the File tab.
<P>
<DT></DT>
<DD><B>2. </B>Leave the Add to Project box selected; the new file will be added to
the project.
<P>
<DT></DT>
<DD><B>3. </B>Choose C++ Source File from the box on the left.
<P>
<DT></DT>
<DD><B>4. </B>Enter <B>HelloWorld</B> as the filename--the extension .cpp will be
added automatically.
<P>
<DT></DT>
<DD><B>5. </B>The New dialog box should resemble Figure 28.1. Click OK.
<P>
</DL>
<P><A HREF="javascript:popUp('28fig01.gif')"><B>FIG. 28.1</B></A><B> </B><I>Create
a C++ source file for your console application.</I></P>
<P>A blank text file is created and named for you and added to the project, all in
one step. Add the code in Listing 28.1 to the new file.</P>
<P>
<H4>Listing 28.1  HelloWorld.cpp</H4>
<PRE>#include <iostream.h>
int main()
{
cout << "Hello from the console!"<< endl;
return 0;
</PRE>
<PRE>}
</PRE>
<P>Choose Build, Execute to compile, link, and execute the program. (A dialog will
ask you to confirm that you want to build the project before executing.) You should
see a DOS box appear that resembles Figure 28.2. The line Press any key to continue
is generated by the system and gives you a chance to read your output before the
DOS box disappears.</P>
<P>
<H3><A NAME="Heading3"></A>Writing an Object-Oriented Console Application</H3>
<P>The HelloWorld application is clearly C++ and would not compile in a C compiler,
which doesn't support stream-based I/O with cout, but it's not object oriented--there's
not an object in it. Replace the code in HelloWorld.cpp with the lines in Listing
28.2.</P>
<P><A HREF="javascript:popUp('28fig02.gif')"><B>FIG. 28.2</B></A><B> </B><I>Your
application appears to be a DOS program.</I></P>
<P>
<H4>Listing 28.2  HelloWorld.cpp--With Objects</H4>
<PRE>// HelloWorld.cpp
//
#include <iostream.h>
#include <afx.h>
class Hello
{
private:
CString message;
public:
Hello();
void display();
};
Hello::Hello()
{
message = "Hello from the console!";
}
void Hello::display()
{
cout << message << endl;
}
int main()
{
Hello hello;
hello.display();
return 0;
</PRE>
<PRE>}
</PRE>
<P>Now this is an object-oriented program, and what's more, it uses CString, an MFC
class. To do so, it must include <afx.h>. If you build the project now, you
will get linker error messages that refer to _beginthreadex and _endthreadex. By
default, console applications are single-threaded, but MFC is multithreaded. By including
afx.h and bringing in MFC, this application is making itself incompatible with the
single-threaded default. To fix this, choose Project Settings and click the C/C++
tab. From the drop-down box at the top of the dialog box, choose Code Generation.
In the drop-down list box labeled Use Run-Time Library, choose Debug Multithreaded.
(The completed dialog box is shown in Figure 28.3.) Click OK and rebuild the project.</P>
<P><A HREF="javascript:popUp('28fig03.gif')"><B>FIG. 28.3</B></A><B> </B><I>Make
your console application multithreaded so that it can use MFC.</I></P>
<P>The output of this object-oriented program is just like that of the preceding
program--this is just a sample. But you see that console applications can use MFC,
be built around objects, and be quite small. They must have a main() function, and
it is this function that is called by the operating system when you run the application.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Although this application is small, Visual C++ creates a lot
of overhead files. The Debug directory occupies about 7.8MB, of which about 1.3MB
is HelloWorld.exe. The rest is the MFC libraries--they aren't small. 
<HR>
</BLOCKQUOTE>
<H3><A NAME="Heading4"></A>Scaffolding Discrete Algorithms</H3>
<P>One important reason to build a console application these days is to <I>scaffold</I>
small code fragments or single objects. This refers to building a temporary framework
around the code you want to test. (Some developers call this a <I>test harness</I>.)
The simplest possible framework is a console application like the one you just built:
In fact, you'll build a scaffold later in this chapter.</P>
<P>To scaffold an object or function, you should do the following:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Create a new console application just for the scaffolding process.
<P>
<DT></DT>
<DD><B>2. </B>Add a main() function to the .CPP file you plan to scaffold.
<P>
<DT></DT>
<DD><B>3. </B>Include the header file for the object or function to be tested.
<P>
<DT></DT>
<DD><B>4. </B>Add code to main() that exercises the function or object in a variety
of test cases.
<P>
</DL>
<P>Having followed those steps, you can now test the code thoroughly, focusing only
on the performance characteristics and correctness of this small piece of your large
project. Scaffolding holds true to the canon of software development that states,
"Design in the large and program in the small."</P>
<P>By applying a scaffold to any algorithm, you are helping to ensure the accuracy
in the small. Remember there are additional benefits involved, too: By placing the
scaffold code directly into the module, you are clearly documenting that the code
has been tested and how to use it. You make it available for further testing, debugging,
or extending at a later date.</P>
<P>
<H2><A NAME="Heading5"></A>Creating and Using a 32-Bit Dynamic Link Library</H2>
<P>Dynamic link libraries (DLLs) are the backbone of the Windows 95 and Windows NT
operating systems. Windows 95 uses Kernel32.dll, User32.dll, and Gdi32.dll to perform
the vast majority of its work, and you can use them as well. The Visual C++ online
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -