现在的位置: 首页 > 综合 > 正文

如何建立共享目录

2013年09月07日 ⁄ 综合 ⁄ 共 5120字 ⁄ 字号 评论关闭
 急的发抖:如何建立共享目录?
楼主zhouhui123(菲菲)2005-12-26 11:43:55 在 VC/MFC / 基础类 提问

急死了,请教如何建立共享目录,请提供点代码
问题点数:100、回复次数:7Top

1 楼Mackz(在相互)回复于 2005-12-26 12:00:19 得分 0

NetShareAdd  
   
  The   NetShareAdd   function   shares   a   server   resource.  
   
   
  NetShareAdd   Sample   (Windows   95/98/Me)  
   
  The   following   code   sample   demonstrates   how   to   share   a
  resource   on   the   local   computer   with   a   call   to   the  
NetShareAdd   function.  
   
   
  The   code  
sample   specifies   the   share_info_50   structure   and   no  
password   on   the   share.   The   sample   also   allocates   and  
frees   the   memory   required   for   the   information   buffer.  
   
  #include   <stdio.h>  
  #include   <windows.h>    
  #include   <svrapi.h>  
   
  int   main(int   argc,   char   FAR   *   argv[])  
  {  
        char   FAR   *   pszServerName   =   NULL;  
        short   nLevel   =   50;  
        struct   share_info_50*   pBuf   =   NULL;  
        unsigned   short   cbBuffer;  
        NET_API_STATUS   nStatus;  
        //  
        //   ServerName   can   be   NULL   to   indicate   the   local   computer.  
        //  
        if   ((argc   <   3)   ||   (argc   >   4))  
        {  
              printf("Usage:   %s   [////ServerName]   ShareName   SharePath/n",   argv[0]);  
              exit(1);  
        }  
   
        if   (argc   ==   4)  
              pszServerName   =   argv[1];  
        //  
        //   Allocate   the   memory   required   to   specify   a    
        //       share_info_50   structure.  
        //  
        cbBuffer   =   sizeof(struct   share_info_50);  
        pBuf   =   malloc(cbBuffer);  
   
        if   (pBuf   ==   NULL)  
              printf("No   memory/n");  
        //  
        //   Assign   values   to   the   share_info_50   structure.  
        //  
        strcpy(pBuf->shi50_netname,   argv[argc-2]);  
        pBuf->shi50_type   =   STYPE_DISKTREE;  
        pBuf->shi50_flags   =   SHI50F_FULL;  
        pBuf->shi50_remark   =   NULL;  
        pBuf->shi50_path   =   argv[argc-1];  
        pBuf->shi50_rw_password[0]   =   '/0';   //   No   password  
        pBuf->shi50_ro_password[0]   =   '/0';   //   No   password  
        //  
        //   Call   the   NetShareAdd   function  
        //     specifying   information   level   50.  
        //  
        nStatus   =   NetShareAdd(pszServerName,  
                                                    nLevel,  
                                                    (char   FAR   *)pBuf,  
                                                    cbBuffer);  
        //  
        //   Display   the   result   of   the   function   call.  
        //  
        if   (nStatus   ==   NERR_Success)  
              printf("Share   added   successfully/n");  
        else  
              fprintf(stderr,   "A   system   error   has   occurred:   %d/n",   nStatus);  
        //  
        //   Free   the   allocated   memory.  
        //  
        if   (pBuf   !=   NULL)  
              free(pBuf);  
   
        return   0;  
  }  
   
  Top

2 楼laiyiling(陌生人[MVP])回复于 2005-12-26 12:06:43 得分 0

