使用C++与AD连接的LDAP连接
我正在尝试与Active Directory建立LDAP连接以获取用户列表。但是,我什至无法使用C ++来编译简单的代码以使用广告进行身份验证。
我尝试了许多C ++示例程序,但只有汇编错误。我真的只想使用C ++与AD连接而没有任何错误。因此,您能告诉我这个代码中我在做错了什么,该代码试图将新用户添加到广告中。我在下面添加了环境详细信息,代码和错误,以供参考。
代码:
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <lm.h>
#include<iostream>
int main()
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
//
// Set up the USER_INFO_1 structure.
// USER_PRIV_USER: name identifies a user,
// rather than an administrator or a guest.
// UF_SCRIPT: required
//
ui.usri1_name = L"username";
ui.usri1_password = L"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;
//
// Call the NetUserAdd function, specifying level 1.
//
nStatus = NetUserAdd(L"servername",
dwLevel,
(LPBYTE)&ui,
&dwError);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully added on %s\n",
L"user", L"dc");
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
return 0;
}
错误:
PS C:\Users\user\Desktop\Sandbox\Cpp> cd "c:\Users\user\Desktop\Sandbox\Cpp\" ; if ($?) { g++ ldap.cpp -o ldap } ; if ($?) { .\ldap }
ldap.cpp: In function 'int main()':
ldap.cpp:22:20: warning: ISO C++ forbids converting a string constant to 'LPWSTR' {aka 'wchar_t*'} [-Wwrite-strings]
22 | ui.usri1_name = L"username";
| ^~~~~~~~~~~
ldap.cpp:23:24: warning: ISO C++ forbids converting a string constant to 'LPWSTR' {aka 'wchar_t*'} [-Wwrite-strings]
23 | ui.usri1_password = L"password";
| ^~~~~~~~~~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user-1~1\AppData\Local\Temp\ccByZfCT.o:ldap.cpp:(.text+0xfb): undefined reference to `NetUserAdd'
collect2.exe: error: ld returned 1 exit status
我的系统在Windows 10 64bit上运行 使用MingW64编译器安装了MSYS。
I'm trying to just make an LDAP connection with Active directory to get the list of users. But I'm not even able to compile the simple code for just authentication with the AD using C++.
I have tried many C++ example programs but only got compilation errors. I really just want to connect with AD using C++ without any errors. So can you please tell me what I'm doing wrong in this code which attempts to add a new user to the AD. I have added the environment details, code and errors below for reference.
CODE:
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <lm.h>
#include<iostream>
int main()
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
//
// Set up the USER_INFO_1 structure.
// USER_PRIV_USER: name identifies a user,
// rather than an administrator or a guest.
// UF_SCRIPT: required
//
ui.usri1_name = L"username";
ui.usri1_password = L"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;
//
// Call the NetUserAdd function, specifying level 1.
//
nStatus = NetUserAdd(L"servername",
dwLevel,
(LPBYTE)&ui,
&dwError);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully added on %s\n",
L"user", L"dc");
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
return 0;
}
ERROR:
PS C:\Users\user\Desktop\Sandbox\Cpp> cd "c:\Users\user\Desktop\Sandbox\Cpp\" ; if ($?) { g++ ldap.cpp -o ldap } ; if ($?) { .\ldap }
ldap.cpp: In function 'int main()':
ldap.cpp:22:20: warning: ISO C++ forbids converting a string constant to 'LPWSTR' {aka 'wchar_t*'} [-Wwrite-strings]
22 | ui.usri1_name = L"username";
| ^~~~~~~~~~~
ldap.cpp:23:24: warning: ISO C++ forbids converting a string constant to 'LPWSTR' {aka 'wchar_t*'} [-Wwrite-strings]
23 | ui.usri1_password = L"password";
| ^~~~~~~~~~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user-1~1\AppData\Local\Temp\ccByZfCT.o:ldap.cpp:(.text+0xfb): undefined reference to `NetUserAdd'
collect2.exe: error: ld returned 1 exit status
My system run on Windows 10 64bit
Installed MSYS with MinGW64 compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是C ++或MINGW专家,但是我有一些经验,我进行了一些谷歌搜索。这是唯一的错误:
是其他警告。
通过您的输出,看起来您要编译的命令是:
尝试将
-lnetapi32
添加到结尾处:如果要解决这些警告,我认为您可以声明用户名和密码的变量而不是直接将文字分配给结构:
I'm no C++ or MinGW expert, but I have a little experience, and I did some Googling. This is the only error:
The others are warnings.
By your output, it looks like your command to compile is this:
Try adding
-lnetapi32
to the end of that:If you want to resolve those warnings, I think you can declare variables for the username and password rather than assigning literals directly to the struct: