Click here to Skip to main content
15,893,594 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																	*
*																								*
*	Modifications:																				*
*																								*
************************************************************************************************/

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

#define SHOWPATH 0
#define SHOWNETNAME 1

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

	char			szBuffer[MAX_PATH];
	
	BOOL			bSuccess = TRUE;
	
	DWORD			er		= 0;
	DWORD			tr		= 0;
	DWORD			resume	= 0;
	DWORD			i;
	
	PSHARE_INFO_502 BufPtr;
	PSHARE_INFO_502	p;
	NET_API_STATUS	res;
	NET_API_STATUS	res2;

	LPTSTR			lpszMachine1;
	LPTSTR			lpszMachine2;
	

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

	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\\m - Sets the maximum use value of shared resource to a value given by user");
		printf("\n\neg: ShareIt \\p \\\\MAINSERVER \\\\BACKSERVER\n");
		return ;
	}
	
	// Parse command line
	if(wcscmp((LPWSTR)lpszArgv[1], L"\\p") == 0 || wcscmp((LPWSTR)lpszArgv[1], L"-p") == 0)
	{
		iShow = SHOWPATH;
		lpszMachine1 = lpszArgv[2];
		lpszMachine2 = lpszArgv[3];
	}
	else if(wcscmp((LPWSTR)lpszArgv[1], L"\\s") == 0 || wcscmp((LPWSTR)lpszArgv[1], L"-s") == 0)
	{
		iShow = SHOWNETNAME;
		lpszMachine1 = lpszArgv[2];
		lpszMachine2 = lpszArgv[3];
	}
	else
	{
		lpszMachine1 = lpszArgv[1];
		lpszMachine2 = lpszArgv[2];
	}

		
	wprintf(L"Transferring sharing rights from %s to %s:\n\n", lpszMachine1, lpszMachine2);
	do 
	{
		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);

					
  					printf("%s ", szBuffer);
					for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
						printf(".");
					
					// Try to add sharing
					res = NetShareAdd(lpszMachine2, 502, (LPBYTE)p, NULL);
					switch(res)
					{

					case NERR_Success:
						printf(" SUCCESS\n");
						break;
						
					case NERR_DuplicateShare:
						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 ;
}



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