Thread: Change Computer Name SetComputerNameEx()

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Change Computer Name SetComputerNameEx()

    I'm having a hard time finding a good way to change the name of a computer in C++, both NetBIOS and DNS.
    I have found SetComputerNameEx(), but i can't see to get it to work, I am new to C++.

    Here is what I have for far.

    Code:
    typedef enum _COMPUTER_NAME_FORMAT {
      ComputerNameNetBIOS,
      ComputerNameDnsHostname,
      ComputerNameDnsDomain,
      ComputerNameDnsFullyQualified,
      ComputerNamePhysicalNetBIOS,
      ComputerNamePhysicalDnsHostname,
      ComputerNamePhysicalDnsDomain,
      ComputerNamePhysicalDnsFullyQualified,
      ComputerNameMax
    } COMPUTER_NAME_FORMAT;
    
    
    bool WINAPI SetComputerNameEx(COMPUTER_NAME_FORMAT NameType, LPCTSTR lpBuffer);
    
    SetComputerNameEx(ComputerNamePhysicalDnsHostname,"testname");
    This is the error I am currently getting which makes no sense to me.
    main.cpp||undefined reference to`_Z17SetComputerNameEx21_COMPUTER_NAME_FORMATPKc @8'|

    I don't have to use SetComputerNameEx() but it's all I've found so far. Any help and suggestions would be greatly appreciated.

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to tell the compiler that SetComputerNameEx is a "C" function, so stick
    Code:
    extern "C"
    in front of the declaration.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    As you can tell I'm new to this and this is probably a stupid question

    Here's what I have

    Code:
    typedef enum _COMPUTER_NAME_FORMAT {
      ComputerNameNetBIOS,
      ComputerNameDnsHostname,
      ComputerNameDnsDomain,
      ComputerNameDnsFullyQualified,
      ComputerNamePhysicalNetBIOS,
      ComputerNamePhysicalDnsHostname,
      ComputerNamePhysicalDnsDomain,
      ComputerNamePhysicalDnsFullyQualified,
      ComputerNameMax
    } COMPUTER_NAME_FORMAT;
    
    extern "C" bool WINAPI SetComputerNameEx(COMPUTER_NAME_FORMAT NameType, LPCTSTR lpBuffer);
    
    SetComputerNameEx(ComputerNamePhysicalDnsHostname,"testname");
    But I'm not getting this error
    main.cpp|69|undefined reference to `SetComputerNameEx@8'|

    Thanks for your help so far matsp

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you linking kernel32.dll (or kernel32.lib) into your project?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    I haven't added any source code to link to kernel32.dll or kernel32.lib which would probably be the problem, but I don't know how. I did add kernel32.lib to the library linker, but no change.
    I'm using Code::Block if it makes a difference.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One possibility: The default value of _WIN32_WINNT, at least on my system in Code::Blocks, is 0x0400; this function requires 0x0500 or larger. You should #define the correct value of _WIN32_WINNT before you include windows.h, and you can look up "correct value" at MSDN. (Because the one thing you shouldn't be doing is typing those declarations at the top of your file.)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Update: It appears that C::B and VS don't use the same windows.h header (there's a windows.h in my C:\MinGW\include directory, and there's one ... somewhere .... in my VS path). The code you have works just fine in VS, but not in C::B, and I'm guessing that may be the issue. I don't know how or where to get a newer windows.h, but perhaps that's something you can search for.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    I found both Windows.h(VS) and windows.h(C::B). I tried to copy the VS Windows.h but there where a ton of headers that the Windows.h(VS) had that the C::B windows.h doesn't use = lots of errors. I'll try compiling in VS later and see if it work.

    Does anyone know a different way of changing the computers name.
    Thanks for you help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  2. Problems shutting off computer
    By frenchfry164 in forum Tech Board
    Replies: 9
    Last Post: 04-22-2003, 06:19 PM
  3. Which distro should I run on my old computer?
    By joshdick in forum Tech Board
    Replies: 5
    Last Post: 04-09-2003, 01:37 AM
  4. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

Tags for this Thread