Hello everybody, I'm trying to get the user name using GetUserName() API function, it was sucessful in the console project however in the Visual C++ project it gives me this error:

1>MyProgram.obj : error LNK2028: unresolved token (0A00000F) "extern "C" int __stdcall GetUserNameA(char *,unsigned long *)" (?GetUserNameA@@$$J18YGHPADPAK@Z) referenced in function "private: void __clrcall MyProgram::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@MyProgram@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>MyProgram.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetUserNameA(char *,unsigned long *)" (?GetUserNameA@@$$J18YGHPADPAK@Z) referenced in function "private: void __clrcall MyProgram::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@MyProgram@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

And I'm using this piece of code:

char LoginID[256];
DWORD buffer = 256;
GetUserName(LoginID, &buffer);

Any Help appreciated :).

error LNK2028: unresolved token (0A00000F) "extern "C" int __stdcall GetUserNameA(char *,unsigned long *)"

Where is GetUserNameA() defined?

It's part of the windows.h library I think?

It's part of the windows.h library I think?

Yes it is, but you need to be sure that you link with Advapi32.lib.

How do I do that? Can you explain please?

In Visual Studio, open

Project properties / Configuration properties / Linker / Input / Additional Dependencies

and add Advapi32.lib there.

Thank you it worked 100%! :D


Thread Solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.