Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi, all!

I create new user account and set admin membership for it. Account created, but privileges setting failed with error.

void createUser()
{
	WCHAR userName[] = L"test_user";
	NET_API_STATUS nStatus;
	USER_INFO_1 ui;
	DWORD dLevel = 1;
	DWORD dwError = 0;
	ui.usri1_name = userName;
	ui.usri1_password = L"123456123456";
	ui.usri1_priv = USER_PRIV_USER;
	ui.usri1_home_dir = NULL;
	ui.usri1_comment = NULL;
	ui.usri1_flags = UF_SCRIPT;
	ui.usri1_script_path = NULL;
	nStatus = NetUserAdd(NULL,
						 dLevel,
						 (LPBYTE)&ui,
						 &dwError);
	if (nStatus == NERR_Success)
	{
		cout << "User account created successfully" << endl;
	}
	else
	{
		cout << "User account creating failed" << endl;
	}

	GROUP_USERS_INFO_0 gi;
	gi.grui0_name = L"Administrators";
	nStatus = NetUserSetGroups(
		NULL,
		userName,
		0,
		(LPBYTE)&gi,
		1);
	if (nStatus == NERR_Success)
	{
		cout << "Adding to Administrators completed" << endl;
	}
	else
	{
		cout << "Adding to Administrators failed" << endl;
	}
}


This code return error 997: Overlapped I/O operation in progress.

Somebody know, how to do it right?

Thanks.
Posted

1 solution

Run it under debugger and find out where exactly exception was thrown, in what line.
It looks like you need to wait until one operation is not complete when you start another one.
Can you conform it? Try to do NetUserAdd without NetUserSetGroups. Does it passes? What status is shown?

Your check (nStatus == NERR_Success) is wrong. How about all other values? You should output any other value if it is not NERR_Success and do not continue operation unconditionally as you do. Also, throw exception out of your method on non-successful operation; you will catch them somewhere on top of the stack.

If you still cannot figure out the problem, get all the additional information and post it. Use "Improve question".

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900