共享/删除共享除了直接调用标准的Win32API函数NetShareAdd和NetShareDel  
   
  回复人:   acptvb(微软全球技术中心   VB技术支持)    
  您还可以通过ADSI方式的IADsFileShare对象设置共享,如下例(VB):  
   
  Sub   foo()  
   
              Dim   comp   As   IADsComputer  
              Dim   serv   As   IADsService  
              Dim   fserv   As   IADsContainer  
              Dim   share   As   IADsFileShare  
              Dim   shareNew   As   IADsFileShare  
              Dim   v   As   Variant  
   
              '   Replace   DOMAIN,   SERVER   &   SHARE   with   the   appropriate  
              '   domain,   server     and   share   names  
   
              Set   share   =   GetObject("WinNT://DOMAIN/SERVER/lanmanserver/SHARE")  
   
              v   =   share.Path   '   Gets   directory   path   on   server  
   
              Set   share   =   nothing  
   
              '   Replace   DOMAIN   &   SERVER   with   the   appropriate   domain   and   server   names  
   
              Set   fserv   =   GetObject("WinNT://DOMAIN/SERVER/lanmanserver")  
   
              '   Enumerate   existing   shares  
   
              For   Each   share   In   fserv  
                  v   =   share.Class  
                  v   =   share.ADsPath  
                  v   =   share.HostComputer  
                  v   =   share.Path  
              Next   share  
   
              '   Create   share   in   fileservice   container  
   
              Set   shareNew   =   fserv.Create("fileshare",   "newshare")  
              shareNew.Path   =   "C:/"  
              shareNew.SetInfo     '   Commit   new   share  
   
              '   Delete   share  
   
              fserv.Delete   "fileshare",   "newshare"  
   
              '   Fails   since   share   is   gone  
   
              shareNew.GetInfo  
   
  End   Sub  
   
  请参考下面的例子:  
   
  Q169398   HOWTO:   Manipulate   File   Shares   with   ADSI   (VB   Sample)  
  http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q169398  
   
  Q234234   How   to   Manage   File   Shares   Using   ADSI  
  http://support.microsoft.com/support/kb/articles/q234/2/34.asp  
   
   
  然后,您可以利用ADsSecurity.dll   来获取文件夹的security  
descriptor,通过IADsAccessControlList的AddAce方法为文件夹的AccessControlList添加用户权限。
       
   
  您可以在以下链接获得相关代码及ADsSecurity.dll的有关信息。  
  HOWTO:   Use   Visual   Basic   and   ADsSecurity.dll   to   Properly   Order   ACEs   in   an   ACL  
  http://support.microsoft.com/default.aspx?scid=kb;en-us;Q269159  
   
  HOWTO:   Use   ADsSecurity.dll   to   Add   an   Access   Control   Entry   to   an   NTFS   Folder  
  http://support.microsoft.com/default.aspx?scid=kb;en-us;Q279682  
   
  需要注意的是,如果您的文件夹包含子目录,文件夹的权限不会自动的向下传播,您需要递归的设置所有子目录的权限属性。  
  相关代码及详细信息请参考:  
  Q266461   HOWTO:Set   Automatic   Inheritance   of   File/Folder   Permissions-   ADSI  
  http://support.microsoft.com/support/kb/articles/q266/4/61.aspTop

3 楼snakebite2008(3DFX)回复于 2005-12-26 12:19:11 得分 0

为什么微软的大部分例程都是VB的?  
  Top

4 楼wangk(倒之)回复于 2005-12-26 14:45:39 得分 0

来晚了。  
  补充:使用NetShareAdd在98和nt系列(NT、2000、Xp)是要包含不同的头文件。  
  98:  
  #include   <Svrapi.h>  
  #pragma   comment(lib,"Svrapi.lib")  
   
  nt:  
  #include   <Lm.h>  
  #pragma   comment(lib,"Netapi32.lib")Top

5 楼yayaniuniu502(老唐)回复于 2005-12-28 10:56:35 得分 0

好强,帮顶一个Top

6 楼aa3000(杀手K)回复于 2005-12-28 11:13:09 得分 0

我也顶一下呀Top

7 楼ytfrdfiw()回复于 2006-04-27 08:52:16 得分 0

太强,五颗星都来了。Top

相关问题

抱歉!评论已关闭.