不用控件,用代码取得自己的IP -----------------------------Option ExplicitPrivate Type TYPEWsa wVersion As Integer wHighVersion As Integer szDescription(0 To 255) As Byte szSystemStatus(0 To 128) As Byte iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As Long End TypePrivate Declare Function inet_ntoa Lib "wsock32" (ByVal addr As Long) As Long Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long Private Declare Function RtlMoveMemory Lib "kernel32" (ByRef lpvDest As Any, ByRef lpvSource As Any, ByVal lpvLength As Long) As Long Private Declare Function WSACleanup Lib "wsock32" () As Long Private Declare Function WSAStartup Lib "wsock32" (ByVal wVersionRequired As Long, IPAWSAType As TYPEWsa) As Long Private Declare Function gethostname Lib "wsock32" (ByVal szHost As String, ByVal dwHostLen As Long) As Long Private Declare Function gethostbyname Lib "wsock32" (ByVal szHost As String) As Long Private Declare Function lstrlenA Lib "kernel32" (ByRef lpString As Any) As Long Private Declare Function lstrcpyA Lib "kernel32" (ByRef lpString1 As Any, ByRef lpString2 As Any) As Long'// -网络IP属性- Private Property Get IpAddress() As String On Error Resume Next Dim HostName As String Dim IPATmpAddress As Long Dim IPAOne As Long, IPATwo As Long Dim IpAAddress As Long, IPAWSAType As TYPEWsa HostName = String$(256, Chr(0)) Call GetComputerNameA(HostName, 256) HostName = Left$(HostName, InStr(HostName & Chr(0), Chr(0)) - 1) Call WSAStartup(IIf(1 > 0, &H101, 0), IPAWSAType) IPAOne = gethostbyname(HostName + String(64 - Len(HostName), 0)) + 12 If IPAOne <> 12 Then Call RtlMoveMemory(IPAOne, ByVal IPAOne, 4) Call RtlMoveMemory(IPATwo, ByVal IPAOne, 4) Call RtlMoveMemory(IpAAddress, ByVal IPATwo, 4) IPATmpAddress = inet_ntoa(IpAAddress) IpAddress = String$(lstrlenA(ByVal IPATmpAddress), Chr(0)) If IpAddress <> "" Then Call lstrcpyA(ByVal IpAddress, ByVal IPATmpAddress) Call WSACleanup End If
Call Err.Clear DoEvents End PropertyPrivate Sub Form_Load() MsgBox IpAddress End Sub
到超级猛料里,COPY一下那个网络相关的代码吧,我记得有一人获取本机IP的函数的
procedure TForm1.Button3Click(Sender: TObject); function LookupName(const Name: string): TInAddr; var HostEnt: PHostEnt; InAddr: TInAddr; begin HostEnt := gethostbyname(PChar(Name)); FillChar(InAddr, SizeOf(InAddr), 0); if HostEnt <> nil then begin with InAddr, HostEnt^ do begin S_un_b.s_b1 := h_addr^[0]; S_un_b.s_b2 := h_addr^[1]; S_un_b.s_b3 := h_addr^[2]; S_un_b.s_b4 := h_addr^[3]; end; end; Result := InAddr; end; var p : pchar; l : DWORD; ht : TInAddr; WSAData1 : TWSAData; begin l := MAX_COMPUTERNAME_LENGTH + 1; p :=AllocMem(l); WSAStartup($0101,WSAData1); GetComputerName(p,l); ht :=LookupName(p); ShowMessage(inet_ntoa(ht)); FreeMem(p); WSACleanup; end;
-----------------------------Option ExplicitPrivate Type TYPEWsa
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End TypePrivate Declare Function inet_ntoa Lib "wsock32" (ByVal addr As Long) As Long
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function RtlMoveMemory Lib "kernel32" (ByRef lpvDest As Any, ByRef lpvSource As Any, ByVal lpvLength As Long) As Long
Private Declare Function WSACleanup Lib "wsock32" () As Long
Private Declare Function WSAStartup Lib "wsock32" (ByVal wVersionRequired As Long, IPAWSAType As TYPEWsa) As Long
Private Declare Function gethostname Lib "wsock32" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "wsock32" (ByVal szHost As String) As Long
Private Declare Function lstrlenA Lib "kernel32" (ByRef lpString As Any) As Long
Private Declare Function lstrcpyA Lib "kernel32" (ByRef lpString1 As Any, ByRef lpString2 As Any) As Long'// -网络IP属性-
Private Property Get IpAddress() As String
On Error Resume Next
Dim HostName As String
Dim IPATmpAddress As Long
Dim IPAOne As Long, IPATwo As Long
Dim IpAAddress As Long, IPAWSAType As TYPEWsa
HostName = String$(256, Chr(0))
Call GetComputerNameA(HostName, 256)
HostName = Left$(HostName, InStr(HostName & Chr(0), Chr(0)) - 1)
Call WSAStartup(IIf(1 > 0, &H101, 0), IPAWSAType)
IPAOne = gethostbyname(HostName + String(64 - Len(HostName), 0)) + 12
If IPAOne <> 12 Then
Call RtlMoveMemory(IPAOne, ByVal IPAOne, 4)
Call RtlMoveMemory(IPATwo, ByVal IPAOne, 4)
Call RtlMoveMemory(IpAAddress, ByVal IPATwo, 4)
IPATmpAddress = inet_ntoa(IpAAddress)
IpAddress = String$(lstrlenA(ByVal IPATmpAddress), Chr(0))
If IpAddress <> "" Then Call lstrcpyA(ByVal IpAddress, ByVal IPATmpAddress)
Call WSACleanup
End If
Call Err.Clear
DoEvents
End PropertyPrivate Sub Form_Load()
MsgBox IpAddress
End Sub
function LookupName(const Name: string): TInAddr;
var
HostEnt: PHostEnt;
InAddr: TInAddr;
begin
HostEnt := gethostbyname(PChar(Name));
FillChar(InAddr, SizeOf(InAddr), 0);
if HostEnt <> nil then
begin
with InAddr, HostEnt^ do
begin
S_un_b.s_b1 := h_addr^[0];
S_un_b.s_b2 := h_addr^[1];
S_un_b.s_b3 := h_addr^[2];
S_un_b.s_b4 := h_addr^[3];
end;
end;
Result := InAddr;
end;
var
p : pchar;
l : DWORD;
ht : TInAddr;
WSAData1 : TWSAData;
begin
l := MAX_COMPUTERNAME_LENGTH + 1;
p :=AllocMem(l);
WSAStartup($0101,WSAData1);
GetComputerName(p,l);
ht :=LookupName(p);
ShowMessage(inet_ntoa(ht));
FreeMem(p);
WSACleanup;
end;