在哪可以找到实现设置和取消文件夹共享的API函数

解决方案 »

  1.   

    样在Delphi程序中设置共享文件夹?
    1、设置HKEY_LOCAL_MACHINE2、SVRAPI.DLL里有NetShareAdd;NetShareDel;NetShareGetInfo;NetShareSetInfo;NetShareAddThe NetShareAdd function shares a server resource.Security RequirementsOnly 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_FUNCTIONNetShareAdd(const char FAR * pszServer,short sLevel,const char FAR * pbBuffer,unsigned short cbBuffer);Parametersservername[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 Meaning2 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 Meaning50 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 ValuesIf 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 MeaningERROR_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. ResWindows 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 drivep.shi2_re = 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) &p, &parm_err);//// If the call succeeds, inform the user.//if(res==0)printf("Share created."); // Otherwise, print an error,// and identify the parameter in error.//elseprintf("Error: %u=%u", 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.RequirementsWindows 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)
      

  2.   

    局域网中文件夹的共享 Windows NT/2000/XP   
    局域网中文件夹的共享 Windows NT/2000/XP在局域网中通过程序实现文件夹的共享,就我知道的应该至少有两种实现方式。一是修改注册表,但是这种方法存在的问题也是很明显的,必须重起机器才能生效。二就是利用 Windows Api函数 NetShareAdd ,通过这个函数我们可以很容易的实现文件夹的共享,而且无需重起计算机。使用这个函数时我们必须注意的是在 Windows NT/2000/XP 和 Windows 95/98/Me 下用法是有很大差别的,这一点我相信大家都有体会,明明在 95 或 98 下实现好好的,可是一到 NT 下就出问题。其实不光是各位仁兄,我早就提出过这个问题,怎奈一直都没有解决掉。现在好了,希望读完后能给大家一点点帮助。Windows 95/98/Me 下 NetShareAdd 函数声明在 SVRAPI.DLL 动态连接库中,而在 2000/XP/NT 下声明在 NETAPI32.DLL 动态连接库中。所以我们在不同的操作系统下一定要注意调用不同的 DLL 库。这些函数详细的声明,在新版 MSDN 2002 中有介绍。由于在Delphi中没有声明这些函数和他们的参数所以我们要想实现这个函数还必须自己声明(可能delphi 有声明我不知道在那个单元中)。顺便说一句,我使用的是 delphi5.0 版,可惜他的帮助文件实在是太陈旧了,还是先看看 MSDN 2002 中关于 NetShareAdd 函数的声明巴!Windows NT/2000/XP: NET_API_STATUS NetShareAdd( 
    LPWSTR servername, //对应 Delphi 中 PWideChar
    DWORD level, //对应 DELPHI 中 DWOED
    LPBYTE buf, //对应 DELPHI 中 PBYTE
    LPDWORD parm_err // 对应 DELPHI 中 PDWORD 
    );
    Windows 95/98/Me: 下面的对应参数就不用说了吧!可以直接看看DELPHI帮助文件。extern API_FUNCTION
    NetShareAdd(
    const char FAR * pszServer, 
    short sLevel, 
    const char FAR * pbBuffer, 
    unsigned short cbBuffer 
    ); 特别强调:我们在声明上面的函数时,函数参数一定要写对,也就是一定要正确对应到DELPHI 自己的类型上。不然函数功能无法实现,这一点我已经尝试了。之所以在NT 下实现不了主要还是,参数类型对应的不对。我们还需要声明一个记录类型,在98/95/me 和 nt/2000/xp下声明如下: 
    Windows NT/2000/XP: SHARE_INFO_2 和 SHARE_INFO_502 结构 
    Windows 95/98/Me: share_info_50 结构对以上这个结构的声明更应该注意参数类型的正确对应。原始声明如下:typedef struct _SHARE_INFO_502 {
    LPWSTR shi502_netname; // PWideChar; 
    DWORD shi502_type; // DWORD; 
    LPWSTR shi502_re; // PWideChar;
    DWORD shi502_permissions; // DWORD; 
    DWORD shi502_max_uses; // DWORD;
    DWORD shi502_current_uses; //DWORD; 
    LPWSTR shi502_path; //PWideChar; 
    LPWSTR shi502_passwd; // PWideChar; 
    DWORD shi502_reserved; // DWORD ;
    // PSECURITY_DESCRIPTOR ;一般设为 Nil
    PSECURITY_DESCRIPTOR shi502_security_descriptor;
    } SHARE_INFO_502, *PSHARE_INFO_502, *LPSHARE_INFO_502;对应 Delphi 纪录声明如下:一定要注意参数类型的正确对应,如果你把PWideChar 声明为 pchar 函数将无法实现此功能,我已经尝试了,你可以再试试,至于原因是什么,我也不太清楚。type 
    TSHARE_INFO_502 = record
    shi502_netname: PWideChar;
    shi502_type: DWORD;
    shi502_re: 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;下面是完成的程序代码,其中有两部分,主程序和单元文件。运行环境 windows 2000 Ads 开发工具 Delphi5.0 。运行通过。unit Share; 
    interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    StdCtrls,FileCtrl,My_Share;
    type
    TFormShare = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BtSelect: TButton;
    EditDir: TEdit;//文件共享目录
    EditSharename: TEdit; //共享名称
    EditInfo: TEdit;//备注
    Button1: TButton;
    Button2: TButton;
    procedure BtSelectClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end; 
    var
    FormShare: TFormShare;
    implementation{$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.以下是单元文件: 
    unit My_Share;
    interface
    uses
    Windows,Sysutils ;
    type
    //纪录类型声明,注意参数类型的正确对应,最好别看 delphi 的帮助,引起误导
    TSHARE_INFO_502 = record
    shi502_netname: PWideChar;
    shi502_type: DWORD;
    shi502_re: 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;
    //添加共享
    function NetShareAdd(servername:Widestring; level: DWORD; Buf: PBYTE;
    var parm_err: PDWORD ): DWORD; stdcall;
    //删除共享
    function NetShareDel(ServerName:Widestring; NetName: Widestring;
    Reserved: DWord): Integer; StdCall;
    const
    {共享类型}
    STYPE_DISKTREE = 0 ;
    STYPE_PRINTQ = 1 ;
    STYPE_DEVICE = 2 ;
    STYPE_IPC = 3 ;
    {访问权限}
    ACCESS_READ = 0 ;
    ACCESS_WRITE = 1 ;
    ACCESS_CREATE = 2 ;
    ACCESS_EXEC = 3 ;
    ACCESS_DELETE = 4 ;
    ACCESS_ALL = 7 ;//自己声明的函数,为了调用方便,参数就不用说明了吧!
    function ShareResource(ServerName,FilePath,NetName, Re : string): Integer;
    //function DeleteShare(ServerName: string; NetName: string): Integer;
    implementation
    //注意在 windows95/98/me 下面 dll 库是 SVRAPI.DLL ,而且参数类型也要随之改变的吆!
    function NetShareAdd; external 'netapi32.DLL' name 'NetShareAdd';
    function NetShareDel; external 'netapi32.DLL' name 'NetShareDel';function ShareResource(ServerName,FilePath,NetName, Re : string): Integer;var 
    ShInfo: TSHARE_INFO_502; 
    parm_err:PDWORD; 
    _FilePath,_NetName, _Re : PWideChar ; 
    _ServerName : Pchar ; 
    begin 
    GetMem(_ServerName,255) ; //分配内存
    GetMem(_FilePath,255);
    GetMem(_NetName,255);
    GetMem(_Re,255);
    StringToWideChar(FilePath,_FilePath,255); //字符串转换,一定要转换正确
    StringToWideChar(NetName,_NetName,255);
    StringToWideChar(Re,_Re,255);
    strpcopy(_ServerName,ServerName);
    //开始创建结构
    with ShInfo do
    begin
    shi502_netname := _NetName;
    shi502_type := STYPE_DISKTREE ;
    shi502_re := _Re ;
    shi502_max_uses := $FFFFFFFF;
    shi502_current_uses := 10;
    shi502_path := _FilePath;
    shi502_passwd := nil;
    shi502_reserved := 0;
    shi502_security_descriptor := nil;
    shi502_permissions := ACCESS_ALL;
    end;
    try
    Result := NetShareAdd(_ServerName, 502, @ShInfo, parm_err);
    Finally // 别忘了释放内存
    FreeMem(_ServerName,255);
    FreeMem(_FilePath,255);
    FreeMem(_NetName,255);
    FreeMem(_Re,255);
    end;
    end;
    end.总结:运行完程序后,相信大家和我有同样的感觉,其实成功就在眼前,就差一点,可是就这一点也很难跨越。程序之所以一直实现不了,恐怕我们都没有考虑参数类型声明是否正确。不过我应该非常感谢csdn 上“绝对菜鸟”的回复。要不是他的程序,恐怕我还在等待中探索