chengjian
asked on
GET IP
Hi
forgive my poor English(In fact my specialty is English:)) and
poor knowledge of programming
Get both the PC's Network address and Subnet Mask.//I do not know how,
for I do not know the type Net work adapter card the user has installed.
If u do not read it from registry I will very happy but if u do so be sure
I want both windows NT and Windows 95(95/97/98)
Source required
Thanks a lot
e=mail:cheng-jian@163.net
forgive my poor English(In fact my specialty is English:)) and
poor knowledge of programming
Get both the PC's Network address and Subnet Mask.//I do not know how,
for I do not know the type Net work adapter card the user has installed.
If u do not read it from registry I will very happy but if u do so be sure
I want both windows NT and Windows 95(95/97/98)
Source required
Thanks a lot
e=mail:cheng-jian@163.net
see F.Piette's http://www.rtfm.be/fpiette/indexuk.htm
Hi! I'm very happy to saw a chinese man here!
May I talk to u in chinese ?
àË£¡ÄÜÔÚÕâÀï¼ûµ½ÖйúÈËÕæÊÇ ¸ßÐË£¡
ÇëÄãÒ²ÔÁÂÎÒµÄõ¿½ÅÓ¢ÓÔÛ ÃÇ»¹ÊÇÓÃÖÐ ÎÄÀ´Ëµ°É¡£
ÄãÄÜÔÙ½«ÄãµÄÎÊÌâÓÃÖÐÎÄ˵һ ±éÂð£¿Ò²Ðí ÎÒÄÜ°ïÄã¡£
(ÀÏÏç¼ûÀÏÏ磬Á½ÑÛÀáÍôÍô)
May I talk to u in chinese ?
àË£¡ÄÜÔÚÕâÀï¼ûµ½ÖйúÈËÕæÊÇ
ÇëÄãÒ²ÔÁÂÎÒµÄõ¿½ÅÓ¢ÓÔÛ
ÄãÄÜÔÙ½«ÄãµÄÎÊÌâÓÃÖÐÎÄ˵һ
(ÀÏÏç¼ûÀÏÏ磬Á½ÑÛÀáÍôÍô)
or here: http://www.preview.org/q/q2056.shtml
hi, chengjian!
maybe this helps you:
function Get_Hostname: string;
var
WSAData: TWSAData;
sHostName: string;
begin
if WSAStartup($0101, WSAData) <> 0 then begin
Result := 'WSAStartup error';
Exit;
end;
try
sHostName := '';
SetLength(sHostName, 100);
if GetHostName(PChar(sHostNam e), 100) = 0 then begin
SetLength(sHostName, StrLen(PChar(sHostName)));
Result := sHostName;
end
else case WSAGetLastError of
WSAEFAULT : Result := 'WSAEFault';
WSANOTINITIALISED: Result := 'WSANotInitialised';
WSAENETDOWN : Result := 'WSAENetDown';
WSAEINPROGRESS : Result := 'WSAEInProgress';
end;
finally
WSACleanup;
end;
end;
function Get_HostIP: string;
var
HEnt: pHostEnt;
WSAData: TWSAData;
iCnt: Integer;
sHostName: string;
sIPAddr: string;
begin
if WSAStartup($0101, WSAData) <> 0 then begin
Result := 'WSAStartup error';
Exit;
end;
try
sIPAddr := '';
sHostName := '';
SetLength(sHostName, 100);
if GetHostName(PChar(sHostNam e), 100) = 0 then begin
SetLength(sHostName, StrLen(PChar(sHostName)));
HEnt := GetHostByName(PChar(sHostN ame));
if HEnt <> nil then begin
for iCnt := 0 to HEnt^.h_length - 1 do
sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_ list^[iCnt ])) + '.';
SetLength(sIPAddr, Length(sIPAddr) - 1);
Result := sIPaddr;
end
else
Result := 'Hostname not found';
end
else case WSAGetLastError of
WSAEFAULT : Result := 'WSAEFault';
WSANOTINITIALISED: Result := 'WSANotInitialised';
WSAENETDOWN : Result := 'WSAENetDown';
WSAEINPROGRESS : Result := 'WSAEInProgress';
end;
finally
WSACleanup;
end;
end;
bye,
Black Death.
maybe this helps you:
function Get_Hostname: string;
var
WSAData: TWSAData;
sHostName: string;
begin
if WSAStartup($0101, WSAData) <> 0 then begin
Result := 'WSAStartup error';
Exit;
end;
try
sHostName := '';
SetLength(sHostName, 100);
if GetHostName(PChar(sHostNam
SetLength(sHostName, StrLen(PChar(sHostName)));
Result := sHostName;
end
else case WSAGetLastError of
WSAEFAULT : Result := 'WSAEFault';
WSANOTINITIALISED: Result := 'WSANotInitialised';
WSAENETDOWN : Result := 'WSAENetDown';
WSAEINPROGRESS : Result := 'WSAEInProgress';
end;
finally
WSACleanup;
end;
end;
function Get_HostIP: string;
var
HEnt: pHostEnt;
WSAData: TWSAData;
iCnt: Integer;
sHostName: string;
sIPAddr: string;
begin
if WSAStartup($0101, WSAData) <> 0 then begin
Result := 'WSAStartup error';
Exit;
end;
try
sIPAddr := '';
sHostName := '';
SetLength(sHostName, 100);
if GetHostName(PChar(sHostNam
SetLength(sHostName, StrLen(PChar(sHostName)));
HEnt := GetHostByName(PChar(sHostN
if HEnt <> nil then begin
for iCnt := 0 to HEnt^.h_length - 1 do
sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_
SetLength(sIPAddr, Length(sIPAddr) - 1);
Result := sIPaddr;
end
else
Result := 'Hostname not found';
end
else case WSAGetLastError of
WSAEFAULT : Result := 'WSAEFault';
WSANOTINITIALISED: Result := 'WSANotInitialised';
WSAENETDOWN : Result := 'WSAENetDown';
WSAEINPROGRESS : Result := 'WSAEInProgress';
end;
finally
WSACleanup;
end;
end;
bye,
Black Death.
& don't forget the winsock unit in the uses clause...
ASKER
Hi annlee: I am a chinese. but i cannot know ur chinese:))
mail me in chinese:cjcjc@990.net
thanks BlackDeath.i will try ur code
Yours Quession Asker
mail me in chinese:cjcjc@990.net
thanks BlackDeath.i will try ur code
Yours Quession Asker
ASKER
BlackDeath that worked,could u explain it?
Is it require a hosts file? or other?
Thanks a lot
Is it require a hosts file? or other?
Thanks a lot
you don't need no host file.
this whole thing works via windows sockets support.
(therefore the WSAStartup/WSACleanup).
what exactly happens in there - i can't tell ya.
only thing i can say is:
take a look into win32.hlp -
"WSAStartup" for windows sockets startup/end
"GetHostName" for getting name of local computer
"GetHostByName" for getting address of computer "name" (in this case, the name was returned by gethostname and is the name of the local computer, therefore, return will be the address of the local computer)
"WINSOCK.H" for structure hostent
o.k.?
bye,
Black Death.
this whole thing works via windows sockets support.
(therefore the WSAStartup/WSACleanup).
what exactly happens in there - i can't tell ya.
only thing i can say is:
take a look into win32.hlp -
"WSAStartup" for windows sockets startup/end
"GetHostName" for getting name of local computer
"GetHostByName" for getting address of computer "name" (in this case, the name was returned by gethostname and is the name of the local computer, therefore, return will be the address of the local computer)
"WINSOCK.H" for structure hostent
o.k.?
bye,
Black Death.
ASKER
Good thank you.
Why do not put this as an a?
I will will give u score
btw:I think it can work under NT, i did not test for i do not
have nt installed now.
Why do not put this as an a?
I will will give u score
btw:I think it can work under NT, i did not test for i do not
have nt installed now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi BlackDeath
I am so sorry that I cannot give u point now. I also want the subnet mask just i mentioned in the quession. Could u help me?
Thank you
Chengjian
I am so sorry that I cannot give u point now. I also want the subnet mask just i mentioned in the quession. Could u help me?
Thank you
Chengjian
ok.
here's something i figured out before i knew how to get the ip address via winapi. you can use this and modify so that you compare the corresponding ipaddresses of all protocols you may find having an ipaddress to the ipaddress you received by gethostbyname, so that you know you're in the correct branch of the registry describing the service you are searching for.
the code is as follows:
const
C_NT = $01;
C_95 = $02;
function MyGetIPAddresses(idSys: Byte; var IPList: TStringList):
Boolean;
// 95 & NT (Parameter 1: C_95 / C_NT, returns False on failure
// IPList returns on success with 3 entries: IPAddress, SubnetMask, DefaultGateway
var
bCnt: Byte;
iCnt: Integer;
boSuccess: Boolean;
sTemp: string;
begin
// create instance of TRegistry
Reg := TRegistry.Create;
boSuccess := False;
try
with Reg do
begin
// set root key
RootKey := HKEY_LOCAL_MACHINE;
case idSys of
C_NT: begin
if OpenKey('\SOFTWARE\Microso ft\Windows NT\CurrentVersion\NetworkC ards',
False) then
begin
boSuccess := True;
slKeyNames := TStringList.Create;
// get all network cards
GetKeyNames(slKeyNames);
for iCnt := 0 to slKeyNames.Count - 1 do
begin
if OpenKey('\SOFTWARE\Microso ft\Windows NT\CurrentVersion\NetworkC ards\' +
slKeyNames[iCnt], False) then
begin
slKeyNames[iCnt] := ReadString('ServiceName');
if OpenKey('\SYSTEM\CurrentCo ntrolSet\S ervices\' +
slKeyNames[iCnt] + '\Parameters\Tcpip', False) then
begin
DataType := REG_BINARY;
BufSize := SizeOf(buf);
if RegQueryValueEx(CurrentKey , PChar('IPAddress'), nil,
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
if RegQueryValueEx(CurrentKey , PChar('SubnetMask'), nil,
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
if RegQueryValueEx(CurrentKey , PChar('DefaultGateway'), nil,
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
end;
end;
end;
slKeyNames.Free;
end
else
boSuccess := False;
end;
C_95: begin
// open key for MSTCP-entries
if OpenKey('\Enum\Network\MST CP', False) then
begin
boSuccess := True;
slKeyNames := TStringList.Create;
GetKeyNames(slKeyNames);
// iterate through MSCTCP-entries
for iCnt := 0 to slKeyNames.Count - 1 do
if OpenKey('\Enum\Network\MST CP\' + slKeyNames[iCnt], False) then
begin
slKeyNames[iCnt] := ReadString('Driver');
if OpenKey('\System\CurrentCo ntrolSet\S ervices\Cl ass\' +
slKeyNames[iCnt], False) then
begin
IPList.Add(ReadString('IPA ddress'));
IPList.Add(ReadString('IPM ask'));
IPList.Add(ReadString('Def aultGatewa y'));
end
else
begin
IPList.Add('OpenKey error on ' + sKey);
IPList.Add('OpenKey error on ' + sKey);
IPList.Add('OpenKey error on ' + sKey);
end;
end;
slKeyNames.Free;
end
else
boSuccess := False;
end;
end;
if not boSuccess then
begin
Result := False;
Exit;
end;
end;
finally
Reg.Free;
Result := True;
end;
end;
remark: don't forget to create your iplist before calling the function
i didn't use this since half a year or so.
if any questions, ask me, i'll dive into it to clarify.
regards,
Black Death.
here's something i figured out before i knew how to get the ip address via winapi. you can use this and modify so that you compare the corresponding ipaddresses of all protocols you may find having an ipaddress to the ipaddress you received by gethostbyname, so that you know you're in the correct branch of the registry describing the service you are searching for.
the code is as follows:
const
C_NT = $01;
C_95 = $02;
function MyGetIPAddresses(idSys: Byte; var IPList: TStringList):
Boolean;
// 95 & NT (Parameter 1: C_95 / C_NT, returns False on failure
// IPList returns on success with 3 entries: IPAddress, SubnetMask, DefaultGateway
var
bCnt: Byte;
iCnt: Integer;
boSuccess: Boolean;
sTemp: string;
begin
// create instance of TRegistry
Reg := TRegistry.Create;
boSuccess := False;
try
with Reg do
begin
// set root key
RootKey := HKEY_LOCAL_MACHINE;
case idSys of
C_NT: begin
if OpenKey('\SOFTWARE\Microso
False) then
begin
boSuccess := True;
slKeyNames := TStringList.Create;
// get all network cards
GetKeyNames(slKeyNames);
for iCnt := 0 to slKeyNames.Count - 1 do
begin
if OpenKey('\SOFTWARE\Microso
slKeyNames[iCnt], False) then
begin
slKeyNames[iCnt] := ReadString('ServiceName');
if OpenKey('\SYSTEM\CurrentCo
slKeyNames[iCnt] + '\Parameters\Tcpip', False) then
begin
DataType := REG_BINARY;
BufSize := SizeOf(buf);
if RegQueryValueEx(CurrentKey
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
if RegQueryValueEx(CurrentKey
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
if RegQueryValueEx(CurrentKey
@DataType, @buf, @bufSize) = ERROR_SUCCESS then
begin
sTemp := '';
for bCnt := 1 to bufSize do
if buf[bCnt] <> 0 then
sTemp := sTemp + Chr(buf[bCnt]);
IPList.Add(sTemp);
end
else
IPList.Add('RegQuery error');
end;
end;
end;
slKeyNames.Free;
end
else
boSuccess := False;
end;
C_95: begin
// open key for MSTCP-entries
if OpenKey('\Enum\Network\MST
begin
boSuccess := True;
slKeyNames := TStringList.Create;
GetKeyNames(slKeyNames);
// iterate through MSCTCP-entries
for iCnt := 0 to slKeyNames.Count - 1 do
if OpenKey('\Enum\Network\MST
begin
slKeyNames[iCnt] := ReadString('Driver');
if OpenKey('\System\CurrentCo
slKeyNames[iCnt], False) then
begin
IPList.Add(ReadString('IPA
IPList.Add(ReadString('IPM
IPList.Add(ReadString('Def
end
else
begin
IPList.Add('OpenKey error on ' + sKey);
IPList.Add('OpenKey error on ' + sKey);
IPList.Add('OpenKey error on ' + sKey);
end;
end;
slKeyNames.Free;
end
else
boSuccess := False;
end;
end;
if not boSuccess then
begin
Result := False;
Exit;
end;
end;
finally
Reg.Free;
Result := True;
end;
end;
remark: don't forget to create your iplist before calling the function
i didn't use this since half a year or so.
if any questions, ask me, i'll dive into it to clarify.
regards,
Black Death.
ach, forget the last one.
i've put some code together in a demo project i'd like to send you. that makes it easier to understand. i've just tested it and here it works, so it should work on your machines, too.
it's delphi 3 code.
i'll send the exe along with the source code.
this will give you the ip-address and the subnet mask.
you can of course get much more information if you want to. just ask me, perhaps i can help you.
so - just post your email-address here and i'll send you the demo-project.
so long,
Black Death.
i've put some code together in a demo project i'd like to send you. that makes it easier to understand. i've just tested it and here it works, so it should work on your machines, too.
it's delphi 3 code.
i'll send the exe along with the source code.
this will give you the ip-address and the subnet mask.
you can of course get much more information if you want to. just ask me, perhaps i can help you.
so - just post your email-address here and i'll send you the demo-project.
so long,
Black Death.
by the way:
some of the code is stone old (e.g. the piece posted above).
so please don't have an eye on the syntax too much - i promise, i've improved a little bit.
;-)
Black Death.
some of the code is stone old (e.g. the piece posted above).
so please don't have an eye on the syntax too much - i promise, i've improved a little bit.
;-)
Black Death.
ASKER
Thanks a lot, my mail address is:
cjcjc@990.net
cjcjc@990.net
ASKER
BTW
Happy spring day. It's a Chinese festival.
Happy spring day. It's a Chinese festival.
ASKER
hi BlackDeath:
I received that thank you. Could u tell me what's different between nt and 95? perhaps 98? Is there a way to use api to get subnet mask?
I received that thank you. Could u tell me what's different between nt and 95? perhaps 98? Is there a way to use api to get subnet mask?
hey, chengjian:
take a look into the code i've sent ya.
as one can easily see, the registration keys for tcp/ip configuration differ significantly in 95 and nt.
if i'd've known a way to get the subnet mask by using api calls, i'd've made use of'em, as sure as the pope is catholic.
concerning 98: i've to admit that i didn't test it on 98, but i believe that there ain't no differences to 95 regarding this point.
good that you've tested. did it work?
so long,
Black Death.
take a look into the code i've sent ya.
as one can easily see, the registration keys for tcp/ip configuration differ significantly in 95 and nt.
if i'd've known a way to get the subnet mask by using api calls, i'd've made use of'em, as sure as the pope is catholic.
concerning 98: i've to admit that i didn't test it on 98, but i believe that there ain't no differences to 95 regarding this point.
good that you've tested. did it work?
so long,
Black Death.
ASKER
HI BlackDeath
Now I have tested ur file, but I cannot get subnet mask I use win98. I edited ur source code and make sure it do the same as win95 or winnt but they did not work. Could u help me?
Now I have tested ur file, but I cannot get subnet mask I use win98. I edited ur source code and make sure it do the same as win95 or winnt but they did not work. Could u help me?
chengjian -
have u used regedit to verify the key in the registry of win98 where the subnet mask can be found?
if it doesn't work, debug it & test the result of OpenKey.
the problem is - i haven't got win98 in a network area here. i can try & set up one of the machines at home - if there's a little spare time...
i'll be back.
Black Death.
have u used regedit to verify the key in the registry of win98 where the subnet mask can be found?
if it doesn't work, debug it & test the result of OpenKey.
the problem is - i haven't got win98 in a network area here. i can try & set up one of the machines at home - if there's a little spare time...
i'll be back.
Black Death.
ASKER
sorry I find it but it does not work
post your code
ASKER
still not work under 98 and osr/2:((
post your code
ASKER
Just use your example
I now use NT at home, your demo works.
but not in my office's 98 and osr/2.
:((
I now use NT at home, your demo works.
but not in my office's 98 and osr/2.
:((
well, i'm gonna give it another try & i'll b back 2morrow or next week.
bye so far,
BlackDeath.
bye so far,
BlackDeath.
ASKER
have u finished?
best regards
best regards
oops, sorry 4 being offline 4 so long.
nevermind, i'm just building the garden of our new house - it eats the time... ;-)
well, i've tested it on win95 osr2 - no problem at all.
i do not know of any changes from 95 osr2 to 98 regarding this specific problem, since osr2 is nearly identical w/ 98.
(press said that if the court'd decided 2 not let m$ get off w/ 98, m$'d simply declared osr2 as 98...)
i'm afraid u've 2 debug & tell me at which line the precise problem occurs. up 2 now the only thing u've said is:
"it doesn't work".
describe the not working.
debug the code & tell me where the prob is.
it's unbelievingly hard 2 help u when u don't tell me more precisely where your problem is located while the app run's fine @my.place
anyway - h.a.n.d.
BlackDeath.
nevermind, i'm just building the garden of our new house - it eats the time... ;-)
well, i've tested it on win95 osr2 - no problem at all.
i do not know of any changes from 95 osr2 to 98 regarding this specific problem, since osr2 is nearly identical w/ 98.
(press said that if the court'd decided 2 not let m$ get off w/ 98, m$'d simply declared osr2 as 98...)
i'm afraid u've 2 debug & tell me at which line the precise problem occurs. up 2 now the only thing u've said is:
"it doesn't work".
describe the not working.
debug the code & tell me where the prob is.
it's unbelievingly hard 2 help u when u don't tell me more precisely where your problem is located while the app run's fine @my.place
anyway - h.a.n.d.
BlackDeath.
ASKER
sorry,the subnet mask not apear:(
I will try again and have a debug
I will try again and have a debug