高手请进!有三个问题在等着您!详情请。。。。(200分)

Z

zyf23

Unregistered / Unconfirmed
GUEST, unregistred user!
(1)、最近在编写一个局域网中数据的异地导入功能!
可是delphi中没有可以直接获取网络上的共享资源的控件,我想要实现的功能是:象浏览器一
样,用户直接在程序中可选择网上邻居的文件!
(2)、如何在程序中把想要共享的目录共享出来!
(3)、第三个问题小弟眼拙,代码居然看不懂:
var
DriveBits: set of 0..25;
begin
Integer(DriveBits) := GetLogicalDrives;//???????????
for DriveNum := 0 to 25 do
begin
if not(DriveNum in DriveBits) then
begin
............
end
else .......
end;
/// integer的用法实在让人不解,小弟查查帮助也不明!

高手请不吝赐教!
先声谢过!
 
得到网络邻居的信息

下面两个过程可能会对你有用

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;

Itm:=Items.Add(TV.Selected,string(NetRes[0].lpProvider));
Itm.ImageIndex:=0;
Itm.SelectedIndex:=0;

{ 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 , 你就可以看到
和网络邻居中一样的机器列表

Yorai Aminov
El-On Software Systems

unit NetBrwsr;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TNetScope = (nsConnected, nsGlobal, nsRemembered, nsContext);

TNetResourceType = (nrAny, nrDisk, nrPrint);
TNetDisplay = (ndDomain, ndGeneric, ndServer, ndShare, ndFile, ndGroup,
ndNetwork, ndRoot, ndShareAdmin, ndDirectory, ndTree, ndNDSContainer);
TNetUsage = set of (nuConnectable, nuContainer);

TNetworkItems = class;

TNetworkItem = class
private
FScope: TNetScope;
FResourceType: TNetResourceType;
FDisplay: TNetDisplay;
FUsage: TNetUsage;
FLocalName: string;
FRemoteName: string;
FComment: string;
FProvider: string;
FSubItems: TNetworkItems;
public
constructor Create;
destructor Destroy; override;
property Scope: TNetScope read FScope;

property ResourceType: TNetResourceType read FResourceType;
property Display: TNetDisplay read FDisplay;
property Usage: TNetUsage read FUsage;
property LocalName: string read FLocalName;
property RemoteName: string read FRemoteName;
property Comment: string read FComment;
property Provider: string read FProvider;
property SubItems: TNetworkItems read FSubItems;
end;

TNetworkItems = class
private
FList: TList;
procedure SetItem(Index: Integer; Value: TNetworkItem);
function GetItem(Index: Integer): TNetworkItem;

function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Item: TNetworkItem);
procedure Delete(Index: Integer);
property Items[Index: Integer]: TNetworkItem read GetItem write
SetItem; default;
property Count: Integer read GetCount;
end;

TNetworkBrowser = class(TComponent)
private
FItems: TNetworkItems;
FScope: TNetScope;
FResourceType: TNetResourceType;
FUsage: TNetUsage;
FActive: Boolean;
procedure Refresh;
procedure SetActive(Value: Boolean);
procedure SetScope(Value: TNetScope);

procedure SetResourceType(Value: TNetResourceType);
procedure SetUsage(Value: TNetUsage);
procedure EnumerateNet(NetItems: TNetworkItems; lpnr: PNetResource);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open;
procedure Close;
property Items: TNetworkItems read FItems;
published
property Scope: TNetScope read FScope write SetScope default nsGlobal;
property ResourceType: TNetResourceType read FResourceType
write SetResourceType default nrAny;
property Usage: TNetUsage read FUsage write SetUsage default [];

property Active: Boolean read FActive write SetActive default False;
end;

implementation

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;

{ TNetworkBrowser }

constructor TNetworkBrowser.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FItems := TNetworkItems.Create;
FScope := nsGlobal;
FResourceType := nrAny;
FUsage := [];
end;

destructor TNetworkBrowser.Destroy;
begin
if FItems <> nil then
FItems.Free;

inherited;
end;

