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.

180 lines
5.3 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: instance.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * This module handles conversion of instance handles (server side handles)
  7. * to instance indecies used by the handle manager for associating a handle
  8. * with a particular instance.
  9. *
  10. * History:
  11. * 11-5-91 Sanfords Created
  12. \***************************************************************************/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #define INST_GROW_COUNT 4
  16. // globals
  17. PHANDLE aInstance = NULL;
  18. int cInstAllocated = 0;
  19. int iFirstFreeInst = 0;
  20. /***************************************************************************\
  21. * AddInstance
  22. *
  23. * Description:
  24. * Adds a server side instance handle to the instance handle array.
  25. * The array index becomes the client-side unique instance index used for
  26. * identifying other client side handles.
  27. *
  28. * Returns:
  29. * client side instance handle or 0 on error.
  30. *
  31. * History:
  32. * 11-1-91 sanfords Created.
  33. \***************************************************************************/
  34. HANDLE AddInstance(
  35. HANDLE hInstServer)
  36. {
  37. int i, iNextFree;
  38. PHANDLE ph, phTemp;
  39. if (iFirstFreeInst >= cInstAllocated) {
  40. if (cInstAllocated == 0) {
  41. aInstance = (PHANDLE)DDEMLAlloc(sizeof(HANDLE) * INST_GROW_COUNT);
  42. } else {
  43. /*
  44. * If the realloc failed, free the old ptr. We continue
  45. * on in order to maintain compatibility with previous DDE code.
  46. */
  47. phTemp = (PHANDLE)DDEMLReAlloc((PVOID)aInstance,
  48. sizeof(HANDLE) * (cInstAllocated + INST_GROW_COUNT));
  49. if (phTemp == NULL) {
  50. DDEMLFree(aInstance);
  51. }
  52. aInstance = phTemp;
  53. }
  54. if (aInstance == 0) {
  55. return (0);
  56. }
  57. ph = &aInstance[cInstAllocated];
  58. i = cInstAllocated + 1;
  59. while (i <= cInstAllocated + INST_GROW_COUNT) {
  60. *ph++ = (HANDLE)(UINT_PTR)(UINT)i++;
  61. }
  62. cInstAllocated += INST_GROW_COUNT;
  63. }
  64. iNextFree = HandleToUlong(aInstance[iFirstFreeInst]);
  65. if (iNextFree > MAX_INST) {
  66. /*
  67. * Instance limit for this process exceeded!
  68. */
  69. return(0);
  70. }
  71. aInstance[iFirstFreeInst] = hInstServer;
  72. i = iFirstFreeInst;
  73. iFirstFreeInst = iNextFree;
  74. return (CreateHandle(0, HTYPE_INSTANCE, i));
  75. }
  76. /***************************************************************************\
  77. * DestroyInstance
  78. *
  79. * Description:
  80. * Removes an instance from the aInstance table. This does nothing for
  81. * the server side instance info.
  82. *
  83. * History:
  84. * 11-19-91 sanfords Created.
  85. \***************************************************************************/
  86. HANDLE DestroyInstance(
  87. HANDLE hInstClient)
  88. {
  89. register HANDLE hInstServerRet = 0;
  90. DestroyHandle(hInstClient);
  91. hInstServerRet = aInstance[InstFromHandle(hInstClient)];
  92. aInstance[InstFromHandle(hInstClient)] = (HANDLE)UIntToPtr( iFirstFreeInst );
  93. iFirstFreeInst = InstFromHandle(hInstClient);
  94. return (hInstServerRet);
  95. }
  96. /***************************************************************************\
  97. * ValidateInstance
  98. *
  99. * Description:
  100. * Verifies the current validity of an instance handle - which is a server
  101. * side handle that also references a client side data structure (pcii).
  102. *
  103. * History:
  104. * 11-19-91 sanfords Created.
  105. \***************************************************************************/
  106. PCL_INSTANCE_INFO ValidateInstance(
  107. HANDLE hInstClient)
  108. {
  109. PCL_INSTANCE_INFO pcii;
  110. pcii = (PCL_INSTANCE_INFO)ValidateCHandle(hInstClient, HTYPE_INSTANCE, HINST_ANY);
  111. if (pcii != NULL) {
  112. if (pcii->tid != GetCurrentThreadId() ||
  113. pcii->hInstClient != hInstClient) {
  114. return (NULL);
  115. }
  116. }
  117. return (pcii);
  118. }
  119. /***************************************************************************\
  120. * SetLastDDEMLError
  121. *
  122. * Description:
  123. * Sets last error value and generates monitor events if monitoring.
  124. *
  125. * History:
  126. * 11-19-91 sanfords Created.
  127. \***************************************************************************/
  128. VOID SetLastDDEMLError(
  129. PCL_INSTANCE_INFO pcii,
  130. DWORD error)
  131. {
  132. PEVENT_PACKET pep;
  133. if (pcii->MonitorFlags & MF_ERRORS && !(pcii->afCmd & APPCLASS_MONITOR)) {
  134. pep = (PEVENT_PACKET)DDEMLAlloc(sizeof(EVENT_PACKET) - sizeof(DWORD) +
  135. sizeof(MONERRSTRUCT));
  136. if (pep != NULL) {
  137. pep->EventType = MF_ERRORS;
  138. pep->fSense = TRUE;
  139. pep->cbEventData = sizeof(MONERRSTRUCT);
  140. #define perrs ((MONERRSTRUCT *)&pep->Data)
  141. perrs->cb = sizeof(MONERRSTRUCT);
  142. perrs->wLastError = (WORD)error;
  143. perrs->dwTime = NtGetTickCount();
  144. perrs->hTask = (HANDLE)LongToHandle( pcii->tid );
  145. #undef perrs
  146. LeaveDDECrit;
  147. Event(pep);
  148. EnterDDECrit;
  149. }
  150. }
  151. #if DBG
  152. if (error != 0 && error != DMLERR_NO_CONV_ESTABLISHED) {
  153. RIPMSG3(RIP_WARNING,
  154. "DDEML Error set=%x, Client Instance=%p, Process=%x.",
  155. error, pcii, GetCurrentProcessId());
  156. }
  157. #endif
  158. pcii->LastError = error;
  159. }