Click here to Skip to main content
Licence 
First Posted 31 Oct 2001
Views 45,957
Bookmarked 17 times

ShareIT

By | 31 Oct 2001 | Article
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

About the Author

Farooque Khan



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionShared rights? Pinmemberbouli4:47 28 Jan '04  
Question502? PinmemberPeter Weyzen12:09 2 Nov '01  
AnswerRe: 502? PinmemberAmmar21:04 3 Nov '01  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 1 Nov 2001
Article Copyright 2001 by Farooque Khan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid