Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 08/29/2013 in all areas

  1. FireFox

    IP Camera stream + record

    I recently bought an IP Camera to monitor my house. There is a web access to view the video stream but not to record it. Here is the IP Camera in question, it should work with every other similar product. So I made a simple example which does the job. Here is the code : #include <GUIConstants.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <WinAPI.au3> #include <GDIPlus.au3> #include <Memory.au3> #region Global Vars Global Const $sProgramTitle = "IP Camera stream + record" ;EDIT THE FOLLOWING LINE ONLY: Global Const $iIPAddress = "192.168.1.99", $iPort = 99, $shtauth = "yourauth==" Global Const $STM_SETIMAGE = 0x0172 Global $blRecording = False, $blGUIMinimized = False Global Const $sRecordDir = @ScriptDir & "\ip_camera_stream" Global $bRecvtmp = Binary(""), $bStream = $bRecvtmp Global $iImgLen = 0, $iStreamLen = 0, $iWritten = 0, $iEOH = 0, $iContLenPos = 0, $hImgFile = 0, $pBuffer = 0, $iImgCount = 0 Global Const $iContLengthLen = StringLen("Content-Length: ") Global $sStream = "", $sTrim2ContLen = "" Global $hBMP = 0, $hGraphics = 0, $hHBITMAP2 = 0, $hFamily = 0, $hFont = 0, $tLayout = "", $hFormat = 0, $hBrush = 0 #endregion Global Vars TCPStartup() Global $iSocket = TCPConnect($iIPAddress, $iPort) If @error Then MsgBox(16, $sProgramTitle, "Could not connect !") Exit -1 EndIf TCPSend($iSocket, _ "GET /videostream.cgi HTTP/1.1" & @CRLF & _ "Host: " & $iIPAddress & ":" & $iPort & @CRLF & _ "Connection: keep-alive" & @CRLF & _ "Authorization: Basic " & $shtauth & @CRLF & @CRLF) #region GUI Global $hGUI = 0, $pPic = 0, $hPic = 0, $btnRecord = 0 $hGUI = GUICreate($sProgramTitle, 640, 525) $pPic = GUICtrlCreatePic("", 0, 0, 640, 480, $SS_BITMAP) GUICtrlSetState($pPic, $GUI_DISABLE) $hPic = GUICtrlGetHandle($pPic) $btnRecord = GUICtrlCreateButton("Record", 10, 490, 80, 26) GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND") GUISetState(@SW_SHOW, $hGUI) #endregion GUI _GDIPlus_Startup() $hFamily = _GDIPlus_FontFamilyCreate("Arial") $hFont = _GDIPlus_FontCreate($hFamily, 17) $tLayout = _GDIPlus_RectFCreate(10, 10, 100, 40) $hFormat = _GDIPlus_StringFormatCreate() $hBrush = _GDIPlus_BrushCreateSolid(0xAFFF0000) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $btnRecord If $blRecording Then GUICtrlSetData($btnRecord, "Record") Else If Not FileExists($sRecordDir) Then DirCreate($sRecordDir) GUICtrlSetData($btnRecord, "Stop recording") EndIf $blRecording = Not $blRecording EndSwitch $bRecvtmp = TCPRecv($iSocket, 4096, 1) ;4kb If @error Then ExitLoop If Not BinaryLen($bRecvtmp) Then ContinueLoop $bStream &= $bRecvtmp If $iImgLen = 0 Then $sStream = BinaryToString($bStream) $iContLenPos = StringInStr($sStream, "Content-Length: ", 2) $iEOH = StringInStr($sStream, @CRLF & @CRLF, 2, 1, $iContLenPos) If $iEOH = 0 Or $iContLenPos = 0 Then ContinueLoop $sTrim2ContLen = StringTrimLeft($sStream, $iContLenPos + $iContLengthLen - 1) $iImgLen = Number(StringLeft($sTrim2ContLen, StringInStr($sTrim2ContLen, @CR, 2) - 1)) $bStream = BinaryMid($bStream, $iEOH + 4) EndIf If $iImgLen = 0 Then ContinueLoop $iStreamLen = BinaryLen($bStream) If $iStreamLen < $iImgLen Then ContinueLoop If Not $blGUIMinimized Then $hBMP = Load_BMP_From_Mem($bStream) If $blRecording Then $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBMP) _GDIPlus_GraphicsDrawStringEx($hGraphics, "[•REC]", $hFont, $tLayout, $hFormat, $hBrush) EndIf $hHBITMAP2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP) _WinAPI_DeleteObject(_SendMessage($hPic, $STM_SETIMAGE, 0, $hHBITMAP2)) _GDIPlus_ImageDispose($hBMP) If $blRecording Then _GDIPlus_GraphicsDispose($hGraphics) _WinAPI_DeleteObject($hHBITMAP2) EndIf If $blRecording Then $pBuffer = DllStructCreate("byte[" & $iImgLen & "]") If $iStreamLen > $iImgLen Then DllStructSetData($pBuffer, 1, BinaryMid($bStream, 1, $iImgLen)) $bStream = BinaryMid($bStream, $iImgLen) Else DllStructSetData($pBuffer, 1, $bStream) $bStream = Binary("") EndIf $hImgFile = _WinAPI_CreateFile($sRecordDir & "\snap_" & StringFormat("%.4d", $iImgCount) & ".jpg", 3, 4, 4) _WinAPI_WriteFile($hImgFile, DllStructGetPtr($pBuffer), $iImgLen, $iWritten) _WinAPI_CloseHandle($hImgFile) $iImgCount += 1 EndIf $iImgLen = 0 WEnd _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BrushDispose($hBrush) _GDIPlus_Shutdown() TCPCloseSocket($iSocket) TCPShutdown() Func WM_SYSCOMMAND($hWnd, $iMsg, $wParam, $lParam) Local Const $SC_MINIMIZE = 0xF020, $SC_RESTORE = 0xF120 Switch BitAND($wParam, 0xFFF0) Case $SC_MINIMIZE, $SC_RESTORE $blGUIMinimized = Not $blGUIMinimized EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_SYSCOMMAND Func Load_BMP_From_Mem($bImage) ;_GDIPlus_BitmapCreateFromMemory2 ;Author: UEZ ;Modified: ProgAndy, Yashied, FireFox If Not IsBinary($bImage) Then Return 0 Local $memBitmap = Binary($bImage) Local $iLen = BinaryLen($memBitmap) Local $GMEM_MOVEABLE = 0x0002 Local $aResult = DllCall("kernel32.dll", "handle", "GlobalAlloc", "uint", $GMEM_MOVEABLE, "ulong_ptr", $iLen) Local $hData = $aResult[0] $aResult = DllCall("kernel32.dll", "ptr", "GlobalLock", "handle", $hData) If @error Then Return 0 Local $tMem = DllStructCreate("byte[" & $iLen & "]", $aResult[0]) DllStructSetData($tMem, 1, $memBitmap) DllCall("kernel32.dll", "bool", "GlobalUnlock", "handle", $hData) If @error Then Return 0 $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hData, "int", True, "ptr*", 0) $hStream = $aResult[3] If @error Then Return 0 $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) If @error Then Return 0 DllCall('oleaut32.dll', 'long', 'DispCallFunc', 'ptr', $hStream, 'ulong_ptr', 8 * (1 + @AutoItX64), 'uint', 4, 'ushort', 23, 'uint', 0, 'ptr', 0, 'ptr', 0, 'str', '') Return $aResult[2] EndFunc ;==>Load_BMP_From_Mem _ Advanced example : Preview : Attachment : IP Camera.au3 IP Camera.au3 (Previous: 34 downloads) Enjoy, I'm watching U !
    1 point
  2. It's not because the window is not visible that this one does not exists. Local $aWl = WinList("[REGEXPTITLE:Hello All - (.*?)]") For $i = 1 To $aWl[0][0] If BitAND(WinGetState($aWl[$i][1]), 2) = 0 Then ContinueLoop WinClose($aWl[$i][1]) ExitLoop Next Br, FireFox.
    1 point
  3. Give this a whirl. The code depends on the XREF spreadsheet format not changing (columns and rows). You can add to each column without harm. The email piece is your thing... #include <Excel.au3> #include <array.au3> ;teams / countries XRFE TBL ; [0] = team name ; [1] = boolean to indicate that an email was sent for this team ; [2]...[n] = country codes local $aXREF_TBL[4][100] Local $sXREFPath = @ScriptDir & '\destinationbreakdown.xlsx' Local $oExcel = _ExcelBookOpen($sXREFPath,0) if $oExcel = 0 then ConsoleWrite('Error opening ' & @ScriptDir & '\destinationbreakdown.xlsx' & @LF) Exit endif Local $sXREFSize = $oExcel.Application.Selection.SpecialCells($xlCellTypeLastCell).Address(True, True, $xlR1C1) $AXREFSize = StringRegExp($sXREFSize, "\A[^0-9]*(\d+)[^0-9]*(\d+)\Z", 3) Local $XREFRow = $aXREFSize[0] Local $XREFCol = $aXREFSize[1] ConsoleWrite('Processing ' & $XREFRow & ' rows and ' & $XREFCol & ' columns' & @LF) $aXREF_TBL[0][0] = $oExcel.Activesheet.Cells(3,2).Value ; team name $aXREF_TBL[1][0] = $oExcel.Activesheet.Cells(3,4).Value ; team name $aXREF_TBL[2][0] = $oExcel.Activesheet.Cells(3,6).Value ; team name $aXREF_TBL[3][0] = $oExcel.Activesheet.Cells(3,8).Value ; team name $aXREF_TBL[0][1] = 0 $aXREF_TBL[1][1] = 0 $aXREF_TBL[2][1] = 0 $aXREF_TBL[3][1] = 0 ; populate South Pacific for $1 = 4 to $XREFRow if $oExcel.Activesheet.Cells($1,1).Value = '' then exitloop $aXREF_TBL[0][$1-2] = $oExcel.Activesheet.Cells($1,1).Value next ; populate Asia for $1 = 4 to $XREFRow if $oExcel.Activesheet.Cells($1,3).Value = '' then exitloop $aXREF_TBL[1][$1-2] = $oExcel.Activesheet.Cells($1,3).Value next ; populate Europe / Africa for $1 = 4 to $XREFRow if $oExcel.Activesheet.Cells($1,5).Value = '' then exitloop $aXREF_TBL[2][$1-2] = $oExcel.Activesheet.Cells($1,5).Value next ; populate Australia for $1 = 4 to $XREFRow if $oExcel.Activesheet.Cells($1,7).Value = '' then exitloop $aXREF_TBL[3][$1-2] = $oExcel.Activesheet.Cells($1,7).Value next _ExcelBookClose($oExcel, 0) Local $sFilePath1 = @ScriptDir & "\p_ebefffrm.xlsx" Local $oExcel = _ExcelBookOpen($sFilePath1,0) switch @error case 1 MsgBox(0, "Error!", "Unable to Create the Excel Object") Exit case 2 MsgBox(0, "Error!", "File does not exist - Shame on you!") Exit endswitch ; get # of rows and columns Local $sLastCell = $oExcel.Application.Selection.SpecialCells($xlCellTypeLastCell).Address(True, True, $xlR1C1) $sLastCell = StringRegExp($sLastCell, "\A[^0-9]*(\d+)[^0-9]*(\d+)\Z", 3) Local $iLastRow = $sLastCell[0] Local $iLastColumn = $sLastCell[1] ; define country results variable local $sMList ; loop through visible entries and add an entry to the country results variable if it does not already exist for $1 = 2 to $iLastRow if $oExcel.Sheets("Todays Data").Rows($1).Hidden = True then Else ; create string of unique country codes if not stringinstr($sMList, $oExcel.Activesheet.Cells($1,1).Value & ',') then $sMList &= $oExcel.Activesheet.Cells($1,1).Value & ',' EndIf next ConsoleWrite(@LF) ConsoleWrite('! Processing entries for countries : ' & stringtrimright($sMList,1) & @LF) ConsoleWrite(@LF) ; create array to search against $aXREF_TBL $aMList = stringsplit(stringtrimright($sMList,1),',',2) ; search XREF TBL and send email to clients for $1 = 0 to ubound($aMList) - 1 for $2 = 0 to ubound($aXREF_TBL) - 1 for $3 = 2 to ubound($aXREF_TBL,2) - 1 if $aXREF_TBL[$2][$3] = $aMList[$1] and $aXREF_TBL[$2][1] = 0 then _send_email($aXREF_TBL[$2][0]) $aXREF_TBL[$2][1] = 1 EndIf Next Next next ConsoleWrite(@LF) _ExcelBookClose($oExcel, 0) func _send_email($team) ConsoleWrite('!' & @tab & 'Sending Email to team name = ' & $team & @LF) endfunc kylomas
    1 point
  4. Well, I like calling java functions directly, not activating their "onchange" or "onclick" function, IF I CAN. And you can do that on your page: #include <IE.au3> $the_URL = 'http://catalog.1001z.ru/bmw/#c3Q9PTQwfHxzdHM9PXsiMTAiOiJcdTA0MWNcdTA0MzBcdTA0NDBcdTA0M2FcdTA0MzAiLCIyMCI6IkJNVyIsIjMwIjoiMSBFODIiLCI0MCI6Ilx1MDQzYVx1MDQ0M1x1MDQzZlx1MDQzNSAxMThkIn18fGJtdD09Qk1XfHxicD09UHx8ZnY9PVZUfHxiYj09RTgyfHxtb2RlbD09MTE4ZHx8c21vZGVsPT1Db3V8fGVtb2RlbD090LrRg9C%2F0LV8fGljbz09MTY1NzI3' $oIE = _IECreate($the_URL) Sleep(2000);time given for JAVA to load some page elements, otherwise, you will get NO "select" elements _ChangeSelect('01', 2);select 2nd element in 1st "select" control Sleep(1000);time given for java to react and execute the function _ChangeSelect('02', 1);select 1st element in 2nd "select" control Sleep(1000);time given for java to react and execute the function _ChangeSelect('03', 1);select 1st element in 3rd "select" control Sleep(1000);time given for java to react and execute the function _ChangeSelect('04', 3);select 3rd element in 4th "select" control Sleep(1000);time given for java to react and execute the function Func _ChangeSelect($SelName, $OptIndex) Local $oSelects = _IETagNameGetCollection($oIE, 'select') For $oSelect in $oSelects If $oSelect.name == $SelName Then Local $oOptions = $oSelect.options Local $Counter = -1 For $oOption in $oOptions $Counter += 1 If $Counter = $OptIndex Then $oIE.document.parentWindow.execScript('HM.set("' & $oOption.value & '");') Return EndIf Next Return EndIf Next EndFunc MsgBox(0, 'DONE!', 'Press OK to close the script') _IEQuit($oIE) Because JAVA is changing htmlsource of the $oIE, then you must call _IETagNameGetCollection($oIE, 'select') after each changed element, like it's shown in the script above.
    1 point
  5. new version of this player with some enhancements like click and run #include <File.au3> #include <GuiListView.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> If FileExists("Lista.ini") Then ;if the list exists is displayed $lista = IniRead("Lista.ini", "Lista" , "Caminho","") $var = $lista Else ;if not, it created a false mp3 directory script FileOpen("No-Music.mp3",1) Sleep(300) $var = @ScriptDir EndIf Local $FileList = _FileListToArray($var, "*.mp3*", 1) Global $tocando = False, $music = 1, $voll = "Volume", $vol = 30, $max = $FileList[0], $title = $FileList[1] #region ### START Koda GUI section ### $Form1_1 = GUICreate("Autoit Sound Play", 500, 313, 192, 124) GUISetBkColor(0x393952) $List1 = GUICtrlCreateListView("", 200, 8, 261, 220) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState() _GUICtrlListView_InsertColumn($List1, 0," " & $FileList[0] & " - músicas ", 257) For $i = 1 To $max Step 1 _GUICtrlListView_AddItem($List1, $FileList[$i], $i) ;generating items according to the number of songs Next $Prev = GUICtrlCreateButton("Prev", 104, 2, 81, 41) GUICtrlSetBkColor(-1, 0xff6633) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $Next = GUICtrlCreateButton("Next", 104, 51, 81, 49) GUICtrlSetBkColor(-1, 0xff6633) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $Play = GUICtrlCreateButton("Play", 8, 2, 89, 97) GUICtrlSetBkColor(-1, 0xbdc6c6) GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif") $Stop = GUICtrlCreateButton("Stop", 8, 110, 177, 33) GUICtrlSetBkColor(-1, 0xbdc6c6) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $List = GUICtrlCreateButton("List", 136, 147, 49, 73) GUICtrlSetBkColor(-1, 0x999999) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $VolUP = GUICtrlCreateButton("VolUP", 72, 147, 57, 73) GUICtrlSetBkColor(-1, 0xbdc6c6) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $AutoSound = GUICtrlCreateLabel("A" & @CR & "U" & @CR & "T" & @CR & "O" & _ @CR & @CR & @CR & "S" & @CR & "O" & @CR & "U" & @CR & "N" & @CR & "D", 478, 10, 257, 217) $VolDown = GUICtrlCreateButton("VolDown", 8, 147, 57, 73) GUICtrlSetBkColor(-1, 0xbdc6c6) $Volume = GUICtrlCreateLabel("Volume " & $vol, 15, 230, 400, 45) GUICtrlSetFont(-1, 30, 500, 0, "MS Sans Serif") $List2 = GUICtrlCreateList($title & " *********************************", 0, 280, 500, 50,3) GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif") GUICtrlSetBkColor(-1,0xbdc6c6) GUISetState(@SW_SHOW) #endregion ### END Koda GUI section ### SoundPlay($var & "\" & $music, 0) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $Play $title = $FileList[$music] SoundPlay($var & "\" & $FileList[$music], 0) GUICtrlSetData($List2, $title & " *********************************") ; $tocando = True Case $Prev If $tocando And $music > 1 Then $music -= 1 $title = $FileList[$music] SoundPlay($var & "\" & $FileList[$music], 0) GUICtrlSetData($List2, $title & " *********************************") EndIf If $tocando And $music < 2 Then SoundPlay($var & "\" & $FileList[$music], 0) EndIf Case $Next If $tocando And $music < $max Then $music += 1 $title = $FileList[$music] GUICtrlSetData($List2,"") GUICtrlSetData($List2, $title & " *********************************") SoundPlay($var & "\" & $FileList[$music], 0) EndIf Case $Stop SoundPlay("nosound", 0) $tocando = False Case $VolDown If $tocando And $vol >= 5 Then $vol -= 5 GUICtrlSetData($Volume, $voll & " " & $vol) EndIf Case $VolUP If $tocando And $vol < 100 Then $vol += 5 GUICtrlSetData($Volume, $voll & " " & $vol) EndIf Case $List $openwin = FileSelectFolder("Escolha um pasta.", "") $var = $openwin ; var equal to the Open Directory Local $FileList = _FileListToArray($var, "*.mp3", 0) ; mp3 transforms into arrays $max = $FileList[0] ;[0] number of mp3 is the limit max $open = FileOpen("Lista.ini", 2) $lista = IniWrite("Lista.ini", "Lista", "Caminho", $var) GUICtrlDelete($List1) $List1 = GUICtrlCreateListView("", 200, 8, 261, 220) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState() _GUICtrlListView_InsertColumn($List1, 0," " & $FileList[0] & " - músicas ", 256) For $i = 1 To $max Step 1 _GUICtrlListView_AddItem($List1, $FileList[$i], $i) ;regenerates items after new directory Next Case $GUI_EVENT_CLOSE Exit EndSwitch SoundSetWaveVolume($vol) Sleep(10) WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) ;recognizes the item double-clicked and executed Local $iCode, $tNMHDR, $hWndListView, $tInfo, $aItem, $hWndListView = $List1, _ $tNMHDR = DllStructCreate($tagNMHDR, $ilParam), $iCode = DllStructGetData($tNMHDR, "Code") Switch $iCode Case $NM_DBLCLK $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam) $aItem = _GUICtrlListView_GetItem($hWndListView, DllStructGetData($tInfo, "Index")) GUICtrlSetData($List2, "") GUICtrlSetData($List2, $aItem[3] & " *********************************") SoundPlay($var & "\" & $aItem[3], 0) $tocando = True ;music playing EndSwitch EndFunc
    1 point
  6. Melba23

    read source code

    mmaannddoo, I can see that you are continually checking this thread, but making no attempt to answer. I can only assume that you are unwilling to do so - and so the inevitable occurs...... M23
    1 point
  7. From what I could figure charposfromxy is not the same as the selpos(?) I dunno, Windows to me is a strange beast that I'm trying to learn little by little.
    1 point
  8. I've been trying all day to get a "request token" from the dropbox rest-api. Basically I'm trying to perform step 1 at https://www.dropbox.com/developers/blog/20/using-oauth-in-plaintext-mode: " 1. Make an API call for a request token: POST https://api.dropbox.com/1/oauth/request_token Your HTTP request should have the following header: Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&" The response body will be a url-encoded string: oauth_token=<request-token>&oauth_token_secret=<request-token-secret> Parse out the request token and secret and save them somewhere." I'm really struggling with how to send the header properly. Copying the example at http://brugbart.com/http-post-request-autoit, I have: ; The data to be sent $sPD = 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="h9y2pwy1nbvzy5e", oauth_signature="imrgkd8i80c2b8g&"' ; Creating the object $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", "https://api.dropbox.com/1/oauth/request_token", False) $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") ; Performing the Request $oHTTP.Send($sPD) ; Download the body response if any, and get the server status response code. $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status If $oStatusCode <> 200 then MsgBox(4096, "Response code", $oStatusCode) EndIf ; Saves the body response regardless of the Response code $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists FileWrite($file, $oReceived) FileClose($file) I think my Authorisation string should be used in the "SetRequestHeader" function, but am worried about replacing the "content-type" already there as it looks important. If anybody can help it would be an enormous help
    1 point
  9. I forgot about using a WMI sink... Modified from basic WMI code for detecting USB drives in Wraithdu's Safely Eject a USB Drive thread I think you will find this to be much faster using less code and no cpu spikes ;coded by rover 2k12 OnAutoItExitRegister("_OnExit") HotKeySet("{ESC}", "_Stop") ;Array with property, property value, your command string Global $aUSBDevProp[2][3] = [["Caption", "Apple iPod", "Whatever program1"],["PNPDeviceID", "USBSTOR\DISK&VEN_SAMSUNG&PROD_ANDROID&REV_FFFF\T499F99F0CB8&0", "Whatever program2"]] ;Global $aUSBDevProp[2][3] = [["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_00\6&366022B6&1&00", "Whatever program1"], ["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_02\6&366022B6&1&02", "Whatever program2"]] Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $oWMISink _MonitorUSBDevices($oWMISink) If @error Then Exit ConsoleWrite("! Error: " & @error & @LF) ConsoleWrite("> Monitoring - Press ESC to Exit: " & @LF & @LF) While 1 Sleep(1000) WEnd Func _MonitorUSBDevices(ByRef $oObj) $oObj = ObjCreate("WbemScripting.SWbemSink") If @error Or Not IsObj($oObj) Then Return SetError(1, 0, -1) ObjEvent($oObj, "SINK_") If @error Then Return SetError(2, 0, -1) Local $Obj_WMIService = ObjGet('winmgmts:\\localhost\root\cimv2') If @error Or Not IsObj($oObj) Then Return SetError(3, 0, -1) $Obj_WMIService.ExecNotificationQueryAsync($oWMISink, "SELECT TargetInstance FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'") EndFunc ;==>_MonitorUSBDevices Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext) #forceref $objAsyncContext Switch $objLatestEvent.Path_.Class Case "__InstanceCreationEvent" For $i = 0 To UBound($aUSBDevProp) - 1 If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then ConsoleWrite("+ Device Connected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF) ConsoleWrite("- Run: " & $aUSBDevProp[$i][2] & @LF & @LF) ;Run($aUSBDevProp[$i][2]) EndIf Next ;ConsoleWrite("> Creation Event" & @CRLF) ;ConsoleWrite("Caption: " & $objLatestEvent.TargetInstance.Caption & @CRLF) ;ConsoleWrite("Name: " & $objLatestEvent.TargetInstance.Name & @CRLF) ;ConsoleWrite("Description: " & $objLatestEvent.TargetInstance.Description & @CRLF) ;ConsoleWrite("PNPDeviceID: " & $objLatestEvent.TargetInstance.PNPDeviceID & @CRLF) ;ConsoleWrite("DeviceID: " & $objLatestEvent.TargetInstance.DeviceID & @CRLF) ;ConsoleWrite("->==========================================" & @CRLF) Case "__InstanceDeletionEvent" For $i = 0 To UBound($aUSBDevProp) - 1 If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then ConsoleWrite("! Device Disconnected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF & @LF) EndIf Next ;ConsoleWrite("> Deletion Event" & @CRLF) EndSwitch EndFunc ;==>SINK_OnObjectReady Func _OnExit() ;$oWMISink.Cancel ConsoleWrite("Exiting." & @LF) EndFunc ;==>_OnExit Func _Stop() ConsoleWrite("WMI Cancel was requested." & @LF) $oWMISink.Cancel ;use if you want to stop and start monitoring Exit EndFunc ;==>_Stop ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _ "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ "err.description is: " & @TAB & $oError.description & @CRLF & _ "err.source is: " & @TAB & $oError.source & @CRLF & _ "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF) Return SetError(1) EndFunc ;==>_ErrFunc
    1 point
×
×
  • Create New...