Rhyous

July 8, 2010

A simple example of creating or deleting a windows share using C++

Filed under: C/C++ — J. Abram barneck @ 2:47 pm
Tags: ,

Hey all,

Here is a simple example of creating or deleting a windows share in C++.

// CreateShare.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include "lm.h"

int _tmain(int argc, _TCHAR* argv[])
{
	// Create share
	if (0 == _tcscmp(argv[1], _T("create")))
	{
		SHARE_INFO_2 si =
		{
			L"ShareName",
			STYPE_DISKTREE,
			L"Any nice comment or remark",
			ACCESS_READ,
			DWORD(-1),
			0,
			L"C:\\Users\\jbarneck\\Desktop\\Share",
			L""
		};

		DWORD parameterError = 0;
		DWORD status = NetShareAdd(NULL, 2, (BYTE *) &si, &parameterError);
		if (status != ERROR_SUCCESS)
		{
			std::cout << "Error: " << status << std::endl;
			if (status == ERROR_ACCESS_DENIED) // 5L in WinError.h
			{
				std::cout << "Access denied." << std::endl;
			}	
			if (status == NERR_ServerNotStarted) // 2114 in LMErr.h
			{
				std::cout << "The Server service is stopped." << std::endl;
			}						
			if (status == NERR_DuplicateShare) // 2118 in LMErr.h
			{
				std::cout << "The share already exists." << std::endl;
			}
		}
		// End program
		return 0;
	}

	// Delete share
	if (0 == _tcscmp(argv[1], _T("delete")))
	{
		DWORD status = NetShareDel(NULL, L"ShareName", 0);
		if (status != ERROR_SUCCESS)
		{
			std::cout << "Could not delete share: " << status << std::endl;
		}
		// End program
		return 0;
	}

}

References:
NetShareAdd – http://msdn.microsoft.com/en-us/library/bb525384%28VS.85%29.aspx
NetShareDel – http://msdn.microsoft.com/en-us/library/bb525386%28v=VS.85%29.aspx

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: