Old 16th April 2018, 09:21   #1
Gluck
Member
 
Join Date: Apr 2018
Posts: 74
Write a line

I want write a line of text (in portable mode) in hosts file...

What have I to write?

Thanks
Gluck is offline   Reply With Quote
Old 16th April 2018, 12:45   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,811
Have you tried searching the forum and wiki?

Portable applications and host files don't really mix since you have to be elevated to change the hosts file and you don't really want to elevate the portable application.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 17th April 2018, 10:55   #3
Gluck
Member
 
Join Date: Apr 2018
Posts: 74
"... since you have to be elevated to change the hosts file and you don't really want to elevate the portable application".

What does it mean???

And If I want elevate the portable application?
Gluck is offline   Reply With Quote
Old 17th April 2018, 19:18   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,811
Quote:
Originally Posted by Gluck View Post

And If I want elevate the portable application?
You just asked about Drag & Drop in another thread and you cannot elevate if you want to be a drag target.

Elevation is not really relevant to the topic at hand, this is about text file manipulation.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 21st April 2018, 09:58   #5
Gluck
Member
 
Join Date: Apr 2018
Posts: 74
I have written:

WindowIcon Off
SilentInstall Silent
AutoCloseWindow True
OutFile Hosts.exe

Section

FileOpen $4 "$SYSDIR\drivers\etc\hosts" a
FileSeek $4 0 END
FileWrite $4 "$\r$\n" ; we write a new line
FileWrite $4 "144.144.144.144"
FileClose $4 ; and close the file

ExecWait "$EXEDIR\App\notepad.exe"

FileOpen $4 "$SYSDIR\drivers\etc\hosts" w
loop:
FileRead $0 $2
IfErrors done
StrCmp $2 "144.144.144$\r$\n" 0 +2
StrCpy $2 "$\r$\n"
FileWrite $1 $2
Goto loop

done:
FileClose $0

SectionEnd

The part BEFORE execution works fine (add a line at the end of hosts file).
The part AFTER execution delete hosts leaving the file empty...

What's wrong?

Thanks.

What is the elevation term? Is like acquire admin privileges for the program?
Gluck is offline   Reply With Quote
Old 21st April 2018, 11:00   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,811
Open with mode "w" deletes the old content.

Yes, elevation is UAC elevation to admin.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 21st April 2018, 13:22   #7
Gluck
Member
 
Join Date: Apr 2018
Posts: 74
I have used a, but the IP remain.
I have used r, and I have found the IP duplicate...

What have I to write?
The code is optimized?
Gluck is offline   Reply With Quote
Old 21st April 2018, 23:35   #8
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,811
Editing the file in-place is hard, try writing it to a new file instead.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 22nd April 2018, 21:43   #9
jooseng
Junior Member
 
Join Date: Apr 2018
Posts: 2
I use Portableapps.com launcher generator to make my portable app. This is what I do in my portable app.

To block a url:

CopyFiles /Silent "$SYSDIR\drivers\etc\hosts" "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable"
SetFileAttributes `$SYSDIR\drivers\etc\hosts` NORMAL
nsExec::Exec `"$SYSDIR\cmd.exe" /c @echo: >> "$SYSDIR\drivers\etc\hosts"`
nsExec::Exec `"$SYSDIR\cmd.exe" /c echo 127.0.0.1 www.xxxx.com >> "$SYSDIR\drivers\etc\hosts"`
SetFileAttributes `$SYSDIR\drivers\etc\hosts` READONLY

To restore the orginal hosts file:

${If} ${FileExists} "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable"
Delete "$SYSDIR\drivers\etc\hosts"
Rename "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable" "$SYSDIR\drivers\etc\hosts"
${EndIf}
jooseng is offline   Reply With Quote
Reply
Go Back   Winamp & SHOUTcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump