Sign in to follow this  
Followers 0
Spatnonkl

RunWait not actually waiting?

22 posts in this topic

#1 ·  Posted (edited)

Hello,

my script uses a custom URL. I want my program to auto-register it, if it is not registered yet.

This needs admin rights, so i chose to build a seperate executable which requires admin. So, the main script doesn't need admin rights on every launch.

Func checkHandlerReg()
   $prot = StringTrimRight($protocolString,3)
   $reg = RegRead("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","")
   $handlerPath = $rootDir&"binURL_handler.exe"
   If Not StringInStr($reg,$handlerPath) Then
      RunWait($rootDir&"binregHandler.exe")
   EndIf
   Sleep(100)
   $reg = RegRead("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","")
   If Not StringInStr($reg,$handlerPath) Then
      MsgBox(4096,$PRODUCTNAME,"Error: Could not register URL-Handler!")
      Exit
   EndIf
EndFunc

My problem:

You might have noticed the Sleep command. If i omit it, the following RegRead returns an empty String and my error MsgBox appears.

The RegWrite which regHandler.exe executes, works every time, The following RegRead is executed too early.

If i replace the RunWait with:

#RequireAdmin
RegWrite("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","","REG_SZ",""""&$rootDir&"binURL_handler.exe"" ""%1""")

Then omitting Sleep() works flawlessly.

Any idea?

Edited by Spatnonkl

Share this post


Link to post
Share on other sites



I hope I understand what your question is -- if not let me know.

You are asking why do I need a Sleep in my script one way and not the other?

Windows always has a "delay write".

This could be anywhere from 10ms to 8000ms depending on interaction between programs and certain conditions.

I see nothing wrong with a Sleep where you have it and where it's needed.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I doubt that reading the Windows cache right after a write would give a distinct result from what was writen.

Another possibility is that the launched program actually invokes another executable (without waiting for completion).

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks for your answers. You got my question right.

The File "regHandler.exe" is also a compiled AutoIT Script.

This is the source code:

#RequireAdmin

#include "../lib/globalLib.au3"

Global $rootDir = StringTrimRight(@ScriptDir,4)

$prot = StringTrimRight($protocolString,3)
RegWrite("HKEY_CLASSES_ROOT"&$prot,"","REG_SZ","URL:"&$prot&" Protocol")
RegWrite("HKEY_CLASSES_ROOT"&$prot,"URL Protocol","REG_SZ","")
RegWrite("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","","REG_SZ",""""&$rootDir&"binURL_handler.exe"" ""%1""")

The problem is that i need to check if regHandler was executed correctly. I guess i could let regHandler return an exit-code?

Share this post


Link to post
Share on other sites

For this instance...I see nothing wrong with using ClipPut($ExitCode) and retrieving it with ClipGet().

There are other ways. Someone else might have a better solution.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Exit($returnCode) would do that.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I had forgotten that could be done with Exit. Thats definitely the way to go. Thanks jchd.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Okay. Now things get really weird.

regHandler should return 0 if any RegWrite fails and 1 if all RegWrite succeed.

Problem: checkHandlerReg() continues before UAC prompt is answered. So RunWait does not wait AGAIN. ExitCode is always 0.

#RequireAdmin seems to launch a new process.

http://www.autoitscript.com/autoit3/docs/keywords/RequireAdmin.htm

So regHandler is launched once without admin privileges, returns und RunWait returns too.

After that i answer the UAC prompt and a new process of regHandler is created with admin rights.

So, what i can do to wait for the right process?

regHandler.au3 Code:

#RequireAdmin

#include "../lib/globalLib.au3"

Global $rootDir = StringTrimRight(@ScriptDir,4)

$prot = StringTrimRight($protocolString,3)
$retCode = 1
$retCode = BitAND($retCode,RegWrite("HKEY_CLASSES_ROOT"&$prot,"","REG_SZ","URL:"&$prot&" Protocol"))
$retCode = BitAND($retCode,RegWrite("HKEY_CLASSES_ROOT"&$prot,"URL Protocol","REG_SZ",""))
$retCode = BitAND($retCode,RegWrite("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","","REG_SZ",""""&$rootDir&"binURL_handler.exe"" ""%1"""))
Exit $retCode

Func checkHandlerReg()
  $prot = StringTrimRight($protocolString,3)
  $reg = RegRead("HKEY_CLASSES_ROOT"&$prot&"shellopencommand","")
  $handlerPath = $rootDir&"binURL_handler.exe"
  If Not StringInStr($reg,$handlerPath) Then
    $returnID = RunWait($rootDir&"binregHandler.exe")
    If $returnID<>1 Then
      MsgBox(4096,$PRODUCTNAME,"Error: Could not register URL-Handler!")
      Exit
    EndIf
  EndIf
EndFunc
Edited by Spatnonkl

Share this post


Link to post
Share on other sites

Have you tried running it with RunAsWait? Because, as you said, using #RequireAdmin will close the first run, then open a new run and ask for Admin privileges. You can see this if you run it in SciTE using ConsoleWrites in a script, you won't see any of the writes because the first process has already closed.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Try compiling the reghandler with:

#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator

Requires the full version of SciTE4AutoIt3 installed.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

@BrewManHH:

I suppose you suggest running as user named "Administrator"? But this is not the same as using admin privileges. I want to run everything as the initial user.

@Jos:

Instead of #RequireAdmin or additional?

I use a make.bat which invokes "AutoIt3aut2exeaut2exe.exe"

Can't use that one?

//edit:

If I compile regHandler with Scite and the rest with aut2exe, the compiled regHandler.exe icon gets a little UAC (sub-)icon. Seems right. But now RunWait does not run regHandler.exe anymore (immediatly returns 0).

Edited by Spatnonkl

Share this post


Link to post
Share on other sites

You indeed still need to run AutoIt3Wrapper. Next version of aut2exe will likely support all these PE settings.

Are you running your base script also as admin with atleast #RequireAdmin?

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Changed my make.bat to use AutoIt3Wrapper.exe

RunWait still can't launch regHandler.exe

Are you running your base script also as admin with atleast #RequireAdmin?

No. Because that's exactly what i'm trying to avoid. Refer to first post.

Edited by Spatnonkl

Share this post


Link to post
Share on other sites

No. Because that's exactly what i'm trying to avoid. Refer to first post.

You could use ShellExecuteWait() but will get the UAC prompt.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

This seems to work for me. Thanks!

Why is #RequireAdmin not enough for the Compiler to know?

Share this post


Link to post
Share on other sites

According to MSDN if you're a standard user and the program you're trying to launch has requireAdministrator in its manifest, you can't run it unless you have certain settings set correctly, and even then might not be able to run the program at all.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

This seems to work for me. Thanks!

Why is #RequireAdmin not enough for the Compiler to know?

You mean for your compiled reghandler.exe ? AutoIt3 will internally re-shell the script thus creating a new process.

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Okay, thanks for the explanation. I would suggest to explain this behaviour of #RequireAdmin in AutoIT Help File.

Share this post


Link to post
Share on other sites

Okay, thanks for the explanation. I would suggest to explain this behaviour of #RequireAdmin in AutoIT Help File.

Doesn't it already by stating?:

As this function launch a new process, some functions as Consolewrite() cannot be captured (Scite will not display anything).

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0