Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: linkdll.cpp
  4. //
  5. // Module: Common Code
  6. //
  7. // Synopsis: Implementation of linkage functions LinkToDll and BindLinkage
  8. //
  9. // Copyright (c) 1999 Microsoft Corporation
  10. //
  11. // Author: quintinb created header 08/19/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. //+----------------------------------------------------------------------------
  15. //
  16. // Function: LinkToDll
  17. //
  18. // Synopsis: Helper function to manage the process of linking to a DLL and
  19. // settings up a function table for later use.
  20. //
  21. // Arguments: HINSTANCE *phInst - A ptr to an hInst to be filled with the hInst of the DLL to be linked.
  22. // LPCTSTR pszDll - Ptr to the name of the DLL to be linked.
  23. // LPCSTR *ppszPfn - Ptr to a table of function names to be retrieved.
  24. // void **ppvPfn - Ptr to table for storage of pointers to DLL functions used.
  25. //
  26. // Returns: BOOL - TRUE if fully loaded and linked.
  27. //
  28. // History: nickball Created Header 1/5/98
  29. //
  30. //+----------------------------------------------------------------------------
  31. BOOL LinkToDll(HINSTANCE *phInst, LPCSTR pszDll, LPCSTR *ppszPfn, void **ppvPfn)
  32. {
  33. MYDBGASSERT(phInst);
  34. MYDBGASSERT(pszDll);
  35. MYDBGASSERT(ppszPfn);
  36. MYDBGASSERT(ppvPfn);
  37. CMTRACE1A("LinkToDll - Loading library - %s", pszDll);
  38. *phInst = LoadLibraryExA(pszDll, NULL, 0);
  39. if (!*phInst)
  40. {
  41. CMTRACE3A("LinkToDll[phInst=%p, *pszDll=%s, ppszPfn=%p,", phInst, MYDBGSTRA(pszDll), ppszPfn);
  42. CMTRACE1A("\tppvPfn=%p] LoadLibrary() failed.", ppvPfn);
  43. return FALSE;
  44. }
  45. //
  46. // Link succeeded now setup function addresses
  47. //
  48. return BindLinkage(*phInst, ppszPfn, ppvPfn);
  49. }
  50. //+----------------------------------------------------------------------------
  51. //
  52. // Function: BindLinkage
  53. //
  54. // Synopsis: Helper function to fill in the given function pointer table with
  55. // the addresses of the functions specified in the given string table.
  56. // Function addresses are retrieved from the DLL specified by hInst.
  57. //
  58. // Arguments: HINSTANCE hInstDll - The hInst of the DLL.
  59. // LPCSTR *ppszPfn - Ptr to a table of function names.
  60. // void **ppvPfn - Ptr to a table of function pointers to be filled in.
  61. //
  62. // Returns: BOOL - TRUE if all addresses were successfully retrieved.
  63. //
  64. // History: nickball Created 1/5/98
  65. //
  66. //+----------------------------------------------------------------------------
  67. BOOL BindLinkage(HINSTANCE hInstDll, LPCSTR *ppszPfn, void **ppvPfn)
  68. {
  69. MYDBGASSERT(ppszPfn);
  70. MYDBGASSERT(ppvPfn);
  71. UINT nIdxPfn;
  72. BOOL bAllLoaded = TRUE;
  73. for (nIdxPfn=0;ppszPfn[nIdxPfn];nIdxPfn++)
  74. {
  75. if (!ppvPfn[nIdxPfn])
  76. {
  77. ppvPfn[nIdxPfn] = GetProcAddress(hInstDll, ppszPfn[nIdxPfn]);
  78. if (!ppvPfn[nIdxPfn])
  79. {
  80. CMTRACE3(TEXT("BindLinkage(hInstDll=%d,ppszPfn=%p,ppvPfn=%p)"), hInstDll, ppszPfn, ppvPfn);
  81. CMTRACE3(TEXT("\tGetProcAddress(hInstDll=%d,*pszProc=%S) failed, GLE=%u."), hInstDll, ppszPfn[nIdxPfn], GetLastError());
  82. bAllLoaded = FALSE;
  83. }
  84. }
  85. }
  86. return (bAllLoaded);
  87. }