How to create a Shared Folder

28 01 2012

If you are writing an installation program, generally you may need to create number of folders in the user’s machine and some of them may be shared over the network.

To establish this Windows provide an API NetShareAdd, which share folders over the network. The 3rd parameter (SHARE_INFO_2) of this function holds the information about the shared folder.

Here the function _wmkdir is used to create a shared directory. Once it successfully created, we can share the same. If the folder is already existed, it returns -1.

  

void CreateSharedFolder()
{
	CString SHARED_FOLDER_PATH = _T("C:\\SharedFolder");

	/* Create a new directory */
	_wmkdir(SHARED_FOLDER_PATH);

	NET_API_STATUS res;
	SHARE_INFO_2 p;
	DWORD parm_err = 0;

	/* Fill in the SHARE_INFO_2 structure. */

	//Shared folder name
	p.shi2_netname		= _T("TestShare");
	// disk drive
	p.shi2_type			= STYPE_DISKTREE;
	//comment about shared folder
	p.shi2_remark		= _T("My Shared Folder");
	// share-level security
	p.shi2_permissions	= ACCESS_READ;
	//max no of connections that the shared folder can accommodate
	p.shi2_max_uses		= 4;
	p.shi2_current_uses = 0;
	// local path for the shared folder
	p.shi2_path = SHARED_FOLDER_PATH.GetBuffer(SHARED_FOLDER_PATH.GetLength());
	// no password
	p.shi2_passwd		= NULL; 

	SHARED_FOLDER_PATH.ReleaseBuffer();

	/* Share the folder. */
	res=NetShareAdd(NULL, 2, (LPBYTE) &p, &parm_err);

	/* Check whether its succeeded or not. */
	if(res==0)
		AfxMessageBox(_T("SUCCEEDED"));
	else
		AfxMessageBox(_T("FAILED"));
}

Don’t forget to include the header files lm.h and direct.h for the functions NetShareAdd and _wmkdir respectively. Also include the Netapi32.lib too…

Advertisements

Actions

Information

One response

8 08 2014
budi

very nice. thanks a lot..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: