在windows 2000下怎样将指定文件夹设置成共享。
完全共享。怎样实现??
谢谢大家!!

解决方案 »

  1.   

    NET_API_STATUS NetShareAdd(
      LPWSTR servername, 
      DWORD level,       
      LPBYTE buf,        
      LPDWORD parm_err   
    );
      

  2.   

    #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_re = 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;
    }
      

  3.   

    直接使用MSDN中的例子有问题的。我修改了一些,才能用了,我把我的创建函数给你看看吧:
    void CAddShareDlg::OnOK() 
    {
    // TODO: Add extra validation here
    UpdateData(); TCHAR szShareName[255];
    WCHAR wszShareName[255];
    TCHAR szShareDir[255];
    WCHAR wszShareDir[255];
        SHARE_INFO_502 si502;
        NET_API_STATUS nas;
        SECURITY_DESCRIPTOR sd;    if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
    return; sprintf(szShareName, m_csNetname);
    sprintf(szShareDir, m_csPath);
    MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255);
    MultiByteToWideChar(CP_ACP, 0, szShareDir, -1, wszShareDir, 255);    si502.shi502_netname = (char*)wszShareName;
        si502.shi502_type = STYPE_DISKTREE;
        si502.shi502_re = NULL;
        si502.shi502_permissions = 0;
    if(m_nUsernumMode == 0)
    si502.shi502_max_uses = SHI_USES_UNLIMITED;
    else
    si502.shi502_max_uses = m_nUsernum;
    if(m_nPermission == 0)
    si502.shi502_permissions = ACCESS_READ;
    else 
    si502.shi502_permissions = ACCESS_ALL;
        si502.shi502_current_uses = 0;
        si502.shi502_path = (char*)wszShareDir;
        si502.shi502_passwd = NULL;
        si502.shi502_reserved = 0;
        si502.shi502_security_descriptor = &sd;    nas = NetShareAdd(
            NULL,         // share is on local machine
            502,            // info-level
            (LPBYTE)&si502, // info-buffer
            NULL            // don't bother with parm
            );
    if(nas != NO_ERROR)
    {
    AfxMessageBox("创建共享目录失败!");
    CDialog::OnCancel();
    }
    else
    {
    // SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH, (LPCVOID)szShareDir, NULL);
    }
    g_csNetname = m_csNetname;

    CDialog::OnOK();
    }
      

  4.   

    直接使用MSDN中的例子有问题的。我修改了一些,才能用了,我把我的创建函数给你看看吧:
    void CAddShareDlg::OnOK() 
    {
    // TODO: Add extra validation here
    UpdateData(); TCHAR szShareName[255];
    WCHAR wszShareName[255];
    TCHAR szShareDir[255];
    WCHAR wszShareDir[255];
        SHARE_INFO_502 si502;
        NET_API_STATUS nas;
        SECURITY_DESCRIPTOR sd;    if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
    return; sprintf(szShareName, m_csNetname);
    sprintf(szShareDir, m_csPath);
    MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255);
    MultiByteToWideChar(CP_ACP, 0, szShareDir, -1, wszShareDir, 255);    si502.shi502_netname = (char*)wszShareName;
        si502.shi502_type = STYPE_DISKTREE;
        si502.shi502_re = NULL;
        si502.shi502_permissions = 0;
    if(m_nUsernumMode == 0)
    si502.shi502_max_uses = SHI_USES_UNLIMITED;
    else
    si502.shi502_max_uses = m_nUsernum;
    if(m_nPermission == 0)
    si502.shi502_permissions = ACCESS_READ;
    else 
    si502.shi502_permissions = ACCESS_ALL;
        si502.shi502_current_uses = 0;
        si502.shi502_path = (char*)wszShareDir;
        si502.shi502_passwd = NULL;
        si502.shi502_reserved = 0;
        si502.shi502_security_descriptor = &sd;    nas = NetShareAdd(
            NULL,         // share is on local machine
            502,            // info-level
            (LPBYTE)&si502, // info-buffer
            NULL            // don't bother with parm
            );
    if(nas != NO_ERROR)
    {
    AfxMessageBox("创建共享目录失败!");
    CDialog::OnCancel();
    }
    else
    {
    // SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH, (LPCVOID)szShareDir, NULL);
    }
    g_csNetname = m_csNetname;

    CDialog::OnOK();
    }
      

  5.   

    #include <windows.h> 
    #include <lm.h> 
    #include <stdio.h>  
    #define RTN_OK 0 
    #define RTN_USAGE 1 
    #define RTN_ERROR 13 
     // Note: UNICODE entry point and argv.  This way, we don't need to bother 
    // with converting commandline args to Unicode //  
    int __cdecl wmain(     int argc,     wchar_t *argv[]     ) 
    {     
    LPWSTR DirectoryToShare;     
    LPWSTR Sharename;     
    LPWSTR Username;     
    LPWSTR Server;      
    PSID pSid = NULL;     
    DWORD cbSid;      
    WCHAR RefDomain[DNLEN + 1];     
    DWORD cchDomain = DNLEN + 1;     
    SID_NAME_USE peUse;      
    SECURITY_DESCRIPTOR sd;     
    PACL pDacl = NULL;     
    DWORD dwAclSize;      
    SHARE_INFO_502 si502;     
    NET_API_STATUS nas;      
    BOOL bSuccess = FALSE; // assume this function fails      
    if(argc < 4) 
    {         
    printf("Usage: %ls <directory> <sharename> <user/group> [\\\\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     //      
    DirectoryToShare = argv[1];     
    Sharename = argv[2];     
    Username = argv[3];      
    if( argc > 4 ) 
    {         
    Server = argv[4];     

    else 
    {         
    Server = NULL; 
    // local machine     
    }      
    //     // initial allocation attempt for Sid     // 
    #define SID_SIZE 96     
    cbSid = SID_SIZE;      
    pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);     
    if(pSid == NULL) 
    {
              printf("HeapAlloc error!\n");         
              return RTN_ERROR;     
              }      
              //     // get the Sid associated with the supplied user/group name     
              // force Unicode API since we always pass Unicode string     //      
              if(!LookupAccountNameW(NULL, // default lookup logic         
              Username,   // user/group of interest from commandline         
              pSid,       // Sid buffer         
              &cbSid,     // size of Sid         
              RefDomain,  // Domain account found on (unused)         
              &cchDomain, // size of domain in chars         
              &peUse         )) 
              {          //         // if the buffer wasn't large enough, try again         //          
              if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
              {              
              pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);              
              if(pSid == NULL) 
              {                 
              printf("HeapReAlloc error!\n");                 
              goto cleanup;             
              }              
              cchDomain = DNLEN + 1;              
              if(!LookupAccountNameW(                 NULL,   // default lookup logic                 
              Username,   // user/group of interest from commandline                 
              pSid,       // Sid buffer                 
              &cbSid,     // size of Sid                 
              RefDomain,  // Domain account found on (unused)                 
              &cchDomain, // size of domain in chars                 
              &peUse                 )) 
              {                     
              printf("LookupAccountName error! (rc=%lu)\n", GetLastError());                     
              goto cleanup;                 
              }          
              } 
              else 
              {             
              printf("LookupAccountName error! (rc=%lu)\n", GetLastError());             
              goto cleanup;         
              }     
              }      
              //     // compute size of new acl     //      
              dwAclSize = sizeof(ACL) +1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) + GetLengthSid(pSid) ;      
              //     // allocate storage for Acl     //      
              pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);     
              if(pDacl == NULL) goto cleanup;      
              if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION))         
              goto cleanup;      
              //     // grant GENERIC_ALL (Full Control) access     //      
              if(!AddAccessAllowedAce(pDacl,ACL_REVISION,GENERIC_ALL,pSid)) 
              goto cleanup;      
              if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))         
              goto cleanup;      
              if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE)) 
              {         
              fprintf(stderr, "SetSecurityDescriptorDacl error! (rc=%lu)\n",GetLastError());         
              goto cleanup;     
              }      
              //     // setup share info structure     //      
              si502.shi502_netname = (LPTSTR) Sharename;     
              si502.shi502_type = STYPE_DISKTREE;     
              si502.shi502_re = NULL;     
              si502.shi502_permissions = 0;     
              si502.shi502_max_uses = SHI_USES_UNLIMITED;     
              si502.shi502_current_uses = 0;     
              si502.shi502_path = (LPTSTR) DirectoryToShare;     
              si502.shi502_passwd = NULL;     
              si502.shi502_reserved = 0;     
              si502.shi502_security_descriptor = &sd;      
              nas = NetShareAdd((LPTSTR) Server,         // share is on local machine        
               502,            // info-level         
               (LPBYTE)&si502, // info-buffer         
               NULL            // don't bother with parm         
               );      
               if(nas != NO_ERROR) 
               {         
                printf("NetShareAdd error! (rc=%lu)\n", nas);         
                goto cleanup;     
              }      
              bSuccess = TRUE; 
              // indicate success  
    cleanup:      
              //     // free allocated resources     //     
              if(pDacl != NULL)         
              HeapFree(GetProcessHeap(), 0, pDacl);      
              if(pSid != NULL)         
              HeapFree(GetProcessHeap(), 0, pSid);      
              if(!bSuccess) 
              {         
              return RTN_ERROR;     
              }      
              return RTN_OK; 
            
    }
      

  6.   

    #include <windows.h> 
    #include <lm.h> 
    #include <stdio.h>  
    #define RTN_OK 0 
    #define RTN_USAGE 1 
    #define RTN_ERROR 13 
     // Note: UNICODE entry point and argv.  This way, we don't need to bother 
    // with converting commandline args to Unicode //  
    int __cdecl wmain(     int argc,     wchar_t *argv[]     ) 
    {     
    LPWSTR DirectoryToShare;     
    LPWSTR Sharename;     
    LPWSTR Username;     
    LPWSTR Server;      
    PSID pSid = NULL;     
    DWORD cbSid;      
    WCHAR RefDomain[DNLEN + 1];     
    DWORD cchDomain = DNLEN + 1;     
    SID_NAME_USE peUse;      
    SECURITY_DESCRIPTOR sd;     
    PACL pDacl = NULL;     
    DWORD dwAclSize;      
    SHARE_INFO_502 si502;     
    NET_API_STATUS nas;      
    BOOL bSuccess = FALSE; // assume this function fails      
    if(argc < 4) 
    {         
    printf("Usage: %ls <directory> <sharename> <user/group> [\\\\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     //      
    DirectoryToShare = argv[1];     
    Sharename = argv[2];     
    Username = argv[3];      
    if( argc > 4 ) 
    {         
    Server = argv[4];     

    else 
    {         
    Server = NULL; 
    // local machine     
    }      
    //     // initial allocation attempt for Sid     // 
    #define SID_SIZE 96     
    cbSid = SID_SIZE;      
    pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);     
    if(pSid == NULL) 
    {
              printf("HeapAlloc error!\n");         
              return RTN_ERROR;     
              }      
              //     // get the Sid associated with the supplied user/group name     
              // force Unicode API since we always pass Unicode string     //      
              if(!LookupAccountNameW(NULL, // default lookup logic         
              Username,   // user/group of interest from commandline         
              pSid,       // Sid buffer         
              &cbSid,     // size of Sid         
              RefDomain,  // Domain account found on (unused)         
              &cchDomain, // size of domain in chars         
              &peUse         )) 
              {          //         // if the buffer wasn't large enough, try again         //          
              if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
              {              
              pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);              
              if(pSid == NULL) 
              {                 
              printf("HeapReAlloc error!\n");                 
              goto cleanup;             
              }              
              cchDomain = DNLEN + 1;              
              if(!LookupAccountNameW(                 NULL,   // default lookup logic                 
              Username,   // user/group of interest from commandline                 
              pSid,       // Sid buffer                 
              &cbSid,     // size of Sid                 
              RefDomain,  // Domain account found on (unused)                 
              &cchDomain, // size of domain in chars                 
              &peUse                 )) 
              {                     
              printf("LookupAccountName error! (rc=%lu)\n", GetLastError());                     
              goto cleanup;                 
              }          
              } 
              else 
              {             
              printf("LookupAccountName error! (rc=%lu)\n", GetLastError());             
              goto cleanup;         
              }     
              }      
              //     // compute size of new acl     //      
              dwAclSize = sizeof(ACL) +1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) + GetLengthSid(pSid) ;      
              //     // allocate storage for Acl     //      
              pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);     
              if(pDacl == NULL) goto cleanup;      
              if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION))         
              goto cleanup;      
              //     // grant GENERIC_ALL (Full Control) access     //      
              if(!AddAccessAllowedAce(pDacl,ACL_REVISION,GENERIC_ALL,pSid)) 
              goto cleanup;      
              if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))         
              goto cleanup;      
              if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE)) 
              {         
              fprintf(stderr, "SetSecurityDescriptorDacl error! (rc=%lu)\n",GetLastError());         
              goto cleanup;     
              }      
              //     // setup share info structure     //      
              si502.shi502_netname = (LPTSTR) Sharename;     
              si502.shi502_type = STYPE_DISKTREE;     
              si502.shi502_re = NULL;     
              si502.shi502_permissions = 0;     
              si502.shi502_max_uses = SHI_USES_UNLIMITED;     
              si502.shi502_current_uses = 0;     
              si502.shi502_path = (LPTSTR) DirectoryToShare;     
              si502.shi502_passwd = NULL;     
              si502.shi502_reserved = 0;     
              si502.shi502_security_descriptor = &sd;      
              nas = NetShareAdd((LPTSTR) Server,         // share is on local machine        
               502,            // info-level         
               (LPBYTE)&si502, // info-buffer         
               NULL            // don't bother with parm         
               );      
               if(nas != NO_ERROR) 
               {         
                printf("NetShareAdd error! (rc=%lu)\n", nas);         
                goto cleanup;     
              }      
              bSuccess = TRUE; 
              // indicate success  
    cleanup:      
              //     // free allocated resources     //     
              if(pDacl != NULL)         
              HeapFree(GetProcessHeap(), 0, pDacl);      
              if(pSid != NULL)         
              HeapFree(GetProcessHeap(), 0, pSid);      
              if(!bSuccess) 
              {         
              return RTN_ERROR;     
              }      
              return RTN_OK; 
            
    }
      

  7.   

    NetShareAdd(
      const char FAR * pszServer,       
      short sLevel,                     
      const char FAR * pbBuffer,        
      unsigned short  cbBuffer          
    );pszServer怎么用?我想设置本机的目录为共享,怎样做?
      

  8.   

    to hearttree(无心树) :
    我的要在windows 2000下做,应该用SHARE_INFO_2吧?
    to mfkzj() :
    你的程序主要用的是哪几个函数?我都看糊涂了。为什么我用的netshareadd函数创建出新的文件夹。我的目的是将现有的本机文件夹变为共享文件夹。因该怎么做??
    上面的例子对么?不对应该怎样改?