Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

ShareIT

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
31 Oct 2001 76K   1.6K   17   3
Demonstrates usage of NetShare APIs.

Introduction

The ShareIT is a simple utility which transfers the sharing rights from one machine to another. Specially useful to system administrators who want to restore sharing rights from one machine (backup may be) to another machine (main server may be). The utility demonstrates NetShare APIs. ShareIT enumerates the source machine sharing rights using the NetShareEnum() API. The security descriptors thus obtained are applied to the target machine's directories using the NetShareAdd() and NetShareSetInfo() APIs. Cleanup is done using NetApiBufferFree() function.

The main logic is shown below:

do 
{
    // Enumerate the shared resources
    res = NetShareEnum ( lpszMachine1, 502, 
            (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
    if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
    {
        // save the share info structure
        p = BufPtr;
        
        for(i=1; i <= er; i++)
        {
            // Check if this is a valid security descriptor. Some are not!
            if( IsValidSecurityDescriptor(p->shi502_security_descriptor) != 0 )
            {
                if(iShow == SHOWNETNAME)
                    WideCharToMultiByte(CP_ACP, 0, 
                      (LPWSTR)p->shi502_netname, MAX_PATH, 
                      szBuffer, MAX_PATH, NULL, NULL);
                else
                    WideCharToMultiByte(CP_ACP, 0, 
                      (LPWSTR)p->shi502_path, MAX_PATH, 
                      szBuffer, MAX_PATH, NULL, NULL);
                
                
                // Print some dots !!!
                  printf("%s ", szBuffer);

                // Should we ask the user ???
                if(bPrompt == TRUE)
                {
                    char ch = getch();
                    if(ch == 'n' || ch == 'N')
                    {
                        for(int k=0; 
                          k < abs(60 - strlen(szBuffer)) && k<60; k++)
                            printf(".");

                        printf(" SKIPPED\n");
                        p++;
                        continue;
                    }
                }

                for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
                    printf(".");

                // Do we need to create the directory,
                // first check if it exists
                if(TRUE == bCreateDir)
                {
                    WideCharToMultiByte(CP_ACP, 0, 
                      (LPWSTR)p->shi502_path, MAX_PATH, 
                      szPath, MAX_PATH, NULL, NULL);
                    if( _chdir(szPath) != 0) 
                    {
                        // Try to create the directory,
                        // it does not seem to exist
                        _mkdir(szPath);
                    }
                    
                }

                
                // Do we need to set the max_uses member of SHARE_INFO_502
                if(bSetMax == TRUE)
                    p->shi502_max_uses = max_use;


                // Try to add sharing
                res = NetShareAdd(lpszMachine2, 502, (LPBYTE)p, NULL);
                switch(res)
                {

                case NERR_Success:
                    printf(" SUCCESS\n");
                    break;
                    
                case NERR_DuplicateShare:
                    // If the folder is already shared,
                    // just set the sharing rights
                    res2 = NetShareSetInfo(lpszMachine2, 
                             p->shi502_netname, 
                             502, (LPBYTE)p, NULL); 
                    if(NERR_Success != res2)
                    {
                        printf(" FAILED(#%d)\n", res2);
                        bSuccess = FALSE;
                    }
                    else
                        printf(" SUCCESS\n");

                    break;
                    
                default:
                    printf(" FAILED(#%d)\n", res);
                    bSuccess = FALSE;
                    
                }
            }
            else
            {
                if(iShow == SHOWPATH)
                {
                    if(lstrlen(p->shi502_path) > 0)
                    {
                        WideCharToMultiByte(CP_ACP, 0, 
                            (LPWSTR)p->shi502_path, MAX_PATH, 
                            szBuffer, MAX_PATH, NULL, NULL);
                        
                          printf("%s ", szBuffer);
                        for(int k=0; 
                          k < abs(60 - strlen(szBuffer)) && k<60; k++)
                            printf(".");

                        printf(" INVALID SD\n");
                    }
                }
                else
                {
                    if(lstrlen(p->shi502_netname) > 0)
                    {
                        WideCharToMultiByte(CP_ACP, 0, 
                          (LPWSTR)p->shi502_netname, MAX_PATH,
                          szBuffer, MAX_PATH, NULL, NULL);
                        
                          printf("%s ", szBuffer);
                        for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
                            printf(".");

                        printf(" INVALID SD\n");
                    }
                }
            }
            
            p++;
        }
        
        if(NetApiBufferFree(BufPtr) != NERR_Success)
            printf("Unable to do cleanup.\n");
    }
    else 
    {
        wprintf(L"(#%ld) No shared devices\\directories found" 
                "on computer %s, or access denied.\n", 
                res, lpszMachine1);
        bSuccess = FALSE;

    }
    
}
while (res==ERROR_MORE_DATA);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionShared rights? Pin
bouli28-Jan-04 4:47
bouli28-Jan-04 4:47 
How to get the rights accesses of a shared folder on WinXP/2003?

Best regards.
Question502? Pin
Peter Weyzen2-Nov-01 12:09
Peter Weyzen2-Nov-01 12:09 
AnswerRe: 502? Pin
Ammar3-Nov-01 21:04
Ammar3-Nov-01 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.