-6

I am trying to get username and domain with GetUserNameExA function

this is my code

 #include <windows.h>
#include <Lmcons.h>
#include <iostream>
using namespace std;

#include <windows.h>
#include <Lmcons.h>
#include <Security.h>
#include <secext.h>


DWORD main()
{
    
        CHAR  *username = [200];
        DWORD dwSize = 199;
        memset(username, 0x00, 200);
        GetUserNameExA(NameSamCompatible, username, &dwSize);
        wcout << L"Hello, " << NameSamCompatible << L"!\n";
    

}

but keep getting an error that you need to declare an identifer

can you plz help me to figure it out?

Chantal87
  • 1
  • 2
  • 1
    What exact error do you keep getting? – 273K Jul 09 '22 at 21:00
  • 1
    First time in C++? I recommend [getting a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to help out. C++ rarely rewards guesswork and is not overly forgiving. – user4581301 Jul 09 '22 at 21:00
  • 1
    Just FYI, the buffer size you pass into `GetUserNameEx()` needs to include space for the null terminator, so you should be setting `dwSize` to 200, not 199. Otherwise, just set `dwSize` to 0 instead and let `GetUserNameEx()` tell you the necessary buffer size. – Remy Lebeau Jul 09 '22 at 21:30
  • 1
    We'll take the love, but note that [Stack Overflow is not a forum](https://stackoverflow.com/tour). – user4581301 Jul 10 '22 at 01:21

1 Answers1

1
CHAR  *username = [200];

should be this

char username[200];

And this

wcout << L"Hello, " << NameSamCompatible << L"!\n";

should be this

cout << "Hello, " << username << "!\n";
john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you john! much appreciated it! Really trying to improve my coding skills. make some changes and now getting only one error which is ```LNK2001 unresolved external symbol GetUserNameExA``` any idea how to fix this – Chantal87 Jul 09 '22 at 21:09
  • 1
    @Chantal87 Read the documentation [GetUserNameExA](https://learn.microsoft.com/en-us/windows/win32/api/secext/nf-secext-getusernameexa) __Requirements__ Library: `Secur32.lib` – Richard Critten Jul 09 '22 at 21:17
  • 1
    Thank you all! I do not know how to say thank you! learned so much! and it is working :) – Chantal87 Jul 09 '22 at 21:22