Hi,
Welcome to share here!
Where did you try to run the code?
I'm afraid i can't give more advice about the c++ snippet.
Scripts to get the Canonical name in the following link for your reference:
https://community.spiceworks.com/topic/2203273-list-all-users-in-ad-to-include-canonical-name-powershell
https://serverfault.com/questions/576497/active-directory-user-names-why-does-the-canonical-name-vary-can-i-do-somethin
This response contains a third-party link. We provide this link for easy reference. Microsoft cannot guarantee the validity of any information and content in this link.
Best Regards,