游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4038|回复: 7

??dll

[复制链接]

62

主题

296

帖子

302

积分

中级会员

Rank: 3Rank: 3

积分
302
QQ
发表于 2004-1-14 12:58:00 | 显示全部楼层 |阅读模式
?????dll??????????????dll????????????????

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:49:00 | 显示全部楼层

Re:??dll

21. ???????

???????????DLL??Microsoft Windows???????????????Windows?????????????????????????????????????Windows???????????????????????????????????????????????????????????????????????
????????????
?

?????????Windows????????????????????????????????????????????????????????????????????????????????????????DLL??????????????????????????????????????????

???????????Windows?????????????????????????????????????????????????(.OBJ)????????(.LIB)??????????????(.RES)????????????Windows?.EXE??????????????????????????????????

KERNEL32.DLL?USER32.DLL?GDI32.DLL??????????KEYBOARD.DRV?SYSTEM.DRV?MOUSE.DRV??????????????????????????????????Windows???????

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Windows?????????????????????????????????????????????Windows????????????????????Windows????????????????????????

???????????????????????.EXE?.FON?????????.DLL????.DLL??????????????Windows?????????????????????????LoadLibrary??LoadLibraryEx????????

??????????????????????????????????Windows??????????????????????????????????????????????????????????????????????.LIB??????????LINK??????????????????????????????????????????????????????????????????????????????????????????????????????????ACCOUNT.DLL?????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????3D?????????????GDI3.DLL?DLL??????????????????????????????????????????????????????????????GDI3.DLL???

????????
?

??????????????????????????????????????????????????????????????????????????????????

????????.LIB??????????????????????????????????.EXE????????Microsoft Visual C++???????????C???????????LIBC.LIB?

??????????????????????????????????????.LIB?????????????????????????????????????????????????????.EXE??????????????????????Microsoft?????KERNEL32.LIB?USER32.LIB?GDI32.LIB???Windows?????????????????Rectangle???Rectangle???LINK?????GDI32.DLL????????????????.EXE????????????Windows???GDI32.DLL??????????????

???????????????????????????????????????????????????????????????????????????Windows??????????????????????????????????????????????.EXE??????????????Windows??????Windows??????????MS-DOS????PATH??????????Windows????????????

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:49:00 | 显示全部楼层

Re:??dll

?????DLL
?

????????????????????????????????????????????????????????????????????????DLL?

??????????????????EDRLIB.DLL?DLL???????EDR????????????easy drawing routines??????EDRLIB???????????EdrCenterText????????????????????????????????EDRTEST.EXE?????EDRLIB.DLL?????????

????????????????????????????Visual C++ ????????????Visual C++ ???????workspaces???????projects??????????????????.EXE???????????.DLL??????????????????????????????????????????????????????????????????EDRTEST???????EDRTEST.EXE?????????EDRLIB.DLL??EDRTEST???????????

???????Visual C++????File???????New???????Workspaces??????????????????????Location???????????????????Workspace Name?????EDRTEST???Enter??

???????????????Developer Studio????????EDRTEST?????????????EDRTEST.DSW???????????

?????????????????????File???????New???????Projects??????????????Win32 Application??????Win32 Dynamic-Link Library????????????Add To Current Workspace??????????EDRTEST? ???????????Project Name???EDRLIB???????OK???????Project Name???EDRLIB??Visual C++????Location??????EDRLIB??EDRTEST?????????????????????Location????EDRLIB??????????EDRTEST???????OK????????????????????????DLL????An Empty DLL Project??????Finish??Visual C++?????????EDRLIB.DSP???????EDRLIB.MAK????Tools Options??????B?uild?????????Export Makefile?????

????????????????????File???????New???????Files?????????C/C++ Header File?????????EDRLIB.H?????21-1??????????????????????File????????New???????Files???????????C++ Source File?????????EDRLIB.C???????21-1??????

??21-1  EDRLIB???????
EDRLIB.H
/*--------------------------------------------------------------------------
           EDRLIB.H header file
----------------------------------------------------------------------------*/

