# Re: 如何用VB得知区域网路所有线上电脑的IP和Name (改贴)

Posted by 琏琏 on September 08, 1999 at 10:46:20:

[略]

: nr1.lpRemoteName = nr.lpRemoteName //这个很奇怪在*.bas 宣告为String而在api 中却是pointer 传回

问题就在这!
lpRemoteName 若设成 String 则对于 WNetOpenEnum 所收到的引数值就不对了
我的做法是
(lpRemoteName as Long)
(BufferString=256)

lpBuff = GlobalAlloc(GPTR, BufferString)
nr.dwUsage = WNet_Usage_Container
nr.lpRemoteName = lpBuff
CopyMemory ByVal lpBuff, ResourceName, BufferString

[略]...

GlobalFree (lpBuff)

透过 lpBuff 所指定的记忆体空间位置来存字串, 在将 Pointer 存入 lpRemoteName

ps. 其实只要设定 nr.lpRemoteName 才是最主要的, nr.lpRemoteName 不指向为 null 时, 可以是 domain 或 Server 皆可! 比如说 ResourceName 可以是 domain ("水利海洋资料库") 或 Server (\\feitsui),
1.若是 null 则列举传回值为 domain,
2.若是 domain 则列举传回值为 Server,
3.若是 Server 则列举传回值为 Disk or Print.

[略]

: 而我的想法递回呼叫应是重新执行WNetOpenEnum,WNetEnumResource
: 这两个函数，所传的参数在递回呼叫应会重新refresh，不知这个想
: 法是那里有错，请您多多指教!!!谢谢!!!

有空我会整理我的 Code 放到我的网页上...

先送你错误讯息好了!!
比较特别的是 Error_Not_Authenticated , 查到 NT 通常会传回给你这个值, 不让你浏览...
若是有加上你在这几节的 MSDN 没看到的 Error Number, 别怀疑, 这都是我实际查网芳碰上的 Error Number

Declare Function WNetGetLastError Lib "mpr.dll" Alias "WNetGetLastErrorA" (lpError As Long, ByVal lpErrorBuf As String, ByVal nErrorBufSize As Long, ByVal lpNameBuf As String, ByVal nNameBufSize As Long) As Long

Private Enum enuWinNet_Error
Error_Invalid_Handle = 6
Error_Dev_Not_Exist = 55
Error_Bad_Dev_Type = 66
Error_Bad_Net_Name = 67
Error_More_Data = 234
Error_No_More_Items = 259
Error_Bad_Provider = 1204
ERROR_Not_Container = 1207
Error_Extended_Error = 1208
Error_No_Network = 1222
Error_Not_Authenticated = 1244
End Enum

Public Function myWNetErrorDescription(ByVal DLLErr)

Select Case DLLErr
Case Is = Error_Invalid_Handle
tDescription = "错误的代号，可能是资源列举代号错误！"
Case Is = Error_Dev_Not_Exist
tDescription = "指定的网路资源或装置不存在！"
Case Is = Error_Bad_Dev_Type
tDescription = "网路资源型态不正确！"
Case Is = Error_Bad_Net_Name
tDescription = "网路电脑名称不存在！"
Case Is = Error_More_Data
tDescription = "程式宣告空间不足，无法储存更多的资料，请洽原程式设计者！"
Case Is = Error_No_More_Items
tDescription = "没有其他资料项目了！"
Case Is = Error_Extended_Error
tErrDesc = myWNetGetLastError(DLLErr)
tDescription = "DLL 档延伸错误，请利用 WNetGetLastError 函数取的错误讯息！" + tErrDesc
Case Is = Error_Bad_Provider
tDescription = "网路提供者发生错误！"
Case Is = Error_No_Network
tDescription = "没有网路系统！"
Case Is = Error_Not_Authenticated
tDescription = "使用者未经认证，请求操作不被允许 [主机不允许未经密码确认的使用者浏览该主机提供资源]！"
Case Else
tDescription = "未定义此错误讯息 [Err=" + CStr(DLLErr) + "]！"
End Select

myWNetErrorDescription = tDescription

End Function

private Function myWNetGetLastError(ByVal lpError)

lpErrorBuf = String(BufferString, Chr$(0))
lpNameBuf = String(BufferString, Chr$(0))

summy = WNetGetLastError(lpError, lpErrorBuf, Len(lpErrorBuf), lpNameBuf, Len(lpNameBuf))

If summy = 0 Then
myWNetGetLastError = "myWNetGetLastError: " + myWNetErrorDescription(lpError)
Else
myWNetGetLastError = "myWNetGetLastError: " + CStr(lpError) + " " + Left(lpErrorBuf, InStr(lpErrorBuf, Chr$(0)) - 1)
End If

End Function

- 培基语言

Follow Ups:

- Re: 如何用VB得知区域网路所有线上电脑的IP和Name (改贴) 琏琏 15:27:16 9/09/99 (1)
  - Re: 如何用VB得知区域网路所有线上电脑的IP和Name (改贴) Smith 08:04:43 9/12/99 (0)