使用C++与AD连接的LDAP连接

发布于 2025-01-22 13:57:17 字数 2292 浏览 0 评论 0原文

我正在尝试与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2025-01-29 13:57:17

我不是C ++或MINGW专家,但是我有一些经验,我进行了一些谷歌搜索。这是唯一的错误:

对`netuseradd''

的不确定引用

是其他警告。

通过您的输出,看起来您要编译的命令是:

g++ ldap.cpp -o ldap

尝试将-lnetapi32添加到结尾处:

g++ ldap.cpp -o ldap -lnetapi32

如果要解决这些警告,我认为您可以声明用户名和密码的变量而不是直接将文字分配给结构:

 wchar_t username[] = L"username";
 wchar_t password[] = L"password";

 ui.usri1_name = username;
 ui.usri1_password = password;

I'm no C++ or MinGW expert, but I have a little experience, and I did some Googling. This is the only error:

undefined reference to `NetUserAdd'

The others are warnings.

By your output, it looks like your command to compile is this:

g++ ldap.cpp -o ldap

Try adding -lnetapi32 to the end of that:

g++ ldap.cpp -o ldap -lnetapi32

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:

 wchar_t username[] = L"username";
 wchar_t password[] = L"password";

 ui.usri1_name = username;
 ui.usri1_password = password;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文