From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically changing the TCP/IP address of Windows Vista / 7 / 8

Hi there!

 

I'm pretty new to LabVIEW (started 2 months ago) and still have a long way ahead of me, so please forgive me if anything that I ask is too basic.

 

I'm trying to change the IP properties of the PC via LabVIEW for some bigger software I'm working on.

 

I can easily do it in Windows XP using the System Exec function to call cmd.exe and then use netsh commands, as previously explained for example here:

But then, since I want to run my software in Windows 7 (& by extension to any Windows OS), I have a problem: netsh asks for elevated rights.

 

So, the next problem is: how do I run cmd.exe (or any other programm?, by extension) as administrator (from LabVIEW)?. Note that I'm using an account with admin rights. But I would prefer not having to disable the User Account Control configuration, and I don't want to force the user to any interaction (clicking in pop-ups, or right-clicking in anything), I want it to be as automatized as possible.

 

I did a bit of research and couldn't find anyway to do it, neither any command to run with cmd, so the two similar solutions that came to my mind are very rudimentary and kind of dirty:

  1. Use DLLs to make LabVIEW press the keys necessary to open cmd running as admin via key shortcuts (using a variation of https://decibel.ni.com/content/docs/DOC-29563 and the key shortcuts from here: http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-r...). Here, I open the Window menu (with LWIN), properly writes "cmd", and then uses CTRL+SHIFT+ENTER ok... but then the User Account Control appears and LabView keeps doing the commands (I want ALT+Y or LEFT & then ENTER)... without affecting to the UAC! If I make LabVIEW wait a bit and manually close the UAC I succesfully have the command prompt... but LabVIEW is still incapable of write the text that I'm giving it to him (the netsh commands). LabVIEW runs the software, but it's like the UAC & the command prompt are running in a higher? level and are not affected by LV. If I do the same over a .txt I succesfuly have the intended text. Any ideas about this?
  2. Create a link to cmd.exe and change the properties to "Run as administrator", then use System Exec to call the link, expecting to open a command prompt with elevated rights, and then write the netsh commands either with DLLs (that might not work according to my first solution) or using the "standard input" of System Exec? (not sure if this is possible). But then System Exec gives me an error (code 2) when trying to execute the .lnk and so the command prompt does not open. The error code stands for "Error 2 occurred at an unidentified location. Possible reason(s): LabVIEW: Memory is full." Any idea about how to solve this?

The conclusion is that I'm stuck with two possible solutions none of them working.

 

  • Maybe there's a more simple solution that I didn't think about?
  • Solutions to these problems?
  • Any different proposals?

 

Thanks in advance for any tip or help!

 

Note: I titled the thread as Windows Vista, 7 & 8 since I assume all of them asks for admin rights when changing IPs. In case there are other people with the same problem, altough right now I'm only using XP & 7.

Alejandro Asensio, CLAD
0 Kudos
Message 1 of 7
(4,569 Views)

Any help would be appreciated Smiley Happy

 

Alejandro Asensio, CLAD
0 Kudos
Message 2 of 7
(4,507 Views)

First question: Why do you have to change OS properties via a custom application? I know such requirements can exist (sigh...) but nevertheless it seems a little bit weird to me 😉

OK, now an idea:

Run cmd.exe via task scheduler. Here is a how to do it: http://maximumpcguides.com/windows-vista/run-a-program-with-administrative-privileges-without-uac-pr...

 

0 Kudos
Message 3 of 7
(4,500 Views)

I just came across this discussion and tried to follow candidus' solution.  It did not work for me.  The command window would open and then immediately close. 

 

I have a similar program where I need to do this.  It is a program already written in LabVIEW which configures some hardware.  To configure some modules on this hardware, it needs to change the computer's IP address to match the default IP address on the hardware, and then communicates with the hardware to configure a new IP address on the hardware, before changing the computer's IP address back to my company's domain.  The UAC on my computer cannot be disabled to do this.  This program worked fine on Windows XP, but will not work on Windows 7 because UAC does not allow it.  The program needs to do this without any prompts for user names and/or passwords.  

 

Thanks for any help anyone can give me.

0 Kudos
Message 4 of 7
(3,772 Views)

Without any prompts is a bugger! If you can't disable UAC then the UAC dialog will pop up no matter what when trying to execute another program with elevated rights. Maybe there is a way to circumvent this with a shell script file (some sort of Visual Basic for the shell) but I'm not positive about that. It would mean that the username and password for the admin account are in an openly readable file on the computer, and that is exactly what UAC is trying to clamp down on.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 7
(3,744 Views)

Few suggestions:

 

1) Get an extra NIC card (e.g. PCI / USB) and have it permanently configured to be on the default IP address of the hardware

2) Instead of configuring the IP address on your PC, get a configurable switch that allows you to change the IP address assigned to you by communicating with the switch

3) It might be possible with a manifest file to specify that your application requires elevation before running - if you can get that to work then it should only prompt for UAC when you launch the program, not each time it tries to do an operation which requires elevation.

4) I heard of a program called 'RunAs' which is supposed to be able to bypass the UAC prompt by pre-supplying the credentials - it's been a while since I looked at it so I might have it mixed up with something else.

 

If none of those work, then you need to talk to your IT department about disabling UAC for you (our team has laptops with UAC disabled as we do LabVIEW development), or providing you with a special laptop or something that doesn't have UAC.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 6 of 7
(3,735 Views)

Thanks for the suggestions.  I think the idea with the extra NIC would work.  The "runas" idea does not work.  I tried several different ways of using it, but basically Windows has made it so that you cannot programmatically launch an administrator level command prompt (this is different than the command prompt you get even when logged in as an administrator).  The only command prompt you can launch programmatically is a command prompt with no elevated rights. 

 

Our solution looks like we are going to change our process such that we will use another process to change the IP address of the hardware to match the company subnet.  Then we can do all the other hardware configuration things we need with the existing LabVIEW program. 

 

Thanks again.

0 Kudos
Message 7 of 7
(3,699 Views)