Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choosing and manipulating network connections 1

Status
Not open for further replies.

rudejohn

IS-IT--Management
Jul 11, 2003
130
US
I have a rather complex question... thanks in advance for anyone who can help me architect a solution. I'm not looking for anyone to write the code for me... just general advice on what APIs to use and how to tackle this logically:

- Machine can have up to three network connections (wired ethernet, wireless wi-fi, and USB).

- We need to change network settings of the WIRED (cat5) connection, but we don't necessarily know which connection it is (is there a way, in code, to find a specific network connection type?)

- Release DHCP, assign a static IP to that same wired connection. Once again, we do not necessarily know which connection is wired (eth0, eth1, etc etc)

- Code needs to be portable between machines (i.e. we can't hardwire MAC addresses or anything)

Thanks in advance for your help!!! :)

************
RudeJohn
************
 
If you can pull out stops to get WMI installed on all machines prior to W2K, then this code will work. This particular bit of code will work out of the box with XP (what i've tested it on)

Code:
    Dim wmgts As Object
    Dim Adapter As Object
    
    Set wmgts = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\localhost")
   
    For Each Adapter In wmgts.instancesof("Win32_NetworkAdapter")
        Debug.Print Adapter.Name & vbNullstring; Adapter.AdapterType & vbNullString; Adapter.AdapterTypeID & vbNullString
    Next

See this for reference on all of the properties of the adapter

See this for reference on Renewing the DHCP

Hope this helps. If you need further, more specific assistant, be sure to post back.
 
Thanks for your help. I'll begin my attempt at implementing this today.

Unfortunately, we have a large number of Win98 machines lurking around various sites. Are you saying this code won't work on Win98? What about Win2K (a large portion of our machines are W2K)? I'll talk with my "customers" and see if we can write this tool and skip Windows 98 compatibility.

Thanks in advance,

John

************
RudeJohn
************
 
Thanks bjd4jc for your help so far. It looks as though we're not going to be able to skip the Win98 machines, and packaging the WMI executable might be difficult in our environment. Any other suggestions for taking care of the Win98 machines?

RJ

************
RudeJohn
************
 
Thx for the star. I don't have much experience working with W98 machines. Perhaps someone else here can help you out.

It would be to your benefit to get WMI on your W98 machines because it opens up a lot of possibilities that would not otherwise be able to do.
 
Okay, another question. You suggest using WMI to find the adapater. The code you have prints out the adapter type, adapter name, etc. Once I have that adapter name (for example, "1394 Net Adapter," how do I then configure that network adapter (for example, I need to change the IP address for that adapter or enable/disable DHCP? All the code I've seen before requires you to input something like "Local Area Connection 3" (the Windows network neighborhood name), not the actual adapter name. But the "NetConnectionID" Property for WMI is not available in Win98 NOR Win2000. So how can I make this compatible?

************
RudeJohn
************
 
Sorry, I had posted two bogus links in my previous post.. well not bogus but not the right ones.

For the Win32_NetworkAdapter class see

For the Win32_NetworkAdapterConfiguration class see

You will want to identify the adapter you want to change with the WIN32_NetworkAdapter class. Return the MACAddress and then use the MACAddress to reference this same adapter via the Win32_NetworkAdapterConfiguration class. This class has a ReleaseDHCPLease method, then call the EnableStatic, SetGateways and the SetDNSServerSearchOrder methods to set the static IP Address.

Here is the code that I created to do this:

Code:
    Dim wmgts As Object
    Dim Adapter As Object
    Dim Config As Object
    Dim HoldMACAddress As String
    
    Set wmgts = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\localhost")
   
    For Each Adapter In wmgts.instancesof("Win32_NetworkAdapter")
        Debug.Print Adapter.Name & vbNullString; Adapter.AdapterType & vbNullString; Adapter.AdapterTypeID & vbNullString
        If Adapter.netconnectionid = "Local Area Connection" Then
            HoldMACAddress = Adapter.macaddress
            Exit For
        End If
    Next
    
    For Each Config In wmgts.instancesof("Win32_NetworkAdapterConfiguration")
        If Config.macaddress = HoldMACAddress Then
            Config.releasedhcplease
            Config.enablestatic Split("192.168.10.114", ""), Split("255.255.255.0", "")
            Config.SetGateways Split("192.168.10.1", "")
            Config.SetDNSServerSearchOrder Split("192.168.10.10;192.168.10.14", ";")
        End If
    Next

I am little confused as to how you are going about getting the adapter itself and whe the NetConnectionID Came from. Are you looking for the adapter based upon its name or based upon the adapter type as specified in your previous post? Give the code above a try and see how far this gets you.
 
Excellent code. The "netconnectionID" is in the properties list for Win32_NetworkAdapter. However, it explicitly states that netconnectionID does NOT work on Win2000 and WinXP.

Now that I see how your code works, I can probably adapt it to use the device name and parse the string for the vendor name. Thanks again and I'll come back if I have any more questions.

RJ

************
RudeJohn
************
 
Thanks again for your help. The code works great to find the right network adapter now, and I have written some modules to change the IP address for that adapter. Now the next step: I need to create a network share on the machine that can be used from that adapter. Here are the details:

-Machine 1 is assigned a 192.168.0.100 IP Address
-Machine 2 is assigned a 192.168.0.200 IP Address
-I connect the two machines with a crossover cable
-I create shared folder (C:\share) on Machine 2.
-I need to connect to shared folder from Machine 1

I need to do this ALL via code. Here is my existing code:

Code:
Dim success 
success = _
   ShareAdd("\\" & Environ$ ("COMPUTERNAME"), "c:\share", _
   "share", "share", "")

This is using the NetShareAdd Lib "netapi32" API.
This code DOES successfully create the share.

However, when I go to Machine 2 and try to connect to this share (i.e. "\\192.168.0.100\share"), I get a generic "Either it's not available or you don't have credentials" error message. I realize this MAY be a networking question, and not a Visual Basic question, but I have the utmost confidence that one of our resident geniuses will be able to help me. :)

Thanks a TON in advance, I know this is a long question and lots of work...

~RJ

************
RudeJohn
************
 
>-I connect the two machines with a crossover cable

>I need to do this ALL via code. Here is my existing code:


Good luck connecting this via code ;-)
 
If you set this up manually does it work?

I guess I would try to get it working by setting it up manually. Then figure out what you did to get the share connectivity working.

Doing this between mulitple operating systems might be a bit of a challenge but this should be possible to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top