BOOL bSuccess = FALSE; // assume this function fails
if(argc < 4) {
printf("Usage: %ls [\\\\Server]\n", argv[0]);
printf(" directory is fullpath of directory to share\n");
printf(" sharename is name of share on server\n");
printf(" user/group is an WinNT user/groupname (REDMOND\\sfield, Administrators, etc)\n");
printf(" optional Server is the name of the computer to create the share on\n");
printf("\nExample: %ls c:\\public public Everyone\n", argv[0]);
printf("c:\\public shared as public granting Everyone full access\n");
printf("\nExample: %ls c:\\private cool$ REDMOND\\sfield \\\\WINBASE\n", argv[0]);
printf("c:\\private on \\\\WINBASE shared as cool$ (hidden) granting REDMOND\\sfield access\n");
return RTN_USAGE;
}
//
// since the commandline was Unicode, just provide pointers to
// the relevant items
//
procedure GetDomainList(TV:TTreeView);
var
a : Integer;
ErrCode : Integer;
NetRes : Array[0..1023] of TNetResource;
EnumHandle : THandle;
EnumEntries : DWord;
BufferSize : DWord;
s : string;
itm : TTreeNode;
begin
{ Start here }
try
With NetRes[0] do begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONTAINER;
lpLocalName :=NIL;
lpRemoteName :=NIL;
lpComment :=NIL;
lpProvider :=NIL;
end;
{ get net root }
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);
If ErrCode=NO_ERROR then begin
EnumEntries:=1;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);
WNetCloseEnum(EnumHandle);
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);
IF ErrCode=No_Error then with TV do try
a:=0;
Items.BeginUpDate;
Items.Clear;
{ get domains }
While a How can I get the contents of the Neighborhood?
The following unit defines a component, TNetworkBrowser, which can be used
to enumerate all resources on the network in a hierarchical tree. The
actual browsing takes a long time (try opening "Entire Network" in Windows
Explorer). If you set the Scope property to nsContext, you'll see the list
of machines from the "Network Neighborhood" window.
下面的一个单元定义了一个组件. TNetworkBrowser, 可以枚举hierachical树上所有
的网络资源. 实际上浏览是要花费很长时间的,这您可以通过在WINDOWS资源管理器
中打开"整个网络" 来比较一下. 如果你设置SCOPE属性 为nsContext , 你就可以看到
和网络邻居中一样的机器列表
type
PNetResourceArray = ^TNetResourceArray;
TNetResourceArray = array[0..0] of TNetResource;
{ TNetworkItem }
constructor TNetworkItem.Create;
begin
inherited;
FSubItems := TNetworkItems.Create;
end;
destructor TNetworkItem.Destroy;
begin
if FSubItems <> nil then
FSubItems.Free;
inherited;
end;
{ TNetworkItems }
constructor TNetworkItems.Create;
begin
inherited;
FList := TList.Create;
end;
destructor TNetworkItems.Destroy;
begin
Clear;
if FList <> nil then
FList.Free;
inherited;
end;
procedure TNetworkItems.SetItem(Index: Integer; Value: TNetworkItem);
begin
if (FList.Items[Index] <> nil) and (FList.Items[Index] <> Value) then
TNetworkItem(FList.Items[Index]).Free;
FList.Items[Index] := Value;
end;
function TNetworkItems.GetItem(Index: Integer): TNetworkItem;
begin
Result := TNetworkItem(FList.Items[Index]);
end;
procedure TNetworkItems.Clear;
begin
while Count > 0 do
Delete(0);
end;
procedure TNetworkItems.Add(Item: TNetworkItem);
begin
FList.Add(Item);
end;
procedure TNetworkItems.Delete(Index: Integer);
begin
if FList.Items[Index] <> nil then
TNetworkItem(FList.Items[Index]).Free;
FList.Delete(Index);
end;
function TNetworkItems.GetCount: Integer;
begin
if FList <> nil then
Result := FList.Count
else
Result := 0;
end;
GetMem(nrArray, cbBuffer);
repeat
dwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer);
if dwResultEnum = NO_ERROR then
for i := 0 to cEntries-1 do
begin
NewItem := TNetworkItem.Create;
case nrArray[i].dwScope of
RESOURCE_CONNECTED: NewItem.FScope := nsConnected;
RESOURCE_GLOBALNET: NewItem.FScope := nsGlobal;
RESOURCE_REMEMBERED: NewItem.FScope := nsRemembered;
RESOURCE_CONTEXT: NewItem.FScope := nsContext;
else
NewItem.FScope := nsGlobal;
end;
case nrArray[i].dwType of
RESOURCETYPE_ANY: NewItem.FResourceType := nrAny;
RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;
RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;
else
NewItem.FResourceType := nrAny;
end;
case nrArray[i].dwDisplayType of
RESOURCEDISPLAYTYPE_GENERIC: NewItem.FDisplay := ndGeneric;
RESOURCEDISPLAYTYPE_DOMAIN: NewItem.FDisplay := ndDomain;
ndShareAdmin;
RESOURCEDISPLAYTYPE_DIRECTORY: NewItem.FDisplay :=
ndDirectory;
RESOURCEDISPLAYTYPE_TREE: NewItem.FDisplay := ndTree;
RESOURCEDISPLAYTYPE_NDSCONTAINER: NewItem.FDisplay :=
ndNDSContainer;
else
NewItem.FDisplay := ndGeneric;
end;
NewItem.FUsage := [];
if nrArray[i].dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then
Include(NewItem.FUsage, nuConnectable);
if nrArray[i].dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then
Include(NewItem.FUsage, nuContainer);
NewItem.FLocalName := StrPas(nrArray[i].lpLocalName);
NewItem.FRemoteName := StrPas(nrArray[i].lpRemoteName);
NewItem.FComment := StrPas(nrArray[i].lpComment);
NewItem.FProvider := StrPas(nrArray[i].lpProvider);
NetItems.Add(NewItem);
// if container, call recursively
if (nuContainer in NewItem.FUsage) and (FScope <> nsContext) then
EnumerateNet(NewItem.FSubItems, @nrArray[i])
end;
until dwResultEnum = ERROR_NO_MORE_ITEMS;
FreeMem(nrArray);
WNetCloseEnum(hEnum);
end;
procedure TNetworkBrowser.Refresh;
begin
FItems.Clear;
if FActive then
EnumerateNet(FItems, nil);
end;
procedure TNetworkBrowser.SetActive(Value: Boolean);
begin
if Value <> FActive then
begin
FActive := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetScope(Value: TNetScope);
begin
if Value <> FScope then
begin
FScope := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetResourceType(Value: TNetResourceType);
begin
if Value <> FResourceType then
begin
FResourceType := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetUsage(Value: TNetUsage);
begin
if Value <> FUsage then
begin
FUsage := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.Open;
begin
Active := True;
end;
procedure TNetworkBrowser.Close;
begin
Active := False;
end;
You can use GetVersionEx to retieve the OS version.As a common solution,you can use WNetEnumResource to enumerate computer names,and use NetshareEnum to enumerate net share resource on the computer,(Note :YOU MUST USE DIFFERENT PARAMETER UNDER 98 AND NT.REFER TO MSDN) ,Use Shell function to call net share command to add/delete net share resource.
'得到工作组和计算机名:添加一个Command;CmdListWorkGroup 一个
'combobox:Combo_WorkGroup 一个listbox:List_ComputerName; 在98中运行通过;2K中有
'问题
Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As NETRESOURCE, lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Public Function EnumDomain() As Boolean
Dim NetS As NETRESOURCE
Dim MaxDomain As Integer
Dim Buf(1024) As Byte
Dim TmpReturn As Long
Dim TmpStr As String
Dim hEnumReturn As Long
Dim I As Integer
TmpReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NetS, hEnumReturn)
If TmpReturn <> 0 Then '如果失败是话,退出函数
EnumDomain = False
Exit Function
End If
TmpReturn = WNetEnumResource(hEnumReturn, -1, Buf(0), 1024) 'hEnmReturn是上面的语句得到的句柄
Call WNetCloseEnum(hEnumReturn) 'hEnmReturn是上面的语句得到的句柄
If TmpReturn <> 0 Then '如果失败是话,退出函数
EnumDomain = False
Exit Function
End If
For I = 200 To 1023 '处理缓冲区的数据
'前面的200个字节是没有用的
If Buf(I) <> "0" Then
TmpStr = TmpStr & Chr(Val(Buf(I)))
Else
If TmpStr <> "" Then
If TmpStr = "Microsoft Network" Then
TmpStr = ""
Else
FrmMain.Combo_WorkGroup.AddItem TmpStr
FrmMain.Combo_WorkGroup.Text = TmpStr
TmpStr = ""
End If
End If
End If
Next I
EnumDomain = True
End Function
Private Sub CmdListWorkGroup_Click()
FrmMain.Combo_WorkGroup.Clear
EnumDomain
If EnumDomain = False Then MsgBox "取得工作组失败!"
End Sub
Public Sub Combo_WorkGroup_Click()
Me.List_ComputerName.Clear
Dim NetS As NETRESOURCE
'Dim MaxDomain As Integer
Dim Buf(1024) As Byte
Dim TmpReturn As Long
Dim TmpStr As String
Dim hEnumReturn As Long
Dim I As Integer
NetS.lpRemoteName = FrmMain.Combo_WorkGroup.Text
TmpReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NetS, hEnumReturn)
If TmpReturn <> 0 Then '如果失败是话,退出函数
' EnumDomain = False
Exit Sub
End If
TmpReturn = WNetEnumResource(hEnumReturn, -1, Buf(0), 1024)
If TmpReturn <> 0 Then '如果失败是话,退出函数
' EnumDomain = False
Exit Sub
End If
Call WNetCloseEnum(hEnumReturn)
For I = 200 To 1023 '处理缓冲区的数据
If Buf(I) <> "0" Then
TmpStr = TmpStr & Chr(Val(Buf(I)))
Else
If TmpStr <> "" Then
If TmpStr = "Microsoft Network" Then
TmpStr = ""
Else
If Left(TmpStr, 2) = "\\" Then
TmpStr = Mid(TmpStr, 3)
FrmMain.List_ComputerName.AddItem TmpStr
TmpStr = ""
Else
TmpStr = ""
End If
Public Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Const HEAP_ZERO_MEMORY = &H8
Public Const LM20_NNLEN = 12
Public Type wshare_info_1 'USE FOR WIN98
shi1_netname(13) As Byte
shi1_pad1 As Byte
shi1_type As Integer
shi1_remark As Byte
End Type
Public Type Share_Info_1 'Use for WINNT/2000
shi1_netname As Long
shi1_type As Long
shi1_remark As Long
End Type
Public Type SHARE_INFO_2
shi2_netname As Long
shi2_type As Long
shi2_remark As Long
shi2_permissions As Long
shi2_max_uses As Long
shi2_current_uses As Long
shi2_path As Long
shi2_passwd As Long
End Type
Public Type MungeLong
x As Long
dummy As Integer
End Type
Public Type MungeInt
XLo As Integer
XHi As Integer
dummy As Integer
End Type
Public Const WM_SETTEXT = &HC
Public Const ERROR_SUCCESS = 0
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_NO_SUCH_ALIAS = 1376&
Public Const STYPE_DISKTREE = 0
Public Const STYPE_PRINTQ = 1
Public Const STYPE_DEVICE = 2
Public Const STYPE_IPC = 3
Option Explicit
'Add a Net Share resource
Private Sub CmdAddShare_Click()
Dim strPath As String, strShare As String, nPtrShare As Long
Dim SParray() As Byte, sSarray() As Byte, retVal As Long
Dim nPtrNetName As Long, nPtrPath As Long, nHandleHeap As Long
nHandleHeap = GetProcessHeap()
If nHandleHeap = 0 Then Exit Sub
strPath = Me.Dir1.Path
strShare = StrConv(Right(strPath, Len(strPath) - InStrRev(strPath, "\")),vbUnicode)
strPath = StrConv(Me.Dir1.Path, vbUnicode)
nPtrNetName = HeapAlloc(nHandleHeap, HEAP_ZERO_MEMORY, LenB(strShare) + 1)
nPtrPath = HeapAlloc(nHandleHeap, HEAP_ZERO_MEMORY, LenB(strPath) + 1)
If IsNull(nPtrNetName) Or IsNull(nPtrPath) Then Exit Sub
lstrcpyW ByVal nPtrPath, ByVal strPath
lstrcpyW ByVal nPtrNetName, ByVal strShare
Dim i As Integer
Dim buf(1 To 32) As Byte
For i = 1 To 32
buf(i) = 0
Next
Dim x As Long
Dim tdfShare_Info As SHARE_INFO_2
tdfShare_Info.shi2_netname = nPtrNetName
tdfShare_Info.shi2_type = 0
tdfShare_Info.shi2_remark = 0
tdfShare_Info.shi2_permissions = &HFF
tdfShare_Info.shi2_max_uses = -1
tdfShare_Info.shi2_current_uses = 0
tdfShare_Info.shi2_path = nPtrPath
tdfShare_Info.shi2_remark = 0
'Delete Net Share Resource
Private Sub CMDDeleteShare_Click()
Dim strShareRes As String, retVal As Long
strShareRes = StrConv(Trim(List1.Text), vbUnicode)
retVal = NetShareDel(ByVal 0, strShareRes, 0)
CmdEnum_Click
End Sub
'Enum Net share resource
Private Sub CmdEnum_Click()
Me.List1.Clear
Dim strNetShareName As String, strNetShareRemark As String, nShareType As Long
Dim nLevel As Long
Dim result As Long, bufptr As Long, entriesread As Long, totalentries As Long, resumehandle As Long, BufLen As Long, _
DNArray() As Byte, SNArray(99) As Byte, UNArray() As Byte, _
SName As String, i As Integer, UNPtr As Long, _
TempPtr As MungeLong, TempStr As MungeInt
BufLen = -1 ' Buffer size
resumehandle = 0 ' Start with the first entry
nLevel = 1
Do
result = NetShareEnum(ByVal 0, nLevel, bufptr, BufLen, entriesread, totalentries, resumehandle)
If result <> ERROR_SUCCESS And result <> ERROR_MORE_DATA Then
MsgBox ("Error " & result & " enumerating share " & entriesread & " of " & totalentries)
Exit Sub
End If
Dim j As Long
For i = 1 To entriesread
' Get pointer to string from beginning of buffer
' Copy 4 byte block of memory each time
j = (i - 1) * 3
result = PtrToInt(TempPtr.x, bufptr + j * 4, 4)
result = PtrToStr(SNArray(0), TempPtr.x)
strNetShareName = Left(SNArray, StrLen(TempPtr.x))