65.9K
CodeProject is changing. Read more.
Home

ShareIT

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Nov 1, 2001

viewsIcon

77026

downloadIcon

1609

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);