找回密码
 加入
搜索
查看: 10120|回复: 21

[系统综合] 如何判断程序是随系统启动还是用户运行

 火.. [复制链接]
发表于 2010-11-21 22:18:05 | 显示全部楼层 |阅读模式
本帖最后由 mgj8721 于 2012-9-13 22:28 编辑

各位高手,请问如何判断程序是随系统启动还是用户运行!谢谢...
发表于 2010-11-21 22:53:48 | 显示全部楼层
帮助文档里有这么一段

命令行参数
当我们传递命令行参数到 AutoIt 脚本中的时候, AutoIt 会初始化一个特殊的数组 $CmdLine :

$CmdLine[0] --- 参数的个数
$CmdLine[1] --- 第一个参数 (在脚本名称后面)
$CmdLine[2] --- 第二个参数
...
$CmdLine[$CmdLine[0]] --- 某个方法获取的最后一个参数...



值得注意的是不能将脚本名作为参数类; 要获取脚本名, 请使用 @ScriptName 代替.

参数中如果含有空格,必须使用 "双引号"把内容围住. 编译 脚本同样也能接受命令行参数.



如果你的脚本是这样运行的:

    AutoIt3.exe myscript.au3 参数1 "这 是 另一个 参数"

$CmdLine[0] --- 等于 2, 命令行有两个参数

$CmdLine[1] --- 等于第一个参数: "参数1"

$CmdLine[2] --- 等于第二个参数: "这 是 另一个 参数"

@ScriptName --- 等于脚本名称: myscript.au3



除 $CmdLine 之外还有一个名为 $CmdLineRaw 的变量, 包含未被拆分的完整命令行.

因此,对于上面的例子:

$CmdLineRaw --- 等于: myscript.au3 参数1 "这 是 另一个 参数"



如果脚本被编译, 并这样运行:

    myscript.exe 参数1 "这 是 另一个 参数"

$CmdLineRaw 等于: 参数1 "这 是 另一个 参数"

注意 $CmdLineRaw 会立即返回参数.



注意 : 只有 63 个参数会被 $CmdLine[...] 返回. 但 $CmdLineRaw 总是返回完整的命令行.



AutoIt 的特定命令行开关
格式 1: AutoIt3.exe [/ErrorStdOut] [/AutoIt3ExecuteScript] 文件 [参数 ...]
                执行 AutoIt3 脚本文件


/ErrorStdOut    允许致命错误重定向到 StdOut, 可以被 Scite 编辑器捕捉. 此开关可用于编译的脚本.


要执行一个标准的 AutoIt 脚本文件 'myscript.au3',使用命令:
'AutoIt3.exe myscript.au3'



格式 2: Compiled.exe [/ErrorStdOut] [参数 ...]
               执行 Aut2Exe 编译的 AutoIt3 脚本文件.

格式 3: Compiled.exe [/ErrorStdOut] [/AutoIt3ExecuteScript file] [参数 ...]
               执行另一个已经编译的 AutoIt3 脚本文件. 您不需要使用 fileinstall 释放一个 AutoIT3.exe 的拷贝


格式 4: AutoIt3.exe [/ErrorStdOut] /AutoIt3ExecuteLine "脚本代码"
                执行一行代码.

要执行一行代码, 使用命令:
Run(@AutoItExe & ' /AutoIt3ExecuteLine  "MsgBox(0, ''你好,世界!'', ''嘿!'')"')

当使用 /AutoIt3ExecuteLine 时,则不显示托盘图标.

注意: 正确的单, 双引号的使用是很重要的, 切记!.  

当使用格式 3 与格式 4 时, 可能会让使用 #NoAutoIt3Execute 编辑的脚本失效.
发表于 2010-11-21 22:55:59 | 显示全部楼层
正好我最近写了个工具,源码给你看看吧

#NoTrayIcon
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=E:\Program Files\ChinaNetSn\res\ico\large1.ico
#AutoIt3Wrapper_outfile=F:\开机自动登入工具.exe
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=由玄亟天制作
#AutoIt3Wrapper_Res_Fileversion=2.5.0.0
#AutoIt3Wrapper_Res_LegalCopyright=版权所有@玄亟天
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Dim $exepath,$username,$password,$hint_text,$exe_name

