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.

277 lines
6.1 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. sample\utils.c
  5. Abstract:
  6. The file contains utility functions
  7. --*/
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. BOOL
  11. IsProtocolInstalled(
  12. IN DWORD dwProtocolId,
  13. IN DWORD dwNameId,
  14. IN DWORD dwLogUninstalled
  15. )
  16. /*++
  17. Routine Description:
  18. Finds if the protocol is already installed.
  19. Arguments:
  20. dwProtocolId - protocol id
  21. dwNameId - protocol name
  22. dwLogUninstalled - TRUE(if not installed display error)
  23. FALSE(if installed display error)
  24. Return Value:
  25. TRUE if protocol already installed, else FALSE
  26. --*/
  27. {
  28. DWORD dwErr = NO_ERROR;
  29. WCHAR *pwszName = NULL;
  30. dwErr = IpmontrGetInfoBlockFromGlobalInfo(dwProtocolId,
  31. NULL,
  32. NULL,
  33. NULL);
  34. pwszName = MakeString(g_hModule, dwNameId);
  35. if ((dwErr isnot NO_ERROR) and (dwLogUninstalled is TRUE))
  36. {
  37. DisplayError(g_hModule, EMSG_PROTO_NOT_INSTALLED, pwszName);
  38. }
  39. else if ((dwErr is NO_ERROR) and (dwLogUninstalled is FALSE))
  40. {
  41. DisplayError(g_hModule, EMSG_PROTO_INSTALLED, pwszName);
  42. }
  43. if (pwszName) FreeString(pwszName);
  44. return (dwErr is NO_ERROR) ? TRUE : FALSE;
  45. }
  46. DWORD
  47. GetIfIndex(
  48. IN HANDLE hMibServer,
  49. IN PWCHAR pwszArgument,
  50. OUT PDWORD pdwIfIndex
  51. )
  52. /*++
  53. Routine Description:
  54. Gets the interface index.
  55. Arguments:
  56. hMibServer - handle to the mib server
  57. pwszArgument - argument specifing interface index or name
  58. pdwIfIndex - interface index
  59. Return Value:
  60. NO_ERROR success
  61. error code o/w
  62. --*/
  63. {
  64. DWORD dwErr = NO_ERROR;
  65. // if index was specified just use it
  66. if (iswdigit(pwszArgument[0]))
  67. {
  68. *pdwIfIndex = wcstoul(pwszArgument, NULL, 10);
  69. return NO_ERROR;
  70. }
  71. // try converting a friendly name to an interface index
  72. dwErr = InterfaceIndexFromName(hMibServer,
  73. pwszArgument,
  74. pdwIfIndex);
  75. return (dwErr is NO_ERROR) ? dwErr : ERROR_INVALID_PARAMETER;
  76. }
  77. DWORD
  78. MibGet(
  79. IN HANDLE hMibServer,
  80. IN MODE mMode,
  81. IN PVOID pvIn,
  82. IN DWORD dwInSize,
  83. OUT PVOID *ppvOut
  84. )
  85. /*++
  86. Routine Description:
  87. Gets the specified mib object.
  88. Arguments:
  89. hMibServer - handle to the mib server
  90. mMode - mode of access (exact, first, next)
  91. pvIn - buffer containing input data
  92. dwInSize - size of input data
  93. ppvOut - pointer to address of output data buffer
  94. Return Value:
  95. NO_ERROR success
  96. error code o/w
  97. --*/
  98. {
  99. DWORD dwErr = NO_ERROR;
  100. DWORD dwOutSize = 0;
  101. DWORD (APIENTRY *pfnMprGet) (
  102. IN MIB_SERVER_HANDLE hMibServer,
  103. IN DWORD dwProtocolId,
  104. IN DWORD dwRoutingPid,
  105. IN LPVOID lpInEntry,
  106. IN DWORD dwInEntrySize,
  107. OUT LPVOID* lplpOutEntry,
  108. OUT LPDWORD lpOutEntrySize
  109. );
  110. *ppvOut = NULL;
  111. switch(mMode)
  112. {
  113. case GET_EXACT:
  114. pfnMprGet = MprAdminMIBEntryGet;
  115. break;
  116. case GET_FIRST:
  117. pfnMprGet = MprAdminMIBEntryGetFirst;
  118. break;
  119. case GET_NEXT:
  120. pfnMprGet = MprAdminMIBEntryGetNext;
  121. break;
  122. }
  123. dwErr = (*pfnMprGet) (
  124. hMibServer,
  125. PID_IP,
  126. MS_IP_SAMPLE,
  127. (LPVOID) pvIn,
  128. dwInSize,
  129. (LPVOID *) ppvOut,
  130. &dwOutSize);
  131. if (dwErr isnot NO_ERROR)
  132. return dwErr;
  133. if (*ppvOut is NULL)
  134. return ERROR_CAN_NOT_COMPLETE;
  135. return NO_ERROR;
  136. }
  137. DWORD
  138. GetDumpString (
  139. IN HANDLE hModule,
  140. IN DWORD dwValue,
  141. IN PVALUE_TOKEN ptvTable,
  142. IN DWORD dwNumArgs,
  143. OUT PWCHAR *pwszString
  144. )
  145. /*
  146. * Do not localize display string
  147. */
  148. {
  149. DWORD dwErr = NO_ERROR ;
  150. ULONG i;
  151. for (i = 0; i < dwNumArgs; i++)
  152. {
  153. if (dwValue is ptvTable[i].dwValue)
  154. {
  155. *pwszString = MALLOC((wcslen(ptvTable[i].pwszToken) + 1) *
  156. sizeof(WCHAR));
  157. if (*pwszString)
  158. wcscpy(*pwszString, ptvTable[i].pwszToken);
  159. break;
  160. }
  161. }
  162. if (i is dwNumArgs)
  163. *pwszString = MakeString(hModule, STRING_UNKNOWN) ;
  164. if (!pwszString)
  165. dwErr = ERROR_NOT_ENOUGH_MEMORY ;
  166. return dwErr ;
  167. }
  168. DWORD
  169. GetShowString (
  170. IN HANDLE hModule,
  171. IN DWORD dwValue,
  172. IN PVALUE_STRING ptvTable,
  173. IN DWORD dwNumArgs,
  174. OUT PWCHAR *pwszString
  175. )
  176. /*
  177. * Localize display string
  178. */
  179. {
  180. DWORD dwErr = NO_ERROR ;
  181. ULONG i;
  182. for (i = 0; i < dwNumArgs; i++)
  183. {
  184. if (dwValue is ptvTable[i].dwValue)
  185. {
  186. *pwszString = MakeString(hModule, ptvTable[i].dwStringId) ;
  187. break;
  188. }
  189. }
  190. if (i is dwNumArgs)
  191. *pwszString = MakeString(hModule, STRING_UNKNOWN) ;
  192. if (!pwszString)
  193. dwErr = ERROR_NOT_ENOUGH_MEMORY ;
  194. return dwErr ;
  195. }
  196. DWORD
  197. GetString (
  198. IN HANDLE hModule,
  199. IN FORMAT fFormat,
  200. IN DWORD dwValue,
  201. IN PVALUE_TOKEN vtTable,
  202. IN PVALUE_STRING vsTable,
  203. IN DWORD dwNumArgs,
  204. OUT PWCHAR *pwszString)
  205. {
  206. if (fFormat is FORMAT_DUMP)
  207. {
  208. return GetDumpString(hModule,
  209. dwValue,
  210. vtTable,
  211. dwNumArgs,
  212. pwszString) ;
  213. }
  214. else
  215. {
  216. return GetShowString(hModule,
  217. dwValue,
  218. vsTable,
  219. dwNumArgs,
  220. pwszString) ;
  221. }
  222. }