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.

196 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1990-1994 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. netid.c
  6. Abstract:
  7. Author:
  8. Environment:
  9. User Mode -Win32
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #define MAXNETIDS 100
  15. extern WCHAR *szRegDevicesPath;
  16. extern WCHAR *szDotDefault;
  17. DWORD
  18. GetNetworkIdWorker(
  19. HKEY hKeyDevices,
  20. LPWSTR pDeviceName);
  21. LONG
  22. wtol(
  23. IN LPWSTR string
  24. )
  25. {
  26. LONG value = 0;
  27. while((*string != L'\0') &&
  28. (*string >= L'0') &&
  29. ( *string <= L'9')) {
  30. value = value * 10 + (*string - L'0');
  31. string++;
  32. }
  33. return(value);
  34. }
  35. DWORD
  36. GetNextNetId(
  37. DWORD pNetTable[]
  38. )
  39. {
  40. DWORD i;
  41. for (i = 0; i < MAXNETIDS; i++) {
  42. if (!ISBITON(pNetTable, i)) {
  43. return(i);
  44. }
  45. }
  46. return((DWORD)-1);
  47. }
  48. DWORD
  49. GetNetworkId(
  50. HKEY hKeyUser,
  51. LPWSTR pDeviceName)
  52. {
  53. HKEY hKeyUserDevices;
  54. DWORD dwNetId;
  55. if (RegOpenKeyEx(hKeyUser,
  56. szRegDevicesPath,
  57. 0,
  58. KEY_READ,
  59. &hKeyUserDevices) != ERROR_SUCCESS) {
  60. return 0;
  61. }
  62. dwNetId = GetNetworkIdWorker(hKeyUserDevices,
  63. pDeviceName);
  64. RegCloseKey(hKeyUserDevices);
  65. return dwNetId;
  66. }
  67. DWORD
  68. GetNetworkIdWorker(
  69. HKEY hKeyUserDevices,
  70. LPWSTR pDeviceName)
  71. /*++
  72. Parses the Devices section of Win.ini to determine if the
  73. printer device is a remote device. We determine this by checking
  74. if the first two characters are "Ne." If they are then we know
  75. that the next two characters are the NetId. If we find a Printer
  76. device mapping the input pDeviceName, then return the id. If we
  77. don't find a Printer device mapping return the next possible /
  78. available id.
  79. --*/
  80. {
  81. LPWSTR p;
  82. WCHAR szData[MAX_PATH];
  83. WCHAR szValue[MAX_PATH];
  84. DWORD cchValue;
  85. DWORD cbData;
  86. DWORD i;
  87. DWORD dwId;
  88. DWORD dwError;
  89. //
  90. // Alloc 104 bits - but we'll use only 100 bits
  91. //
  92. DWORD adwNetTable[4];
  93. memset(adwNetTable, 0, sizeof(adwNetTable));
  94. for (i=0; TRUE; i++) {
  95. cchValue = COUNTOF(szValue);
  96. cbData = sizeof(szData);
  97. dwError = RegEnumValue(hKeyUserDevices,
  98. i,
  99. szValue,
  100. &cchValue,
  101. NULL,
  102. NULL,
  103. (PBYTE)szData,
  104. &cbData);
  105. if (dwError != ERROR_SUCCESS)
  106. break;
  107. if (*szData) {
  108. if (p = wcschr(szData, L',')) {
  109. //
  110. // null set szOutput; szPrinter is now the
  111. // the name of our printer.
  112. //
  113. *p = 0;
  114. //
  115. // Get the Port name out of szOutput
  116. //
  117. p++;
  118. while (*p == ' ') {
  119. p++;
  120. }
  121. if (!_wcsnicmp(p, L"Ne", 2)) {
  122. p += 2;
  123. *(p+2) = L'\0';
  124. dwId = wtol(p);
  125. //
  126. // if we have a match for the id, then
  127. // use it and return, no need to generate
  128. // a table
  129. //
  130. if (!wcscmp(szValue, pDeviceName)) {
  131. return dwId;
  132. }
  133. //
  134. // Error if >= 100!
  135. //
  136. if (dwId < 100)
  137. MARKUSE(adwNetTable, dwId);
  138. }
  139. }
  140. }
  141. }
  142. //
  143. // So we didn't find the printer
  144. //
  145. return GetNextNetId(adwNetTable);
  146. }