procedure TNetworkBrowser.EnumerateNet(NetItems: TNetworkItems; lpnr:
PNetResource);
var
dwResult, dwResultEnum: Integer;
hEnum: THandle;
cbBuffer, cEntries, i: Integer;
nrArray: PNetResourceArray;
NewItem: TNetworkItem;
dwScope, dwType, dwUsage: Integer;
begin
cbBuffer := 16384;
cEntries := $FFFFFFFF;

case FScope of
nsConnected: dwScope := RESOURCE_CONNECTED;
nsGlobal: dwScope := RESOURCE_GLOBALNET;
nsRemembered: dwScope := RESOURCE_REMEMBERED;

nsContext: dwScope := RESOURCE_CONTEXT;
else
dwScope := RESOURCE_GLOBALNET;
end;
case FResourceType of
nrAny: dwType := RESOURCETYPE_ANY;
nrDisk: dwType := RESOURCETYPE_DISK;
nrPrint: dwType := RESOURCETYPE_PRINT;
else
dwType := RESOURCETYPE_ANY;
end;
dwUsage := 0;
if nuConnectable in FUsage then
dwUsage := dwUsage or RESOURCEUSAGE_CONNECTABLE;
if nuContainer in FUsage then
dwUsage := dwUsage or RESOURCEUSAGE_CONTAINER;

dwResult := WNetOpenEnum(dwScope, dwType, dwUsage, lpnr, hEnum);

if dwResult <> NO_ERROR then Exit;

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.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.dwType of
RESOURCETYPE_ANY: NewItem.FResourceType := nrAny;
RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;
RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;
else
NewItem.FResourceType := nrAny;
end;
case nrArray.dwDisplayType of
RESOURCEDISPLAYTYPE_GENERIC: NewItem.FDisplay := ndGeneric;
RESOURCEDISPLAYTYPE_DOMAIN: NewItem.FDisplay := ndDomain;

RESOURCEDISPLAYTYPE_SERVER: NewItem.FDisplay := ndServer;
RESOURCEDISPLAYTYPE_SHARE: NewItem.FDisplay := ndShare;
RESOURCEDISPLAYTYPE_FILE: NewItem.FDisplay := ndFile;
RESOURCEDISPLAYTYPE_GROUP: NewItem.FDisplay := ndGroup;
RESOURCEDISPLAYTYPE_NETWORK: NewItem.FDisplay := ndNetwork;
RESOURCEDISPLAYTYPE_ROOT: NewItem.FDisplay := ndRoot;
RESOURCEDISPLAYTYPE_SHAREADMIN: NewItem.FDisplay :=

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.dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then
Include(NewItem.FUsage, nuConnectable);
if nrArray.dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then

Include(NewItem.FUsage, nuContainer);
NewItem.FLocalName := StrPas(nrArray.lpLocalName);
NewItem.FRemoteName := StrPas(nrArray.lpRemoteName);
NewItem.FComment := StrPas(nrArray.lpComment);
NewItem.FProvider := StrPas(nrArray.lpProvider);
NetItems.Add(NewItem);
// if container, call recursively
if (nuContainer in NewItem.FUsage) and (FScope <> nsContext) then
EnumerateNet(NewItem.FSubItems, @nrArray)

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;

end.



--------------------------------------------------------------------------------


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.


 
第一个问题比较简单,Delphi6里有现成的控件--TShellTreeView.
第二个问题可以使用NetShareAdd函数,下面文字来自Delphi的帮助。
The NetShareAdd function shares a server resource.

Security Requirements

Only members of the Administrators or Account Operators local group or those with Communication, Print, or Server operator group membership can successfully execute NetShareAdd. The Print operator can add only Printer queues. The Communication operator can add only communication-device queues.

NET_API_STATUS NetShareAdd(

LPTSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);


Parameters

servername

Pointer to a Unicode string containing the name of the remote server on which the function is to execute. A NULL pointer or string specifies the local computer.

level

Specifies one of the following values to set the level of information provided.

Value Meaning
2 The buf parameter points to an array of SHARE_INFO_2 structures.
502 The buf parameter points to an array of SHARE_INFO_502 structures.


buf

Pointer to the buffer in which the data set with the level parameter is stored.

parm_err

