Old 2nd December 2015, 06:13   #1
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Write in hosts

I use this:

RequestExecutionLevel admin
......


......


ExecWait "$INSTDIR\x.bat"



In x.bat i have

IF EXIST %windir%\SYSTEM32\DRIVERS\ETC\HOSTS*.* ATTRIB +A -H -R -S %windir%\SYSTEM32\DRIVERS\ETC\HOSTS*.* > NUL

echo 127.0.0.1 something.com >> %windir%\system32\drivers\etc\hosts


....


I tried on XP. work OK.

In Win7-10/64 work random.
Most more didn't work.

I tried run installer (in installer exist command RequestExecutionLevel admin). never ask for rights.
I tried too RunAsAdministrator (rightclik)..

If i try "Runn as administrator on Total commander, go to hosts, F4-edit, made the change, save, work correctly.

Where is the misjate?

Thank you
alibabavl64 is offline   Reply With Quote
Old 2nd December 2015, 12:38   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
There might be some 64-bit path issues here, try running Process Monitor from Sysinternals/Microsoft...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 2nd December 2015, 18:23   #3
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
What is the reason you are doing this from a separate BAT file rather than directly in NSIS?

http://nsis.sourceforge.net/Reading_...iting_in_files

http://nsis.sourceforge.net/Reference/SetFileAttributes

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 2nd December 2015, 20:05   #4
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Thank you, i'll try this.

But...what mean $4 from this commands?
(FileOpen $4...)
alibabavl64 is offline   Reply With Quote
Old 2nd December 2015, 20:16   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
Quote:
Originally Posted by alibabavl64 View Post
But...what mean $4 from this commands?
(FileOpen $4...)
$4 is the register (variable) where the file handle is stored.

You should read the entire documentation before asking basic questions like this.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 2nd December 2015, 20:41   #6
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
This command...
PHP Code:
FileOpen $"SomeFile.txt" 
...tries to open "SomeFile.txt", in append mode. And, if successful, it stores the file handle in user variable $4.

Use the IfErrors command in order to check whether file was opened successfully.

See here for details:
http://nsis.sourceforge.net/Docs/Chapter4.html#fileinst

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 2nd December 2015, 20:45   #7
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
@Anders
1. I never sed "i am guru". If i was a guru maybe didn't ask.
2. My english is not so good.
3. http://nsis.sourceforge.net/Docs/Chapter4.html#fileinst
here wrote:
"FileOpen

user_var(handle output) filename openmode

Opens a file named "filename", and sets the handle output variable with the handle. The openmode should be one of "r" (read) "w" (write, all contents of file are destroyed) or "a" (append, meaning opened for both read and write, contents preserved). In all open modes, the file pointer is placed at the beginning of the file. If the file cannot be opened, the handle output is set to empty, and the error flag is set."

I understud that handle is automaticaly set but not specify if is a problemm if i use

Fileopen $0
Fileopen $1
Fileopen $5
Fileopen $1000

That $x is chosen by me and must only be the same in a sequence to the same file, or must choose respect another rule?

@Lord:
Thank you
alibabavl64 is offline   Reply With Quote
Old 2nd December 2015, 20:48   #8
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
Quote:
Originally Posted by alibabavl64 View Post
I understud that handle is automaticaly set but not specify if is a problemm if i use

Fileopen $0
Fileopen $1
Fileopen $5
Fileopen $1000

That $x is chosen by me and must only be the same in a sequence to the same file, or must choose respect another rule?
$x is the name of the variable where the result is stored!

NSIS provides the variables $0 to $9 as well as $R0 to $R9 by default. There is no $1000, but you can define your own variables.

Read the manual:
http://nsis.sourceforge.net/Docs/Cha...html#variables

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 2nd December 2015, 20:50   #9
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Thank you, Lord Mulder.

Thank you again, work fine!
alibabavl64 is offline   Reply With Quote
Old 22nd February 2016, 20:06   #10
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
I understud that after write in "hosts" a command "ipconfig /flushdns" is mandatory.

Is possible directly or need something like "execdos" plugin?

Thank you.
alibabavl64 is offline   Reply With Quote
Old 22nd February 2016, 20:41   #11
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
Quote:
Originally Posted by alibabavl64 View Post
I understud that after write in "hosts" a command "ipconfig /flushdns" is mandatory.

Is possible directly or need something like "execdos" plugin?

Thank you.
Try nsExec, it is part of the default NSIS install...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 23rd February 2016, 07:58   #12
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
thank you
alibabavl64 is offline   Reply With Quote
Old 3rd November 2016, 12:39   #13
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Hy, i need your help again.

I need write few lines in .....\etc\hosts.
If hosts exist, write without problems.
But i has an Win10 HOME EDITION and me myself see in \etc\ the file named "hosts" not exist.

1. Please how can verify first if exist (is correct
SetOutPath "$Windir\system32\drivers\etc"
SetFileAttributes "hosts" 0
FileOpen $0 "hosts" a
If errors ???(how create a file named "hosts"?)

Or exist another command for find the file?
Because i found somewhere "FileOpen $0 "hosts" a" will create a new file named hosts but to me not happend..
Run as admin is activated and installer was run "Run As Administrator"..
2. If Home Edition of Windows haven't this "hosts" file, IF i create and populate with 127.0.0.1 www.myweb.xx, that page will be blocked too or that method don't work in "Home edition"? Another method?

Thank you
alibabavl64 is offline   Reply With Quote
Old 3rd November 2016, 13:22   #14
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
PHP Code:
!include LogicLib.nsh

Section
${If} ${FileExists"C:\something"
  
#...
${Else} 
  
#...
${EndIf}
SectionEnd 
but FileOpen should work, the a mode calls CreateFile with OPEN_ALWAYS

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 4th November 2016, 08:15   #15
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Quote:
Originally Posted by Anders View Post

but FileOpen should work, [...]
Yes, in W10 64 PRO work perfectly. But in that version, Home Edition, didn't work.
alibabavl64 is offline   Reply With Quote
Old 4th November 2016, 09:07   #16
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
Try running Process Monitor and see why FileOpen fails on these systems...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 4th November 2016, 16:34   #17
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Thank you, i'll try this.
alibabavl64 is offline   Reply With Quote
Old 24th November 2016, 12:26   #18
alibabavl64
Junior Member
 
Join Date: Apr 2013
Posts: 29
Need help again:

Let say i installed once my programm.
Uninstall and reinstall.
In this case in hosts will be twice the "locked" pages.

(i don't agree overwrite method, in hosts can be links used by another programs, i think adblock plus and few solutions for cryptolocker prevent use hosts).

I want i check if in hosts i wrote a line like "127.0.0.1 xyz.abc" and if exist, do nothing and if not exist, append.

An idea for checking?
Thank you
alibabavl64 is offline   Reply With Quote
Old 24th November 2016, 14:43   #19
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
You can look at Appendix E in the help file and see if you can use any of the helper functions. If not, there is always plain FileRead + StrCpy & StrCmp to do manual string parsing but NSIS is never going to be the best way to edit text files.

IntOp $PostCount $PostCount + 1
Anders 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