Morteza Posted December 10, 2008 Share Posted December 10, 2008 I wrote a program that create an icon on system tray with three options in right click menu. One of the options is that when I click on it, a Windows command is executed. This Windows command will work just under Administrator permission. In respect to that my program must be available for all of the system users (even limited permission users), so I define it to be ran as a Windows Service. Now, when I log-in as a limited user, my program is worked very well, without any problem. There is just a problem: As I said, my program have three options in its menus. One of the options is Exit. If my program be closed by one of the limited users, however it remain running as a service as previous, but when my limited user call my program icon again with double clicking on it and select the option, the Windows command will not be executed because permission limitation; He/She only must restart the system to the option work without any problem again. My question is how can I communicate with this running service to when my program put its icon in system tray after closing-again-running process, the option work with Admin permission just as when I have restarted the system completely? I hope my explanation be enough for explaining my problem. Best Regards Link to comment Share on other sites More sharing options...
rabbitkillrun Posted December 10, 2008 Share Posted December 10, 2008 never tried this, but noticed when looking through the FAQ i saw this...: (wouldn't quote properly...) Q5. How can I start/stop or otherwise control a service? A1. There are two include libraries that are designed specifically to interact with services. These are the following: ServiceControl.au3 made by SumTingWong. Functionality: _StartService() _StopService() _ServiceExists() _ServiceRunning() _CreateService() _DeleteService() _NTServices.au3 made by CatchFish. Functionality: _ServiceStart() _ServiceStop() _ServiceStatus() _ServicePause() the service stop thing looks useful... hope that helps... Link to comment Share on other sites More sharing options...
Morteza Posted December 10, 2008 Author Share Posted December 10, 2008 (edited) (...)the service stop thing looks useful...hope that helps... Thanks for your reply. I know that, but the problem is limited users cannot Stop and Start services!. So the GUI of my program cannot be activated by this way.Regards Edited December 11, 2008 by Morteza Link to comment Share on other sites More sharing options...
rabbitkillrun Posted December 13, 2008 Share Posted December 13, 2008 so you need a way of the GUI to work with the already running service, as you can't start another one...? Link to comment Share on other sites More sharing options...
Morteza Posted December 13, 2008 Author Share Posted December 13, 2008 so you need a way of the GUI to work with the already running service, as you can't start another one...?I think so. Link to comment Share on other sites More sharing options...
Mattraks Posted December 14, 2008 Share Posted December 14, 2008 (edited) #RequireAdmin ? or... If Not IsAdmin() Then If @Compiled Then RunAs(InputBox("Username", "Enter an admin username"), '', InputBox("Password", "Enter an admin password", "", "*"), 4, @ScriptFullPath) Else MsgBox(16, "Error", "It needs to be compiled") EndIf EndIf Edited December 14, 2008 by Mattraks Link to comment Share on other sites More sharing options...
Morteza Posted December 14, 2008 Author Share Posted December 14, 2008 #RequireAdmin ? or... If Not IsAdmin() Then If @Compiled Then RunAs(InputBox("Username", "Enter an admin username"), '', InputBox("Password", "Enter an admin password", "", "*"), 4, @ScriptFullPath) Else MsgBox(16, "Error", "It needs to be compiled") EndIf EndIfAsking password is a good idea, but not useful for my need, because I wouldn't like to give my Admin password to my limited users. Of course I can include my admin password in my code, but it is not doable too, because my program must be executed on different computers and different computers have not the same password. The best work is that when a service is running as Admin permission, my GUI can communicate with it under limited users environments without asking any password. I know this is possible, because some commercial programs can does it. A very good example of these programs is USB Safely Remove. This program must be installed as Admin (because scanning of USB ports need admin permission and Windows does not let limited users to scan USB ports by command-line or any other code). After installation, all users, even limited users, can use it without asking any password, even if they close and open the GUI of this program frequently. Best Regards Link to comment Share on other sites More sharing options...
Mattraks Posted December 14, 2008 Share Posted December 14, 2008 Then make two scripts, one that is to be run in the limited user and another to be run as a service. The service one will listen for connections and then the limited one will connect to it and then they can communicate and the limited one can tell the service to do certain things that the limited doesn't have permissions to do. Link to comment Share on other sites More sharing options...
Morteza Posted December 14, 2008 Author Share Posted December 14, 2008 (edited) Then make two scripts, one that is to be run in the limited user and another to be run as a service. The service one will listen for connections and then the limited one will connect to it and then they can communicate and the limited one can tell the service to do certain things that the limited doesn't have permissions to do.But how? Would you like show me an example? Even a simple practical example will be appreciated.Thanks beforehand. Edited December 14, 2008 by Morteza Link to comment Share on other sites More sharing options...
Morteza Posted December 14, 2008 Author Share Posted December 14, 2008 (edited) Thank you very much dear Mattraks, your suggestion help me very much.I think about your suggestion and I found a very simple solution way for that. The follow is a sample code in my solution. I hope be useful for feature needers. The code was tested and worked without any problem.The code of Service part:#NoTrayIcon $AutoItTitle = "Test";The GUI internal name (it is because Service recognize if GUI is running or not) While 1 HotKeySet("^r","_Main") If WinExists($AutoItTitle) Then $s="true" Else $s="false" EndIf WEnd Func _Main() if $s="true" Then MsgBox(0,"","Service was called by GUI") EndIf EndFuncThe code of GUI part:Opt("TrayMenuMode",1) ; Check if the GUI is running previously, $AutoItTitle = "Test" If WinExists($AutoItTitle) Then MsgBox(64,"Note","'Test' is running previously!!") Exit;Didn't run the program again EndIf AutoItWinSetTitle($AutoItTitle);Set an internal name for recognizing the GUI by the Service ; Menu defining $RunCommand = TrayCreateItem("Run my command" & @TAB & "Ctrl+R") TrayCreateItem("") $exititem = TrayCreateItem("Exit") TraySetState() TraySetClick(16) While 1 $msg = TrayGetMsg() Select Case $msg = $RunCommand Send("^r") Case $msg = $exititem ExitLoop EndSelect WEnd exit(0)Note: Service must be ran with checked "Allow service to interact with desktop"Any other better solution way will be welcomed.Best Regards Edited December 15, 2008 by Morteza Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now