Optional pointer to a DWORD that contains the index of the first parameter that causes ERROR_INVALID_PARAMETER. If NULL, the parameter is not returned on error.



See Also

NetShareDel
 
小弟用的是d5,不知道d6的东西,首先谢过硕鼠!

我正在测试!有结果一定给楼上加。。。。
 
1.你用TOpenDialog,也可以选择网上邻居里的共享目录。
2.NetShareAdd()函数的详细应用和参数意义,可以看MSDN.
eg.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type

PShare2Info = ^TShare2Info;

TShare2Info = packed Record
ShareName: PWideChar;
ShareType: DWORD;
ShareRemark: PWideChar;
SharePermissions: DWORD;
ShareMaxUser: DWORD;
ShareCurUser: DWORD;
SharePath: PWideChar;
SharePasswd: PWideChar;
end;

TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TSHARE_INFO_502 = record //根据API里那个Struct的要求而用Delphi仿制的
shi502_netname: PWideChar;
shi502_type: DWORD;
shi502_remark: PWideChar;
shi502_permissions: DWORD;
shi502_max_uses: DWORD;
shi502_current_uses: DWORD;
shi502_path: PWideChar;
shi502_passwd: PWideChar;
shi502_reserved: DWORD;
shi502_security_descriptor: PSECURITY_DESCRIPTOR;
end;


// PBYTE = ^BYTE;
// PWideChar=^WideChar;
function NetShareAdd502(ServerName:pWideChar;
level: DWORD;
buf: PBYTE;
VAR parm_err: DWORD ): DWORD; stdcall;

function NetShareAdd(ServerName:pWideChar;
Level: DWord;
Buf: PByte;
var Parm_err: DWORD
): DWORD; stdcall;

function NetShareDel(ServerName: PWideChar;
NetName: PWideChar;
Reserved: DWord): DWord; stdcall
var
Form1: TForm1;

implementation

{$R *.DFM}

function NetShareAdd; external 'netapi32.dll' name 'NetShareAdd';
function NetShareAdd502; external 'netapi32.DLL' name 'NetShareAdd';
function NetShareDel; external 'netapi32.DLL' name 'NetShareDel';

// Use NetShareAdd;
procedure TForm1.Button1Click(Sender: TObject);
var
FInfo: TShare2Info;
P, S: Dword;
begin
FInfo.ShareName := 'abc';
FInfo.ShareType := 0;
Finfo.ShareRemark := nil;
FInfo.SharePermissions :=0;
FInfo.ShareMaxUser := $FFFFFFFF;
FInfo.ShareCurUser := 0;
FInfo.SharePath := 'D:/persnal';
FInfo.SharePasswd := 'shdjdm';

S := NetShareAdd('//shd2k', 2, @FInfo, p);
Edit1.Text := IntToStr(S);
if S = 0 then ShowMessage('OKOK')
end;

// Use NetShareAdd502;
procedure TForm1.Button2Click(Sender: TObject);
var
si: TSHARE_INFO_502;
r: DWORD;
parm_err:Dword;
begin
si.shi502_netname := 'share1'; //共享名
si.shi502_type := 0; //STYPE_DISKTREE
si.shi502_remark := nil;
si.shi502_max_uses := $FFFFFFFF;
si.shi502_current_uses := 10;
si.shi502_path := 'D:/test'; //在机器shd2k上的绝对路径
si.shi502_passwd := nil; //共享目录口令
si.shi502_reserved := 0;
si.shi502_security_descriptor := nil;
// si.shi502_permissions:=TRUSTEE_ACCESS_ALL;
r := NetShareAdd502('//Shd2k', 502, @si, parm_err );
Edit1.Text := Format( '%d', [r] ); //显示结果总是53--未知的机器名
Edit2.Text:=inttostr(parm_err);
end;

// Use NetShareDel;
procedure TForm1.Button3Click(Sender: TObject);
begin
Edit2.Text := IntTOstr(NetShareDel('//shd2k', 'Share1', 0));
end;

end.

3.强制转换位整型数据。
 
to shd:
先声谢!
第二问题我试了一下,提示我:连接文件project.exe到不存在的输出Netapi32.dll:NetShareAdd;
我把Netapi32.dll放置在同一目录下,仍是如此;是不是函数名要区分大小写?
我对你的实现思路仍是不明,函数的声明也不明白。
interface
.
.
.
private
{ Private declarations }
public
{ Public declarations }
end;
function NetShareAdd502(ServerName:pWideChar;level: DWORD;buf: PBYTE;vAR parm_err: DWORD ): DWORD; stdcall;
function NetShareAdd(ServerName:pWideChar;Level: DWord;Buf: PByte;var Parm_err: DWORD): DWORD; stdcall;
function NetShareDel(ServerName: PWideChar; NetName: PWideChar; Reserved: DWord): DWord; stdcall;
var
Form4: TForm4;

implementation
{$R *.DFM}
function NetShareAdd; external 'netapi32.dll' name 'NetShareAdd';
function NetShareAdd502; external 'netapi32.DLL' name 'NetShareAdd502';
function NetShareDel; external 'netapi32.DLL' name 'NetShareDel';

procedure TForm1.Button1Click(Sender: TObject);
var
FInfo: TShare2Info;
P, S: Dword;
begin
FInfo.ShareName := 'abc';
FInfo.ShareType := 0;
Finfo.ShareRemark := nil;
FInfo.SharePermissions :=0;
FInfo.ShareMaxUser := $FFFFFFFF;
FInfo.ShareCurUser := 0;
FInfo.SharePath := 'D:/persnal';
FInfo.SharePasswd := 'shdjdm';

S := NetShareAdd('//shd2k', 2, @FInfo, p);
Edit1.Text := IntToStr(S);
if S = 0 then ShowMessage('OKOK')
end;
这样做编译也能通过,但运行不起来!提示是:接文件project.exe到不存在的输出Netapi32.dll:NetShareAdd;
这种用法我在书上没见过!!
眼拙,还望明示!
(3)GetLogicalDrives 的返回值是一个数
var
DriveBits: set of 0..25;
begin
Integer(DriveBits) := GetLogicalDrives;//???????????
for DriveNum := 0 to 25 do
begin
if not(DriveNum in DriveBits) then
begin
............
end
else .......
end;
但这段代码的反回值是多个,并且集合DriveBits中的值是[0,2..6](我有4个逻辑驱动器)
integer一次应该只能是一个值吧,可是它的操作数是个集合。。
不明白!


 
GetLogicalDrives返回在是一个整数,每一位为1表示该位代表的盘符存在,第0位代表A盘。
而array of 0..25中内存中也是一个整数,每一位为1表示该位代表的集合成员存在,第0位
代表最小的成员。在例中是整数0,所以才这样使用。
 
我的环境是2000/NT + Delphi5
和大小写无关。
 
NetShareAdd函数必须在NT下运行,在95/98下设置目录的共享必须使用另外一个函数,可惜我

不记得了,在win32 api中肯定有
 
delphi 6 在 Samples 页新增加了三个作资源管理器的东东,应该满足你的要求。
 
TO LXGGC:
拜托你好好想想,好好想想!
 
你看一下MSDN的说明,支持NT3.51、win95 以上:
实在不行,把你的mail给我,我给你发。
Platform SDK: Network Management
NetShareAdd
The NetShareAdd function shares a server resource.

Security Requirements
Only members of the Administrators or Account Operators local group or those with Communication, Print, or Server operator group membership can successfully execute the NetShareAdd function. The Print operator can add only Printer queues. The Communication operator can add only communication-device queues.

Windows NT/2000 or later: The parameter order is as follows.

NET_API_STATUS NetShareAdd(
LPWSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);
Windows 95/98/Me: You must specify the size of the information buffer, in bytes, using the cbBuffer parameter. The Windows NT/Windows 2000 parm_err parameter is not available on this platform. Therefore, the parameter list is as follows.

