How to use NetServerEnum from NetApi32.dll

BlitzMax Forums/BlitzMax Beginners Area/How to use NetServerEnum from NetApi32.dll

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I ported the C# example over to blitzmax, but i dont get any machines listed. Maybe youl have better luck?

SuperStrict

Private

Type _SERVER_INFO_100
	Field sv100_platform_id:Int
	Field sv100_name:Byte Ptr
EndType

Global DLL:Int = 0
InitNetApi()
OnEnd( FreeNetApi)

Global NetServerEnum_:Int( name:Byte Ptr, level:Int, buf:Byte Ptr Var, maxlen:Int, entriesread:Int Var, totalentries:Int Var, servertype:Int, domain:Byte Ptr, resumehandle:Int Var)

Extern "Win32"
	Function FreeLibrary:Int( handle:Int)
EndExtern

Public


Const SV_TYPE_WORKSTATION:Int = 1
Const SV_TYPE_SERVER:Int = 2

Const NERR_SUCCESS:Int = 0

Global NetApiBufferFree:Int( buf:Byte Ptr)

Function InitNetApi()
	If Not DLL Then
		DLL = LoadLibraryA( "NetApi32.dll")
		If Not DLL Then RuntimeError( "Unable to load NetApi32.dll")
		NetServerEnum_ = GetProcAddress( DLL, "NetServerEnum")
		If Not NetServerEnum_ Then RuntimeError( "Error getting function NetServerEnum")
		NetApiBufferFree = GetProcAddress( DLL, "NetApiBufferFree")
		If Not NetApiBufferFree Then RuntimeError( "Error getting function NetApiBufferFree")
	EndIf
EndFunction

Function FreeNetApi()
	If DLL Then FreeLibrary( DLL)
	DLL = 0
EndFunction

Function NetServerEnum:Int( name_:String, level:Int, buf:Byte Ptr Var, maxlen:Int, entriesread:Int Var, totalentries:Int Var, servertype:Int, domain_:String, resumehandle:Int Var)
	Local name:Byte Ptr, domain:Byte Ptr
	If name_ Then name = name_.ToCString()
	If domain_ Then domain = domain_.ToCString()	
	Local res:Int = NetServerEnum_( name, level, buf, maxlen, entriesread, totalentries, servertype, domain, resumehandle)
	If name Then MemFree name
	If domain Then MemFree domain
	Return res
EndFunction

Function GetNetworkComputers:String[]()
	Const MAX_PREFERRED_LENGTH:Int = -1
	
	Local buf:Byte Ptr, handle:Int, entries:Int, total:Int
	If NetServerEnum( Null, 100, buf, MAX_PREFERRED_LENGTH, entries, total, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, Null, handle) = NERR_SUCCESS Then
		Local list:String[]
		If total > 0 Then
			list = New String[total]
			For Local i:Int = 0 Until total
				Local tmp:Byte Ptr = buf + (i * SizeOf(_SERVER_INFO_100))
				list[i] = String.FromWString( Short Ptr Int Ptr(tmp)[1])
			Next
		EndIf
		NetApiBufferFree( buf)
		Return list
	EndIf
	NetApiBufferFree( buf)
	Return Null
EndFunction


'
' TEST
'
Print "listing:"
For Local s:String = EachIn GetNetworkComputers()
	Print ":" + s
Next