Leaked source code of windows server 2003
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.

123 lines
2.8 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2001 - 2003
  4. *
  5. * TITLE: fusutils.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * DATE: 14-Feb-2001
  10. *
  11. * DESCRIPTION: Fusion utilities
  12. *
  13. *****************************************************************************/
  14. #pragma once
  15. #include "precomp.h"
  16. #include "globals.h"
  17. #include "fusutils.h"
  18. // StrSafe.h needs to be included last
  19. // to disallow bad string functions.
  20. #include <STRSAFE.H>
  21. #define MAX_LOOP 10
  22. HANDLE GetMyActivationContext()
  23. {
  24. // Make sure we've created our activation context.
  25. CreateMyActivationContext();
  26. // Return the global.
  27. return ghActCtx;
  28. }
  29. BOOL CreateMyActivationContext()
  30. {
  31. if(INVALID_HANDLE_VALUE != ghActCtx)
  32. {
  33. return TRUE;
  34. }
  35. ghActCtx = CreateActivationContextFromResource(ghInstance, MAKEINTRESOURCE(MANIFEST_RESOURCE));
  36. return (INVALID_HANDLE_VALUE != ghActCtx);
  37. }
  38. HANDLE CreateActivationContextFromResource(HMODULE hModule, LPCTSTR pszResourceName)
  39. {
  40. DWORD dwSize = MAX_PATH;
  41. DWORD dwUsed = 0;
  42. DWORD dwLoop;
  43. PTSTR pszModuleName = NULL;
  44. ACTCTX act;
  45. HANDLE hActCtx = INVALID_HANDLE_VALUE;
  46. // Get the name for the module that contains the manifest resource
  47. // to create the Activation Context from.
  48. dwLoop = 0;
  49. do
  50. {
  51. // May need to allocate or re-alloc buffer for module name.
  52. if(NULL != pszModuleName)
  53. {
  54. // Need to re-alloc bigger buffer.
  55. // First, delete old buffer.
  56. delete[] pszModuleName;
  57. // Second, increase buffer alloc size.
  58. dwSize <<= 1;
  59. }
  60. pszModuleName = new TCHAR[dwSize];
  61. if(NULL == pszModuleName)
  62. {
  63. goto Exit;
  64. }
  65. // Try to get the module name.
  66. dwUsed = GetModuleFileName(hModule, pszModuleName, dwSize);
  67. // Check to see if it failed.
  68. if(0 == dwUsed)
  69. {
  70. goto Exit;
  71. }
  72. // If dwUsed is equla to dwSize or larger,
  73. // the buffer passed in wasn't big enough.
  74. } while ( (dwUsed >= dwSize) && (++dwLoop < MAX_LOOP) );
  75. // Now let's try to create an activation context
  76. // from manifest resource.
  77. ::ZeroMemory(&act, sizeof(act));
  78. act.cbSize = sizeof(act);
  79. act.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;
  80. act.lpResourceName = pszResourceName;
  81. act.lpSource = pszModuleName;
  82. hActCtx = CreateActCtx(&act);
  83. Exit:
  84. //
  85. // Clean up.
  86. //
  87. if(NULL != pszModuleName)
  88. {
  89. delete[] pszModuleName;
  90. }
  91. return hActCtx;
  92. }