Another example of creating and adding a local group to Windows System

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

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 or use #pragma comment(lib, "netapi32.lib")

To do: Another example on how to add a local group to Windows system

To show: The Windows user management functions

 

 

//********* cplus.cpp **********

// For WinXp

#define _WIN32_WINNT 0x0501

// Wide character/Unicode based program

#ifndef UNICODE

#define UNICODE

#endif

#include <windows.h>

#include <stdio.h>

#include <lm.h>

 

// Creating a local group...

// This program accept 3 arguments: servername, GroupName and Comment.

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

{

LOCALGROUP_INFO_1 lgi1;

DWORD dwLevel = 1;

DWORD dwError = 0;

NET_API_STATUS nStatus;

 

if(argc != 4)

{

fwprintf(stderr, L"Usage: %s ServerName GroupName Comment\n", argv[0]);

fwprintf(stderr, L"Example: %s mypersonal AnotherLocalGroup \"Some comment for this local group\"\n", argv[0]);

// Just exit, no further processing

exit(1);

}

 

// Set up the LOCALGROUP_INFO_1 structure. Assign the group name and comment

lgi1.lgrpi1_name = argv[2]; // Local group name

lgi1.lgrpi1_comment = argv[3]; // Comment

 

// Call the NetLocalGroupAdd() function, specifying level 1.

nStatus = NetLocalGroupAdd(argv[1],

dwLevel,

(LPBYTE)&lgi1,

&dwError);

 

// If the call succeeds, inform the user.

if(nStatus == NERR_Success)

fwprintf(stderr, L"%s local group has been created successfully on %s machine.\n", argv[2], argv[1]);

 

// Otherwise, print the system error.

else

fprintf(stderr, "A system error has occurred: %d\n", nStatus);

 

return 0;

}

 

Output example:

 (This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus ServerName GroupName Comment

Example: cplus mypersonal AnotherLocalGroup "Some comment for this local group"

F:\vc2005project\cplus\debug>cplus mypersonal AnotherLocalGroup "Just another local group"

AnotherLocalGroup local group has been created successfully on mypersonal machine.

F:\vc2005project\cplus\debug>

 

 

Verify the local group creation as mentioned in the previous posts...

 

 

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