$exepath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","exepath");路径
$username = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","username");账号
$password = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","password");密码
$exe_name = StringMid($exepath,StringInStr($exepath,"\",1,-1)+1)
$workpath = StringLeft($exepath,StringInStr($exepath,"\",1,-1))

If $CmdLine[0] > 0 Then
        If $CmdLine[1]="-run" Then _Run($exepath,$username,$password,$exe_name,$workpath)
Else
        #Region ### START Koda GUI section ### Form=e:\program files\autoit3\我的脚本\闪讯账号管理系统窗口.kxf
        $main = GUICreate("闪讯开机自动登入工具 by 玄亟天", 410, 242, -1, -1,-1,$WS_EX_ACCEPTFILES)
        GUISetOnEvent($GUI_EVENT_CLOSE, "main")
        GUICtrlCreateGroup("闪讯所在路径:", 8, 8, 393, 65)
        $exepath_input = GUICtrlCreateInput("", 16, 32, 321, 21)
        GUICtrlSetState(-1,$GUI_DROPACCEPTED)
        $browse_button = GUICtrlCreateButton("浏览", 344, 30, 49, 25)
        GUICtrlSetOnEvent($browse_button, "main")
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $apply_button = GUICtrlCreateButton("应用设置", 304, 90, 81, 33)
        GUICtrlSetOnEvent($apply_button, "main")
        $delete_button = GUICtrlCreateButton("清除信息", 304, 135, 81, 33)
        GUICtrlSetOnEvent($delete_button, "main")
        $exit_button = GUICtrlCreateButton("退出", 304, 180, 81, 33)
        GUICtrlSetOnEvent($exit_button, "main")
        GUICtrlCreateLabel("账号:", 16, 88, 40, 17)
        GUICtrlCreateLabel("密码:", 16, 120, 40, 17)
        $username_input = GUICtrlCreateInput("", 56, 86, 217, 21)
        $password_input = GUICtrlCreateInput("", 56, 118, 217, 21)
        GUICtrlCreateGroup("提示", 8, 150, 280, 80)
        $hint = GUICtrlCreateLabel("", 24, 165, 250, 50)
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        GUISetState(@SW_SHOW)
        #EndRegion ### END Koda GUI section ###

        GUICtrlSetData($exepath_input,$exepath)
        GUICtrlSetData($username_input,$username)
        GUICtrlSetData($password_input,$password)

        If $exepath="" Or FileExists($exepath)=0 Then
                $tmpexepath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\闪讯","DisplayIcon")
                If $tmpexepath="" Then
                        $hint_text &= "没有找到闪讯程序或指定的路径无效,可能程序并未正确安装,请按“浏览”手动指定程序路径!"&@CRLF&@CRLF
                Else
                        GUICtrlSetData($exepath_input,$tmpexepath)
                EndIf
        EndIf
        ;E:\Program Files\ChinaNetSn\bin\NetKeeper.exe

        If $username="" Or $password="" Then $hint_text &= "未定义用户名或密码,可能是由于第一次运行此程序或系统经过了重装,请重新指定!"&@CRLF&@CRLF

        GUICtrlSetData($hint,$hint_text)
        $hint_text=""
        
        While 1
                Sleep(100)
        WEnd        
EndIf

Func main()
        Switch @GUI_CtrlId
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $exit_button
                        Exit
                Case $browse_button
                        $Fpath = FileOpenDialog("选择闪讯所在","C:\","EXE文件(*.exe)",1+2,"NetKeeper.exe",$main)
                        If Not @error Then GUICtrlSetData($exepath_input,$Fpath)
                Case $delete_button
                        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","autosignin")
                        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具")
                        GUICtrlSetData($exepath_input,"")
                        GUICtrlSetData($username_input,"")
                        GUICtrlSetData($password_input,"")
                        GUICtrlSetData($hint,"注册信息卸载成功,同时程序将不在开机时启动!")
                Case $apply_button
                        $exepath_in = GUICtrlRead($exepath_input)
                        $username_in = GUICtrlRead($username_input)
                        $password_in = GUICtrlRead($password_input)
                        If $exepath_in="" Or $username_in="" Or $password_in="" Then
                                GUICtrlSetData($hint,"路径或账户或密码为空,应用失败!")
                        Else
                                $Autostart = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","autosignin");开机启动
                                If $Autostart="" Or $Autostart<>@AutoItExe Then RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","autosignin","REG_SZ",@AutoItExe&" -run")
                                RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","exepath","REG_SZ",$exepath_in)
                                RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","username","REG_SZ",$username_in)
                                RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具\闪讯","password","REG_SZ",$password_in)
                                GUICtrlSetData($hint,"应用成功!同时成功写入启动项,程序将在系统启动时自动启动闪讯并登入!"&@CRLF&"注意:请在移动此工具后重新运行此工具并点击应用以便更新工具的开机启动路径!")
                        EndIf
        EndSwitch
EndFunc


Func _Run($exepath,$username,$password,$exe_name,$workpath)
        Global $wintitle = "[TITLE:闪讯; CLASS:#32770]",$Buttontitle = "[ID:1010; TEXT:连接; CLASS:Button]"
        Global $Edittitle = "[ID:1001; CLASS:Edit]",$Edit2title = "[ID:1009; CLASS:Edit]"
        Global $Button2title = "[ID:1013; TEXT:记住密码; CLASS:Button]",$Button3title = "[ID:1037; TEXT:开机时自动运行; CLASS:Button]"
        Global $statusbar = "[ID:1019; CLASS:msctls_statusbar32]"
        Global $on_off=0
        While 1
                If ProcessExists($exe_name)=0 Then
                        Run($exepath,$workpath)
                        If @error Then
                                $msg = MsgBox(4,"警告!","未发现闪讯,可能已被卸载或路径被改变,选择“否”重新手动运行此工具进行设置,选择“是”卸载此工具的注册表信息,这将导致此工具不在开机时启动")
                                If $msg=6 Then
                                        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","autosignin")
                                        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\开机自动登入工具")
                                        MsgBox(0,"","注册信息卸载成功!")
                                        Exit
                                Else
                                        Run(@AutoItExe)
                                EndIf
                        EndIf
                        WinWait($wintitle)
                        Do
                                Sleep(100)
                        Until ControlCommand($wintitle,"",$Buttontitle,"IsEnabled", "")=1
                ElseIf ProcessExists($exe_name)<>0 And Ping("www.baidu.com")=0 And $on_off=1 Then
                        ProcessClose($exe_name)
                        $on_off=0
                EndIf
                Sleep(300)
                If ControlCommand($wintitle,"",$Buttontitle,"IsVisible","")=1 And ControlCommand($wintitle,"",$Buttontitle,"IsEnabled", "")=1 Then
                        If ControlGetText($wintitle,"",$Edittitle)<>$username Or ControlGetText($wintitle,"",$Edit2title)<>$password Then
                                ControlSetText($wintitle,"",$Edittitle,$username)
                                ControlSetText($wintitle,"",$Edit2title,$password)
                        EndIf;判断用户名与密码,两者之一若不符则输入定义的用户名和密码
                        If ControlCommand($wintitle,"",$Button2title,"IsChecked","")=0 Then ControlCommand($wintitle,"",$Button2title,"Check","");判断记住密码勾选
                        If ControlCommand($wintitle,"",$Button3title,"IsChecked","")=1 Then ControlCommand($wintitle,"",$Button3title,"UnCheck","");判断开机时自动运行勾选
                        ControlClick($wintitle,"",$Buttontitle)
                        Sleep(300)
                        If ControlCommand($wintitle,"",$Buttontitle,"IsEnabled", "")=1 Then
                                ContinueLoop
                        Else
                                Do
                                        Sleep(100)
                                Until ControlCommand($wintitle,"",$statusbar,"IsVisible","")=1
                        EndIf
                        $on_off=1
                EndIf
                Sleep(1000)
                If ControlCommand($wintitle,"",$statusbar,"IsVisible","")=1 And BitAND(WinGetState($wintitle),16)<>16 Then WinSetState($wintitle,"",@SW_MINIMIZE)
                If BitAND(WinGetState($wintitle),16)=16 And Ping("www.baidu.com")>0 Then Exit
        WEnd
EndFunc

;Ping("www.baidu.com")=0
发表于 2010-11-21 22:58:20 | 显示全部楼层
我觉得用户启动程序和随系统启动的程序没有什么分别的吧
但如果在启动目录或注册表启动项里的程序可能是随系统启动的,不在的一定是用户启动的
发表于 2010-11-21 22:58:59 | 显示全部楼层
如果是要在程序运行后再进行判断的话。。。。不能的吧。。。。。不管是系统启动的,还是用户运行的,在内存中都是一样的程序。。。。
发表于 2010-11-22 03:41:23 | 显示全部楼层
获取进程的用户?
发表于 2010-11-22 13:51:33 | 显示全部楼层
取系统启动时间,取进程已运行时间
对比
发表于 2010-11-22 15:48:11 | 显示全部楼层
不愧是超版,话说这时间怎么获取呀
发表于 2010-11-22 16:50:16 | 显示全部楼层
注册表的run里面能带参数的话就带个参数去,系统启动启动的时候判断参数就是系统启动,手动启动的话就没用参数
发表于 2010-11-22 16:55:41 | 显示全部楼层
本帖最后由 netegg 于 2010-11-22 16:57 编辑

回复 9# gudam
太局限了,服务的程序多数都是随系统启动的,没在run里,当然,还有winlogon一类的内核程序
发表于 2010-11-23 09:18:32 | 显示全部楼层
回复 8# 飘云

偶帮你  百了下,看答案点之入门     在代码里找一找自个需要的部分
发表于 2010-11-23 14:26:46 | 显示全部楼层
回复 11# lchl0588


    多谢多谢,好东西,学习了
发表于 2010-11-23 15:32:29 | 显示全部楼层
个人认为这是没有办法区分的
发表于 2010-11-23 19:25:51 | 显示全部楼层
回复 13# h20040606

谁说的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2010-11-23 19:28:31 | 显示全部楼层
看来只能从程序与系统的启动时间上来对比区分了,没别的办法,用户名肯定是没用的,注册表也没用来着,比如服务项就不算在注册表里。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-20 06:01 , Processed in 0.085840 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表