使用netUserAdd函数创buildWindows用户configuration文件

我已经使用NetUserAdd函数( 这里 )实现了一个代码,在运行它之后创build一个Windows用户configuration文件。 我在Visual Studio 2008(Visual Studio编译器)中运行它,并正常工作。 现在我需要使用mingw编译器(在Qt或CodeBlocks中)运行它,但是每次我尝试编译它时,都会在Qt或CodeBlocks中出现以下错误:

未定义的引用'NetUserAdd @ 16'

下面是我用它在Windows中创build用户configuration文件的代码。

#ifndef UNICODE #define UNICODE #endif #pragma comment(lib, "netapi32.lib") #include <stdio.h> #include <windows.h> #include <lm.h> int addUserFunc(int argc) { USER_INFO_1 ui; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; char uText[] = "TestUser"; wchar_t wText[20]; mbstowcs(wText, uText, strlen(uText)+1);//Plus null LPWSTR uName = wText; char pText[] = "123456"; mbstowcs(wText, pText, strlen(pText)+1);//Plus null LPWSTR passWord = wText; if (argc != 3) { fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", L"USER"); exit(1); } ui.usri1_name = uName; ui.usri1_password = passWord; 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, dwLevel, (LPBYTE)&ui, &dwError); if (nStatus == NERR_Success) fwprintf(stderr, L"User %s has been successfully added on %s\n", ui.usri1_password); else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } int main() { addUserFunc(3); return 0; } 

所以我想知道是否有人告诉我如何可以用mingw编译器编译这个代码

我希望你能帮助你!

 //FileName: ApiAddUser.c //gcc ApiAddUser.c -lnetapi32 //you need to run an application with administrator rights in Windows //Windows 7 and Windows 8.x test OK //请在管理员权限运行该程序#ifndef UNICODE #define UNICODE #endif #include <stdio.h> #include <windows.h> #include <lm.h> #pragma comment(lib, "netapi32.lib") int addUserFunc(void)//const wchar_t *username ,const wchar_t *password { USER_INFO_1 ui; LOCALGROUP_MEMBERS_INFO_3 account; NET_API_STATUS ret; NET_API_STATUS Status; memset(&ui, 0, sizeof(ui)); memset(&account, 0, sizeof(account)); ui.usri1_name = L"test1"; //UserName ui.usri1_password = L"12345"; //PassWord ui.usri1_priv = USER_PRIV_USER; ui.usri1_home_dir = NULL; ui.usri1_comment = NULL; ui.usri1_flags= UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD; ui.usri1_script_path = NULL; ret=NetUserAdd(NULL,1,(LPBYTE)&ui,NULL); if(ret!=NERR_Success) { MessageBox(NULL,L"Add Failed!",L"Err",MB_OK); return 0; } account.lgrmi3_domainandname=L"test1"; Status=NetLocalGroupAddMembers(NULL,L"Administrators",3,(LPBYTE)&account,1); //Add User Group ("Administrators") if(Status!=NERR_Success) { MessageBox(NULL,L"Add Group Failed!",L"Err",MB_OK); return 0; } return 1; } int main(int agrc,char *agrv[]) { addUserFunc(); return 0; } 

MinGW不是“netapi32.lib”和“编译注释”,所以:

 #pragma comment(lib, "netapi32.lib") -> //#pragma comment(lib, "netapi32.lib") 

Windows控制台:

 gcc source.c -lnetapi32 

祝你好运