#ifdef         __cplusplus
#define         EXPORT extern "C" __declspec (dllexport)
#else
#define         EXPORT __declspec (dllexport)
#endif

EXPORT         BOOL CALLBACK EdrCenterTextA (HDC, PRECT, PCSTR) ;
EXPORT         BOOL CALLBACK EdrCenterTextW (HDC, PRECT, PCWSTR) ;

#ifdef         UNICODE
#define         EdrCenterText EdrCenterTextW
#else
#define         EdrCenterText EdrCenterTextA
#endif
EDRLIB.C
/*---------------------------------------------------------------------------
           EDRLIB.C -- Easy Drawing Routine Library module
                                                               (c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/
#include  windows.h>
#include "edrlib.h"

int WINAPI DllMain (        HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
             return TRUE ;
}

EXPORT BOOL CALLBACK EdrCenterTextA (        HDC hdc, PRECT prc, PCSTR pString)
{
             int  iLength ;
             SIZE size ;

             iLength = lstrlenA (pString) ;
             GetTextExtentPoint32A (hdc, pString, iLength, &size) ;
             return TextOutA (hdc,(        prc->right - prc->left - size.cx) / 2,
                                    (        prc->bottom - prc->top - size.cy) / 2,
                                              pString, iLength) ;
}

EXPORT BOOL CALLBACK EdrCenterTextW (HDC hdc, PRECT prc, PCWSTR pString)
{
             int  iLength ;
             SIZE size ;

             iLength = lstrlenW (pString) ;
             GetTextExtentPoint32W (hdc, pString, iLength, &size) ;
             return TextOutW (hdc,         (        prc->right - prc->left - size.cx) / 2,
                                           (        prc->bottom - prc->top - size.cy) / 2,
                                                      pString, iLength) ;
}
??????Release?????????Debug?????EDRLIB.DLL????RELEASE?DEBUG?????EDRLIB.LIB?????????????????EDRLIB.DLL????????????

???????????????????UNICODE?????????Unicode??Unicode?????????????DLL???????????????Unicode??Unicode??????????EDRLIB.C?????EdrCenterTextA?ANSI???EdrCenterTextW???????EdrCenterTextA???????PCSTR???const????????EdrCenterTextW????????PCWSTR???const????????EdrCenterTextA?????lstrlenA?GetTextExtentPoint32A?TextOutA?EdrCenterTextW???lstrlenW?GetTextExtentPoint32W?TextOutW??????UNICODE?????EDRLIB.H?EdrCenterText???EdrCenterTextW??????EdrCenterTextA????????Windows?????

EDRLIB.H?????DllMain????DLL??WinMain?????????????????deinitialization??????????????????????DllMain??TRUE?

?????????????????????EXPORT????DLL?????????????????exported???????????????????????????EDRLIB.LIB?????????????????????????????????????????????EDRLIB.DLL????????EXPORT????????????__declspec?dllexport?????????C++?????????C???????????C++????????name mangling??????????C?C++????????DLL?

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:50:00 | 显示全部楼层

Re:??dll

?????????
?

??????????????????????DllMain???DllMain??????????????????????????????????????DialogBox???????????hInstance??????????DllMain?????????????

fdwReason????????????????Windows???DllMain????????????????????????????Windows??????????????????????????????process??

fdwReason????DLL_PROCESS_ATTACH???????????????????????????????????????????????????????????????????????????????????????????DllMain????DLL_PROCESS_ATTACH????????DLL????????????????DLL_PROCESS_ATTACH???DllMain??????????????

????????DllMain???????0????0???Windows???????

?fdwReason???DLL_PROCESS_DETACH???????????DLL?????????????????????32???Windows??????????????????????????????

??????DLL_THREAD_ATTACH?fdwReason????DllMain????????????????????????????Windows?DLL_THREAD_DETACH?fdwReason????DllMain??????????????????????????????????????????????????DLL_THREAD_ATTACH???DLL_THREAD_DETACH???

?????DLL_THREAD_DETACH????DllMain????????????????????????????????????????????PostMessage???????????????????????????

????
?

??????EDRTEST??????????????????EDRTEST?????EDRLIB.DLL??Visual C++???EDRTEST?????????File???????New??????New?????????Projects???????????Win32 Application?????????Add To Current Workspace??????????EDRTEST????Locations???????EDRTEST???????OK???????????????An Empty Project????Finish??

??File?????????New?????Files??????????C++ Source File?????Add To Project????????EDRTEST?????EDRLIB????????EDRTEST.C???????21-2??????????EdrCenterText????????????????

??21-2  EDRTEST
EDRTEST.C
/*---------------------------------------------------------------------------
           EDRTEST.C -- Program using EDRLIB dynamic-link library
                                                                (c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include "edrlib.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                                                            PSTR szCmdLine, int iCmdShow)
{
             static TCHAR         szAppName[] = TEXT ("StrProg") ;
             HWND                                         hwnd ;
             MSG                                          msg ;
             WNDCLASS                             wndclass ;

             wndclass.style                         = CS_HREDRAW | CS_VREDRAW ;
             wndclass.lpfnWndProc                   = WndProc ;
             wndclass.cbClsExtra                    = 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          = NULL ;
             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 ;
}       

LRESULT CALLBACK WndProc (        HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
             HDC                         hdc ;
             PAINTSTRUCT                 ps ;
             RECT                        rect ;
     
             switch (message)
             {
             case         WM_PAINT:
                                  hdc = BeginPaint (hwnd, &ps) ;
         
                                  GetClientRect (hwnd, &rect) ;
         
                                  EdrCenterText (        hdc, &rect,
                    TEXT ("This string was displayed by a DLL")) ;
         
                                  EndPaint (hwnd, &ps) ;
                                  return 0 ;
         
             case         WM_DESTROY:
                                  PostQuitMessage (0) ;
                                  return 0 ;
             }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
???????EdrCenterText???EDRTEST.C??EDRLIB.H??????????WM_PAINT?????????

??????????????????????????Project???????Select Active Project?????????EDRLIB???EDRTEST?????EDRTEST????????????????????????????????Project?????????Dependencies????Select Project To Modify?????????EDRTEST????Dependent On The Following Project(s)??????EDRLIB??????????EDRTEST??EDRLIB????????????????EDRTEST?????????????????EDRTEST????????EDRLIB?

??Project???????Settings?????General????????????????EDRLIB????EDRTEST???????????Win32 Release?????????????Intermediate Files???Output Files????RELEASE?????????Win32 Debug?????DEBUG???????????????????EDRLIB.DLL?EDRTEST.EXE???????????????DLL?????????

??Project Setting???????????EDRTEST?????C/C++???????????????Preprocessor Definitions?????UNICODE????Debug???

????????Debug???Release????????EDRTEST.EXE??????Visual C++????????EDRLIB?RELEASE?DEBUG?????EDRLIB.LIB????????EDRLIB.DLL??Developer Studio??EDRTEST?????????????

??EDRTEST.EXE??????EdrCenterText?????????????????EDRLIB.DLL???EdrCenterText????????EDRTEST.EXE??EDRLIB.DLL?

??EDRTEST.EXE??Windows??????????????????????????Windows??????????Windows??????EDRLIB????????Windows?EDRLIB.DLL?????????????EDRLIB???????EDRTEST??EdrCenterText????????EDRLIB?????

?EDRTEST.C????????EDRLIB.H???WINDOWS.H?????EDRLIB.LIB???Windows????????USER32.LIB????????????????EDLIB.DLL??????USER32.DLL????????????????Windows????

???????????????????????

????????DLL??Windows??????????????????DLL???????????????????????????????????????DLL?????????DLL?????????DLL???????????????????????DLL???Windows???????????????

????????????????????????????DLL?????????????????????DLL??????????????????????????????????????????

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:50:00 | 显示全部楼层

Re:??dll

?DLL??????
?

???????Windows??????????????????????????????????????????????DLL????????????????????????????????????????????????????????????????

??????????????????STRPROG???????string program???????????STRLIB????????string library????????STRLIB????????STRPROG????????????STRLIB??????????STRPROG???callback???

STRLIB??????????????????????256?????STRLIB???????????????????????STRLIB??????STRPROG??????????????STRLIB??????????STRPROG?????????????Enter???Delete?????????????????????????????STRPROG?????????????STRLIB???????

???????STRLIB????????????STRLIB???????

EXPORT BOOL CALLBACK AddString (pStringIn)
??pStringIn??????????AddString???????????STRLIB??????????????????????????????????AddString???TRUE???0???????FALSE??0??????????0???????????????????????256???????????FALSE?

STRLIB???STRLIB??????????????

EXPORT BOOL CALLBACK DeleteString (pStringIn)
?????pStringIn??????????????????????????????????DeleteString???TRUE???0???????FALSE??0?????FALSE????????0??????????????

STRLIB?????????????callback????????????STRLIB??????????

EXPORT int CALLBACK GetStrings (pfnGetStrCallBack, pParam)
???????callback????????????

EXPORT BOOL CALLBACK GetStrCallBack (PSTR pString, PVOID pParam)
GetStrings???pfnGetStrCallBack??callback?????callback?????FALSE??0??GetStrings???????????GetStrCallBack?GetStrings?????callback???????pParam???????????????????????

???????????Unicode??????STRLIB????????Unicode??Unicode??????EDRLIB???????????A???W??????????STRLIB?Unicode???????????Unicode?????STRLIB???????????AddStringA?DeleteStringA?GetStringsA??????Unicode??Unicode?????

?STRPROG?STRLIB???????????STRPROG?????EDRTEST????????????21-3?????STRLIB.DLL????????????????

??21-3  STRLI
STRLIB.H
/*----------------------------------------------------------------------------
           STRLIB.H header file
-----------------------------------------------------------------------------*/

#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif

             // The maximum number of strings STRLIB will store and their lengths
#define         MAX_STRINGS 256
#define         MAX_LENGTH  63

             // The callback function type definition uses generic strings

typedef BOOL (CALLBACK * GETSTRCB) (PCTSTR, PVOID) ;

             // Each function has ANSI and Unicode versions

EXPORT         BOOL CALLBACK AddStringA (PCSTR) ;
EXPORT         BOOL CALLBACK AddStringW (PCWSTR) ;

EXPORT         BOOL CALLBACK DeleteStringA (PCSTR) ;
EXPORT         BOOL CALLBACK DeleteStringW (PCWSTR) ;

EXPORT         int CALLBACK GetStringsA (GETSTRCB, PVOID) ;
EXPORT         int CALLBACK GetStringsW (GETSTRCB, PVOID) ;

             // Use the correct version depending on the UNICODE identifier

#ifdef         UNICODE
#define         AddString            AddStringW
#define         DeleteString                 DeleteStringW
#define         GetStrings                   GetStringsW
#else
#define         AddString            AddStringA
#define         DeleteString                 DeleteStringA
#define         GetStrings                   GetStringsA
#endif
STRLIB.C
/*---------------------------------------------------------------------------
           STRLIB.C -         Library module for STRPROG program
                                                       (c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include <wchar.h>                                       // for wide-character string functions
#include "strlib.h"

        // shared memory section (requires /SECTION:shared,RWS in link options)
#pragma                 data_seg ("shared")
int                           iTotal = 0 ;
WCHAR                         szStrings [MAX_STRINGS][MAX_LENGTH + 1] = { '\0' } ;
#pragma                 data_seg ()

#pragma                 comment(linker,"/SECTION:shared,RWS")

int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
        return TRUE ;
}

EXPORT BOOL CALLBACK AddStringA (PCSTR pStringIn)
{
             BOOL                  bReturn ;
             int                   iLength ;
             PWSTR                 pWideStr ;

        // Convert string to Unicode and call AddStringW
             iLength = MultiByteToWideChar (CP_ACP, 0, pStringIn, -1, NULL, 0) ;
             pWideStr = malloc (iLength) ;
             MultiByteToWideChar (CP_ACP, 0, pStringIn, -1, pWideStr, iLength) ;
             bReturn = AddStringW (pWideStr) ;
             free (pWideStr) ;

             return bReturn ;
}

EXPORT BOOL CALLBACK AddStringW (PCWSTR pStringIn)
{
             PWSTR                 pString ;
             int                   i, iLength ;
     
             if (iTotal == MAX_STRINGS - 1)
                                  return FALSE ;
             if ((iLength = wcslen (pStringIn)) == 0)
                                  return FALSE ;
                          // Allocate memory for storing string, copy it, convert to uppercase
             pString = malloc (sizeof (WCHAR) * (1 + iLength)) ;
             wcscpy (pString, pStringIn) ;
             _wcsupr (pString) ;

                          //  Alphabetize the strings
             for (i = iTotal ; i > 0 ; i-)
             {
                          if (wcscmp (pString, szStrings[i - 1]) >= 0)
                                               break ;
                          wcscpy (szStrings, szStrings[i - 1]) ;
             }
             wcscpy (szStrings, pString) ;
             iTotal++ ;

             free (pString) ;
             return TRUE ;
}

EXPORT BOOL CALLBACK DeleteStringA (PCSTR pStringIn)
{
             BOOL                  bReturn ;
             int                   iLength ;
             PWSTR                 pWideStr ;

                                  // Convert string to Unicode and call DeleteStringW

             iLength = MultiByteToWideChar (CP_ACP, 0, pStringIn, -1, NULL, 0) ;
             pWideStr = malloc (iLength) ;
             MultiByteToWideChar (CP_ACP, 0, pStringIn, -1, pWideStr, iLength) ;
             bReturn = DeleteStringW (pWideStr) ;
             free (pWideStr) ;

             return bReturn ;
}

EXPORT BOOL CALLBACK DeleteStringW (PCWSTR pStringIn)
{
             int i, j ;
             if (0 == wcslen (pStringIn))
                                  return FALSE ;
             for (i = 0 ; i < iTotal ; i++)
          {
                          if (_wcsicmp (szStrings, pStringIn) == 0)
                                               break ;
           }
                          // If given string not in list, return without taking action
             if (i == iTotal)
                                  return FALSE ;
                          // Else adjust list downward
             for (j = i ; j < iTotal ; j++)
                                  wcscpy (szStrings[j], szStrings[j + 1]) ;
             szStrings[iTotal-][0] = '\0' ;
             return TRUE ;
}

EXPORT int CALLBACK GetStringsA (GETSTRCB pfnGetStrCallBack, PVOID pParam)
{
             BOOL                 bReturn ;
             int                  i, iLength ;
             PSTR                 pAnsiStr ;

        for (i = 0 ; i < iTotal ; i++)
             {
                                       // Convert string from Unicode
iLength = WideCharToMultiByte (        CP_ACP, 0, szStrings, -1, NULL, 0, NULL, NULL) ;
                        pAnsiStr = malloc (iLength) ;
          WideCharToMultiByte (        CP_ACP, 0, szStrings, -1, pAnsiStr, iLength, NULL, NULL) ;

                                                       // Call callback function

                        bReturn = pfnGetStrCallBack (pAnsiStr, pParam) ;
         
                        if (bReturn == FALSE)
                                                       return i + 1 ;

                        free (pAnsiStr) ;
             }
             return iTotal ;
}

EXPORT int CALLBACK GetStringsW (GETSTRCB pfnGetStrCallBack, PVOID pParam)
{
             BOOL                 bReturn ;
             int                  i ;
     
             for (i = 0 ; i < iTotal ; i++)
             {
                          bReturn = pfnGetStrCallBack (szStrings, pParam) ;
                          if (bReturn == FALSE)
                                               return i + 1 ;
             }
             return iTotal ;
}
??DllMain?????STRLIB????????????????????????EXPORT??????LINK?STRLIB.LIB???????????

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:51:00 | 显示全部楼层

Re:??dll

STRPROG??
?

STRPROG?????21-4????????????????????(Enter?Delete)????????????????????STRPROG??AddString??DeleteString??????????????????GetStrings?????GetStrCallBack??????????

??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.cbClsExtra                            = 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?????????????????????????????

45

主题

286

帖子

314

积分

中级会员

???????

Rank: 3Rank: 3

积分
314
QQ
发表于 2004-1-15 13:52:00 | 显示全部楼层

Re:??dll

?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?

?????DLL??
?

???????????????????????????????????GetMessage?PeekMessage??????????????????????????????????????????????????????????????????Windows?????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.EXE?????????????????????????????????

??????????????????????????????CreateWindow???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????DialogBox???????????????????????????DialogBox?hwndParent?????NULL?

?????????????
?

????????????????????Windows??????????????????????????????????????????????Rectangle???

Rectangle (hdc, xLeft, yTop, xRight, yBottom) ;
?????GDI32.LIB???????????????Rectangle?????????????

?????????????Rectangle????typedef?Rectangle?????????

typedef BOOL (WINAPI * PFNRECT) (HDC, int, int, int, int) ;
?????????

HANDLE        hLibrary ;
PFNRECT        pfnRectangle ;
???hLibrary??????????lpfnRectangle???Rectangle??????

hLibrary = LoadLibrary (TEXT ("GDI32.DLL"))
pfnRectangle = (PFNPRECT) GetProcAddress (hLibrary, TEXT ("Rectangle"))
?????????????????????LoadLibrary????NULL??????????????????

pfnRectangle (hdc, xLeft, yTop, xRight, yBottom) ;
FreeLibrary (hLibrary) ;
???????????????????Rectangle?????????????????????????????????????????????????????

?????????LoadLibrary?FreeLibrary???Windows??????????????????????LoadLibrary?????????Windows????????????????????????FreeLibrary???????????????????????????????????????Windows???????????????????????

??????
?

??Windows??????????????????????????????????DLL????????????????DLL??????????????

?????????????????Windows?????????????????????????????LoadBitmap???????????????????????????????????Windows??????????????????????????????????????????????????????????????

??21-5????????9???????BITLIB.DLL??????????BITLIB.RC????????????????????????????????BITLIB.DLL???9???BITMAP1.BMP?BITMAP2.BMP???????????????????????????Visual C++????????????ID?1?9????

??21-5  BITLIB
BITLIB.C
/*--------------------------------------------------------------
           BITLIB.C --        Code entry point for BITLIB dynamic-link library
                                                               (c) Charles Petzold,  1998
------------------------------------------------------------------*/

