Using gethostname(), or Some Other Method for Getting Hostname?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2455 Views - Last Post: 20 July 2017 - 07:49 AM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Using gethostname(), or Some Other Method for Getting Hostname?

Posted 18 July 2017 - 10:00 AM

Hey all,
Does anyone know how to use the gethostname() command? I'm confused as to why it's declared as an integer? I've tried assigning to String^ and std::string

private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) 
{
	String^ hostNameLocal = gethostname();
	
}



I get the following error:

Error (active) a value of type "int" cannot be used to initialize an entity of type "System::String ^" Utility c:\xxxxxx\QTransfer.h 765

using std::string gives this error:
Error (active) no suitable constructor exists to convert from "int" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" Utility c:\xxxxx\QTransfer.h 765

This post has been edited by hexagod: 18 July 2017 - 10:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using gethostname(), or Some Other Method for Getting Hostname?

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,652
  • Joined: 10-May 07

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 18 July 2017 - 10:06 AM

I would imagine that the function gethostname() is declared int because the return type can be checked for failure. The return value is not simply the string value that you are searching for (which appears how you are trying to use it), but an array.
Was This Post Helpful? 1
  • +
  • -

#3 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 18 July 2017 - 12:10 PM

ahhhhh so I have to use an array to grab the value?? Back to the drawing board. Ty for the help.
Was This Post Helpful? 0
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 18 July 2017 - 01:42 PM

I've been reading about every google link on this and I am so incredibly lost. This is never going to happen lol. Who would have thought that simply getting a string out of the registry would be this @#$@ing difficult in managed C++. Definitely starting to understand why you guys told me not to use C++... LOL ... probably should have listened. Thing is, I'm already so far into this project in C++ that I can't turn back now. It's taken me close to a year to develop this in my spare time at work and I don't want to throw the whole project away because I've been able to do just about everything in managed C++ with no problems. However, for some reason this returns some type of value that I need to understand buffers and TCHARs etc a bunch of stuff I never learned as I've taught myself everything I know about C++ other than what you guys have taught me.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,652
  • Joined: 10-May 07

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 18 July 2017 - 01:53 PM

This shouldn't be this difficult. Can you post your updated code?

Also, I'm curious why you are using gethostname() , but then mention getting a string from the registry. I was under the impression that gethostname() is part of TCP/IP.
Was This Post Helpful? 1
  • +
  • -

#6 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 07:39 AM

Well if you type hostname in "CMD.exe" it will return the NETBIOS name of the computer. .. I am also somewhat confused about this but from what I've read this will return the NETBIOS name of the computer and then if you want the IP you have to do a table conversion, I think? I'll post the code in just a sec.. thanks 4 the help.
Was This Post Helpful? 0
  • +
  • -

#7 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 08:50 AM

private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) 
{
	string hostNameLocal = gethostname();

	String^ hostNameLocalManaged = marshal_as<System::String^>(hostNameLocal); 

	textBox4->Text = hostNameLocalManaged;

	
}


returns this error:

too few arguments in function call

'gethostname': function does not take 0 arguments

no suitable constructor exists to convert from "int" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"

So basically looks like I'm missing parameters that should be on the inside of the gethostname() command.

Also, as I said before it's outputting an int and not a string... I just don't know what to put in the () ..pretty sure I need to assign a buffer which I've never done before since I haven't needed to. Again, don't know how to get the output as a string but it may put a string in the buffer??? I don't know enough about managed C++ to have an idea where to go from here. I thought this would be as simple as including a library and using a command to output as a string or String^ ... I may just abandon this it will be easier to have the textBox just save the last hostname used and the user will only have to type once. But if you can help with this it would be awesome. I've been checking google for a few days and so far I can't find a good solution in managed C++ and even the non managed code I can't get to work.

This post has been edited by hexagod: 19 July 2017 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13401
  • View blog
  • Posts: 53,498
  • Joined: 12-June 08

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 09:03 AM

Did you read the docs? Yeah there are two parameters.. _AND_ it explains the return value.

https://msdn.microso...7(v=vs.85).aspx
Was This Post Helpful? 2
  • +
  • -

#9 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 01:08 PM

Yeah I read the MSDN topic. I don't know how to create a buffer :baaa:
Was This Post Helpful? 0
  • +
  • -

#10 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 01:17 PM

I should have listened. I bet C# doesn't make me create a buffer lol :surrender:
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13401
  • View blog
  • Posts: 53,498
  • Joined: 12-June 08

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 01:24 PM

What? I.. I don't get you.

From the example:
int gethostname(
  _Out_ char *name,
  _In_  int  namelen
);



A bit further down you see this:

Quote

Parameters

name [out]
A pointer to a buffer that receives the local host name.


Hmm... Okay.. so why isn't your first reaction to look at the parameter in question and just declare one?

Quote

_Out_ char *name,


I mean.. you seemed to have built some franken-app per your previous posts doing lord knows what. I don't see how you are muddled on picking out the parameter in question, eyeballing the data type, and chucking one into the code a head of the function..

To check against the inevitable future question - look up character points and character arrays.
http://www.cs.bu.edu...g/array-vs-ptr/
Was This Post Helpful? 1
  • +
  • -

#12 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 01:29 PM

private: System::Void QTransfer_Load(System::Object^  sender, System::EventArgs^  e) 
{
	char hostname[128];
	gethostname(hostname, sizeof hostname);

	string str(hostname);

	String^ myString = marshal_as<System::String^>(hostname);

	textBox4->Text = myString;

}


I tried that but got some errors about unresolved externals

1>QTransfer.obj : error LNK2028: unresolved token (0A000511) "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected]) referenced in function "private: void __clrcall Utility::QTransfer::QTransfer_Load(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA$AAMXP$[email protected]@@P$[email protected]@@Z)
1>MyForm.obj : error LNK2028: unresolved token (0A0005A8) "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected]) referenced in function "private: void __clrcall Utility::QTransfer::QTransfer_Load(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA$AAMXP$[email protected]@@P$[email protected]@@Z)
1>FileMove.obj : error LNK2028: unresolved token (0A000582) "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected]) referenced in function "private: void __clrcall Utility::QTransfer::QTransfer_Load(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA$AAMXP$[email protected]@@P$[email protected]@@Z)
1>FileMove.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected]) referenced in function "private: void __clrcall Utility::QTransfer::QTransfer_Load(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA$AAMXP$[email protected]@@P$[email protected]@@Z)
1>MyForm.obj : error LNK2001: unresolved external symbol "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected])

also,
LNK2001 unresolved external symbol "extern "C" int __stdcall gethostname(char *,int)" ([email protected]@$$[email protected])

Am I missing an #include?

Quote

I mean.. you seemed to have built some franken-app per your previous posts doing lord knows what. I don't see how you are muddled on picking out the parameter in question, eyeballing the data type, and chucking one into the code a head of the function..



.. LOL

yeah I'm researching it now. I just originally thought it might be easier. I figured it would output a string not an array, unless it's just a char array (in which case why didn't they just make it a string? )
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13401
  • View blog
  • Posts: 53,498
  • Joined: 12-June 08

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 01:30 PM

Are you not including the winsock2 and linking with the 'Ws2_32.lib'??
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,652
  • Joined: 10-May 07

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 05:00 PM

View Posthexagod, on 19 July 2017 - 04:29 PM, said:

I tried that but got some errors about unresolved externals
...
Am I missing an #include?
...

Yes.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,879
  • Joined: 05-May 12

Re: Using gethostname(), or Some Other Method for Getting Hostname?

Posted 19 July 2017 - 06:03 PM

Here is the easy way to get the NetBIOS name:

Environment::MachineName
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2