dllload.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 8k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include <windows.h>
  2. #include "netmpr.h"
  3. #include "pwlapi.h"
  4. #include <pcerr.h>
  5. /* Avoid inconsistent-DLL-linkage warnings on shell32 and comctl32 APIs. */
  6. #undef DECLSPEC_IMPORT
  7. #define DECLSPEC_IMPORT
  8. #include <prsht.h>
  9. #include <shellapi.h>
  10. #pragma warning(disable:4229)  // No warnings when modifiers used on data
  11. // Delay loading mechanism.  This allows you to write code as if you are
  12. // calling implicitly linked APIs, and yet have these APIs really be
  13. // explicitly linked.  You can reduce the initial number of DLLs that 
  14. // are loaded (load on demand) using this technique.
  15. //
  16. // Use the following macros to indicate which APIs/DLLs are delay-linked
  17. // and -loaded.
  18. //
  19. //      DELAY_LOAD
  20. //      DELAY_LOAD_HRESULT
  21. //      DELAY_LOAD_SAFEARRAY
  22. //      DELAY_LOAD_UINT
  23. //      DELAY_LOAD_INT
  24. //      DELAY_LOAD_VOID
  25. //
  26. // Use these macros for APIs that are exported by ordinal only.
  27. //
  28. //      DELAY_LOAD_ORD
  29. //      DELAY_LOAD_ORD_VOID     
  30. //
  31. // Use these macros for APIs that only exist on the integrated-shell
  32. // installations (i.e., a new shell32 is on the system).
  33. //
  34. //      DELAY_LOAD_SHELL
  35. //      DELAY_LOAD_SHELL_HRESULT
  36. //      DELAY_LOAD_SHELL_VOID     
  37. //
  38. // 
  39. // These macros produce code that looks like
  40. #if 0
  41. BOOL GetOpenFileNameA(LPOPENFILENAME pof)
  42. {
  43.     static BOOL (*pfnGetOpenFileNameA)(LPOPENFILENAME pof);
  44.     _GetProcFromDLL(&g_hinstCOMDLG32, "COMDLG32.DLL",  "GetOpenFileNameA", &pfnGetoptnFileNameA);
  45.     if (pfnGetOpenFileNameA)
  46.         return pfnGetOpenFileNameA(pof);
  47.     return -1;
  48. }
  49. #endif
  50. /**********************************************************************/
  51. void _GetProcFromDLL(HINSTANCE* phinst, LPCSTR pszDLL, FARPROC* ppfn, LPCSTR pszProc)
  52. {
  53.     // If it's already loaded, return.
  54.     if (*ppfn) {
  55.         return;
  56.     }
  57.     if (*phinst == NULL) {
  58.         *phinst = LoadLibrary(pszDLL);
  59.         if (*phinst == NULL) {
  60.             return;
  61.         }
  62.     }
  63.     *ppfn = GetProcAddress(*phinst, pszProc);
  64. }
  65. #define DELAY_LOAD_MAP(_hinst, _dll, _ret, _fnpriv, _fn, _args, _nargs, _err) 
  66. _ret __stdcall _fnpriv _args                
  67. {                                       
  68.     static _ret (* __stdcall _pfn##_fn) _args = NULL;   
  69.     _GetProcFromDLL(&_hinst, #_dll, (FARPROC*)&_pfn##_fn, #_fn); 
  70.     if (_pfn##_fn)               
  71.         return _pfn##_fn _nargs; 
  72.     return (_ret)_err;           
  73. }
  74. #define DELAY_LOAD_ERR(_hinst, _dll, _ret, _fn, _args, _nargs, _err)    DELAY_LOAD_MAP(_hinst, _dll, _ret, _fn, _fn, _args, _nargs, _err)
  75. #define DELAY_LOAD(_hinst, _dll, _ret, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, _ret, _fn, _args, _nargs, 0)
  76. #define DELAY_LOAD_HRESULT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, HRESULT, _fn, _args, _nargs, E_FAIL)
  77. #define DELAY_LOAD_SAFEARRAY(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, SAFEARRAY *, _fn, _args, _nargs, NULL)
  78. #define DELAY_LOAD_DWORD(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, DWORD, _fn, _args, _nargs, 0)
  79. #define DELAY_LOAD_UINT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, UINT, _fn, _args, _nargs, 0)
  80. #define DELAY_LOAD_INT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, INT, _fn, _args, _nargs, 0)
  81. #define DELAY_MAP_DWORD(_hinst, _dll, _fnpriv, _fn, _args, _nargs) DELAY_LOAD_MAP(_hinst, _dll, DWORD, _fnpriv, _fn, _args, _nargs, 0)
  82. #define DELAY_LOAD_VOID(_hinst, _dll, _fn, _args, _nargs) 
  83. void __stdcall _fn _args                
  84. {                                       
  85.     static void (* __stdcall _pfn##_fn) _args = NULL;   
  86.     _GetProcFromDLL(&_hinst, #_dll, (FARPROC*)&_pfn##_fn, #_fn); 
  87.     if (_pfn##_fn)              
  88.         _pfn##_fn _nargs;       
  89.     return;                     
  90. }
  91. //
  92. // For private entrypoints exported by ordinal.
  93. // 
  94. #define DELAY_LOAD_ORD_ERR(_hinst, _dll, _ret, _fn, _ord, _args, _nargs, _err) 
  95. _ret __stdcall _fn _args                
  96. {                                       
  97.     static _ret (* __stdcall _pfn##_fn) _args = NULL;   
  98.     _GetProcFromDLL(&_hinst, #_dll, (FARPROC*)&_pfn##_fn, (LPCSTR)_ord);   
  99.     if (_pfn##_fn)               
  100.         return _pfn##_fn _nargs; 
  101.     return (_ret)_err;           
  102. }
  103.         
  104. #define DELAY_LOAD_ORD(_hinst, _dll, _ret, _fn, _ord, _args, _nargs) DELAY_LOAD_ORD_ERR(_hinst, _dll, _ret, _fn, _ord, _args, _nargs, 0)
  105. #define DELAY_LOAD_ORD_VOID(_hinst, _dll, _fn, _ord, _args, _nargs) 
  106. void __stdcall _fn _args                
  107. {                                       
  108.     static void (* __stdcall _pfn##_fn) _args = NULL;   
  109.     _GetProcFromDLL(&_hinst, #_dll, (FARPROC*)&_pfn##_fn, (LPCSTR)_ord);   
  110.     if (_pfn##_fn)              
  111.         _pfn##_fn _nargs;       
  112.     return;                     
  113. }
  114. /**********************************************************************/
  115. /**********************************************************************/
  116. // --------- MSPWL32.DLL ---------------
  117. HINSTANCE g_hinstMSPWL32 = NULL;
  118. #define DELAY_LOAD_PWL(_fn, _ord, _args, _nargs) DELAY_LOAD_ORD_ERR(g_hinstMSPWL32, mspwl32.dll, APIERR, _fn, _ord, _args, _nargs, IERR_CachingDisabled)
  119. DELAY_LOAD_PWL(OpenPasswordCache, 10,
  120.    (LPHPWL lphCache,const CHAR *pszUsername,const CHAR *pszPassword,BOOL fWritable),
  121.    (lphCache, pszUsername, pszPassword, fWritable));
  122. DELAY_LOAD_PWL(ClosePasswordCache, 11,
  123.    (HPWL hCache, BOOL fDiscardMemory), (hCache, fDiscardMemory));
  124. DELAY_LOAD_PWL(CreatePasswordCache, 12,
  125.    (LPHPWL lphCache,const CHAR *pszUsername,const CHAR *pszPassword),
  126.    (lphCache, pszUsername, pszPassword));
  127. DELAY_LOAD_PWL(DeletePasswordCache, 22,
  128.    (const CHAR *pszUsername), (pszUsername));
  129. DELAY_LOAD_PWL(FindCacheResource, 16,
  130.    (HPWL hCache,const CHAR *pbResource,WORD cbResource,CHAR *pbBuffer,WORD cbBuffer,UCHAR nType),
  131.    (hCache, pbResource, cbResource, pbBuffer, cbBuffer, nType));
  132. DELAY_LOAD_PWL(DeleteCacheResource, 17,
  133.    (HPWL hCache,const CHAR *pbResource,WORD cbResource,UCHAR nType),
  134.    (hCache, pbResource, cbResource, nType));
  135. DELAY_LOAD_PWL(AddCacheResource, 18,
  136.    (HPWL hCache,const CHAR *pbResource,WORD cbResource,const CHAR *pbPassword,WORD cbPassword,UCHAR nType,UINT fnFlags),
  137.    (hCache, pbResource, cbResource, pbPassword, cbPassword, nType, fnFlags));
  138. DELAY_LOAD_PWL(SetCachePassword, 21,
  139.    (HPWL hCache, const CHAR *pszNewPassword),
  140.    (hCache, pszNewPassword));
  141. // --------- MPR.DLL ---------------
  142. HINSTANCE g_hinstMPR = NULL;
  143. DELAY_LOAD_ERR(g_hinstMPR, mpr.dll, DWORD, WNetCachePassword,
  144.                (LPSTR pbResource, WORD cbResource, LPSTR pbPassword, WORD cbPassword, BYTE nType, UINT fnFlags),
  145.                (pbResource, cbResource, pbPassword, cbPassword, nType, fnFlags),
  146.                WN_CANCEL);
  147. DELAY_LOAD_ERR(g_hinstMPR, mpr.dll, DWORD, WNetGetCachedPassword,
  148.                (LPSTR pbResource, WORD cbResource, LPSTR pbPassword, LPWORD pcbPassword, BYTE nType),
  149.                (pbResource, cbResource, pbPassword, pcbPassword, nType),
  150.                WN_CANCEL);
  151. DELAY_LOAD_ERR(g_hinstMPR, mpr.dll, DWORD, WNetRemoveCachedPassword,
  152.                (LPSTR pbResource, WORD cbResource, BYTE nType),
  153.                (pbResource, cbResource, nType),
  154.                WN_CANCEL);
  155. // --------- COMCTL32.DLL ---------------
  156. HINSTANCE g_hinstCOMCTL32 = NULL;
  157. DELAY_LOAD(g_hinstCOMCTL32, comctl32.dll, HPROPSHEETPAGE, CreatePropertySheetPageA,
  158.            (LPCPROPSHEETPAGEA lpPage), (lpPage));
  159. DELAY_LOAD(g_hinstCOMCTL32, comctl32.dll, int, PropertySheetA,
  160.            (LPCPROPSHEETHEADERA lpHdr), (lpHdr));
  161. // --------- SHELL32.DLL ----------------
  162. HINSTANCE g_hinstSHELL32 = NULL;
  163. #ifdef UNICODE
  164. DELAY_LOAD(g_hinstSHELL32, SHELL32.DLL, BOOL, ShellExecuteExW,
  165. (LPSHELLEXECUTEINFOW lpExecInfo), (lpExecInfo));
  166. #else
  167. DELAY_LOAD(g_hinstSHELL32, SHELL32.DLL, BOOL, ShellExecuteExA,
  168. (LPSHELLEXECUTEINFOA lpExecInfo), (lpExecInfo));
  169. #endif
  170. DELAY_LOAD_ORD_VOID(g_hinstSHELL32, SHELL32.DLL, SHFlushSFCache, 526, (), ());
  171. #pragma warning(default:4229)