📄 ch28.htm
字号:
<pre><font color="#008000"> 80 DeleteMenu</font></pre>
<pre><font color="#008000"> 122 GetSystemMenu</font></pre>
<pre><font color="#008000"> 1A1 PeekMessageA</font></pre>
<pre><font color="#008000"> 100 GetLastActivePopup</font></pre>
<pre><font color="#008000"> 2 AdjustWindowRectEx</font></pre>
<pre><font color="#008000"> 4 AppendMenuA</font></pre>
<pre><font color="#008000"> 51 CreatePopupMenu</font></pre>
<pre><font color="#008000"> C1 EnumWindows</font></pre>
<pre><font color="#008000"> B1 EnumChildWindows</font></pre>
<pre><font color="#008000"> 188 MessageBoxA</font></pre>
<pre><font color="#008000"> 206 SetWindowPlacement</font></pre>
<pre><font color="#008000"> A BringWindowToTop</font></pre>
<pre><font color="#008000"> 197 OffsetRect</font></pre>
<pre><font color="#008000"> 132 GetWindowPlacement</font></pre>
<pre><font color="#008000"> 13A GetWindowWord</font></pre>
<pre><font color="#008000"> 15E IsWindowUnicode</font></pre>
<pre><font color="#008000"> 243 WinHelpA</font></pre>
<pre><font color="#008000"> DF GetClipboardFormatNameA</font></pre>
<pre><font color="#008000"> 12F GetWindowDC</font></pre>
<pre><font color="#008000"> 160 IsZoomed</font></pre>
<pre><font color="#008000"> 1B9 ReleaseDC</font></pre>
<pre><font color="#008000"> D0 GetCapture</font></pre>
<pre><font color="#008000"> 33 ClientToScreen</font></pre>
<pre><font color="#008000"> 246 WindowFromPoint</font></pre>
<pre><font color="#008000"> 1D8 SetCursor</font></pre>
<pre><font color="#008000"> 237 UpdateWindow</font></pre>
<pre><font color="#008000"> 12D GetWindow</font></pre>
<pre><font color="#008000"> 15F IsWindowVisible</font></pre>
<pre><font color="#008000"> E8 GetDesktopWindow</font></pre>
<pre><font color="#008000"> 204 SetWindowLongA</font></pre>
<pre><font color="#008000"> 1B8 ReleaseCapture</font></pre>
<pre><font color="#008000"> 1D0 SetCapture</font></pre>
<pre><font color="#008000"> 1A8 PtInRect</font></pre>
<pre><font color="#008000"> F0 GetFocus</font></pre>
<pre><font color="#008000"> 120 GetSysColor</font></pre>
<pre><font color="#008000"> CB FrameRect</font></pre>
<pre><font color="#008000"> 9B DrawFocusRect</font></pre>
<pre><font color="#008000"> F9 GetKeyState</font></pre>
<pre><font color="#008000"> 187 MessageBeep</font></pre>
<pre><font color="#008000"> 22C TranslateMessage</font></pre>
<pre><font color="#008000"> 8C DispatchMessageA</font></pre>
<pre><font color="#008000"> 159 IsIconic</font></pre>
<pre><font color="#008000"> 1 AdjustWindowRect</font></pre>
<pre><font color="#008000"> 133 GetWindowRect</font></pre>
<pre><font color="#008000"> 1BF ScreenToClient</font></pre>
<pre><font color="#008000"> C2 EqualRect</font></pre>
<pre><font color="#008000"> 148 InvalidateRect</font></pre>
<pre><font color="#008000"> DC GetClientRect</font></pre>
<pre><font color="#008000"> 123 GetSystemMetrics</font></pre>
<pre><font color="#008000"> 15C IsWindow</font></pre>
<pre><font color="#008000"> D9 GetClassNameA</font></pre>
<pre><font color="#008000"> 130 GetWindowLongA</font></pre>
<pre><font color="#008000"> D3 GetClassInfoA</font></pre>
<pre><font color="#008000"> 1A3 PostMessageA</font></pre>
<pre><font color="#008000"> 20E SetWindowsHookExA</font></pre>
<pre><font color="#008000"> 22E UnhookWindowsHookEx</font></pre>
<pre><font color="#008000"> 7D DefWindowProcA</font></pre>
<pre><font color="#008000"> 139 GetWindowThreadProcessId</font></pre>
<pre><font color="#008000"> 1AF RegisterClipboardFormatA</font></pre>
<pre><font color="#008000"> DB GetClassWord</font></pre>
<pre><font color="#008000"> 86 DestroyWindow</font></pre>
<pre><font color="#008000"> 1AB RegisterClassA</font></pre>
<pre><font color="#008000"> 52 CreateWindowExA</font></pre>
<pre><font color="#008000"> AB EnableWindow</font></pre>
<pre><font color="#008000"> 1C6 SendMessageA</font></pre>
<pre><font color="#008000"> 115 GetParent</font></pre>
<pre><font color="#008000"> 1E2 SetForegroundWindow</font></pre>
<pre><font color="#008000"> 232 UnpackDDElParam</font></pre>
<pre><font color="#008000"> 216 ShowWindow</font></pre>
<pre><font color="#008000">Summary</font></pre>
<pre><font color="#008000"> 10000 .data</font></pre>
<pre><font color="#008000"> 3000 .idata</font></pre>
<pre><font color="#008000"> 8000 .rdata</font></pre>
<pre><font color="#008000"> 8000 .reloc</font></pre>
<pre><font color="#008000"> F000 .rsrc</font></pre>
<pre><font color="#008000"> 36000 .text</font></pre>
<P>As you can see, the utility program Spy++ uses the User32.Dll extensively.</P>
<P>You can call the Windows DLLs in any of your programs and more importantly, you can write DLLs of your own.</P>
<P><B>Making a 32-bit DLL</B></P>
<P>There are two kinds of DLLs in Visual C++: those that use MFC and those that do not. Each kind of DLL has its own AppWizard, as you will see shortly.</P>
<P>If you gather three or four functions into a DLL, your DLL <I>exports</I> those functions for other programs to use. Quite often a DLL will also <I>import</I> functions from other DLLs to get its work done.</P>
<P><B>Importing and Exporting Functions</B></P>
<P>To designate a symbol as exportable, use the following syntax:</P>
<pre><font color="#008000">__declspec(dllexport) data_type int var_name; // for variables</font></pre>
<P>or</P>
<pre><font color="#008000">__declspec(ddlexport) return_type func_name( </font><I><font color="#008000">[argument_list ] </font></I><font color="#008000">); // </font><font color="#008000">for functions</font></pre>
<P>Importing functions is almost identical: simply replace the keyword tokens, __declspec(dllexport) with __declspec(dllimport). Using an actual function and variable to demonstrate the syntax this time:</P>
<pre><font color="#008000">__declspec(dllimport) int referenceCount;</font></pre>
<pre><font color="#008000">__declspec(dllimport) void DiskFree( lpStr Drivepath );</font></pre>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>Two underscores precede the keyword __declspec.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>By convention Microsoft uses a header file and a preprocessor macro to make the inclusion of DLL declarations much simpler. The technique simply requires that you make a preprocessor token using a unique token—the header file name works easily,
and requires very little in the way of memorization—and define a macro which will replace the token with the correct import or export statement. Thus, assuming a header file named DISKFREE.H, the preprocessor macro in the header file would be as
follows:</P>
<P><I>Listing 28.4—Diskfree.h</I></P>
<pre><font color="#008000">// DISKFREE.H - Contains a simpler function for returning the amount </font><font color="#008000">of free disk space.</font></pre>
<pre><font color="#008000">// Copyright (c) 1996. All Rights Reserved.</font></pre>
<pre><font color="#008000">// By Paul Kimmel. Okemos, MI USA</font></pre>
<pre><font color="#008000">#ifndef __DISKFREE_H</font></pre>
<pre><font color="#008000">#define __DISKFREE_H</font></pre>
<pre><font color="#008000">#ifndef __DISKFREE__</font></pre>
<pre><font color="#008000">#define DISKFREELIB __declspec(dllimport)</font></pre>
<pre><font color="#008000">#else</font></pre>
<pre><font color="#008000">#define DISKFREELIB __declspec(dllexport)</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000">// Use the macro to control an import or export declaration.</font></pre>
<pre><font color="#008000">DISKFREELIB unsigned long DiskFree( unsigned int drive ); // (e.g. o </font><font color="#008000">= A:, 1 = B:, 2 = C:</font></pre>
<pre><font color="#008000">#endif</font></pre>
<P>Simply by including the header file you can let the preprocessor decide whether DiskFree is being imported or exported. Now you can share the header file for the DLL developer and the DLL user, and that means less maintenance headaches.</P>
<P><B>Creating the DiskFree DLL</B></P>
<P>The DiskFree utility provides a simple way to determine the amount of free disk space for any given drive. The underlying functionality is the <font color="#008000">GetDiskFreeSpace()</font> function found in Kernel32.Dll. </P>
<P>To create a non-MFC DLL, choose <U>F</U>ile, <U>N</U>ew, click the Projects tab, select Win32 DLL from the list on the left, and enter DiskFree for the project name, as shown in Figure 28.4. Click OK and the project is created with no files in it.</P>
<A HREF="CCfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch28/CCfigs04.gif"><b>Fig. 28.4</b></A>
<P><I>Creating a non-MFC DLL project is a one-step process.</I></P>
<P>Add a C++ header file called DiskFree.h and type in the code from Listing 28.5. Add a C++ source file called DiskFree.cpp and type in the code from Listing 28.6 (or copy these files from the CD.)</P>
<P><I>Listing 28.</I><I>5</I><I>—DiskFree.h</I></P>
<pre><font color="#008000">#ifndef __DISKFREE_H</font></pre>
<pre><font color="#008000">#define __DISKFREE_H</font></pre>
<pre><font color="#008000">#ifndef __DISKFREE__</font></pre>
<pre><font color="#008000">#define __DISKFREELIB__ __declspec(dllimport) </font></pre>
<pre><font color="#008000">#else</font></pre>
<pre><font color="#008000">#define __DISKFREELIB__ __declspec(dllexport)</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000">// Returns the amount of free space on drive number (e.g. 0 = A:, 1= </font><font color="#008000">B:, 2 = c:)</font></pre>
<pre><font color="#008000">__DISKFREELIB__ unsigned long DiskFree( unsigned int drive );</font></pre>
<pre><font color="#008000">#endif</font></pre>
<P><I>Listing 28.</I><I>6</I><I>—DiskFree.Cpp</I></P>
<pre><font color="#008000">#include <afx.h></font></pre>
<pre><font color="#008000">#include <winbase.h> // Contains the kernel32 GetDiskFreeSpace </font><font color="#008000">declaration.</font></pre>
<pre><font color="#008000">#define __DISKFREE__ // Define the token before the inclusion of </font><font color="#008000">the library</font></pre>
<pre><font color="#008000">#include "diskfree.h"</font></pre>
<pre><font color="#008000">// Returns the amount of free space on drive number (e.g. 0 = A:, 1= </font><font color="#008000">B:, 2 = c:)</font></pre>
<pre><font color="#008000">__DISKFREELIB__ unsigned long DiskFree( unsigned int drive )</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> unsigned long bytesPerSector, sectorsPerCluster,</font></pre>
<pre><font color="#008000"> freeClusters, totalClusters;</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -