局域网中文件夹的共享 Windows NT/2000/XP
局域网中文件夹的共享 Windows NT/2000/XP
在局域网中通过程序实现文件夹的共享,就我知道的应该至少有两种实现方式。一是修改注册表,但是这种方法存在的问题也是很明显的,必须重起机器才能生效。二就是利用 Windows Api函数 NetShareAdd ,通过这个函数我们可以很容易的实现文件夹的共享,而且无需重起计算机。使用这个函数时我们必须注意的是在 Windows NT/2000/XP 和 Windows 95/98/Me 下用法是有很大差别的,这一点我相信大家都有体会,明明在 95 或 98 下实现好好的,可是一到 NT 下就出问题。
{$R *.DFM}
procedure TFormShare.BtSelectClick(Sender: TObject);
var
directory: string;
begin
if SelectDirectory('选择一个目录','', directory) then
EditDir.Text := directory;
end;
procedure TFormShare.Button1Click(Sender: TObject);
begin
if EditDir.Text = '' then
begin
Application.MessageBox('请先选择一个目录!', '共享', MB_ICONINFORMATION + MB_OK);
BtSelect.Click;
Exit;
end;
if EditSharename.Text = '' then
begin
Application.MessageBox('请先输入共享名称!', '共享', MB_ICONINFORMATION + MB_OK);
EditSharename.SetFocus;
Exit;
end;
ShareResource('eengi',EditDir.Text,EditSharename.Text,EditInfo.Text);
{注意:如果在共享目录名称后面添加 $ 符号,共享后在网络邻居里看不到此文件夹但实际上已经共享了,你可以在本地看到}
end;
end.
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: The parameter order is as follows.
NET_API_STATUS NetShareAdd(
LPWSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);
Windows 95/98: 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: 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: 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: See the NetShareAdd Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetShareAdd function.
Windows NT/2000: 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");
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");
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: Requires Windows NT 3.1 or later.
Windows 95/98: 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)