快救命,快救命,怎么获得局域网内的所有计算机的ip地址或机器名(能极时获得)(200分)

  • 主题发起人 主题发起人 jqdelphi
  • 开始时间 开始时间
J

jqdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,快救命(关系饭碗问题,快被老板炒了)
用什么方法能极时获得局域网内的所有机算机的ip地址或机器名。用 WNetOpenEnum,WNetEnumResource等函数好像得的不是很准确(不知对不对)。比如关闭一台机器,网上应该看不见该机器,但用上面的函
数还是能获得。好像要等一会才能够让该机器消失(十几分钟)。打开一台机器也一样,要等一会才能看见该机器。
如有源码给我一份,本人万分感极!!!!!!!!!!!email(jq76052186jq@sina.com)
 
搜索老帖[ip]或[计算机名]或[局网]。。。
 
你的局域网是在同一个网段的把,那你 可以 用ping来实现呀,这样不就可以实时知道
是否在线,这样的源程序我写过
 
http://delphi.mychangshu.com/dispdoc.asp?id=893
http://delphi.mychangshu.com/dispdoc.asp?id=868
http://delphi.mychangshu.com/dispdoc.asp?id=377
 
各位说明白一点就是我在客户端装了一client程序,如有没装client程序的机器上来我能知道
它的ip或机器名.没装client程序的机器
不一定和我一个网段,如从0.0.0.0到255.255.255.255全ping 那也太费时了
 
sniffer也许可以,但是不一定准确,可以查一下关于这个的资料
同时,对于经过交换机的,这个就不好办了。
 
有谁知道NetServerGetInfo这个函数的用法同样送上200分最好有例子(源码更好)
 
来自:http://go5.163.com/nowcan/tech/netshare.htm

#include <windows.h>
#include <stdio.h>
#include <lm.h>


