delphi - how to connect to a NIC card or network adapter knowing its IP address? -
say have 2 nic or adapter card on system , found ip address through following code:
procedure tform4.retrievelocaladapterinformation(strings: tmemo); var padapterinfo, ptempadapterinfo: pip_adapter_info; adapterinfo: ip_adapter_info; buflen: dword; status: dword; strmac: string; i: integer; begin strings.clear; buflen:= sizeof(adapterinfo); padapterinfo:= @adapterinfo; status:= getadaptersinfo(nil, buflen); padapterinfo:= allocmem(buflen); try status:= getadaptersinfo(padapterinfo, buflen); if (status <> error_success) begin case status of error_not_supported: strings.lines.add('getadaptersinfo not supported operating ' + 'system running on local computer.'); error_no_data: strings.lines.add('no network adapter on local computer.'); else strings.lines.add('getadaptersinfo failed error #' + inttostr(status)); end; dispose(padapterinfo); exit; end; while (padapterinfo <> nil) begin strings.lines.add('description: ' + padapterinfo^.description); strings.lines.add('name: ' + padapterinfo^.adaptername); strmac := ''; := 0 padapterinfo^.addresslength - 1 strmac := strmac + '-' + inttohex(padapterinfo^.address[i], 2); delete(strmac, 1, 1); strings.lines.add('mac address: ' + strmac); strings.lines.add('ip address: ' + padapterinfo^.ipaddresslist.ipaddress.s); strings.lines.add('ip subnet mask: ' + padapterinfo^.ipaddresslist.ipmask.s); strings.lines.add('gateway: ' + padapterinfo^.gatewaylist.ipaddress.s); strings.lines.add('dhcp enabled: ' + inttostr(padapterinfo^.dhcpenabled)); strings.lines.add('dhcp: ' + padapterinfo^.dhcpserver.ipaddress.s); strings.lines.add('have wins: ' + booltostr(padapterinfo^.havewins,true)); strings.lines.add('primary wins: ' + padapterinfo^.primarywinsserver.ipaddress.s); strings.lines.add('secondary wins: ' + padapterinfo^.secondarywinsserver.ipaddress.s); ptempadapterinfo := padapterinfo; padapterinfo:= padapterinfo^.next; if assigned(padapterinfo) dispose(ptempadapterinfo); end; dispose(padapterinfo); end; end;
how connect or direct of network traffic through specific nic or network adapter card?
i able accomplish following code on windows 7, code won't run on windows 10. keeps raising access denied message when program run user not when run administrator. however, administrator program won't run background process on windows 10....
procedure tdxcommdlg.getconnectionlist(strings,idlist: tstrings); var penum: ienumvariant; vnetcon: olevariant; dwretrieved: cardinal; puser: netconlib_tlb.pusertype1; netcon : inetconnection; begin strings.clear; idlist.clear; penum := (netsharingmanager1.enumeveryconnection._newenum ienumvariant); while (penum.next(1, vnetcon, dwretrieved) = s_ok) begin (iunknown(vnetcon) inetconnection).getproperties(puser); netcon := (iunknown(vnetcon) inetconnection); if (puser.status in [ncs_connected,ncs_connecting])//remove if want disabled nic cards , (puser.mediatype in [ncm_lan,ncm_sharedaccesshost_lan,ncm_isdn] ) , (getmacaddress(guidtostring(puser.guidid))<>'' ) begin //we want valid network cards enabled strings.add(puser.pszwname); idlist.add(guidtostring(puser.guidid)); end; end; end; function tdxcommdlg.getmacaddress(cardid: string): string; var reg: tregistry; keyvalues: tstringlist; i: integer; cardinstanceid,cardaddress: string; begin result := ''; reg := tregistry.create; keyvalues := tstringlist.create; try reg.rootkey:=hkey_local_machine; if reg.openkey(maclocation,false) begin reg.getkeynames(keyvalues); reg.closekey; := 0 keyvalues.count-1 if reg.openkey(maclocation+'\'+keyvalues[i],false) begin cardinstanceid := reg.readstring('netcfginstanceid'); cardaddress := reg.readstring('networkaddress'); reg.closekey; if cardinstanceid = cardid begin if cardaddress='' cardaddress := 'hardware'; result := cardaddress; break; end; end; end; reg.free; keyvalues.free; end; end; procedure tdxcommdlg.resetnic(const aconnection: string); var penum: ienumvariant; vnetcon: olevariant; dwretrieved: cardinal; puser: netconlib_tlb.pusertype1; begin enabled := false; try penum := (netsharingmanager1.enumeveryconnection._newenum ienumvariant); while (penum.next(1, vnetcon, dwretrieved) = s_ok) begin (iunknown(vnetcon) inetconnection).getproperties(puser); if puser.pszwname = aconnection begin (iunknown(vnetcon) inetconnection).disconnect; (iunknown(vnetcon) inetconnection).connect; sleep(2000); break; end; end; enabled := true; end; end;
update access denied raised following line above , procedure quits. doesn't go further.
penum := (netsharingmanager1.enumeveryconnection._newenum ienumvariant);
it sounds though library use requires elevated rights. nothing significant has changed in windows 10 regarding uac. if program running elevated succeed. fails when not elevated. problem appear failing execute elevated.
since program requires elevation make sure manifest that. add requireadministrator
option application manifest.
you request write access hklm not granted standard user. call openkeyreadonly
rather openkey
. there's no point asking write access when reading.
Comments
Post a Comment