#include <windows.h>
int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
             return TRUE ;
}
BITLIB.RC ????
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
// Bitmap
1                 BITMAP  DISCARDABLE     "bitmap1.bmp"
2                 BITMAP  DISCARDABLE     "bitmap2.bmp"
3                 BITMAP  DISCARDABLE     "bitmap3.bmp"
4                 BITMAP  DISCARDABLE     "bitmap4.bmp"
5                 BITMAP  DISCARDABLE     "bitmap5.bmp"
6                 BITMAP  DISCARDABLE     "bitmap6.bmp"
7                 BITMAP  DISCARDABLE     "bitmap7.bmp"
8                 BITMAP  DISCARDABLE     "bitmap8.bmp"
9                 BITMAP  DISCARDABLE     "bitmap9.bmp"
???SHOWBIT????????BITLIB??????SHOWBIT????????????21-6???SHOWBIT?????????????????BITLIB???SHOWBIT????????????BITLIB.LIB???????BITLIB???????????????BITLIB.LIB????????????BITLIB?SHOWBIT?????????????Active Project?????????

SHOWBIT.C?BITLIB??????????????????????????????????

??21-6  SHOWBIT
SHOWBIT.C
/*--------------------------------------------------------------------------
           SHOWBIT.C -- Shows bitmaps in BITLIB dynamic-link library
                                                                (c) Charles Petzold, 1998
---------------------------------------------------------------------------*/

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName [] = TEXT ("ShowBit") ;

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.cbClsExtra                            = 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  = NULL ;
             wndclass.lpszClassName                 = szAppName ;
     
             if (!RegisterClass (&wndclass))
     {
                     MessageBox (        NULL, TEXT ("This program requires Windows NT!"),
                                                                              szAppName, MB_ICONERROR) ;
                          return 0 ;
     }

     hwnd = CreateWindow (szAppName,
            TEXT ("Show Bitmaps from BITLIB (Press Key)"),
                 WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT, CW_USEDEFAULT,
                 CW_USEDEFAULT, CW_USEDEFAULT,
                 NULL, NULL, hInstance, NULL) ;

        if (!hwnd)
                                  return 0 ;
             ShowWindow (hwnd, iCmdShow) ;
             UpdateWindow (hwnd) ;
     
        while (GetMessage (&msg, NULL, 0, 0))
             {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
             }
             return msg.wParam ;
}