void ShowError(DWORD Code)
{
printf("/nError: %ld----",Code);
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
Code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // Default language
(LPTSTR) &amp;lpMsgBuf,
0,
NULL);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
//MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONINFORMATION );
printf("%S/n",(LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}

void EnumGroup(TCHAR *pszServerName)
{
NET_API_STATUS nStatus,mRet;
DWORD dwLevel = 1;
PLOCALGROUP_INFO_1 pBuf=NULL,p;
DWORD Read=0,Entry=0,Resume=0;
PLOCALGROUP_MEMBERS_INFO_2 mBuf=NULL,m;
DWORD mRead=0,mEntry=0,mResume=0;

printf("-------------GROUP INFORMATION-------------/n");

nStatus=NetLocalGroupEnum(pszServerName,dwLevel,(LPBYTE *)&amp;pBuf,MAX_PREFERRED_LENGTH,&amp;Read, &amp;Entry, &amp;Resume);
if(nStatus == NERR_Success)
{
p=pBuf;
for(DWORD i=0;i<Read;i++)
{
printf("Group: %-30S",p->lgrpi1_name);
printf("Comment: %-30S/n",p->lgrpi1_comment);
mRet = NetLocalGroupGetMembers(pszServerName,p->lgrpi1_name, 2, (LPBYTE *)&amp;mBuf, MAX_PREFERRED_LENGTH, &amp;mRead, &amp;mEntry, &amp;mResume);
if(mRet==NERR_Success)
{
m=mBuf;
printf("-----Members-----/n");
for(DWORD j=0;j<mRead;j++)
{
printf("%S/n",m->lgrmi2_domainandname);
m++;
}
printf("/n");
}
else
{
ShowError(mRet);
}
NetApiBufferFree(mBuf);
p++;
}
}
else
{
ShowError(nStatus);
}

NetApiBufferFree (pBuf);
printf("/n/n");

}


int DispServer(TCHAR *pszServerName)
{
DWORD dwLevel = 101;
LPSERVER_INFO_101 pBuf = NULL;
NET_API_STATUS nStatus;

//
// Call the NetServerGetInfo function, specifying level 101.
//
nStatus = NetServerGetInfo(pszServerName,
dwLevel,
(LPBYTE *)&amp;pBuf);
//
// If the call succeeds,
//
if (nStatus == NERR_Success)
{
//
// Check for the type of server.
//
if ((pBuf->sv101_type &amp; SV_TYPE_DOMAIN_CTRL) ||
(pBuf->sv101_type &amp; SV_TYPE_DOMAIN_BAKCTRL) ||
(pBuf->sv101_type &amp; SV_TYPE_SERVER_NT))
printf("Type: Server/n");
else
printf("Type: Workstation/n");
printf("Name: %S/n",pBuf->sv101_name);
printf("OS: ");
switch(pBuf->sv101_platform_id)
{
case PLATFORM_ID_DOS:
printf("DOS/n");
break;
case PLATFORM_ID_OS2:
printf("OS2/n");
break;
case PLATFORM_ID_NT:
printf("NT/n");
break;
case PLATFORM_ID_OSF:
printf("OSF/n");
break;
case PLATFORM_ID_VMS:
printf("VMS/n");
break;
default:
printf("Unknown/n");
break;
}
printf("Version: %d.%d/n",pBuf->sv101_version_major,pBuf->sv101_version_minor);
printf("Comment: %S/n",pBuf->sv101_comment);
}
//
// Otherwise, print the system error.
//
//else
// printf("A system error has occurred: %d/n", nStatus);
//
// Free the allocated memory.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);
printf("/n/n");
return 0;
}
void EnumShare(TCHAR *lpszServer)
{
PSHARE_INFO_1 BufPtr,p;
NET_API_STATUS res;
DWORD er=0,tr=0,resume=0, i;
//
// Print a report header.
//
printf("Share Share Type: /n");
printf("--------------------------------------------------/n");
//
// Call the NetShareEnum function; specify level 1.
//
do // begin do
{
res = NetShareEnum (lpszServer, 1, (LPBYTE *) &amp;BufPtr, -1, &amp;er, &amp;tr, &amp;resume);
//
// If the call succeeds,
//
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
{
p=BufPtr;
//
// Loop through the entries;
// print retrieved data.
//
for(i=1;i<=er;i++)
{
printf("%-20S",p->shi1_netname);
switch(p->shi1_type)
{
case STYPE_DISKTREE:
printf("%-30s/n","Disk driver");
break;
case STYPE_PRINTQ:
printf("%-30s/n","Printe queue");
break;
case STYPE_DEVICE:
printf("%-30s/n","Communication device");
break;
case STYPE_IPC:
printf("%-30s/n","Interprocess communication (IPC)");
break;
case STYPE_SPECIAL:
printf("%-30s/n","Special share");
break;
default:
printf("%-30s/n","Unknown share");
break;
}

p++;
}
//
// Free the allocated buffer.
//
NetApiBufferFree(BufPtr);
}
else
{
ShowError(res);
}
// Continue to call NetShareEnum while
// there are more entries.
//
}while (res==ERROR_MORE_DATA); // end do
return;
}

void DisplayStruct(LPNETRESOURCE lpnr)
{
switch(lpnr->dwDisplayType)
{
case RESOURCEDISPLAYTYPE_DOMAIN:
printf("--------Domain--%S--------/n",lpnr->lpRemoteName);
break;
case RESOURCEDISPLAYTYPE_SERVER:
{
TCHAR IPC[MAX_PATH];
NETRESOURCE netr;
printf("--------Server--%S--------/n",lpnr->lpRemoteName);
wsprintf(IPC,L"%s//IPC$",lpnr->lpRemoteName);
netr.dwScope = RESOURCE_GLOBALNET;
netr.dwType = RESOURCETYPE_ANY;
netr.lpLocalName = L"";
netr.lpRemoteName = IPC;
netr.lpProvider = NULL;
WNetAddConnection2(&amp;netr,L"",L"",NULL);
DispServer(lpnr->lpRemoteName);
EnumShare(lpnr->lpRemoteName);
WNetCancelConnection2(IPC,CONNECT_UPDATE_PROFILE,TRUE);
printf("/n/n");
}
break;
// case RESOURCEDISPLAYTYPE_SHARE:
// printf("Share:%S/n",lpnr->lpRemoteName);
// break;
default:
// printf("Unknown:%S/n",lpnr->lpRemoteName);
break;
}
}

BOOL WINAPI EnumerateFunc(LPNETRESOURCE lpnr)
{
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
DWORD i;
//
// Call the WNetOpenEnum function to begin the enumeration.
//
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_ANY, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
&amp;hEnum); // handle to the resource

if (dwResult != NO_ERROR)
{
return FALSE;
}
//
// Call the GlobalAlloc function to allocate resources.
//
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);

