Archive: gethostbyname - machine name


gethostbyname - machine name
Does anyone have a NSIS utility that will get the host name of the machine?

Thanks!


If you find a Windows API function that can do this you can use System.dll to call it.


Can you elaborate or this a little more? Or point me to a similar example that uses an API call so I can use it as a template?

Thanks!


For more information about the System.dll plug-in have a look in Contrib\System. Examples can be found by searching System::Call in the Archive and in this forum. There are also some examples in Appendix C of the documentation in the latest CVS version.

All of this is said assuming you're using NSIS 2.


514 in this script means required version 2.2 of windows sockets. Lower version doesn't work at my winxp. BTW there is error at C.5 paragraph of the documentation: call at the script should look like
System::Call 'CondMgr::CmGetHotSyncExecPath(t, *i) i(.r0, r1r1).r2'
or the script will never get the actual path length (djc should think better ;) joking of course :)
Also the phrase 'There is one final wrinkle: placing a period/dot ('.') in front of a System.dll variable means 'makes no change to source' and it appears to be used everywhere except where a pointer is returned.' shows no sense to me...

P.S. Ohh. Almost forgotten :) The script:

System::Alloc 400
pop $2
System::Call 'ws2_32::WSAStartup(i 514, i r2) i .r3'
System::Call 'ws2_32::gethostname(t .r0, i ${NSIS_MAX_STRLEN}) i .r1'
System::Call 'ws2_32::WSACleanup() i .r4'
System::Free $2
MessageBox MB_OK "WSA startup result $3, WSA cleanup result $4, gethostname result $1, hostname $0"


wahooo! Thanks so much.


Opps. It seems you wanted another thing... ;)

System::Alloc 400
pop $2
System::Call 'ws2_32::WSAStartup(i 514, i r2) i .r3'
System::Call 'ws2_32::gethostname(t .r0, i ${NSIS_MAX_STRLEN}) i .r1'
System::Call 'ws2_32::getaddrinfo(t "www.google.com", t "80", i 0, *i .r5) i.r6'
System::Call '*$5(i,i,i,i,i,i,i.r8)' ; get sockaddr address from addr info structure
System::Call '*$8(&i2, &i2, &i1.R0, &i1.R1, &i1.R2, &i1.R3)' ;get IP address from sockaddr structure
System::Call 'ws2_32::freeaddrinfo(i r5)'
System::Call 'ws2_32::WSACleanup() i .r4'
System::Free $2
MessageBox MB_OK "WSA results ($3, $1, $6, $4), hostaddr $R0.$R1.$R2.$R3"


This code is exactly what I need, but I wonder why I got an error for the getaddrinfo, return IP 0.0.0.0? I pasted exactly the code!
gethostname does work.

thanks for any help.


huh sorry, get addr info works on XP/Server2003 only.

Microsoft Note: The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

Cool, they are pushing developers to develop applications incompatible with previous windows version.


Good to know ;)... Well how should I do to get the IP on previous windows station with gethostbyname?


Sorry. my memory is playng dirty games ;) Here it is (use one of the variants and remove the other).

System::Alloc 400
pop $R0
System::Call 'ws2_32::WSAStartup(i 0x202, i R0)'
System::Call 'ws2_32::gethostbyname(t "www.google.com") i.R1'
System::Call '*$R1(&v12,*i.R2)'

; now there are 2 variants:

; variant a - gets R4 as ip-addr
System::Call '*$R2(i.R3)'
System::Call 'ws2_32::inet_ntoa(i R3) t.R4'

; variant b - gets R4 as 4 ip-addr bytes
System::Call '*$R2(&i1.R5,&i1.R6,&i1.R7,&i1.R8)'

; clear everything anyway
System::Call 'ws2_32::WSACleanup()'
System::Free $R0

MessageBox MB_OK "hostaddr $R4, or bytes $R5.$R6.$R7.$R8"


Work perfectly ;) thank you much! 8-)


What if I want to get the IP of the machine I am running the program on without entering the host name?


Here what I have done... getting the hostname myself...

System::Alloc 400
pop $R0
System::Call 'ws2_32::WSAStartup(i 0x202, i R0)'
System::Call 'ws2_32::gethostname(t .r0, i ${NSIS_MAX_STRLEN}) i .r1'
System::Call 'ws2_32::gethostbyname(t "$0") i.R1'
System::Call '*$R1(&v12,*i.R2)'

System::Call '*$R2(i.R3)'
; now there are 2 variants:
; variant a - gets R4 as ip-addr
System::Call 'ws2_32::inet_ntoa(i R3) t.R4'

; variant b - gets R4 as 4 ip-addr bytes
System::Call '*$R2(&i1.R5,&i1.R6,&i1.R7,&i1.R8)'

; clear everything anyway
System::Call 'ws2_32::WSACleanup()'
System::Free $R0

MessageBox MB_OK "hostname $0, hostaddr $R4, or bytes $R5.$R6.$R7.$R8"


Unfortunately, the latest scripts are crashing in case I've enter 'www.google,com' (',' instead of '.').
How to prevent this?


OK, it seems that one should check $R1 after gethostbyname and return failure result in case it is 0.