I'm trying to retrieve the name of the computer that my programm is running on. This is the code:

TCHAR compName[MAX_COMPUTERNAME_LENGTH + 1]; 
  DWORD cnt = MAX_COMPUTERNAME_LENGTH + 1;
  std::string name = "";
 if( GetComputerName( compName, &cnt) )
  {
    name = compName;
  }
 return name;

The compiler comes up with this error:
Error C2660: 'GetComputerNameA' : function does not take 2 arguments.

Does anyone has an idea ??

Grtz,

Dani AI

Generated

Nice catch from — that compiler message almost always means the compiler is seeing a different declaration for GetComputerName in the project (a user function or a macro), not the Win32 API prototype. The API itself expects two arguments, so a "does not take 2 arguments" error means name collision or a mismatched prototype is hiding the real function.

Practical steps to find and fix it:

  • Search the entire solution for GetComputerName, GetComputerNameA, and GetComputerNameW.
  • In Visual Studio use "Go To Definition" / "Peek Definition" to see which declaration the editor/compiler uses.
  • Generate the preprocessed file (cl /P) or inspect included headers for any #define GetComputerName or stray prototype.
  • If you find a user function with the same name, rename it. If a macro is the culprit, #undef it before using the API (or rename the macro).

If you want to avoid TCHAR/UNICODE confusion while debugging, call the explicit Unicode or ANSI variant and ensure the buffer/length are correct. Example:

WCHAR buf[256];
DWORD len = _countof(buf);
if (GetComputerNameW(buf, &len)) {
    std::wstring name(buf);
    // convert name if you need a UTF-8 std::string
}

Notes and gotchas:

  • GetComputerName is a macro that maps to GetComputerNameW or GetComputerNameA depending on UNICODE; prefer the explicit variant to be clear.
  • len is a character count, not bytes. Initialize it to the buffer size in characters.
  • If a fully qualified DNS name is needed instead of the NetBIOS name, use GetComputerNameEx with the appropriate format.
  • As discovered, the fix is usually renaming the conflicting symbol or calling the correct API directly.

For API details refer to the Microsoft docs for the GetComputerName family.

Recommended Answers

All 2 Replies

You must have another function in your program called GetComputerName(). Your code snippet compiled without error in a simple test program that only contains a main() function.

Stupid, stupid, stupid :confused: :confused: :sad: :sad:

Thanks Ancient Dragon...

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.