do
{
//
// Initialize the buffer.
//
ZeroMemory(lpnrLocal, cbBuffer);
//
// Call the WNetEnumResource function to continue
// the enumeration.
//
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&amp;cEntries, // defined locally as -1
lpnrLocal, // LPNETRESOURCE
&amp;cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
//
if (dwResultEnum == NO_ERROR)
{
for(i = 0; i < cEntries; i++)
{
// Call an application-defined function to
// display the contents of the NETRESOURCE structures.
//
DisplayStruct(&amp;lpnrLocal);

// If the NETRESOURCE structure represents a container resource,
// call the EnumerateFunc function recursively.

if(RESOURCEUSAGE_CONTAINER == (lpnrLocal.dwUsage &amp; RESOURCEUSAGE_CONTAINER))
EnumerateFunc(&amp;lpnrLocal);
}
}
// Process errors.
//
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
break;
}
}while(dwResultEnum != ERROR_NO_MORE_ITEMS);
//
// Call the GlobalFree function to free the memory.
//
GlobalFree((HGLOBAL)lpnrLocal);
//
// Call WNetCloseEnum to end the enumeration.
//
dwResult = WNetCloseEnum(hEnum);

if(dwResult != NO_ERROR)
{
return FALSE;
}

return TRUE;
}

int wmain(int argc,TCHAR **argv)
{
if(argc==2)
{
TCHAR IPC[MAX_PATH];
NETRESOURCE netr;
wsprintf(IPC,L"%s//IPC$",argv[1]);
netr.dwScope = RESOURCE_GLOBALNET;
netr.dwType = RESOURCETYPE_ANY;
netr.lpLocalName = L"";
netr.lpRemoteName = IPC;
netr.lpProvider = NULL;
WNetAddConnection2(&amp;netr,L"",L"",NULL);
DispServer(argv[1]);
EnumGroup(argv[1]);
EnumShare(argv[1]);
WNetCancelConnection2(IPC,CONNECT_UPDATE_PROFILE,TRUE);
printf("/n/n");

}
else
{
EnumerateFunc(NULL);
}

}

实际上效果不会比
http://delphi.mychangshu.com/dispdoc.asp?id=377
http://delphi.mychangshu.com/dispdoc.asp?id=868
好。
 
调用FindComputers,返回WinAPI错误代码(如果有的话)。这个函数将会在TStringList类型的Computers对象中填入计算机名称。

以下代码是在Delphi5上开发的,但是应该是向后兼容的。

Answer:


unit FindComp;

interface

uses
Windows, Classes;

function FindComputers: DWORD;

var
Computers: TStringList;

implementation

uses
SysUtils;

const
MaxEntries = 250;

function FindComputers: DWORD;

var
EnumWorkGroupHandle, EnumComputerHandle: THandle;
EnumError: DWORD;
Network: TNetResource;
WorkGroupEntries, ComputerEntries: DWORD;
EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
EnumBufferLength: DWORD;
I, J: DWORD;

begin

Computers.Clear;

FillChar(Network, SizeOf(Network), 0);
with Network do
begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_ANY;
dwUsage := RESOURCEUSAGE_CONTAINER;
end;

EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @Network, EnumWorkGroupHandle);

if EnumError = NO_ERROR then
begin
WorkGroupEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumWorkGroupBuffer);
EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries, @EnumWorkGroupBuffer, EnumBufferLength);

if EnumError = NO_ERROR then
begin
for I := 1 to WorkGroupEntries do
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @EnumWorkGroupBuffer, EnumComputerHandle);
if EnumError = NO_ERROR then
begin
ComputerEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumComputerBuffer);
EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries, @EnumComputerBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
for J := 1 to ComputerEntries do
Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName, 3, Length(EnumComputerBuffer[J].lpRemoteName) - 2));
WNetCloseEnum(EnumComputerHandle);
end;
end;
end;
WNetCloseEnum(EnumWorkGroupHandle);
end;

if EnumError = ERROR_NO_MORE_ITEMS then
EnumError := NO_ERROR;
Result := EnumError;

end;

initialization

Computers := TStringList.Create;

finalization

Computers.Free;

end.
 
各位大虾,看清前面的要求,我要能很快的获得用WNetOpenEnum,WNetEnumResource这些我也会,NetServerGetInfo的c语言用法我知道
我要delphi版的.
 
你没看到这两个源程序吗?
http://delphi.mychangshu.com/dispdoc.asp?id=893
http://delphi.mychangshu.com/dispdoc.asp?id=377
它们够快!
 
程序员大本营2001中有例子,a盘(borland版)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部