PinvokeWindowsNetworking.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. namespace CLIP.eForm.Consent.Web
  8. {
  9. public class PinvokeWindowsNetworking
  10. {
  11. #region Consts
  12. const int RESOURCE_CONNECTED = 0x00000001;
  13. const int RESOURCE_GLOBALNET = 0x00000002;
  14. const int RESOURCE_REMEMBERED = 0x00000003;
  15. const int RESOURCETYPE_ANY = 0x00000000;
  16. const int RESOURCETYPE_DISK = 0x00000001;
  17. const int RESOURCETYPE_PRINT = 0x00000002;
  18. const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;
  19. const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
  20. const int RESOURCEDISPLAYTYPE_SERVER = 0x00000002;
  21. const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
  22. const int RESOURCEDISPLAYTYPE_FILE = 0x00000004;
  23. const int RESOURCEDISPLAYTYPE_GROUP = 0x00000005;
  24. const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
  25. const int RESOURCEUSAGE_CONTAINER = 0x00000002;
  26. const int CONNECT_INTERACTIVE = 0x00000008;
  27. const int CONNECT_PROMPT = 0x00000010;
  28. const int CONNECT_REDIRECT = 0x00000080;
  29. const int CONNECT_UPDATE_PROFILE = 0x00000001;
  30. const int CONNECT_COMMANDLINE = 0x00000800;
  31. const int CONNECT_CMD_SAVECRED = 0x00001000;
  32. const int CONNECT_LOCALDRIVE = 0x00000100;
  33. #endregion
  34. #region Errors
  35. const int NO_ERROR = 0;
  36. const int ERROR_ACCESS_DENIED = 5;
  37. const int ERROR_ALREADY_ASSIGNED = 85;
  38. const int ERROR_BAD_DEVICE = 1200;
  39. const int ERROR_BAD_NET_NAME = 67;
  40. const int ERROR_BAD_PROVIDER = 1204;
  41. const int ERROR_CANCELLED = 1223;
  42. const int ERROR_EXTENDED_ERROR = 1208;
  43. const int ERROR_INVALID_ADDRESS = 487;
  44. const int ERROR_INVALID_PARAMETER = 87;
  45. const int ERROR_INVALID_PASSWORD = 1216;
  46. const int ERROR_MORE_DATA = 234;
  47. const int ERROR_NO_MORE_ITEMS = 259;
  48. const int ERROR_NO_NET_OR_BAD_PATH = 1203;
  49. const int ERROR_NO_NETWORK = 1222;
  50. const int ERROR_BAD_PROFILE = 1206;
  51. const int ERROR_CANNOT_OPEN_PROFILE = 1205;
  52. const int ERROR_DEVICE_IN_USE = 2404;
  53. const int ERROR_NOT_CONNECTED = 2250;
  54. const int ERROR_OPEN_FILES = 2401;
  55. private struct ErrorClass
  56. {
  57. public int num;
  58. public string message;
  59. public ErrorClass(int num, string message)
  60. {
  61. this.num = num;
  62. this.message = message;
  63. }
  64. }
  65. // Created with excel formula:
  66. // ="new ErrorClass("&A1&", """&PROPER(SUBSTITUTE(MID(A1,7,LEN(A1)-6), "_", " "))&"""), "
  67. private static ErrorClass[] ERROR_LIST = new ErrorClass[] {
  68. new ErrorClass(ERROR_ACCESS_DENIED, "Error: Access Denied"),
  69. new ErrorClass(ERROR_ALREADY_ASSIGNED, "Error: Already Assigned"),
  70. new ErrorClass(ERROR_BAD_DEVICE, "Error: Bad Device"),
  71. new ErrorClass(ERROR_BAD_NET_NAME, "Error: Bad Net Name"),
  72. new ErrorClass(ERROR_BAD_PROVIDER, "Error: Bad Provider"),
  73. new ErrorClass(ERROR_CANCELLED, "Error: Cancelled"),
  74. new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"),
  75. new ErrorClass(ERROR_INVALID_ADDRESS, "Error: Invalid Address"),
  76. new ErrorClass(ERROR_INVALID_PARAMETER, "Error: Invalid Parameter"),
  77. new ErrorClass(ERROR_INVALID_PASSWORD, "Error: Invalid Password"),
  78. new ErrorClass(ERROR_MORE_DATA, "Error: More Data"),
  79. new ErrorClass(ERROR_NO_MORE_ITEMS, "Error: No More Items"),
  80. new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "Error: No Net Or Bad Path"),
  81. new ErrorClass(ERROR_NO_NETWORK, "Error: No Network"),
  82. new ErrorClass(ERROR_BAD_PROFILE, "Error: Bad Profile"),
  83. new ErrorClass(ERROR_CANNOT_OPEN_PROFILE, "Error: Cannot Open Profile"),
  84. new ErrorClass(ERROR_DEVICE_IN_USE, "Error: Device In Use"),
  85. new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"),
  86. new ErrorClass(ERROR_NOT_CONNECTED, "Error: Not Connected"),
  87. new ErrorClass(ERROR_OPEN_FILES, "Error: Open Files"),
  88. };
  89. private static string getErrorForNumber(int errNum)
  90. {
  91. foreach (ErrorClass er in ERROR_LIST)
  92. {
  93. if (er.num == errNum) return er.message;
  94. }
  95. return "Error: Unknown, " + errNum;
  96. }
  97. #endregion
  98. [DllImport("Mpr.dll")]
  99. private static extern int WNetUseConnection(
  100. IntPtr hwndOwner,
  101. NETRESOURCE lpNetResource,
  102. string lpPassword,
  103. string lpUserID,
  104. int dwFlags,
  105. string lpAccessName,
  106. string lpBufferSize,
  107. string lpResult
  108. );
  109. [DllImport("Mpr.dll")]
  110. private static extern int WNetCancelConnection2(
  111. string lpName,
  112. int dwFlags,
  113. bool fForce
  114. );
  115. [StructLayout(LayoutKind.Sequential)]
  116. private class NETRESOURCE
  117. {
  118. public int dwScope = 0;
  119. public int dwType = 0;
  120. public int dwDisplayType = 0;
  121. public int dwUsage = 0;
  122. public string lpLocalName = "";
  123. public string lpRemoteName = "";
  124. public string lpComment = "";
  125. public string lpProvider = "";
  126. }
  127. public static string connectToRemote(string remoteUNC, string username, string password)
  128. {
  129. return connectToRemote(remoteUNC, username, password, false);
  130. }
  131. public static string connectToRemote(string remoteUNC, string username, string password, bool promptUser)
  132. {
  133. NETRESOURCE nr = new NETRESOURCE();
  134. nr.dwType = RESOURCETYPE_DISK;
  135. nr.lpRemoteName = remoteUNC;
  136. // nr.lpLocalName = "F:";
  137. int ret;
  138. if (promptUser)
  139. ret = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
  140. else
  141. ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
  142. if (ret == NO_ERROR) return null;
  143. return getErrorForNumber(ret);
  144. }
  145. public static string disconnectRemote(string remoteUNC)
  146. {
  147. int ret = WNetCancelConnection2(remoteUNC, CONNECT_UPDATE_PROFILE, false);
  148. if (ret == NO_ERROR) return null;
  149. return getErrorForNumber(ret);
  150. }
  151. }
  152. }