Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.03 KB | None | 0 0
  1. Attribute VB_Name = "modIP"
  2. Option Explicit
  3.  
  4. Public Const MAX_PREFERRED_LENGTH As Long = -1
  5. Public Const NERR_SUCCESS As Long = 0&
  6. Public Const ERROR_MORE_DATA As Long = 234&
  7.  
  8. Public Const SV_TYPE_ALL As Long = &HFFFFFFFF
  9. Public Const SV_TYPE_WORKSTATION As Long = &H1
  10. Public Const SV_TYPE_SERVER As Long = &H2
  11.  
  12. Public Const STYPE_DISKTREE As Long = 0
  13. Public Const STYPE_PRINTQ As Long = 1
  14. Public Const STYPE_DEVICE As Long = 2
  15. Public Const STYPE_IPC As Long = 3
  16. Public Const STYPE_SPECIAL As Long = &H80000000
  17. Public Const STYPE_TEMPORARY As Long = &H40000000
  18. Public Const ACCESS_READ As Long = &H1
  19. Public Const ACCESS_WRITE As Long = &H2
  20. Public Const ACCESS_CREATE As Long = &H4
  21. Public Const ACCESS_EXEC As Long = &H8
  22. Public Const ACCESS_DELETE As Long = &H10
  23. Public Const ACCESS_ATRIB As Long = &H20
  24. Public Const ACCESS_PERM As Long = &H40
  25. Public Const ACCESS_ALL As Long = ACCESS_READ Or ACCESS_WRITE Or ACCESS_CREATE Or ACCESS_EXEC Or ACCESS_DELETE Or ACCESS_ATRIB Or ACCESS_PERM
  26.  
  27. Public Const PERM_FILE_READ = &H1 'user has read access
  28. Public Const PERM_FILE_WRITE = &H2 'user has write access
  29. Public Const PERM_FILE_CREATE = &H4 'user has create access
  30.  
  31. Public Const WSADESCRIPTION_LEN = 256
  32. Public Const WSASYS_STATUS_LEN = 128
  33.  
  34.  
  35. 'ネットワーク接続定数
  36. Public Const RESOURCETYPE_DISK = &H1 'ディスク
  37. Public Const CONNECT_UPDATE_PROFILE = &H1 '次回ログオン時に再接続
  38. Public Const RESOURCE_CONNECTED = &H1
  39. Public Const RESOURCETYPE_ANY = &H0
  40. Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
  41.  
  42.  
  43. 'ネットワークエラー定数
  44. Public Const ERROR_SUCCESS = 0 '正常終了
  45. Public Const ERROR_BAD_NETPATH = 53 'ネットワークパスが不正
  46. Public Const ERROR_ACCESS_DENIED = 8 'ネットワーク資源へのアクセスが拒否されました。
  47. Public Const ERROR_ALREADY_ASSIGNED = 85 'lpLocalName で指定したローカルデバイスは既にネットワーク資源に接続されています。
  48. Public Const ERROR_BAD_DEV_TYPE = 66 'ローカルデバイスの種類とネットワーク資源の種類が一致しません。
  49. Public Const ERROR_BAD_DEVICE = 1200 'lpLocalName で指定した値が無効です。
  50. Public Const ERROR_BAD_NET_NAME = 67 'lpRemoteName で指定した値を、どのネットワーク資源のプロバイダも受け付けません。資源の名前が無効か、指定した資源が見つかりません。
  51. Public Const ERROR_BAD_PROFILE = 1206 'ユーザープロファイルの形式が正しくありません。
  52. Public Const ERROR_BAD_PROVIDER = 1204 'lpProvider で指定した値がどのプロバイダとも一致しません。
  53. Public Const ERROR_BUSY = 170 'ルーターまたはプロバイダがビジー( おそらく初期化中)です。この関数をもう一度呼び出してください。
  54. Public Const ERROR_CANCELLED = 1223 'ネットワーク資源のプロバイダのいずれかでユーザーがダイアログボックスを使って接続操作を取り消したか、接続先の資源が接続操作を取り消しました。
  55. Public Const ERROR_CANNOT_OPEN_PROFILE = 1205 '恒久的な接続を処理するためのユーザープロファイルを開くことができません。
  56. Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202 'lpLocalName で指定したデバイスのエントリは既にユーザープロファイル内に存在します。
  57. Public Const ERROR_EXTENDED_ERROR = 1208 'ネットワーク固有のエラーが発生しました。エラーの説明を取得するには、WNetGetLastError 関数を使います。
  58. Public Const ERROR_INVALID_PASSWORD = 86 '指定したパスワードが無効です。
  59. Public Const ERROR_NO_NET_OR_BAD_PATH = 1203 'ネットワークコンポーネントが開始されていないか、指定した名前が利用できないために、操作を行えませんでした。
  60. Public Const ERROR_NO_NETWORK = 1222 'ネットワークに接続されていません。
  61. Public Const ERROR_DEVICE_IN_USE = 2404 '指定したデバイスがアクティブなプロセスによって使用中のため、切断できません。
  62. Public Const ERROR_NOT_CONNECTED = 2250 'lpName パラメータで指定した名前がリダイレクトされているデバイスを表していないか、lpName で指定したデバイスにシステムが接続していません。
  63. Public Const ERROR_OPEN_FILES = 2401 '開いているファイルがあり、fForce が FALSE です。
  64.  
  65.  
  66.  
  67. 'コンピュータ名タイプを指定する列挙型の宣言
  68. Public Enum COMPUTER_NAME_FORMAT
  69. ComputerNameNetBIOS 'NetBIOS名
  70. ComputerNameDnsHostname 'DNSホスト名
  71. ComputerNameDnsDomain 'DNSドメイン名
  72. ComputerNameDnsFullyQualified '完全修飾DNS名
  73. ComputerNamePhysicalNetBIOS '物理的なNetBIOS名
  74. ComputerNamePhysicalDnsHostname '物理的なDNSホスト名
  75. ComputerNamePhysicalDnsDomain '物理的なDNSドメイン名
  76. ComputerNamePhysicalDnsFullyQualified '物理的な完全修飾DNS名
  77. ComputerNameMax '未使用
  78. End Enum
  79.  
  80. Public Type NETRESOURCE
  81. dwScope As Long
  82. dwType As Long
  83. dwDisplayType As Long
  84. dwUsage As Long
  85. lpLocalName As String
  86. lpRemoteName As String
  87. lpComment As String
  88. lpProvider As String
  89. End Type
  90.  
  91. Public Type WSADATA
  92. wVersion As Integer
  93. wHighVersion As Integer
  94. szDescription(WSADESCRIPTION_LEN) As Byte
  95. szSystemStatus(WSASYS_STATUS_LEN) As Byte
  96. iMaxSockets As Integer
  97. iMaxUdpDg As Integer
  98. lpVendorInfo As Long
  99. End Type
  100.  
  101. Public Type hostent
  102. h_name As Long
  103. h_aliases As Long
  104. h_addrtype As Integer
  105. h_length As Integer
  106. h_addr_list As Long
  107. End Type
  108.  
  109. Public Type SERVER_INFO_100
  110. sv100_platform_id As Long
  111. sv100_name As Long
  112. End Type
  113.  
  114. Public Type SHARE_INFO_0
  115. shi0_netname As Long
  116. End Type
  117.  
  118. Public Type SHARE_INFO_1
  119. shi1_netname As Long
  120. shi1_type As Long
  121. shi1_remark As Long
  122. End Type
  123.  
  124. Public Type SHARE_INFO_2
  125. shi2_netname As Long
  126. shi2_type As Long
  127. shi2_remark As Long
  128. shi2_permissions As Long
  129. shi2_max_uses As Long
  130. shi2_current_uses As Long
  131. shi2_path As Long
  132. shi2_passwd As Long
  133. End Type
  134.  
  135. Public Type FILE_INFO_3
  136. fi3_id As Long
  137. fi3_permissions As Long
  138. fi3_num_locks As Long
  139. fi3_pathname As Long
  140. fi3_username As Long
  141. End Type
  142.  
  143.  
  144.  
  145. 'キーボードステータス
  146. Public Declare Function GetAsyncKeyState Lib "user32" _
  147. (ByVal vKey As Long) As Integer
  148.  
  149. 'メモリコピー
  150. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  151. (Destination As Any, _
  152. Source As Any, _
  153. ByVal Length As Long)
  154.  
  155. 'ホスト名取得
  156. Public Declare Function gethostname Lib "wsock32" _
  157. (ByVal Name As String, _
  158. ByVal namelen As Long) As Long
  159.  
  160. '名前解決
  161. Public Declare Function gethostbyname Lib "wsock32" _
  162. (ByVal Name As String) As Long
  163.  
  164. 'Winsockスタート
  165. Public Declare Function WSAStartup Lib "wsock32" _
  166. (ByVal wVersionRequested As Integer, _
  167. lpWSAData As Any) As Long
  168. 'Winsock終了
  169. Public Declare Function WSACleanup Lib "wsock32" () As Long
  170.  
  171. 'Winsockエラー取得
  172. Public Declare Function WSAGetLastError Lib "wsock32" () As Integer
  173.  
  174. 'ネットユーザーグループ列挙
  175. Public Declare Function NetUserGetGroups Lib "Netapi32" _
  176. (lpServer As Any, _
  177. username As Byte, _
  178. ByVal Level As Long, _
  179. lpBuffer As Long, _
  180. ByVal prefmaxlen As Long, _
  181. lpEntriesRead As Long, _
  182. lpTotalEntries As Long) As Long
  183.  
  184. 'ローカルグループ列挙
  185. Public Declare Function NetUserGetLocalGroups Lib "Netapi32" _
  186. (lpServer As Any, _
  187. username As Byte, _
  188. ByVal Level As Long, _
  189. ByVal Flags As Long, _
  190. lpBuffer As Long, _
  191. ByVal MaxLen As Long, _
  192. lpEntriesRead As Long, _
  193. lpTotalEntries As Long) As Long
  194.  
  195. Public Declare Function NetServerEnum Lib "Netapi32" _
  196. (ByVal ServerName As Long, _
  197. ByVal Level As Long, _
  198. buf As Any, _
  199. ByVal prefmaxlen As Long, _
  200. entriesread As Long, _
  201. totalentries As Long, _
  202. ByVal servertype As Long, _
  203. ByVal domain As Long, _
  204. resume_handle As Long) As Long
  205.  
  206. Public Declare Function NetShareEnum Lib "Netapi32" _
  207. (ByVal ServerName As Long, _
  208. ByVal Level As Long, _
  209. bufptr As Long, _
  210. ByVal prefmaxlen As Long, _
  211. entriesread As Long, _
  212. totalentries As Long, _
  213. resume_handle As Long) As Long
  214.  
  215. 'ネットワーク管理関数の解放
  216. Public Declare Function NetApiBufferFree Lib "Netapi32" _
  217. (ByVal pBuffer As Long) As Long
  218.  
  219. 'ネットワークドライブの接続
  220. Public Declare Function WNetAddConnection2 Lib "mpr" Alias "WNetAddConnection2A" _
  221. (ByRef lpNetResource As NETRESOURCE, _
  222. ByVal lpPassword As String, _
  223. ByVal lpUserName As String, _
  224. ByVal dwFlags As Long) As Long
  225.  
  226. 'ネットワークドライブの切断
  227. Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
  228. (ByVal lpName As String, _
  229. ByVal dwFlags As Long, _
  230. ByVal fForce As Long) As Long
  231.  
  232.  
  233. Public Declare Function lstrlenW Lib "kernel32" _
  234. (ByVal lpString As Long) As Long
  235.  
  236. 'コンピュータ名を取得する関数の宣言
  237. Public Declare Function GetComputerNameEx Lib "kernel32" Alias "GetComputerNameExA" _
  238. (ByVal NameType As COMPUTER_NAME_FORMAT, _
  239. ByVal lpBuffer As String, _
  240. lpnSize As Long) As Long
  241.  
  242.  
  243. 'ログインユーザー名
  244. Public Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" _
  245. (ByVal lpBuffer As String, _
  246. lpnSize As Long) As Long
  247.  
  248. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  249. (ByVal hWnd As Long, _
  250. ByVal wMsg As Long, _
  251. ByVal wParam As Long, _
  252. lParam As Any) As Long
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263. Public Declare Function NetFileEnum Lib "Netapi32" _
  264. (ByVal ServerName As Long, _
  265. ByVal basepath As Long, _
  266. ByVal username As Long, _
  267. ByVal Level As Long, _
  268. bufptr As Long, _
  269. ByVal prefmaxlen As Long, _
  270. entriesread As Long, _
  271. totalentries As Long, _
  272. resume_handle As Long) As Long
  273.  
  274. Public Declare Function NetFileGetInfo Lib "Netapi32" _
  275. (ByVal ServerName As Long, _
  276. ByVal FileID As Long, _
  277. ByVal Level As Long, _
  278. bufptr As Long) As Long
  279.  
  280.  
  281.  
  282.  
  283.  
  284. Private Sub Auto_Open()
  285.  
  286. If GetAsyncKeyState(vbKeyControl) Then
  287.  
  288. Else
  289. Debug.Print ThisWorkbook.Worksheets("Setting").Cells(1, 2).Value
  290. If ThisWorkbook.Worksheets("Setting").Cells(1, 2).Value = True Then
  291. Application.Visible = False
  292. UserForm1.Show vbModeless
  293. End If
  294. End If
  295.  
  296. End Sub
  297.  
  298. Private Sub testGetIpAddres()
  299.  
  300. Debug.Print GetIpAddress("PCName")
  301.  
  302. End Sub
  303. Public Function GetIpAddress(sName As String) As String
  304.  
  305. Dim wsa As WSADATA
  306. Dim tpHostent As hostent
  307. Dim pHostent As Long
  308. Dim btIP(3) As Byte
  309.  
  310. If WSAStartup(&H101, wsa) <> 0 Then
  311. Exit Function
  312. End If
  313.  
  314. pHostent = gethostbyname(sName & vbNullChar)
  315. If pHostent <> 0 Then
  316. CopyMemory tpHostent, ByVal pHostent, Len(tpHostent)
  317. CopyMemory pHostent, ByVal tpHostent.h_addr_list, Len(tpHostent.h_addr_list)
  318. CopyMemory btIP(0), ByVal pHostent, Len(pHostent)
  319. GetIpAddress = btIP(0) & "." & btIP(1) & "." & btIP(2) & "." & btIP(3)
  320. End If
  321.  
  322. WSACleanup
  323.  
  324. End Function
  325. Sub testGetUserGroups()
  326.  
  327. Dim strGroups() As String
  328. Dim i As Long
  329.  
  330. strGroups = GetUserGroups("PCName", "User1", True)
  331. For i = 0 To UBound(strGroups)
  332. Debug.Print strGroups(i)
  333. Next i
  334.  
  335. End Sub
  336. Public Function GetUserGroups(ByVal ServerName As String, _
  337. ByVal username As String, Optional bLocalGroups _
  338. As Boolean = False) As String()
  339.  
  340. Dim bytUser() As Byte
  341. Dim bytServer() As Byte
  342.  
  343. Dim lBuffer As Long
  344. Dim lEntries As Long
  345. Dim lMaxLen As Long
  346. Dim lTotalEntries As Long
  347. Dim lRet As Long
  348. Dim lGroups() As Long
  349. Dim sGroups() As String
  350. Dim bytBuffer() As Byte
  351. Dim iCtr As Integer
  352. Dim lLen As Long
  353.  
  354. If bLocalGroups Then
  355. ServerName = vbNullChar
  356. Else
  357. If Left(ServerName, 2) <> "\\" Then
  358. ServerName = "\\" & ServerName
  359. End If
  360. End If
  361.  
  362. bytServer = ServerName & vbNullChar
  363. bytUser = username & vbNullChar
  364.  
  365. If bLocalGroups Then
  366. lRet = NetUserGetLocalGroups(bytServer(0), bytUser(0), 0, 0, _
  367. lBuffer, 1024, lMaxLen, lTotalEntries)
  368.  
  369. Else
  370. lRet = NetUserGetGroups(bytServer(0), bytUser(0), 0, _
  371. lBuffer, 1024, lMaxLen, lTotalEntries)
  372.  
  373. End If
  374.  
  375. If lRet = 0 And lMaxLen > 0 Then
  376. ReDim lGroups(lMaxLen - 1) As Long
  377. ReDim sGroups(lMaxLen - 1) As String
  378. CopyMemory lGroups(0), ByVal lBuffer, lMaxLen * 4
  379. For iCtr = 0 To lMaxLen - 1
  380. lLen = lstrlenW(lGroups(iCtr)) * 2
  381. If lLen > 0 Then
  382. ReDim bytBuffer(lLen - 1) As Byte
  383. CopyMemory bytBuffer(0), ByVal lGroups(iCtr), _
  384. lLen
  385. sGroups(iCtr) = bytBuffer
  386. End If
  387. Next
  388. Else
  389. ReDim sGroups(0) As String
  390. End If
  391.  
  392. If lBuffer > 0 Then
  393. NetApiBufferFree (lBuffer)
  394. End If
  395.  
  396. GetUserGroups = sGroups
  397.  
  398. End Function
  399. Private Sub testsubGetComputerName()
  400.  
  401. Dim lpBuffer As String
  402. Dim lngRet As Long
  403. Dim blRet As Boolean
  404.  
  405. lpBuffer = String(255, vbNullChar)
  406. lngRet = GetComputerNameEx(ComputerNameNetBIOS, lpBuffer, Len(lpBuffer))
  407. If lngRet <> 0 Then
  408. Debug.Print Left(lpBuffer, InStr(lpBuffer, vbNullChar) - 1)
  409. End If
  410.  
  411. lpBuffer = String(255, vbNullChar)
  412. lngRet = GetComputerNameEx(ComputerNameDnsDomain, lpBuffer, Len(lpBuffer))
  413. If lngRet <> 0 Then
  414. Debug.Print Left(lpBuffer, InStr(lpBuffer, vbNullChar) - 1)
  415. End If
  416.  
  417. lpBuffer = String(255, vbNullChar)
  418. lngRet = GetUserName(lpBuffer, Len(lpBuffer))
  419. If lngRet <> 0 Then
  420. Debug.Print Left(lpBuffer, InStr(lpBuffer, vbNullChar) - 1)
  421. End If
  422.  
  423. End Sub
  424.  
  425. Attribute VB_Name = "Module2"
  426. Option Explicit
  427. Public Function NetConnect(ByVal strResource As String, ByVal strUser As String, ByVal strPass As String) As Long
  428.  
  429. Dim tpNetResource As NETRESOURCE
  430. Dim lngRet As Long
  431.  
  432. '接続先文字列整形
  433. If strResource = "" Then
  434. strResource = "\\" & Environ$("COMPUTERNAME") & "\IPC$"
  435. ElseIf InStr(strResource, "\") = 0 Then
  436. strResource = "\\" & strResource & "\IPC$"
  437. ElseIf Left(strResource, 2) <> "\\" Then
  438. strResource = "\\" & strResource
  439. ElseIf InStr(Mid(strResource, 3), "\") = 0 Then
  440. strResource = strResource & "\IPC$"
  441. End If
  442.  
  443. 'ネットワークリソース設定
  444. With tpNetResource
  445. .dwType = RESOURCETYPE_DISK 'リソースタイプ
  446. .lpLocalName = vbNullString 'ローカル名(割り当てるドライブ)
  447. .lpRemoteName = strResource 'リモートパス(要はサーバ等の共用パス)
  448. .dwScope = RESOURCE_CONNECTED
  449. .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
  450. End With
  451.  
  452. '次回ログオン時に再接続しない場合
  453. lngRet = WNetAddConnection2(tpNetResource, strPass, strUser, 0)
  454.  
  455. NetConnect = lngRet
  456.  
  457. End Function
  458. Public Function NetClose(ByVal strResource As String) As Long
  459.  
  460. Dim lngRet As Long
  461.  
  462. '接続先文字列整形
  463. If strResource = "" Then
  464. strResource = "\\" & Environ$("COMPUTERNAME") & "\IPC$"
  465. ElseIf InStr(strResource, "\") = 0 Then
  466. strResource = "\\" & strResource & "\IPC$"
  467. ElseIf Left(strResource, 2) <> "\\" Then
  468. strResource = "\\" & strResource
  469. ElseIf InStr(Mid(strResource, 3), "\") = 0 Then
  470. strResource = strResource & "\IPC$"
  471. End If
  472.  
  473. lngRet = WNetCancelConnection2(strResource, CONNECT_UPDATE_PROFILE, True)
  474. NetClose = lngRet
  475.  
  476. End Function
  477. Public Function ConnectionError(lngRet As Long) As String
  478.  
  479. Dim strRet As String
  480.  
  481. Select Case lngRet
  482. Case ERROR_SUCCESS
  483. strRet = "正常終了"
  484. Case ERROR_BAD_NETPATH
  485. strRet = "ネットワークパスが不正"
  486. Case ERROR_ACCESS_DENIED
  487. strRet = "ネットワーク資源へのアクセスが拒否されました。"
  488. Case ERROR_ALREADY_ASSIGNED
  489. strRet = "lpLocalName で指定したローカルデバイスは既にネットワーク資源に接続されています。"
  490. Case ERROR_BAD_DEV_TYPE
  491. strRet = "ローカルデバイスの種類とネットワーク資源の種類が一致しません。"
  492. Case ERROR_BAD_DEVICE
  493. strRet = "lpLocalName で指定した値が無効です。"
  494. Case ERROR_BAD_NET_NAME
  495. strRet = "lpRemoteName で指定した値を、どのネットワーク資源のプロバイダも受け付けません。資源の名前が無効か、指定した資源が見つかりません。"
  496. Case ERROR_BAD_PROFILE
  497. strRet = "ユーザープロファイルの形式が正しくありません。"
  498. Case ERROR_BAD_PROVIDER
  499. strRet = "lpProvider で指定した値がどのプロバイダとも一致しません。"
  500. Case ERROR_BUSY
  501. strRet = "ルーターまたはプロバイダがビジー( おそらく初期化中)です。この関数をもう一度呼び出してください。"
  502. Case ERROR_CANCELLED
  503. strRet = "ネットワーク資源のプロバイダのいずれかでユーザーがダイアログボックスを使って接続操作を取り消したか、接続先の資源が接続操作を取り消しました。"
  504. Case ERROR_CANNOT_OPEN_PROFILE
  505. strRet = "恒久的な接続を処理するためのユーザープロファイルを開くことができません。"
  506. Case ERROR_DEVICE_ALREADY_REMEMBERED
  507. strRet = "lpLocalName で指定したデバイスのエントリは既にユーザープロファイル内に存在します。"
  508. Case ERROR_EXTENDED_ERROR
  509. strRet = "ネットワーク固有のエラーが発生しました。エラーの説明を取得するには、WNetGetLastError 関数を使います。"
  510. Case ERROR_INVALID_PASSWORD
  511. strRet = "指定したパスワードが無効です。"
  512. Case ERROR_NO_NET_OR_BAD_PATH
  513. strRet = "ネットワークコンポーネントが開始されていないか、指定した名前が利用できないために、操作を行えませんでした。"
  514. Case ERROR_NO_NETWORK
  515. strRet = "ネットワークに接続されていません。"
  516. Case ERROR_DEVICE_IN_USE
  517. strRet = "指定したデバイスがアクティブなプロセスによって使用中のため、切断できません。"
  518. Case ERROR_NOT_CONNECTED
  519. strRet = "lpName パラメータで指定した名前がリダイレクトされているデバイスを表していないか、lpName で指定したデバイスにシステムが接続していません。"
  520. Case ERROR_OPEN_FILES
  521. strRet = "開いているファイルがあり、fForce が FALSE です。"
  522. End Select
  523. ConnectionError = strRet
  524.  
  525. End Function
  526. Sub testGetShareEnum()
  527.  
  528. Dim strArry() As String
  529. Dim i As Long
  530.  
  531. strArry = GetShareEnum("PCName", "User1", "Pass1", 0)
  532.  
  533. For i = 0 To UBound(strArry)
  534. Debug.Print strArry(i)
  535. Next i
  536.  
  537. End Sub
  538. Public Function GetShareEnum(strServer As String, strUser As String, strPass As String, Optional lngShareType As Long = -1) As String()
  539.  
  540. Dim bufptr As Long
  541. Dim dwServer As Long
  542. Dim dwEntriesread As Long
  543. Dim dwTotalentries As Long
  544. Dim dwResumehandle As Long
  545. Dim lngRet As Long
  546. Dim nSize As Long
  547. Dim i As Long
  548. Dim tpShareInfo2 As SHARE_INFO_2
  549. Dim tpShareInfo0 As SHARE_INFO_0
  550. Dim tpShareInfo1 As SHARE_INFO_1
  551. Dim strIP As String
  552. Dim strTmp() As String
  553.  
  554. 'IPで接続
  555. strIP = GetIpAddress(strServer)
  556.  
  557. '接続
  558. lngRet = NetConnect(strIP & "\IPC$", strUser, strPass)
  559. If lngRet <> ERROR_SUCCESS Then
  560. MsgBox "Error:NetConnect " & CStr(lngRet) & vbCrLf & ConnectionError(lngRet), vbCritical
  561. Exit Function
  562. End If
  563.  
  564. '共有リソース列挙
  565. dwServer = StrPtr(strIP)
  566. lngRet = NetShareEnum(dwServer, _
  567. 1, _
  568. bufptr, _
  569. MAX_PREFERRED_LENGTH, _
  570. dwEntriesread, _
  571. dwTotalentries, _
  572. dwResumehandle)
  573.  
  574. If lngRet = NERR_SUCCESS And lngRet <> ERROR_MORE_DATA Then
  575. nSize = LenB(tpShareInfo1)
  576. 'ReDim strTmp(dwEntriesread - 1) As String
  577. For i = 0 To dwEntriesread - 1
  578. CopyMemory tpShareInfo1, ByVal bufptr + (nSize * i), nSize
  579.  
  580.  
  581. If lngShareType = -1 Then
  582. If Sgn(strTmp) = 0 Then
  583. ReDim strTmp(0) As String
  584. Else
  585. ReDim Preserve strTmp(UBound(strTmp) + 1) As String
  586. End If
  587. strTmp(UBound(strTmp)) = GetPointerToByteStringW(tpShareInfo1.shi1_netname)
  588. Else
  589. If tpShareInfo1.shi1_type = lngShareType Then
  590. If Sgn(strTmp) = 0 Then
  591. ReDim strTmp(0) As String
  592. Else
  593. ReDim Preserve strTmp(UBound(strTmp) + 1) As String
  594. End If
  595. strTmp(UBound(strTmp)) = GetPointerToByteStringW(tpShareInfo1.shi1_netname)
  596.  
  597. End If
  598. End If
  599.  
  600.  
  601.  
  602. Next
  603. End If
  604.  
  605. Call NetApiBufferFree(bufptr)
  606.  
  607. lngRet = NetClose(strIP & "\IPC$")
  608. If lngRet <> ERROR_SUCCESS Then
  609. MsgBox "Error:NetClose " & CStr(lngRet) & vbCrLf & ConnectionError(lngRet), vbCritical
  610. Exit Function
  611. End If
  612.  
  613. GetShareEnum = strTmp
  614.  
  615.  
  616. End Function
  617. Sub testGetFileEnum()
  618.  
  619. Dim strArry() As String
  620. Dim i As Long
  621.  
  622. strArry = GetFileEnum(vbNullString, "User1", "Pass1", 0)
  623.  
  624. For i = 0 To UBound(strArry)
  625. Debug.Print strArry(i)
  626. Next i
  627.  
  628. End Sub
  629.  
  630. Public Function GetFileEnum(strServer As String, strUser As String, strPass As String, Optional lngShareType As Long = -1) As String()
  631.  
  632. Dim bufptr As Long
  633. Dim dwServer As Long
  634. Dim dwEntriesread As Long
  635. Dim dwTotalentries As Long
  636. Dim dwResumehandle As Long
  637. Dim lngRet As Long
  638. Dim nSize As Long
  639. Dim i As Long
  640. Dim strIP As String
  641. Dim strTmp() As String
  642. Dim tpFileInfo3 As FILE_INFO_3
  643.  
  644.  
  645. If strServer <> vbNullString And strServer <> Environ$("COMPUTERNAME") Then
  646. 'IPで接続
  647. strIP = GetIpAddress(strServer)
  648.  
  649. '接続
  650. lngRet = NetConnect(strIP, strUser, strPass)
  651. If lngRet <> ERROR_SUCCESS Then
  652. MsgBox "Error:NetConnect " & CStr(lngRet) & vbCrLf & ConnectionError(lngRet), vbCritical
  653. Exit Function
  654. End If
  655. End If
  656.  
  657. '共有リソース列挙
  658. dwServer = StrPtr(strIP)
  659. nSize = LenB(tpFileInfo3)
  660. lngRet = NetFileEnum(dwServer, _
  661. 0&, _
  662. 0&, _
  663. 3, _
  664. bufptr, _
  665. MAX_PREFERRED_LENGTH, _
  666. dwEntriesread, _
  667. dwTotalentries, _
  668. dwResumehandle)
  669.  
  670. If lngRet = NERR_SUCCESS And lngRet <> ERROR_MORE_DATA Then
  671. ReDim strTmp(dwEntriesread - 1) As String
  672. For i = 0 To dwEntriesread - 1
  673. CopyMemory tpFileInfo3, ByVal bufptr + (nSize * i), nSize
  674. strTmp(i) = GetPointerToByteStringW(tpFileInfo3.fi3_username) & vbTab & _
  675. GetPermissionType(tpFileInfo3.fi3_permissions) & vbTab & _
  676. GetPointerToByteStringW(tpFileInfo3.fi3_pathname)
  677. Next
  678. End If
  679.  
  680.  
  681. Call NetApiBufferFree(bufptr)
  682.  
  683. If strServer <> vbNullString And strServer <> Environ$("COMPUTERNAME") Then
  684. lngRet = NetClose(strIP)
  685. If lngRet <> ERROR_SUCCESS Then
  686. MsgBox "Error:NetClose " & CStr(lngRet) & vbCrLf & ConnectionError(lngRet), vbCritical
  687. Exit Function
  688. End If
  689. End If
  690. GetFileEnum = strTmp
  691.  
  692.  
  693. End Function
  694.  
  695.  
  696. 'cmdExecute
  697. 'txtServer
  698. 'cmbShare
  699. 'txtUser
  700. 'txtPass
  701. 'lstAccess
  702. 'cmdShareGet
  703. 'Label1
  704. 'Label2
  705. 'Label3
  706. 'Label4
  707. 'Label5
  708. 'cmdSaveSetting
  709.  
  710.  
  711. VERSION 5.00
  712. Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} UserForm1
  713. Caption = "UserForm1"
  714. ClientHeight = 6885
  715. ClientLeft = 45
  716. ClientTop = 390
  717. ClientWidth = 6930
  718. OleObjectBlob = "UserForm1.frx":0000
  719. StartUpPosition = 1 'オーナー フォームの中央
  720. End
  721. Attribute VB_Name = "UserForm1"
  722. Attribute VB_GlobalNameSpace = False
  723. Attribute VB_Creatable = False
  724. Attribute VB_PredeclaredId = True
  725. Attribute VB_Exposed = False
  726. Option Explicit
  727. Private Const ADDR_SERVER = "B2"
  728. Private Const ADDR_USER = "B4"
  729. Private Const ADDR_SHARE = "B5"
  730. Private Sub CommandButton1_Click()
  731. Unload Me
  732. Application.Quit
  733.  
  734. End Sub
  735.  
  736. Private Sub cmdExecute_Click()
  737.  
  738. Dim strTmp() As String
  739. Dim i As Long
  740.  
  741. strTmp = GetFileEnum(txtServer.Value, txtUser.Value, txtPass.Value)
  742.  
  743. For i = 0 To UBound(strTmp)
  744. lstAccess.AddItem strTmp(i)
  745. Next i
  746.  
  747. End Sub
  748.  
  749. Private Sub cmdSaveSetting_Click()
  750.  
  751. Dim i As Long
  752.  
  753. With ThisWorkbook.Worksheets("Setting")
  754. .Range(ADDR_SERVER).Value = txtServer.Value
  755. .Range(ADDR_USER).Value = txtUser.Value
  756. For i = 0 To cmbShare.ListCount - 1
  757. .Range(ADDR_SHARE).Offset(i, 0).Value = cmbShare.List(i)
  758. Next i
  759. End With
  760.  
  761. End Sub
  762.  
  763. Private Sub cmdShareGet_Click()
  764.  
  765. Dim strTmp() As String
  766. Dim i As Long
  767.  
  768. strTmp = GetShareEnum(txtServer.Value, txtUser.Value, txtPass.Value)
  769. cmbShare.Clear
  770. For i = 0 To UBound(strTmp)
  771. cmbShare.AddItem strTmp(i)
  772. Next i
  773.  
  774.  
  775. End Sub
  776.  
  777.  
  778. Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  779.  
  780. If GetAsyncKeyState(vbKeyControl) Then
  781. cmdSaveSetting.Visible = True
  782. End If
  783.  
  784.  
  785. End Sub
  786.  
  787. Private Sub UserForm_Initialize()
  788.  
  789. Dim i As Long
  790. With ThisWorkbook.Worksheets("Setting")
  791. txtServer.Value = .Range(ADDR_SERVER).Value
  792. txtUser.Value = .Range(ADDR_USER).Value
  793. cmbShare.Clear
  794. If .Range(ADDR_SHARE).Value <> "" Then
  795. Do While .Range(ADDR_SHARE).Offset(i, 0).Value <> ""
  796. cmbShare.AddItem .Range(ADDR_SHARE).Offset(i, 0).Value
  797. i = i + 1
  798. Loop
  799. End If
  800. End With
  801.  
  802. End Sub
  803.  
  804. Private Sub UserForm_Terminate()
  805.  
  806. If GetAsyncKeyState(vbKeyControl) Then
  807. Application.Visible = True
  808. Else
  809. Application.Quit
  810. End If
  811.  
  812. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement