C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-16-2009, 04:48 PM   #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
dannyboy997 is offline   Reply With Quote
Old 06-16-2009, 04:54 PM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 06-16-2009, 09:53 PM   #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
dannyboy997 is offline   Reply With Quote
Old 06-17-2009, 07:08 AM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Are you linking kernel32.dll (or kernel32.lib) into your project?
tabstop is offline   Reply With Quote
Old 06-17-2009, 09:19 AM   #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.
dannyboy997 is offline   Reply With Quote
Old 06-17-2009, 10:48 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.)
tabstop is offline   Reply With Quote
Old 06-17-2009, 10:54 AM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 06-18-2009, 04:56 PM   #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.
dannyboy997 is offline   Reply With Quote
Reply

Tags
computer, setcomputernameex

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c++builder6 change form names problem Leite33 C++ Programming 2 06-09-2008 08:20 AM
Computer science major? aznprincess888 General Discussions 14 03-26-2008 06:49 AM
Problems shutting off computer frenchfry164 Tech Board 9 04-22-2003 06:19 PM
Which distro should I run on my old computer? joshdick Tech Board 5 04-09-2003 01:37 AM
trying to make change from the price of the item and the amount given. please help!!! tobyman C++ Programming 2 09-04-2001 02:12 PM


All times are GMT -6. The time now is 07:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22