Click here to Skip to main content
Click here to Skip to main content

ShareIT

, 31 Oct 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Farooque Khan

United States United States
No Biography provided

Comments and Discussions

 
QuestionShared rights? Pinmemberbouli28-Jan-04 4:47 
Question502? PinmemberPeter Weyzen2-Nov-01 12:09 
What is the significance of this number -- the number 502 is sprinkled around the code, but the meaning escapes me...
 
Thanks in advance.
-p
AnswerRe: 502? PinmemberAmmar3-Nov-01 21:04 

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.

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