HINSTANCE hLibrary;
FARPROC lpFunc;
hLibrary = LoadLibrary("INFO.DLL");
if (hLibrary >= 32) {
lpFunc
= GetProcAddress(hLibrary, "CreateInfo");
if
(lpFunc != (FARPROC)NULL)
(*lpFunc)((LPSTR)buffer,
512);
FreeLibrary(hLibrary);
}
6.2.3
动态连接库示例DLLDemo
我们编制一个程序DLLDemo,它和5.2.1节中的程序很类似,但是它使用的绘图函数是动态连接库DLLDRAW.DLL提供的,读者可以通过比较这两个程序来加深对动态连接库使用的理解。下面给出DLLDemo程序的C语言源文件:
/****************************************************************************
PROGRAM: Dlldemo.c
PURPOSE: DLL Demo for Windows applications
FUNCTIONS:
WinMain() -
calls initialization function, processes message loop
InitApplication()
- initializes window data and registers window
InitInstance()
- saves instance handle
and creates main window
MainWndProc()
-
processes messages
****************************************************************************/
#include "windows.h"
/* required for all
Windows applications */
#include "dlldemo.h" /* specific to this program */
#include "dlldraw.h" /*
DLL header file */
HANDLE hInst;
/* current instance */
HMENU
hMenu; /*
Menu Handle */
/****************************************************************************
FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
PURPOSE: calls initialization function, processes message loop
****************************************************************************/
int PASCAL WinMain(hInstance, hPrevInstance,
lpCmdLine, nCmdShow)
HANDLE hInstance;
/*
current instance */
HANDLE hPrevInstance; /*
previous instance */
LPSTR lpCmdLine;
/*
command line */
int nCmdShow;
/*
show-window type (open/icon) */
{
MSG msg;
/*
message
*/
if (!hPrevInstance) /*
Other instances of app running? */
if (!InitApplication(hInstance)) /*
Initialize shared things */
return (FALSE); /*
Exits if unable to initialize */
/* Perform initializations that apply to a specific instance */
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
/* Acquire and dispatch messages until a WM_QUIT message is received. */
while (GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg); /*
Translates virtual key codes */
DispatchMessage(&msg); /*
Dispatches message to window
*/
}
return (msg.wParam); /* Returns the value from PostQuitMessage
*/
}
/****************************************************************************
FUNCTION: InitApplication(HANDLE)
PURPOSE: Initializes window data and registers window class
****************************************************************************/
BOOL InitApplication(hInstance)
HANDLE hInstance;
/*
current instance */
{
WNDCLASS wc;
/* Fill in window class structure with parameters that describe the main
window. */
wc.style = NULL; /* Class
style(s). */
wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages
for */
/* windows of
this class. */
wc.cbClsExtra = 0;
/*
No per-class extra data. */
wc.cbWndExtra = 0;
/*
No per-window extra data. */
wc.hInstance = hInstance; /*
Application that owns the class. */
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName =
NULL; /*
No class menu. */
wc.lpszClassName = "MenuWClass"; /*
Name used in call to CreateWindow. */
/* Register the window class and return success/failure code. */
return (RegisterClass(&wc));
}
/****************************************************************************
FUNCTION:
InitInstance(HANDLE, int)
PURPOSE: Saves instance
handle and creates main window
****************************************************************************/
BOOL InitInstance(hInstance, nCmdShow)
HANDLE
hInstance; /* Current instance
identifier.
*/
int
nCmdShow; /* Param for first
ShowWindow() call. */
{
HWND hWnd; /*
Main window handle. */
/* Save the instance handle in static variable, which will be used
in */
/* many subsequent calls from this application to Windows.
*/
hInst = hInstance;
hMenu = LoadMenu(hInst, "SampleMenu");
/* Create a main window for this application instance. */
hWnd = CreateWindow("MenuWClass", "Draw Box & Circle
",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMenu, hInstance, NULL);
/* If window could not be created, return "failure" */
if (!hWnd)
return (FALSE);
/* Make the window visible; update its client area; and return
"success" */
ShowWindow(hWnd, nCmdShow);
/*
Show the window */
UpdateWindow(hWnd); /*
Sends WM_PAINT message */
return (TRUE);
/* Returns the value from
PostQuitMessage */
}
/****************************************************************************
FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
PURPOSE: Processes messages
MESSAGES:
WM_COMMAND - application menu (About
dialog box)
WM_DESTROY - destroy window
****************************************************************************/
HPEN hPen,
hOldPen; /*
Handle of Pen */
HBRUSH hBrush,
hOldBrush; /*
Handle of Brush */
long CALLBACK __export MainWndProc(hWnd, message,
wParam, lParam)
HWND hWnd;
/*
window handle
*/
UINT message;
/* type of message */
WPARAM wParam; /*
additional information
*/
LPARAM lParam;
/*
additional information */
{
FARPROC
lpProcAbout; /*
function to the "About" function */
HDC hDC; /*
Handle of device context */
PAINTSTRUCT
ps; /*
paint structure */
COLORREF
cref = RGB(255, 0, 0);
static count = 0;
switch (message)
{
case
WM_COMMAND: /*
message: command from application menu */
switch
(wParam)
{
case
IDM_EXIT:
DestroyWindow(hWnd);
break;
case
IDM_ABOUT:
lpProcAbout
= MakeProcInstance((FARPROC)About, hInst);
DialogBox(hInst,
/*
current instance */
"AboutBox", /*
resource to use */
hWnd, /*
parent handle */
(DLGPROC)lpProcAbout);
/*
About() instance address */
FreeProcInstance(lpProcAbout);
break;
case
IDM_BOX:
DrawBox(hWnd,
hPen, hBrush);
break;
case
IDM_CIRCLE:
DrawCircle(hWnd,
hPen, hBrush);
ModifyMenu(hMenu,
IDM_CIRCLE, MF_BYCOMMAND, IDM_PIE, "&Pie");
break;
case
IDM_PIE:
DrawPie(hWnd,
hPen, hBrush);
ModifyMenu(hMenu,
IDM_PIE, MF_BYCOMMAND, IDM_CIRCLE, "&Circle");
break;
case
IDM_RED:
cref
= RGB(255, 0, 0);
DeleteObject(hPen);
DeleteObject(hBrush);
hPen
= CreatePen(PS_SOLID, 1, cref); /*
Create a solid red pen */
hBrush
= CreateSolidBrush(cref); /*
Create a solid red brush */
break;
case
IDM_GREEN:
cref
= RGB(0, 255, 0);
DeleteObject(hPen);
DeleteObject(hBrush);
hPen
= CreatePen(PS_SOLID, 1, cref); /*
Create a solid green pen */
hBrush
= CreateSolidBrush(cref); /*
Create a solid green brush */
break;
case
IDM_BLUE:
cref
= RGB(0, 0, 255);
DeleteObject(hPen);
DeleteObject(hBrush);
hPen
= CreatePen(PS_SOLID, 1, cref); /*
Create a solid blue pen */
hBrush
= CreateSolidBrush(cref); /*
Create a solid blue brush */
break;
default:
return
(DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_PAINT:
hDC
= BeginPaint(hWnd, &ps);
TextOut(hDC,
250, 10, "Window Output Demo", 18);
EndPaint(hWnd,
&ps);
break;
case WM_CREATE:
hPen
= CreatePen(PS_SOLID, 1, cref); /*
Create a solid red pen */
hBrush
= CreateSolidBrush(cref); /*
Create a solid red brush */
break;
case WM_DESTROY: /*
message: window being destroyed */
DeleteObject(hPen);
DeleteObject(hBrush);
PostQuitMessage(0);
break;
default:
/* Passes it on if unprocessed */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
/****************************************************************************
FUNCTION: About(HWND, unsigned, WORD, LONG)
PURPOSE: Processes messages
for "About" dialog box
MESSAGES:
WM_INITDIALOG - initialize
dialog box
WM_COMMAND - Input received
****************************************************************************/
BOOL __export CALLBACK About(hDlg, message,
wParam, lParam)
HWND hDlg;
/* window handle
of the dialog box */
unsigned message; /* type
of message */
WORD wParam;
/* message-specific
information */
LONG lParam;
{
switch (message)
{
case WM_INITDIALOG: /* message: initialize dialog box */
return (TRUE);
case WM_COMMAND:
/* message: received a command */
if (wParam == IDOK ||
wParam == IDCANCEL)
{
EndDialog(hDlg, TRUE); /* Exits the dialog box */
return (TRUE);
}
break;
}
return (FALSE);
/* Didn't process a message */