PDA

Click to See Complete Forum and Search --> : Set and retrieve DNS,IP and gateway, how ?


Sep 10th, 2000, 03:44 AM
Hallo ?
I need to set and retrieve Ip , DNS and gateway(s) network addresses on a PC. How can I do it ? Which API's need ?
Thanks to anyone

da_silvy
Sep 11th, 2000, 12:06 AM
this code opens winipcfg then checks for the class
"Static" (labels) inside that program.
it then checks if the first character is a number,
then adds it to the listbox if it is.

Tell me what you think!

put this is a module

Option Explicit
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5

Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

Public Declare Function GetDesktopWindow Lib "user32" () As Long

Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long



Public Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Public Const WM_SETTEXT = &HC


put this on your form code
also add a listbox for the numbers

Option Explicit
Dim myBool As Boolean
Public Function GethWndFromProcessID(hProcessIDToFind As Long) As Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProcessID As Long

On Local Error GoTo GethWndFromProcessID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindow()

'get the first child under the desktop
hWndChild = GetWindow(hWndDesktop, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the ThreadProcessID of the window
Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

'if it matches the target, exit returning that value
If hWndChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hWndChild
Exit Do
End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

Exit Function

GethWndFromProcessID_Error:
GethWndFromProcessID = 0
Exit Function

End Function

Private Sub Form_Load()
Dim hProcessID As Long, StartShell As Long, myNewHwnd As Long
hProcessID = Shell("winipcfg") 'shell Windows IP Configuration
StartShell = GethWndFromProcessID(hProcessID) 'Get it's Handle
'used to return window handles.
Dim TitleToFind As String, ClassToFind As String, lbCount As String
Dim r As Long
List1.Clear
TitleToFind = "*"
ClassToFind = "Static"
Call FindWindowLike(0, "*", "Static", StartShell)
End Sub
Function CheckNumber(strString As String) As Boolean
On Error GoTo ErrHandler:
Dim myAsc As Integer
myAsc = Asc(strString)
If myAsc >= 48 And myAsc <= 57 Then
CheckNumber = True 'if it's a number
Else
CheckNumber = False 'if it isn't
End If
Exit Function
ErrHandler:
CheckNumber = False
Exit Function
End Function
Private Function FindWindowLike(ByVal hWndStart As Long, WindowText As String, Classname As String, StartShell As Long) As Long

Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long
Static level As Integer

If level = 0 Then
If hWndStart = 0 Then hWndStart = StartShell
End If


level = level + 1

hwnd = GetWindow(hWndStart, GW_CHILD)

Do Until hwnd = 0

Call FindWindowLike(hwnd, WindowText, Classname, StartShell)


sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)

sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)


If (sWindowText Like WindowText) And (sClassname Like Classname) Then

myBool = CheckNumber(sWindowText)
If myBool Then List1.AddItem sWindowText 'the value of the textboxes
FindWindowLike = hwnd


End If


hwnd = GetWindow(hwnd, GW_HWNDNEXT)

Loop


level = level - 1

End Function

you will have to close winipcfg after this though, (i don't know how :( )

the first number in the listbox is the adapter address
the second is the ip address
the third is the subnet mask
the fourth is the default gateway

Good Luck

shtirliz
Sep 11th, 2000, 11:42 AM
You can use methods exposed in IPHLPAPI.DLL (Use MSDN to find declarations). This DLL is fully supported in W2K, and partially available in NT 4.0. If you are running 95/98 enumrate the keys under HKLM\System\CurrentControlSet\Services\Class\NetTrans
to get Adapter specific info.

Hope that helps.

Sep 16th, 2000, 07:25 AM
Originally posted by shtirliz
You can use methods exposed in IPHLPAPI.DLL (Use MSDN to find declarations). This DLL is fully supported in W2K, and partially available in NT 4.0. If you are running 95/98 enumrate the keys under HKLM\System\CurrentControlSet\Services\Class\NetTrans
to get Adapter specific info.

Hope that helps.


Thanks also to you.
Second solution is probably more "direct".
I'm trying to write code to do it in this way.
I'll post the code as soon.
Thanks to all