Google
 

Thursday, February 14, 2008

Setting IP Address, Subnet dan Gateway computer di Delphi

Artikel dibawah ini menerangkan bagaimana setting IP Address, Subnet dan Gateway computer. Caya yang digunakan adalah dengan menggunakan class WMI API OLE


Contoh

jika SetIpConfig('192.168.1.5') = 0 maka Set ke STATIC IP
jika SetIpConfig('') = 0. Maka Set ke DHCP
jika SetupConfig('dhcp') = 0 Maka Set ke DHCP
jika SetIpConfig('196.11.175.221','196.11.175.1') = 0 itu berarti menuliskan IP STATIC + GATEWAY

Unit reference yang digunakan adalah : ComObj, ActiveX, UrlMon

Function yang akan dubuat adalah :
function SetIpConfig(const AIpAddress : string; const AGateWay : string = ''; const ASubnetMask : string = '') : integer;
function SetDnsServers(const APrimaryDNS : string; const AAlternateDNS : string = '') : integer;

Dengan Return Value sbb :
0 Successful completion, no reboot required.
1 Successful completion, reboot required.
-1 Unknown OLE Error
64 Method not supported on this platform.
65 Unknown failure.
66 Invalid subnet mask.
67 An error occurred while processing an instance that was returned.
68 Invalid input parameter.
69 More than five gateways specified.
70 Invalid IP address.
71 Invalid gateway IP address.
72 An error occurred while accessing the registry for the info.
73 Invalid domain name.
74 Invalid host name.
75 No primary or secondary WINS server defined.
76 Invalid file.
77 Invalid system path.
78 File copy failed.
79 Invalid security parameter.
80 Unable to configure TCP/IP service.
81 Unable to configure DHCP service.
82 Unable to renew DHCP lease.
83 Unable to release DHCP lease.
84 IP not enabled on adapter.
85 IPX not enabled on adapter.
86 Frame/network number bounds error.
87 Invalid frame type.
88 Invalid network number.
89 Duplicate network number.
90 Parameter out of bounds.
91 Access denied.
92 Out of memory.
93 Already exists.
94 Path, file, or object not found.
95 Unable to notify service.
96 Unable to notify DNS service.
97 Interface not configurable.
98 Not all DHCP leases could be released or renewed.
100 DHCP not enabled on adapter.

---------------------------------------------------------}


uses ComObj, ActiveX, UrlMon;


function SetIpConfig(const AIpAddress : string; const AGateWay : string = '';
const ASubnetMask : string = '') : integer;
var Retvar : integer;
___oBindObj : IDispatch;
___oNetAdapters,oNetAdapter,
___oIpAddress,oGateWay,
___oWMIService,oSubnetMask : OleVariant;
___i,iValue : longword;
___oEnum : IEnumvariant;
___oCtx : IBindCtx;
___oMk : IMoniker;
___sFileObj : widestring;
begin
___Retvar := 0;
___sFileObj := 'winmgmts:\\.\root\cimv2';

___// Create OLE [IN} Parameters
___oIpAddress := VarArrayCreate([1,1],varOleStr);
___oIpAddress[1] := AIpAddress;
___oGateWay := VarArrayCreate([1,1],varOleStr);
___oGateWay[1] := AGateWay;
___oSubnetMask := VarArrayCreate([1,1],varOleStr);
___if ASubnetMask = '' then
______oSubnetMask[1] := '255.255.255.0'
___else
______oSubnetMask[1] := ASubnetMask;

___// Connect to WMI - Emulate API GetObject()
___OleCheck(CreateBindCtx(0,oCtx));
___OleCheck(MkParseDisplayNameEx(oCtx,PWideChar(sFileObj),i,oMk));
___OleCheck(oMk.BindToObject(oCtx,nil,IUnknown,oBindObj));
___oWMIService := oBindObj;

oNetAdapters := oWMIService.ExecQuery('Select * from ' +
__________________ 'Win32_NetworkAdapterConfiguration ' +
__________________ 'where IPEnabled=TRUE');
___oEnum := IUnknown(oNetAdapters._NewEnum) as IEnumVariant;

___while oEnum.Next(1,oNetAdapter,iValue) = 0 do begin
___try
______// Set by DHCP ? (Gateway and Subnet ignored)
______if (AIpAddress = '') or SameText(AIpAddress,'DHCP') then
_________Retvar := oNetAdapter.EnableDHCP
______// Set via STATIC ?
______else begin
_________Retvar := oNetAdapter.EnableStatic(oIpAddress,oSubnetMask);
_________// Change Gateway ?
_________if (Retvar = 0) and (AGateWay <> '') then
____________Retvar := oNetAdapter.SetGateways(oGateway);

______end;
___except
_________Retvar := -1;
___end;

______oNetAdapter := Unassigned;
___end;

___oGateWay := Unassigned;
___oSubnetMask := Unassigned;
___oIpAddress := Unassigned;
___oNetAdapters := Unassigned;
___oWMIService := Unassigned;
___Result := Retvar;
end;



function SetDnsServers(const APrimaryDNS : string;
__________________ const AAlternateDNS : string = '') : integer;
var Retvar : integer;
___oBindObj : IDispatch;
___oNetAdapters,oNetAdapter,
___oDnsAddr,oWMIService : OleVariant;
___i,iValue,iSize : longword;
___oEnum : IEnumvariant;
___oCtx : IBindCtx;
___oMk : IMoniker;
___sFileObj : widestring;
begin
___Retvar := 0;
___sFileObj := 'winmgmts:\\.\root\cimv2';
___iSize := 0;
___if APrimaryDNS <> '' then inc(iSize);
___if AAlternateDNS <> '' then inc(iSize);

___// Create OLE [IN} Parameters
___if iSize > 0 then begin
______oDnsAddr := VarArrayCreate([1,iSize],varOleStr);
______oDnsAddr[1] := APrimaryDNS;
______if iSize > 1 then oDnsAddr[2] := AAlternateDNS;
___end;

___// Connect to WMI - Emulate API GetObject()
___OleCheck(CreateBindCtx(0,oCtx));
___OleCheck(MkParseDisplayNameEx(oCtx,PWideChar(sFileObj),i,oMk));
___OleCheck(oMk.BindToObject(oCtx,nil,IUnknown,oBindObj));
___oWMIService := oBindObj;

___oNetAdapters := oWMIService.ExecQuery('Select * from ' +
_________ 'Win32_NetworkAdapterConfiguration ' +
_________ 'where IPEnabled=TRUE');
___oEnum := IUnknown(oNetAdapters._NewEnum) as IEnumVariant;
___while oEnum.Next(1,oNetAdapter,iValue) = 0 do begin
___try
______if iSize > 0 then
_________Retvar := oNetAdapter.SetDNSServerSearchOrder(oDnsAddr)
______else
_________Retvar := oNetAdapter.SetDNSServerSearchOrder();
___except
______Retvar := -1;
___end;

______oNetAdapter := Unassigned;
___end;

___oDnsAddr := Unassigned;
___oNetAdapters := Unassigned;
___oWMIService := Unassigned;
___Result := Retvar;
end;



fungsi lain yang ada pada Win32_NetworkAdapterConfiguration Class

DisableIPSec
EnableDHCP
EnableDNS
EnableIPFilterSec
EnableIPSec
EnableStatic
EnableWINS
ReleaseDHCPLease
ReleaseDHCPLeaseAll
RenewDHCPLease
RenewDHCPLeaseAll
SetArpAlwaysSourceRoute
SetArpUseEtherSNAP
SetDatabasePath
SetDeadGWDetect
SetDefaultTTL
SetDNSDomain
SetDNSServerSearchOrder
SetDNSSuffixSearchOrder
SetDynamicDNSRegistration
SetForwardBufferMemory Specifies
SetGateways
SetIGMPLevel
SetIPConnectionMetric
SetIPUseZeroBroadcast
SetIPXFrameTypeNetworkPairs
SetIPXVirtualNetworkNumber
SetKeepAliveInterval
SetKeepAliveTime
SetNumForwardPackets
SetPMTUBHDetect
SetPMTUDiscovery
SetTcpipNetbios
SetTcpMaxConnectRetransmissions
SetTcpMaxDataRetransmissions
SetTcpNumConnections
SetTcpUseRFC1122UrgentPointer
SetTcpWindowSize
SetWINSServer

No comments: