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

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
#include
#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; } } [/sourcecode] 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

Leave a Reply

How to post code in comments?