Retrieving network name

BlitzMax Forums/BlitzMax Programming/Retrieving network name

Thareh(Posted 2014) [#1]
Ohoy Blitzers!

I'd like to retrieve the name of the network my computer's currently connected to through code, it only has to work on Windows.

I've tracked it down to this function: http://msdn.microsoft.com/en-us/library/aa365917(VS.85).aspx but I'm not very familiar with the concept of including windows functions :/

Can anyone help?

Thanks,
-- Thareh


Henri(Posted 2014) [#2]
Hello,

you could try the following function:

EDIT: made a small correction to take into account unicode strings

Strict

Extern "Win32"
	Function  GetComputerNameExW(NameType:Int, lpBuffer:Short Ptr, lpnSize:Int Ptr)
EndExtern

'Try different values
Const GET_LOCAL:Int	= 1
Const GET_DOMAIN:Int	= 2

Local bufferSize:Int = 64
Local buffer:Short[bufferSize]

If Not GetComputerNameExW(GET_DOMAIN, buffer, Varptr bufferSize) Then RuntimeError("Error: Could not get name")

Local name:String = String.FromWString(buffer)

Print name





Xerra(Posted 2014) [#3]
I tried this out of curiosity. It seems my network name is :

&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&<SNIP>


Henri(Posted 2014) [#4]
Allright, made a small adjustment (I made the original post in a haste)

-Henri


Thareh(Posted 2014) [#5]
@Henri
Your code snippet doesn't work at all for me sadly :(


Kryzon(Posted 2014) [#6]
The BNetEx module by Oliver Skawroneck and Inkubus has cross-platform information retrieval from adapters:
http://vertex.dreamfall.at/projects.php#bnetex


Henri(Posted 2014) [#7]
If you are not inside domain (but rather inside workgroup like usually in home computer) the GET_DOMAIN parameter returns empty string and GET_LOCAL returns your computer DNS name. The function that you posted in the first post gives network adapters information that are installed in your computer (similar to typing ipconfig in command prompt I suspect, not sure though) . Was this what you were looking for ?

-Henri


Thareh(Posted 2014) [#8]
@Henri
The GET_DOMAIN doesn't return anything for me, and the GET_LOCAL returns my computer name - not my network name :(

The ipconfig command returns the 'Connection-specific DNS Suffix' which I can use instead of the network name, and thus the 'GetAdaptersInfo' which I'm unable to wrap should do the trick =P

@Kryzon
I'll check it out, thanks!


Thareh(Posted 2014) [#9]
@Kryzon
I couldn't find anything about network name in that module, but I did find how to see my own IP-address which I also need, thanks =P


Derron(Posted 2014) [#10]
@Thareh

bnetx.bmx
Type TNetwork
...
	Function GetHostName:String(HostIp:Int)



bye
Ron


Thareh(Posted 2014) [#11]
@Derron
That will only get me a computers name right? I want to get the actual networks name, e.g my Wireless networks name =P


Derron(Posted 2014) [#12]
The msdn-link you posted does not provide that information either.


bye
Ron


Thareh(Posted 2014) [#13]
@Derron
I doesn't? Couldn't find any better info than that :/


Derron(Posted 2014) [#14]
For me it just gives back information bnetex gives back too.

Find a proper function (whink you should not use a WMI one) and someone here might provide you with the correct "external ... " code.


Maybe you can use something different - what do you want to achieve?
You wrote "connection specific dns ip" - all your computers will have this IP set. Mostly this is the router (so it can cache dns requests), sometimes it is the dns of your provider.
In the latter one, other clients (in other networks) with the same provider might get the same dns assigned... so you wont be able to distinguish them.


bye
Ron


Henri(Posted 2014) [#15]
Just for fun, to see AdapterInfo-function (in 1:st post) in action:

Save these two files in same folder

network_name.bmx


network_sample.cpp (by yoggy)


-Henri


BlitzSupport(Posted 2014) [#16]
This was absolute torture to locate and get working, but it does at least work here on a normal home "workgroup" -- not tested on a corporate-style "domain" though!


' -----------------------------------------------------------------------------
' Paste at top of code, should only be called once...
' -----------------------------------------------------------------------------

Global NetGetJoinInformation (lpServer:Byte Ptr, lpNameBuffer:Byte Ptr, BufferType:Byte Ptr)
Global NetApiBufferFree (Buffer:Byte Ptr)

Local netapi32:Int = LoadLibraryA ("netapi32.dll")
If Not netapi32 Then RuntimeError "NetAPI32 not available"

NetGetJoinInformation	= GetProcAddress (netapi32, "NetGetJoinInformation")
NetApiBufferFree		= GetProcAddress (netapi32, "NetApiBufferFree")

' -----------------------------------------------------------------------------
' Function to return domain/workgroup name...
' -----------------------------------------------------------------------------

Function GetNetworkName:String ()

	Local domain:String

	Local domainbuffer:Short Ptr	= Null
	Local result:Long Ptr		= Null
	
	If NetGetJoinInformation (Null, Varptr domainbuffer, Varptr result) = 0
		domain = String.FromWString (domainbuffer)
		NetApiBufferFree domainbuffer
	EndIf
	
	Return domain
	
End Function

' -----------------------------------------------------------------------------
' D E M O . . .
' -----------------------------------------------------------------------------

Print GetNetworkName ()



Longer version below can optionally return network type, too -- whether workgroup or domain:

http://www.blitzbasic.com/codearcs/codearcs.php?cat=4


Thareh(Posted 2014) [#17]
@Derron
Yeah, I can use something different but it'd be the best to be able to get the network name =P
I'm writing an applicaton for keeping track of my work time on my laptop, and thus I want to check if I'm connected to my works network.
I already implemented pinging against an IP (like the works router), checking my own IP-address and the final nail to the coffin would be to compare network name :D

@Henri
It doesn't compile for me :(
BlitzMax/bin/ld.exe: cannot find -liphlpapi

@BlitzSupport
That function only returns a 1 for me :(

Thanks everyone for taking time and helping me btw =P


skidracer(Posted 2014) [#18]
Try typing set from a command prompt. There are a number of domain environment variables that could possibly reflect if you are logged in to your work system.

Or you could change to using two user names, switching to say just your first name for home use.


Henri(Posted 2014) [#19]
There are enviroment variables:
Print getenv_("USERDNSDOMAIN")
Print getenv_("USERDOMAIN")
Print getenv_("COMPUTERNAME")

@James
Tried your code in corporate domain, but it didn't return any string-data

@Thareh
Do you have MinGW installed in your system ? Is IPHLPAPI.DLL present in Windows\system32 - folder (in win32)

-Henri


Derron(Posted 2014) [#20]
@BlitzSupport

ReleaseNonThreaded-Build


DebugNonThreaded-Build


Threaded or not does not matter.

So you see: seems not to work for DEBUG builds.

@Thareh
Did you try Release-Build?


bye
Ron


Thareh(Posted 2014) [#21]
@skidracer
No, sorry - I set the network up at work and at home with the same settings except for the IP's and network name - it has the same workgroup and domain =P

@Henri
Those environment variables doesn't help sadly :(

I have MingW installed and have compiled modules with it, and yes I have IPHLPAPI.DLL present in Windows\system32 =P

Tried copying the dll to the same folder as the executable and still no luck :/

@Derron
Oh, yeah I had debug mode on apparently - but it still only show the workgroup, not the actual network name :(


Derron(Posted 2014) [#22]
for me "WORKGROUP" is the actual name of the network.

Normally I use a different one, but for virtual machines I am way to lazy to adjust that.


bye
Ron


BlitzSupport(Posted 2014) [#23]
I'll have another look later -- it might be that it returns information in a different format for corporate domains, though that seems doubtful.

The NetGetJoinInformation call is really weird, as you have to give it a pointer to a pointer, then it allocates the memory for the string and amends your pointer to point to it -- bizarre way of doing it, never seen anything else like that in Win32! -- so this might relate to why it doesn't work in debug mode. (Took many tries to get it to return the workgroup name without crashing.)


Thareh(Posted 2014) [#24]
@Derron
Oh okey hehe =P

@BlitzSupport
Thank you, appreciate it!
That sounds weird =P


Henri(Posted 2014) [#25]
@James
Forget what I said earlier, it does give proper result under managed domain, but as Ron pointed out it doesn't work in debug mode.

What Thareh is looking for probably is hostname of default gateway(aka routers hostname)

-Henri


BlitzSupport(Posted 2014) [#26]
@Henri: interesting to hear it gets the right info anyway. (Still not really sure why it doesn't work in Debug mode, but must be *something* to do with the way Windows requires you to pass a pointer it can fill in.)


What Thareh is looking for probably is hostname of default gateway(aka routers hostname)



Well, for the default gateway, this NASTY code gets my router's IP correctly (home router with no name), so then takes a few seconds to look up and find there's no name. In theory it 'should' get the gateway then the domain... no idea what'll happen if there are multiple gateways, though -- it just returns the first gateway in the list known to the PC:



... and if that fails, I think I'll have to give up!


Henri(Posted 2014) [#27]
That is a noble effort, although quite nasty :-)

To determine the size needed you could execute the function two times. First time with pointer to zero size, which would fail with errorcode insufficient buffer. But good news is that now the size variable would hold the correct size (size depends on how many adapters are installed).

That would still leave the problem which adapter is the correct one...well, now off to bed.

-Henri


BlitzSupport(Posted 2014) [#28]
Yeah, I do actually know this (I have working PureBasic code* from which the above is taken), but you're meant to walk through the list of adapters/gateways using pointers, which is much easier with proper structure support -- just want to see if this works in any way for Thareh.

At this point, it's not entirely clear if the gateway's name is what we actually need. This basis could be used if it's at least going in the right direction... and it's worth bearing in mind that it actually only needs to work for Thareh's specific situation!

* Here it is -- try the executable in the archive to see if it gets the hostname for any default gateway/s found!


Henri(Posted 2014) [#29]
You are right of course. Only reason I mentioned it was because without correct size it will fail and it probably will fail for Thareh as his/hers setup likely varies.

On other note, what is there left besides routers hostname ? He/she did mention WLAN-network's name, so from that I gather it's what he/she is looking for.

-Henri


Thareh(Posted 2014) [#30]
@BlitzSupport
Thank you very much for your effort! It doesn't get me the network name (SSID for wireless, or the user defined network name SEE THIS IMAGE) but it does get me the routers hostname which is usable as well! =P


Derron(Posted 2014) [#31]
That thing on your image ("LatterDays") ... is the one called "WORKGROUP" by default windows network setups (including capital letters).

So the example he gave above (and I responded to - that it works for release mode) exactly gave back the correct response.

[/img]

bye
Ron


col(Posted 2014) [#32]

(Still not really sure why it doesn't work in Debug mode, but must be *something* to do with the way Windows requires you to pass a pointer it can fill in.)



You forgot the "Win32" calling convention on the end of the dll functions...

Global NetGetJoinInformation (lpServer:Byte Ptr, lpNameBuffer:Byte Ptr, BufferType:Byte Ptr)"Win32"
Global NetApiBufferFree (Buffer:Byte Ptr)"Win32"

[...]



Derron(Posted 2014) [#33]
That indeed does result in a non-empty and correct output in a debug build.

sometimes people just miss small things.


bye
Ron


Thareh(Posted 2014) [#34]
@Derron
Sadly they're not the same, atleast not on Win7 :/ --> LINK HERE <--


Henri(Posted 2014) [#35]
You haven't changed your 'WORKGROUP' name which is essentially a home domain. Could you change the 'WORKGROUP' name to something unique and use that to distinguish between home and work ?

-Henri


Derron(Posted 2014) [#36]
I assume that the "direktonline 2.4ghz" thing on your image is something called "HomeGroup" and is a special of Windows OS.

https://en.wikipedia.org/wiki/Features_new_to_Windows_7#HomeGroup

bye
Ron


Thareh(Posted 2014) [#37]
@Henri
I'm not very familiar with workgroups, do I have to change it in the router and on every computer at work for everything to work as usual? (File transfers between computers etc)

Derron
Yeah, alright! - then it's the HomeGroup name I'm looking to retrieve! =P

But I'm satisfied with the help I've got and I'll just skip the HomeGroup crap and use what you've supplied me instead! :D

Thanks to you all!


BlitzSupport(Posted 2014) [#38]
Well, I have found where this "Homegroup crap" is stored in the Registry.

This is where the ID of the network profile is located (in bold):

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\{hex string}

(Note that under this key you should find an entry named "ProfileName" which contains the display name, as in "Direktonline 2.4GHz".)

... then when you switch between "Home" and "Work" networks it places your {hex string} ID from above into either of:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\HomeGroup\NetworkLocations\Home
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\HomeGroup\NetworkLocations\Work

... leaving the opposite location empty. If you switch to using the "Public" network, both are empty.

But reading from the Registry is a monumental PITA, so I'll leave it as an "exercise for the reader". There is a Registry module here but I couldn't get it to work on a quick test.

I'm also not really clear on how these network profiles work -- this is just a quick summary of how my setup seems to operate!


BlitzSupport(Posted 2014) [#39]
Oh, and thanks to col for pointing out the missing "win32" -- was never too clear on calling conventions and assumed that if it worked at all then it was fine!