extern API_FUNCTION
NetShareAdd(
const char FAR * pszServer,
short sLevel,
const char FAR * pbBuffer,
unsigned short cbBuffer
);
Parameters
servername
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the name of the remote server on which the function is to execute. The string must begin with //. If this parameter is NULL, the local computer is used.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.
Windows NT/2000 or later: The following levels are valid. Value Meaning
2 Specifies information about the shared resource, including name of the resource, type and permissions, and number of connections. The buf parameter points to a SHARE_INFO_2 structure.
502 Specifies information about the shared resource, including name of the resource, type and permissions, number of connections, and other pertinent information. The buf parameter points to a SHARE_INFO_502 structure.



Windows 95/98/Me: The following level is valid. Value Meaning
50 Specifies information about the shared resource, including the name and type of the resource, a comment associated with the resource, and passwords. The pbBuffer parameter points to a share_info_50 structure. Note that the string you specify in the shi50_path member must contain only uppercase characters. If the path contains lowercase characters, calls to NetShareAdd can fail with NERR_UnknownDevDir or ERROR_BAD_NET_NAME.



buf
[in] Pointer to the buffer that specifies the data. The format of this data depends on the value of the level parameter.
parm_err
[out] Pointer to a DWORD value that receives the index of the first member of the share information structure that causes the ERROR_INVALID_PARAMETER error. If this parameter is NULL, the index is not returned on error. For more information, see the NetShareSetInfo function.
Return Values
If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
ERROR_INVALID_LEVEL The value specified for the level parameter is invalid.
ERROR_INVALID_NAME The character or file system name is invalid.
ERROR_INVALID_PARAMETER The specified parameter is invalid.
NERR_DuplicateShare The share name is already in use on this server.
NERR_RedirectedPath The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.
NERR_UnknownDevDir The device or directory does not exist.


Remarks
Windows 95/98/Me: See the NetShareAdd Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetShareAdd function.

Windows NT/2000 or later: The following code sample demonstrates how to share a network resource using a call to the NetShareAdd function. The code sample fills in the members of the SHARE_INFO_2 structure and calls NetShareAdd, specifying information level 2.

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

void wmain( int argc, TCHAR *argv[ ])
{
NET_API_STATUS res;
SHARE_INFO_2 p;
DWORD parm_err = 0;

if(argc<2)
printf("Usage: NetShareAdd server/n");
else
{
//
// Fill in the SHARE_INFO_2 structure.
//
p.shi2_netname = TEXT("TESTSHARE");
p.shi2_type = STYPE_DISKTREE; // disk drive
p.shi2_remark = TEXT("TESTSHARE to test NetShareAdd");
p.shi2_permissions = 0;
p.shi2_max_uses = 4;
p.shi2_current_uses = 0;
p.shi2_path = TEXT("C://");
p.shi2_passwd = NULL; // no password
//
// Call the NetShareAdd function,
// specifying level 2.
//
res=NetShareAdd(argv[1], 2, (LPBYTE) &amp;p, &amp;parm_err);
//
// If the call succeeds, inform the user.
//
if(res==0)
printf("Share created./n");

// Otherwise, print an error,
// and identify the parameter in error.
//
else
printf("Error: %u/tparmerr=%u/n", res, parm_err);
}
return;
}
If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions. For more information, see IADsFileShare.

Requirements
Windows NT/2000 or later: Requires Windows NT 3.1 or later.
Windows 95/98/Me: Requires Windows 95 or later.
Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).

See Also
Network Management Overview, Network Management Functions, Share Functions, NetShareDel, NetShareSetInfo, SHARE_INFO_2, SHARE_INFO_502, share_info_50

Platform SDK Release: February 2001 Contact Platform SDK Order a Platform SDK CD Online



Requirements
Windows NT/2000 or later: Requires Windows NT 3.1 or later.
Windows 95/98/Me: Requires Windows 95 or later.
Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).
See Also
Network Management Overview, Network Management Functions, Share Functions, NetShareDel, NetShareSetInfo, SHARE_INFO_2, SHARE_INFO_502, share_info_50
 
我的mail:zyf23@163.net
oCIQ:15878778
欢迎随时“拨打”;
 
接受答案了.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
975
SUNSTONE的Delphi笔记
S
D
回复
0
查看
750
DelphiTeacher的专栏
D
D
回复
0
查看
752
DelphiTeacher的专栏
D
D
回复
0
查看
600
DelphiTeacher的专栏
D
顶部