c++中怎么对windows下面的一个文件夹进行共享操作?然后设置共享名!在线等待

解决方案 »

  1.   

    #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("设置共享名");    
          p.shi2_type = STYPE_DISKTREE; // disk drive
          p.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.\n");
          
          // Otherwise, print an error,
          //  and identify the parameter in error.
          //
          else
             printf("Error: %u\tparmerr=%u\n", res, parm_err);
       }
       return;
    }
      

  2.   

    NetShareAdd Sample (Windows 95/98/Me)
    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_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;
    }