Beginner: Win32 API NetUserAdd can't run on Windows 10

Here is my code which works on Windows 7 (Visual Studio 2013 Express installed )
not on my Windows 10 (Visual Studio 2015 Community installed)

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib") //heard this is dynamic linking ?

#include <windows.h>
#include <lm.h>

int main()
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
//NET_API_STATUS nStatus;

ui.usri1_name = L"OMG";
ui.usri1_password = L"Bitch@2015";
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_PASSWD_CANT_CHANGE;
ui.usri1_script_path = NULL;

NetUserAdd(NULL,dwLevel,(LPBYTE)&ui,&dwError);

}

//Just call the NetUserAdd() function ! nothing else
//It requires netapi32.dll to run ! and maybe netapi32.lib too

//I know how to compile .lib against source.cpp to make a standalone program

Here are my questions :

1. Why can't the code work on another Windows 7 without Visual Studio installed ?(Even there is a netapi32.dll file in the system folder !)
2. Why can't it run on Windows 10 ?
3. Where to find the netapi32.lib file ? so I can use it to make a standalone program .(static linking )(it requires netapi32.dll to run , so I think it is still a dynamic linked program )

Help please , I've been researching on this topic for so long .

I read other people's post , my problem solved ,anyone encounters this problem ,can refer to
this link : http://www.cplusplus.com/forum/windows/182467/
Good luck everyone !
Last edited on
The netapi32.lib is an import library. The compiler uses this to link your program against the dynamic library netapi32.dll. You do not need the netapi32.lib file on the other machines to be able to run the code there, only to compile it. In fact, all you need is netapi32.dll, which should be on all windows systems by default. If you are missing the dll, you will get an error dialog saying netapi32.dll was not found on the system.

I think the problem is more of a privilege problem. You probably execute the code as administrator on your own computer, but as a regular user on the other computers. Adding a user requires administrator privileges, so you could try running it as administrator to solve the problem.

Try printing the result of NetUserAdd, these give a good indication as to what problem is going on. Take a look at https://msdn.microsoft.com/en-us/library/windows/desktop/aa370649%28v=vs.85%29.aspx in the result value section for more information on what this function might return and when.
Thanks ,It's true that I didn't notice privilege problem .
With Administrator privilege ,I now can run the statically linked program as planned,created a user successfully ! .
but the dynamically linked program ,which this time it didn't complain
missing netapi32.dll!(OS does have this dll !) ,instead , it complains
missing VCRUNTIME140.dll ! , Following is what I found about VCRUMTIME140.dll :
It belongs to Visual C++ Redistributable Packages for Visual Studio
is one of Visual C++ Runtime Files

Question: Why did it happen ?
(NetUserAdd() MSDN page says it only requires netapi32.dll .....)
This is the runtime required to run applications compiler with the Visual C++ compiler. These files are usually already on the computers running the applications (the installer might take care of installing these files), but since you just copied the files over, you have to manually make sure the files are available. Most of the time, this file will be there already, but I guess you are unlucky in this game (probably due to the fact that no applications have ever required this runtime before on the target computer). You can always download the runtime from here:

For visual studio 2015:
https://www.microsoft.com/en-us/download/details.aspx?id=48145
For visual studio 2013:
https://www.microsoft.com/en-us/download/details.aspx?id=40784

Note, you must install the file corresponding to the visual C++ version you compiled your code with.

Note that all applications using Visual C++ require this runtime, so this problem is not related to NetUserAdd(). This problem will occur on all executables.
Last edited on
It is possible to statically link to the C Runtime Libraries...the option is in the project properties.

Under the configuration Properties, in the C/C++ tab, open the Code Generation dialog. You'll see an option for "Runtime Library". You can select from some combination of three choices: Debug or Release, Multithreaded or Singlethreaded, Static or DLL. The default is Multithreaded DLL, with Debug or Release depending on if you're compiling as Debug or Release. Just change ti to Multithreaded (without the "DLL") to statically link to the runtime.

You'll have to change this for each configuration; usually there are two, Release and Debug.

And keep in mind this will make your exe larger, since code that is normally called from a DLL has to be included in the file.

Note that this applies to *any* C/C++ program built with visual studio.
Topic archived. No new replies allowed.