We help IT Professionals succeed at work.

Insert data from an application into text file

thesurvivor
thesurvivor asked
on
341 Views
Last Modified: 2010-04-17
I am trying to work around the fact that I do not have a static IP at home. I'm behind a firewall and to configure Morpheus correctly I need to put the IP my ISP assigns me in the settings. The problem is my address changes about every 5 minutes when I'm running Morpheus. I tried using DynDNS.org but Morpheus does not resolve the hostname to my IP, it has to have a literal address. On the good side, Morpheus keeps this setting in a file called MorphConfig.ini and I have software that checks my IP every 5 minutes and updates my DynDNS record.

Soooo,...

I want to insert my current IP address into the MorphConfig.ini file every 5 minutes. I can get my current IP from this other application or from a web page. How do I Insert data from this other application (or web page) into the .ini text file?

Below is the line where I want to update my IP
-------------------------------------------------------
[Local]
ForcedHost=0.0.0.0
Comment
Watch Question

Commented:
You don't mention a language, so I'll just outline it:

Set timer to fire every 5 minutes
In timer routine:
1) Get IP any number of ways = API calls, shell out to  ipconfig, import from other app, etc
2) open INI file
3) Create INIX file
4) Read from INI, copyingt lines to INIX until encountering ForcedHost=
5) Write out new (possibly identical) ForcedHost= line
6) Copy over the remaining lines from INI to INIX
7) Delete INI
8) Rename INIX to INI

Author

Commented:
VB would be good. I'm running windows 2000 so something I don't have install anything new for.

I appreciate the outline.

It has to import from the other app, IPconfig will only get the IP of my machine. I need the IP on the other side of my router. I do not know how to grab data from another application.

Commented:
For a timer, put a timer control on a form
In form load:
Timer1.interval=5*60*1000
Timer1.Enabled=true

In Timer1_Timer() event:
INI="fullpathtoINIfile"
INIX="fullpathofINIXfile"
' Get the IP however you import it from the other app
Kill INIX
Open INI For Input As 1
Open INIX For Output as 2
Do While Not EOF(1)
    Line Input #1, INILine
    ' Don't waste time checking for a change in IP, just blast out the current one
    if left(UCase(Trim(INILine)),10)="FORCEDHOST" then
       print #2,"ForcedHost=" + IPAddrStringFromOtherApp
    else
      print #2,INILine
       endif
    Loop
close(2)
close(1)
kill INI
filecopy inix,ini
kill inix




Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
It's more simple in VB.
Create a new form. Put a timer on it (Timer1).
Insert this code into the form:
-----------------------------------------------------------------------------------------------
' First declare two API functions - write/read to/from an ini file

Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" _
                         (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
                          ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
                         
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" _
                         (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
                          ByVal lpFileName As String) As Long

' Change the values of this three constants for the program to suit your needs
Const PathToINI As String = "c:\Morph\"           ' Where your INI file is...
Const MyForcedIP As String = "10.20.30.252"       ' What IP you want to have...
Const TimerInterval As Integer = 10000            ' Interval to check the INI file (in miliseconds 10000 = 10 seconds)

' You have to set the interval to a lesser value than the real change. You could also do some
' calculations. -> set the interval to 1 sec. Then, when the change occours set it to
' 4.8 minutes, then again to 1 sec., until the change appears, and again to 4.8 minutes...

Private Sub Form_Load()
  ' Let's check every minute
  Timer1.Interval = TimerInterval
  ' Start timer
  Timer1.Enabled = True
End Sub

Private Function INI_Read(mySection As String, myVariable As String, myFileName As String) As String
Dim strTemp As String
  ' Reading from an INI file
  strTemp = String(255, vbNullChar)
  INI_Read = Left(strRet, GetPrivateProfileString(mySection, ByVal myVariable, "", strTemp, Len(strTemp), myFileName))
End Function

Private Sub INI_Write(mySection As String, myVariable As String, myNewValue, myFileName As String)
Dim intTemp As Integer
  ' Writing to an INI file
  intTemp = WritePrivateProfileString(mySection, ByVal myVariable, CStr(myNewValue), myFileName)
End Sub

Private Sub Timer1_Timer()
Dim myIP As String
  ' Read from the INI
  myIP = INI_Read("Local", "ForcedHost", PathToINI & "MorphConfig.ini")
  ' Check if the value changed
  If MyForcedIP = myIP Then Exit Sub
  ' If it changed -> change it back
  INI_Write "Local", "ForcedHost", MyForcedIP, PathToINI & "MorphConfig.ini"
End Sub

-----------------------------------------------------------------------------------------------
And voila...

Commented:
I was wondering if this was still an issue, if so did you get a chance to try what I suggested... (or do you have any questions about it?)

Regards,

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.