I need my program to print out the name of the computer. Is this possible through C++ at all?
Thanks in advance.
Printable View
I need my program to print out the name of the computer. Is this possible through C++ at all?
Thanks in advance.
Code:#include<windows.h>
char buffer[256];
DWORD len = 256;
GetComputerName(buffer, &len);
cout<<buffer<<endl;
Thank you VERY much. Worked perfectly.Quote:
Originally Posted by jinhao
Next time, say which OS/Compiler you're using.
It was just a lucky guess that you were using windows.
Ah. Thanks. I'll be sure to state that next time. :o.Quote:
Originally Posted by Salem
heihei, i am so lucky :)
Thanks for the previous help, guys. However, I must compile it this code on a Unix machine now. I'm using the standard Unix build, cc xxx.cpp.
How should I print out the system name using that? I assume that I should include a different header file than windows.h.
Thanks in advance.
Hum.. I dunno .. but you could try to acess the environement variables and try to see if there's something that mathces the computer name
NOTE: this main declaration ISN'T standard, although it's provided with many compilers as extension., I think. Please correct me if wrong.Code:#include <iostream>
int main(int argc, char *argv[], char *env[]){
int i;
for(i=0;env[i];i++){
std::cout<<env[i]<<std::endl;
}
return 0;
}
http://www.opengroup.org/onlinepubs/...bd/envvar.html
http://www.cplusplus.com/ref/cstdlib/getenv.html
That looks good. Although I'm not sure whether it'll be fine for Unix. I will try it once I get to the office Friday.Quote:
Originally Posted by xErath
Any other ideas, guys? :).
Yeah, that doesn't seem to be working on Unix machines :(. Any other ideas, guys? :o.Quote:
Originally Posted by xErath
Code:NAME
uname − get name and information about current kernel
SYNOPSIS
#include <sys/utsname.h>
int uname(struct utsname *buf);
I get this error :o - fatal error C1083: Cannot open include file: 'sys/utsname.h': No such file or directoryQuote:
Originally Posted by Salem
Error executing cl.exe.
Oh please - you asked for a unix answer and I gave you one.
Then you turn around and try and compile it with a windows compiler! :rolleyes:
It has to work on both :o.Quote:
Originally Posted by Salem
Tough - it won't, nor can it ever work on both.
Certainly, there's no standard way to find out.
I suppose you could find some very common library somewhere, which might happen to tell you which machine you're on...
I'll look into it. Thanks for your help though :o.Quote:
Originally Posted by Salem
Why should it ?!? It's OS specific coding, therefore you won't find support for other OS rrather that Windows on a MS compiler...Quote:
Originally Posted by JizJizJiz
Code:#define WINSOURCE (defined(WIN32) || defined(WIN64))
#if WINSOURCE
#include<windows.h>
#else
#include<sys/utsname.h>
#endif
char *get_pcname(){
static char buffer[256];
#if WINSOURCE
DWORD len = 256;
GetComputerName(buffer, &len);
#else
struct utsname booh;
memset(&booh,0,sizeof(booh));
uname(&booh);
int len = strlen(booh.nodename);
memcpy(buffer,booh.nodename,len>255?255,len);
buffer[255]=0;
#endif
return buffer;
}
Understood. I was a bit slow on Friday...doing lots of work at once :o. Sorry.Quote:
Originally Posted by xErath