void DrawBitmap (HDC hdc, int xStart, int yStart, HBITMAP hBitmap)
{       
             BITMAP                 bm ;
             HDC                           hMemDC ;
             POINT                          pt ;

             hMemDC = CreateCompatibleDC (hdc) ;
             SelectObject (hMemDC, hBitmap) ;
             GetObject (hBitmap, sizeof (BITMAP), &bm) ;
             pt.x = bm.bmWidth ;
             pt.y = bm.bmHeight ;
     
        BitBlt (hdc, xStart, yStart, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY) ;
             DeleteDC (hMemDC) ;
}

LRESULT CALLBACK WndProc (        HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
             static HINSTANCE                 hLibrary ;
             static int                                       iCurrent = 1 ;
             HBITMAP                                                  hBitmap ;
             HDC                                                              hdc ;
             PAINTSTRUCT                                      ps ;

             switch (message)
             {       
             case         WM_CREATE:
                                  if ((hLibrary = LoadLibrary (TEXT ("BITLIB.DLL"))) == NULL)
                                  {
           MessageBox (        hwnd, TEXT ("Can't load BITLIB.DLL."),
                        szAppName, 0) ;
           return -1 ;
                               }
                          return 0 ;
         
             case         WM_CHAR:
                                  if (hLibrary)
                                  {
                                               iCurrent ++ ;
                                               InvalidateRect (hwnd, NULL, TRUE) ;
                                  }
                                  return 0 ;
             case         WM_PAINT:
                                  hdc = BeginPaint (hwnd, &ps) ;
         
                                  if (hLibrary)
                                  {
                               hBitmap = LoadBitmap (hLibrary, MAKEINTRESOURCE (iCurrent)) ;

                                               if (!hBitmap)
                                               {
                                                            iCurrent = 1 ;
                                                            hBitmap = LoadBitmap (        hLibrary,
                                        MAKEINTRESOURCE (iCurrent)) ;
                                               }
                                               if (hBitmap)
                                                       {
                                                                    DrawBitmap (hdc, 0, 0, hBitmap) ;
                                                            DeleteObject (hBitmap) ;
               }
                                  }
                                  EndPaint (hwnd, &ps) ;
                                  return 0 ;
         
             case         WM_DESTROY:
                                  if (hLibrary)
                                                       FreeLibrary (hLibrary) ;

                                  PostQuitMessage (0) ;
                                  return 0 ;
             }
             return DefWindowProc (hwnd, message, wParam, lParam) ;
}
???WM_CREATE???????SHOWBIT???BITLIB.DLL????

if ((hLibrary = LoadLibrary (TEXT ("BITLIB.DLL"))) == NULL)
??BITLIB.DLL?SHOWBIT.EXE????????Windows????????????????LoadLibrary??NULL?SHOWBIT????????????????WM_CREATE????-1?????WinMain??CreateWindow????NULL??????????

SHOWBIT????????????????LoadBitmap?????????????

hBitmap = LoadBitmap (hLibrary, MAKEINTRESOURCE (iCurrent)) ;
????iCurrent????????????????????????????????

???WM_DESTROY????SHOWBIT??????

FreeLibrary (hLibrary) ;
?SHOWBIT?????????????BITLIB.DLL???????0?????????????????????????????????????????????????????????????metafile????metafile????????????????????

9

主题

311

帖子

333

积分

中级会员

Rank: 3Rank: 3

积分
333
发表于 2004-1-15 14:58:00 | 显示全部楼层

Re:??dll

?????Windows Programming?????
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-7-23 12:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表