October 31, 2025, 11:44:15 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


LAN detection ?

Started by paja, May 10, 2010, 04:38:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

paja

Hi,

Does exist any way to get information that is connected any active equipment into LAN connector ?

Thanks

sapero

May 11, 2010, 01:46:09 AM #1 Last Edit: May 11, 2010, 02:08:10 AM by sapero
Hi, check this WMI example, it uses the NetConnectionStatus property of Win32_NetworkAdapter:

$include "wbemdisp.inc"
$include "commctrl.inc"

CONST IDC_LIST = 1000
DIALOG d1
CREATEDIALOG d1,0,0,556,311,0x80CA0080,0,"Caption",&d1_handler
CONTROL d1,@LISTVIEW,"",9,9,537,289,0x50010009,IDC_LIST
CoInitializeEx(0, COINIT_MULTITHREADED)
domodal d1
CoUninitialize()

SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
CONTROLCMD d1, IDC_LIST, @LVINSERTCOLUMN, 0, "Adapter"
CONTROLCMD d1, IDC_LIST, @LVINSERTCOLUMN, 1, "State"
CONTROLCMD d1, IDC_LIST, @LVSETCOLWIDTH, 0, 240
CONTROLCMD d1, IDC_LIST, @LVSETCOLWIDTH, 1, 240
ListView_SetExtendedListViewStyle(GetControlHandle(d1, IDC_LIST), LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP)
CloseHandle(CreateThread(0,0,&Refresh,0,0,&@LPARAM))

CASE @IDCLOSEWINDOW
CLOSEDIALOG d1,@IDOK

CASE @IDCONTROL
if (@CONTROLID = IDC_LIST)
select (@NOTIFYCODE)
case LVN_KEYDOWN
if (*<NMLVKEYDOWN>@LPARAM.wVKey = VK_F5)
Refresh()
endif
endselect
ENDIF

ENDSELECT
RETURN
ENDSUB

sub Refresh(opt int reserved)
CONTROLCMD d1, IDC_LIST, @LVDELETEALL
CONTROLCMD d1, IDC_LIST, @LVINSERTITEM, 0, "Loading..."
int item = 0
ISWbemServices pServices
if (!CoGetObject(L"winmgmts:{impersonationLevel=impersonate}", 0, _IID_ISWbemServices, &pServices))
CONTROLCMD d1, IDC_LIST, @LVDELETEALL
ISWbemObjectSet pObjectSet=0
BSTR bstrClass = SysAllocString(L"Win32_NetworkAdapter")
if (!pServices->InstancesOf(bstrClass, wbemFlagReturnImmediately, 0, &pObjectSet))
' pObjectSet contais all instances of NetworkAdapter. Enumerate them
IEnumVariant enumv
if (GetEnumerator(pObjectSet, &enumv))
VARIANT var
while (!enumv->_Next(1, &var, 0))
ISWBemObject object
if (GetObjectFromVariant(var, _IID_ISWBemObject, &object))

' get the properties
ISWbemPropertySet props=0
if (!object->get_Properties_(&props) and props)

' property Description
VARIANT value
BOOL failure = TRUE
if (GetProperty(props, L"Description", VT_BSTR, value))
CONTROLCMD d1, IDC_LIST, @LVINSERTITEM, item, w2s(value.*<wstring>bstrVal)
VariantClear(&value)
failure = FALSE
endif
if (failure) then goto _skip

' property NetConnectionStatus
if (GetProperty(props, L"NetConnectionStatus", VT_I4, value))
select (value.lVal)
case 0: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Disconnected"
case 1: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Connecting"
case 2: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Connected"
case 3: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Disconnecting"
case 4: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Hardware not present"
case 5: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Hardware disabled"
case 6: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Hardware malfunction"
case 7: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Media disconnected"
case 8: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Authenticating"
case 9: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Authentication succeeded"
case 10: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Authentication failed"
case 11: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Invalid address"
case 12: CONTROLCMD d1, IDC_LIST, @LVSETTEXT, item, 1, "Credentials required"
endselect
endif
VariantClear(&value)
item++
label _skip
props->Release()
endif
object->Release()
endif
VariantClear(&var)
endwhile
enumv->Release()
endif
pObjectSet->Release()
endif
SysFreeString(bstrClass)
pServices->Release()
endif
endsub

sub GetEnumerator(ISWbemObjectSet pObjectSet, pointer ppEnum),BOOL
BOOL success = FALSE
IUnknown unk=0

if (!pObjectSet->get__NewEnum(&unk)) ' use get__NewEnum() because we do not know names for Item()
success = (!unk->QueryInterface(_IID_IEnumVariant, ppEnum))
unk->Release()
endif

return success
endsub

sub GetObjectFromVariant(VARIANT var, IID refiid, pointer ppv),BOOL
return (((var.vt=VT_UNKNOWN) or (var.vt=VT_DISPATCH)) and var.pUnkVal and !var.pUnkVal->QueryInterface(refiid, ppv))
endsub

sub GetProperty(ISWbemPropertySet props, wstring name, VARTYPE _type, VARIANT value),BOOL
BOOL success = FALSE
ISWbemProperty property=0
BSTR bstrProperty = SysAllocString(name)
value.vt = VT_EMPTY

if (!props->Item(bstrProperty, 0, &property))
if (!property->get_Value(&value))
success = SUCCEEDED(VariantChangeType(&value, &value, 0, _type))
endif
property->Release()
endif

SysFreeString(bstrProperty)
return success
endsub

paja

OK - but I have not

$include "wbemdisp.inc"
$include "commctrl.inc"

Paja


sapero

Paja, follow the "Aurora and EBasic SDK Headers" link from my signature, find the latest links, download the pak for Ebasic and install it. You don't even need to restart EB.
If you have custom .inc files in the /include subdirectory, make a backup because 1521 (or less) new files will be added.