Creating a share on the Windows domain servers

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server (Standard Edition)

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Creating a share on the Windows domain servers

To show: The various network management functions - sharing resources

 

 

 

// This example run on member server creating share on the domain controller (DC)...

// Don't forget to link to netapi32.lib to your project. The return value of share functions code can be found in Network Management Error Codes in MSDN

 

// For Win 2000 server, adjust accordingly for other Windows version

#define _WIN32_WINNT 0x0500

// #define UNICODE

#include <windows.h>

#include <stdio.h>

#include <lm.h>

 

// Unicode/wide character main()...

int wmain(int argc, TCHAR *argv[ ])

{

NET_API_STATUS res;

SHARE_INFO_2 p;

DWORD parm_err = 0;

 

// Some prompt...

if(argc != 4)

printf("Usage: %ls <server_name> <share_name> <share_remark>\n", argv[0]);

else

{

// Fill in the SHARE_INFO_2 structure.

p.shi2_netname = LPWSTR(argv[2]);

p.shi2_type = STYPE_DISKTREE; // disk drive including the directory...

p.shi2_remark = LPWSTR(argv[3]); // share remark

p.shi2_permissions = ACCESS_ALL; // all permission

p.shi2_max_uses = -1; // unlimited

p.shi2_current_uses = 0; // no current uses

// Try finding a way to accept the share path through the command line...

p.shi2_path = TEXT("E:\\Domainshare"); // share path, here we want to share a folder

p.shi2_passwd = NULL; // no password

 

// Call the NetShareAdd() function, specifying level 2.

res = NetShareAdd(argv[1], 2, (LPBYTE) &p, &parm_err);

 

// If the call succeeds, inform the user.

if(res==0)

printf("%ls share created successfully.\n", p.shi2_netname);

// Otherwise, print an error, and identify the parameter in error.

else

printf("Failed to create %ls, error: %u parmerr = %u\n", p.shi2_netname, res, parm_err);

}

return 0;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus <server_name> <share_name> <share_remark>

Example: cplus myservername sharedname "Setting a new remarks"

F:\vc2005project\cplus\debug>myarray mail anothertestfolder "Setting the domain shared folder remarks"

anothertestfolder share created successfully.

F:\vc2005project\cplus\debug>

 

 

Note: This program run on the Windows 2003 member server. The domain shared folder was created on another Windows 2003 DC server named mail in the same domain. Verify at the mail server that the shared folder was created. Start->All Programs->Administrative Tools->Computer Management->Expand the Shared Folders->Click the Shares subfolder. The created shared folder should be there.

 

 

C and C++ Programming Resources | C & C++ Code Example Index