Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / MFC

ShareIT

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
31 Oct 2001 76.3K   1.6K   17  
Demonstrates usage of NetShare APIs.
/***********************************************************************************************\
*	ShareIt - Network sharing rights transfer application										*
*																								*
*	Usage  : ShareIt <machine1> <machine2>														*
*	Author : M. Farooque A. Khan																*
*	Created: December 8, 2000																	*
*	Version: 0.9																				*
*																								*
*	Modifications:																				*
*																								*
\***********************************************************************************************/

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <lm.h>
#include <aclapi.h>
#include <conio.h>
#include <direct.h>

#define SHOWPATH 0
#define SHOWNETNAME 1

BOOL CheckComputerName(LPTSTR lpszName);

void wmain( int argc, TCHAR *lpszArgv[ ])
{
	
	int				iShow = SHOWPATH;
	int				av;

	char			szBuffer[MAX_PATH];
	char			szPath[MAX_PATH];
	
	BOOL			bSuccess	= TRUE;
	BOOL			bSetMax		= FALSE;
	BOOL			bPrompt		= FALSE;
	BOOL			bCreateDir	= FALSE;	 

	DWORD			er		= 0;
	DWORD			tr		= 0;
	DWORD			resume	= 0;
	DWORD			i;
	DWORD			max_use	= -1;
	
	PSHARE_INFO_502 BufPtr;
	PSHARE_INFO_502	p;

	NET_API_STATUS	res;
	NET_API_STATUS	res2;

	LPTSTR			lpszMachine1;
	LPTSTR			lpszMachine2;
	

	// Printf info
	printf("\n-------------------------------------------------\n");
	printf("ShareIt - Network Sharing Rights Transfer Utility\n");
	printf("Version 1.0\n");
	printf("\n");
	printf("Concretio India Pvt. Ltd.\n");
	printf("www.concretioindia.com\n");
	printf("-------------------------------------------------\n\n");

	// Atleast 2 arguments required
	if(argc < 3)
	{
		printf("Usage: ShareIt [\\p][\\s] <machine1> <machine2> \n\n\tmachine1 - Computer from which to take the sharing rights\n\tmachine2 - Computer on which to transfer the sharing rights\n");
		printf("\n\t\\p    - Shows PATH of shared resource while transferring (default)");
		printf("\n\t\\s    - Shows SHARED NAME of shared resource while transferring");
		printf("\n\t\\mXXX - Sets the maximum use value of shared resource to XXX");
		printf("\n\t\\yn   - Prompts you before continuing");
		printf("\n\t\\c    - Creates directory on current machine if it is the target machine\n");
		printf("             and if directory does not exist.");
		printf("\n\neg: ShareIt \\s \\m100 \\\\MAINSERVER \\\\BACKSERVER\n");
		return ;
	}
	

	// Last two arguments should be the machine names
	lpszMachine1 = lpszArgv[argc-2];
	lpszMachine2 = lpszArgv[argc-1];

	// Parse command line
	for(av=1; av < argc; av++)
	{
		if(wcscmp((LPWSTR)lpszArgv[av], L"\\p") == 0 || wcscmp((LPWSTR)lpszArgv[av], L"-p") == 0)
			iShow = SHOWPATH;

		if(wcscmp((LPWSTR)lpszArgv[av], L"\\s") == 0 || wcscmp((LPWSTR)lpszArgv[av], L"-s") == 0)
			iShow = SHOWNETNAME;

		if(wcsncmp((LPWSTR)lpszArgv[av], L"\\m", 2) == 0 || wcsncmp((LPWSTR)lpszArgv[av], L"-m", 2) == 0)
		{
			bSetMax = TRUE;

			char	maxCmd[3];
			char	temp[10];
			
			WideCharToMultiByte(CP_ACP, 0, (LPWSTR)lpszArgv[av], 10, temp, 10, NULL, NULL);

			for(int t = 2; t<5; t++)
				maxCmd[t-2] = temp[t];

			sscanf(maxCmd, "%ld", &max_use);
		}

		if(wcscmp((LPWSTR)lpszArgv[av], L"\\yn") == 0 || wcscmp((LPWSTR)lpszArgv[av], L"-yn") == 0)
			bPrompt = TRUE;

		if(wcscmp((LPWSTR)lpszArgv[av], L"\\c") == 0 || wcscmp((LPWSTR)lpszArgv[av], L"-c") == 0)
		{
			bCreateDir = CheckComputerName(lpszMachine2);
		}

	}// end of parsing

	
	
	if(bPrompt == FALSE)// Should we prompt the user
		wprintf(L"Transferring sharing rights from %s to %s:\n\n", lpszMachine1, lpszMachine2);
	else
		wprintf(L"Transferring sharing rights from %s to %s:\nPress n to skip, any other key to continue:\n\n", lpszMachine1, lpszMachine2);
	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); 
	
	if(bSuccess == TRUE)
		printf("\nAll sharing rights successfully transfered.\n");
	else
		printf("\nNot all sharing rights could be transfered.\n");
	
	
	return ;
}


// Checks to see if lpszName is the local machine name
BOOL CheckComputerName(LPTSTR lpszName)
{
	char	czComputer[MAX_COMPUTERNAME_LENGTH + 1];
	char	givenName[MAX_COMPUTERNAME_LENGTH + 1];

	WCHAR	szComputer[MAX_COMPUTERNAME_LENGTH + 1];

	DWORD	size = MAX_COMPUTERNAME_LENGTH + 1;

	GetComputerNameW(szComputer, &size);

	WideCharToMultiByte(CP_ACP, 0, szComputer, MAX_COMPUTERNAME_LENGTH + 1, czComputer, 
		MAX_COMPUTERNAME_LENGTH + 1, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, (LPWSTR)lpszName, MAX_COMPUTERNAME_LENGTH + 1, givenName, 
		MAX_COMPUTERNAME_LENGTH + 1, NULL, NULL);

	if(stricmp(givenName, czComputer) != 0)
	{
		char temp[MAX_COMPUTERNAME_LENGTH + 1] = "\\\\";
		
		strcat(temp, czComputer);

		if(stricmp(givenName, temp) != 0)
			return FALSE;
		else
			return TRUE;
	}
	else
		return TRUE;
}

/***********************************************************************************************\
*																								*
*										End of File												*
*																								*
\***********************************************************************************************/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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