동적 IP, DNS 변경 함수
델파이2008. 11. 12. 09:36
동적 IP와 DNS를 변경하는 함수입니다.
uses ComObj, ActiveX, UrlMon;
// ======================================================================
// SetIpConfig()
// Set IPAddress, Gateway and Subnetmask via WMI
// Arguments ...
// AIpAddress - If Null String or 'DHCP' then DHCP is ENABLED
// else STATIC IP is set.
// AGateWay - [Optional] If Omitted then GATEWAY is left unchanged.
// SubnetMask - [Optional] If Omited then default = '255.255.255.0'.
//
// SetDnsServers()
// Set DNS Servers via WMI
// Arguments ...
// APrimaryDNS - If Null String then DNS Server List is CLEARED.
// AAlternateDNS - [Optional]
//
// Return Values ...
// 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.
// ======================================================================
// ==================================================================
// IP Address,Gateway and Subnet Mask
// EnableStatic takes array of string as a parameter
// for the Addresses. You may wish to rewrite this using
// array of string as parameter for multiple IP Addresses.
// I only have use for 1 IP address and Gateway in our application
// but it's nice to be able to expand it for other users.
// ==================================================================
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);
// *** This is where we need some sort of ***
// *** Network Mapped Resource Refresh ***
end;
except
Retvar := -1;
end;
oNetAdapter := Unassigned;
end;
oGateWay := Unassigned;
oSubnetMask := Unassigned;
oIpAddress := Unassigned;
oNetAdapters := Unassigned;
oWMIService := Unassigned;
Result := Retvar;
end;
// ====================================================
// Set DNS Servers
// Instead of Primary and Alternate you may wish
// to rewrite this using array of string as the
// parameters as SetDNSServerSearchOrder will take
// a list of many DNS addresses. I only have use for
// Primary and Alternate.
// ====================================================
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;
(* ----------------------------------------------------
See the Miscrosoft MSDN Documentation for the
Win32_NetworkAdapterConfiguration Class
(implemented as oNetAdatper in my examples),
There are many more calls besides EnableStatic,
EnableDHCP and SetDNBSServerSearchOrder.
Some of them are ...
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
------------------------------------------------ *)
출처 : http://www.delphi3000.com/
uses ComObj, ActiveX, UrlMon;
// ======================================================================
// SetIpConfig()
// Set IPAddress, Gateway and Subnetmask via WMI
// Arguments ...
// AIpAddress - If Null String or 'DHCP' then DHCP is ENABLED
// else STATIC IP is set.
// AGateWay - [Optional] If Omitted then GATEWAY is left unchanged.
// SubnetMask - [Optional] If Omited then default = '255.255.255.0'.
//
// SetDnsServers()
// Set DNS Servers via WMI
// Arguments ...
// APrimaryDNS - If Null String then DNS Server List is CLEARED.
// AAlternateDNS - [Optional]
//
// Return Values ...
// 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.
// ======================================================================
// ==================================================================
// IP Address,Gateway and Subnet Mask
// EnableStatic takes array of string as a parameter
// for the Addresses. You may wish to rewrite this using
// array of string as parameter for multiple IP Addresses.
// I only have use for 1 IP address and Gateway in our application
// but it's nice to be able to expand it for other users.
// ==================================================================
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);
// *** This is where we need some sort of ***
// *** Network Mapped Resource Refresh ***
end;
except
Retvar := -1;
end;
oNetAdapter := Unassigned;
end;
oGateWay := Unassigned;
oSubnetMask := Unassigned;
oIpAddress := Unassigned;
oNetAdapters := Unassigned;
oWMIService := Unassigned;
Result := Retvar;
end;
// ====================================================
// Set DNS Servers
// Instead of Primary and Alternate you may wish
// to rewrite this using array of string as the
// parameters as SetDNSServerSearchOrder will take
// a list of many DNS addresses. I only have use for
// Primary and Alternate.
// ====================================================
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;
(* ----------------------------------------------------
See the Miscrosoft MSDN Documentation for the
Win32_NetworkAdapterConfiguration Class
(implemented as oNetAdatper in my examples),
There are many more calls besides EnableStatic,
EnableDHCP and SetDNBSServerSearchOrder.
Some of them are ...
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
------------------------------------------------ *)
출처 : http://www.delphi3000.com/
'델파이' 카테고리의 다른 글
현재 페이지의 모든 쿠키 읽어 오기 (0) | 2009.01.29 |
---|---|
서버 상태 확인 (0) | 2009.01.12 |
BeforeNavigate2 이벤트에서 PostData 읽어오기 (GET 방식과 POST 방식의 구분) (0) | 2008.12.04 |
ActiveX의 ProgID 변경하기 (0) | 2008.11.26 |
생성자 오버로딩 하기 (0) | 2008.11.20 |
IE Band에서 탭키, 엔터키, 델리트키(VK_TAB, VK_RETURN, VK_DELETE) 입력가능하도록 하기 (0) | 2008.10.17 |
소수점 이하 제거하는 함수 (0) | 2008.10.06 |
Delphi 2007에서 DSPack 설치하기 (0) | 2008.09.24 |
Internet Explorer7 에서 현재 활성화 되있는 탭윈도우 핸들 얻기 (0) | 2008.08.28 |
Delphi로 ActiveX 제작시 DAX 에러나는 문제 해결법 (0) | 2008.08.07 |