|
程式21-4 STRPROG
STRPROG.C
- /*----------------------------------------------------------------------------
- STRPROG.C - Program using STRLIB dynamic-link library
- (c) Charles Petzold, 1998
- -----------------------------------------------------------------------------*/
- #include <windows.h>
- #include "strlib.h"
- #include "resource.h"
- typedef struct
- {
- HDC hdc ;
- int xText ;
- int yText ;
- int xStart ;
- int yStart ;
- int xIncr ;
- int yIncr ;
- int xMax ;
- int yMax ;
- }
- CBPARAM ;
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
- TCHAR szAppName [] = TEXT ("StrProg") ;
- TCHAR szString [MAX_LENGTH + 1] ;
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
- PSTR szCmdLine, int iCmdShow)
- {
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
-
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbCl***tra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
- wndclass.lpszMenuName = szAppName ;
- wndclass.lpszClassName = szAppName ;
-
- if (!RegisterClass (&wndclass))
- {
- MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
- szAppName, MB_ICONERROR) ;
- return 0 ;
- }
- hwnd = CreateWindow ( szAppName, TEXT ("DLL Demonstration Program"),
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL) ;
-
- ShowWindow (hwnd, iCmdShow) ;
- UpdateWindow (hwnd) ;
-
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- return msg.wParam ;
- }
- BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- SendDlgItemMessage (hDlg, IDC_STRING, EM_LIMITTEXT, MAX_LENGTH, 0) ;
- return TRUE ;
-
- case WM_COMMAND:
- switch (wParam)
- {
- case IDOK:
- GetDlgItemText (hDlg, IDC_STRING, szString, MAX_LENGTH) ;
- EndDialog (hDlg, TRUE) ;
- return TRUE ;
-
- case IDCANCEL:
- EndDialog (hDlg, FALSE) ;
- return TRUE ;
- }
- }
- return FALSE ;
- }
- BOOL CALLBACK GetStrCallBack (PTSTR pString, CBPARAM * pcbp)
- {
- TextOut ( pcbp->hdc, pcbp->xText, pcbp->yText,
- pString, lstrlen (pString)) ;
-
- if ((pcbp->yText += pcbp->yIncr) > pcbp->yMax)
- {
- pcbp->yText = pcbp->yStart ;
- if ((pcbp->xText += pcbp->xIncr) > pcbp->xMax)
- return FALSE ;
- }
- return TRUE ;
- }
- LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HINSTANCE hInst ;
- static int cxChar, cyChar, cxClient, cyClient ;
- static UINT iDataChangeMsg ;
- CBPARAM cbparam ;
- HDC hdc ;
- PAINTSTRUCT ps ;
- TEXTMETRIC tm ;
-
- switch (message)
- {
- case WM_CREATE:
- hInst = ((LPCREATESTRUCT) lParam)->hInstance ;
- hdc = GetDC (hwnd) ;
- GetTextMetrics (hdc, &tm) ;
- cxChar = (int) tm.tmAveCharWidth ;
- cyChar = (int) (tm.tmHeight + tm.tmExternalLeading) ;
- ReleaseDC (hwnd, hdc) ;
- // Register message for notifying instances of data changes
- iDataChangeMsg = RegisterWindowMessage (TEXT ("StrProgDataChange")) ;
- return 0 ;
- case WM_COMMAND:
- switch (wParam)
- {
- case IDM_ENTER:
- if (DialogBox (hInst, TEXT ("EnterDlg"), hwnd, &DlgProc))
- {
- if (AddString (szString))
- PostMessage (HWND_BROADCAST, iDataChangeMsg, 0, 0) ;
- else
- MessageBeep (0) ;
- }
- break ;
-
- case IDM_DELETE:
- if (DialogBox (hInst, TEXT ("DeleteDlg"), hwnd, &DlgProc))
- {
- if (DeleteString (szString))
- PostMessage (HWND_BROADCAST, iDataChangeMsg, 0, 0) ;
- else
- MessageBeep (0) ;
- }
- break ;
- }
- return 0 ;
-
- case WM_SIZE:
- cxClient = (int) LOWORD (lParam) ;
- cyClient = (int) HIWORD (lParam) ;
- return 0 ;
- case WM_PAINT:
- hdc = BeginPaint (hwnd, &ps) ;
-
- cbparam.hdc = hdc ;
- cbparam.xText = cbparam.xStart = cxChar ;
- cbparam.yText = cbparam.yStart = cyChar ;
- cbparam.xIncr = cxChar * MAX_LENGTH ;
- cbparam.yIncr = cyChar ;
- cbparam.xMax = cbparam.xIncr * (1 + cxClient / cbparam.xIncr) ;
- cbparam.yMax = cyChar * (cyClient / cyChar - 1) ;
-
- GetStrings ((GETSTRCB) GetStrCallBack, (PVOID) &cbparam) ;
-
- EndPaint (hwnd, &ps) ;
- return 0 ;
-
- case WM_DESTROY:
- PostQuitMessage (0) ;
- return 0 ;
- default:
- if (message == iDataChangeMsg)
- InvalidateRect (hwnd, NULL, TRUE) ;
- break ;
- }
- return DefWindowProc (hwnd, message, wParam, lParam) ;
- }
复制代码
STRPROG.RC (摘录)
- //Microsoft Developer Studio generated resource script.
- #include "resource.h"
- #include "afxres.h"
- /////////////////////////////////////////////////////////////////////////////
- // Dialog
- ENTERDLG DIALOG DISCARDABLE 20, 20, 186, 47
- STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "Enter"
- FONT 8, "MS Sans Serif"
- BEGIN
- LTEXT "&Enter:",IDC_STATIC,7,7,26,9
- EDITTEXT IDC_STRING,31,7,148,12,ES_AUTOHSCROLL
- DEFPUSHBUTTON "OK",IDOK,32,26,50,14
- PUSHBUTTON "Cancel",IDCANCEL,104,26,50,14
- END
- DELETEDLG DIALOG DISCARDABLE 20, 20, 186, 47
- STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "Delete"
- FONT 8, "MS Sans Serif"
- BEGIN
- LTEXT "&Delete:",IDC_STATIC,7,7,26,9
- EDITTEXT IDC_STRING,31,7,148,12,ES_AUTOHSCROLL
- DEFPUSHBUTTON "OK",IDOK,32,26,50,14
- PUSHBUTTON "Cancel",IDCANCEL,104,26,50,14
- END
- /////////////////////////////////////////////////////////////////////////////
- // Menu
- STRPROG MENU DISCARDABLE
- BEGIN
- MENUITEM "&Enter!", IDM_ENTER
- MENUITEM "&Delete!", IDM_DELETE
- END
- RESOURCE.H (摘录)
- // Microsoft Developer Studio generated include file.
- // Used by StrProg.rc
- #define IDC_STRING 1000
- #define IDM_ENTER 40001
- #define IDM_DELETE 40002
- #define IDC_STATIC -1
复制代码
STRPROG.C包含STRLIB.H表头档案,其中定义了STRPROG将使用的STRLIB中的三个函式。
当您执行STRPROG的多个执行实体的时候,本程式的奥妙之处就会显露出来。STRLIB将在共用记忆体中储存字串及其指标,并允许STRPROG中的所有执行实体共用此资料。让我们看一下它是如何执行的吧。
在STRPROG执行实体之间共用资料
Windows在一个Win32程序的位址空间周围筑了一道墙。通常,一个程序的位址空间中的资料是私有的,对别的程序而言是不可见的。但是执行STRPROG的多个执行实体表示了STRLIB在程式的所有执行实体之间共用资料是毫无问题的。当您在一个STRPROG视窗中增加或者删除一个字串时,这种改变将立即反映在其他的视窗中。
在全部常式之间,STRLIB共用两个变数:一个字元阵列和一个整数(记录已储存的有效字串的个数)。STRLIB将这两个变数储存在共用的一个特殊记忆体区段中:
#pragma data_seg ("shared")
int iTotal = 0 ;
WCHAR szStrings [MAX_STRINGS][MAX_LENGTH + 1] = { '\0' } ;
#pragma data_seg ()
第一个#pragma叙述建立资料段,这里命名为shared。您可以将这段命名为任何一个您喜欢的名字。在这里的#pragma叙述之後的所有初始化了的变数都放在shared资料段中。第二个#pragma叙述标示段的结束。对变数进行专门的初始化是很重要的,否则编译器将把它们放在普通的未初始化资料段中而不是放在shared中。
连结器必须知道有一个「shared」共享资料段。在「Project Settings」对话方块选择「Link」页面标签。选中「STRLIB」时在「Project Options」栏位(在Release和Debug设定中均可),包含下面的连结叙述:
/SECTION:shared,RWS
字母RWS表示段具有读、写和共用属性。或者,您也可以直接用DLL原始码指定连结选项,就像我们在STRLIB.C那样:
#pragma comment(linker,"/SECTION:shared,RWS")
共用的记忆体段允许iTotal变数和szStrings字串阵列在STRLIB的所有常式之间共用。因为MAX_STRINGS等於256,而MAX_LENGTH等於63,所以,共用记忆体段的长度为32,772位元组-iTotal变数需要4位元组,256个指标中的每一个都需要128位元组。
使用共用记忆体段可能是在多个应用程式间共用资料的最简单的方法。如果需要动态配置共用记忆体空间,您应该查看记忆体映射档案物件的用法,文件在/Platform SDK/Windows Base Services/Interprocess Communication/File Mapping。 |
|