Jump to content

[SOLVED] ClipGet() consumes 100% of CPU resources


k4rl3on
 Share

Recommended Posts

hello

I have problem with ClipGet () function: If the clipboard contains a large amount of data the use of the function pushes the consumption of cpu resources to 100% and my script unresponds until I force it to end.
I tried to work around the problem by limiting the size of the recovered data from the clipboard

 

Local = $ClipBoardData = StringMid(ClipGet (), 1.1024)

I also used:


 

DllCall ("psapi.dll", "int", "EmptyWorkingSet", "long", -1)

to free the memory after each call to ClipGet ()

Edited by k4rl3on
Link to comment
Share on other sites

Welcome to Autoit and the forum!

A few questions:

  • How large is "a large amount of data"? How many MB?
  • What type of data? Text, picture ...?
  • Which version of Windows do you run?
  • Which version of AutoIt do you run?
Local = $ClipBoardData = StringMid(ClipGet (), 1.1024)

won't work as it has some syntax errors. I think it should be

Local $ClipBoardData = StringMid(ClipGet (), 1, 1024)

to grab the first 1024 bytes of the data from the clipboard. But this will not solve your performance problems as it first retrieves ALL data from the clipboard before StringMid is being executed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

 

  • the large amount of data: i try to copy 3650 .png files with a size of: 16px = 3650*1.12 = 4088 / 1024 = 3,9 MB
  • type: i want just the list of those files names as ClipGet() returns it = Text
  • version of Windows: XP 500MB RAM.
  • AutoIt (Latest Stable Version) v3.3.14.2

Yes i use

Local $ClipBoardData = StringMid(ClipGet (), 1, 1024)

 

Thank you

Edited by k4rl3on
Link to comment
Share on other sites

How do you fill the clipboard?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Do I understand you correctly that you just want the names of the selected files, not the files itself?
If this is true then this little script shows how to grab the list of selected files from Windows Explorer:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thank you for your help, 

But what i need is to get the content of the clipboard, however if its a simple copied text or a list of files names, my problem like is presented above: ClipGet() do correctly that until there is big data in the clipboard, ClipGet() consumes 100% of CPU resources. what i need is an alternative to ClipGet() that dont crash my script when there is big data in the clipboard.

Link to comment
Share on other sites

You might want to try using _Clipboard_GetData with the text format to see if it's any faster or lighter on your CPU.

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

Link to comment
Share on other sites

Why not post it?

Part of finding a problem is letting other people try it.

So many times I've seen threads like these go on for weeks, then the op says "oops sorry I had spilled iron brew on my laptop and broke it"

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Hi,

Following the suggestion from jguinch

about Using _ClipBoard_SetViewer instead of ClipGet()

Here is what i done:

#include <Clipboard.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>

Global $g_idMemo, $g_hNext = 0

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Clipboard", 400, 100)
;~  $hGUI = WinGetHandle(WinGetTitle(""))
;~  $g_idMemo = GUICtrlCreateEdit("", 2, 2, 596, 396, $WS_VSCROLL)
;~  GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)

    ; Initialize clipboard viewer
    $g_hNext = _ClipBoard_SetViewer($hGUI)

    GUIRegisterMsg($WM_CHANGECBCHAIN, "WM_CHANGECBCHAIN")
    GUIRegisterMsg($WM_DRAWCLIPBOARD, "WM_DRAWCLIPBOARD")

    MemoWrite("GUI handle ....: " & $hGUI)
    MemoWrite("Viewer handle .: " & _ClipBoard_GetViewer())

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    ; Shut down clipboard viewer
    _ClipBoard_ChangeChain($hGUI, $g_hNext)
EndFunc   ;==>Example

; Write message to memo
Func MemoWrite($sMessage = "")
    ConsoleWrite($sMessage & @CRLF)
EndFunc   ;==>MemoWrite

; Handle $WM_CHANGECBCHAIN messages
Func WM_CHANGECBCHAIN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Show that message was received
    MemoWrite("***** $WM_CHANGECBCHAIN *****")

    ; If the next window is closing, repair the chain
    If $wParam = $g_hNext Then
        $g_hNext = $lParam
        ; Otherwise pass the message to the next viewer
    ElseIf $g_hNext <> 0 Then
        _SendMessage($g_hNext, $WM_CHANGECBCHAIN, $wParam, $lParam, 0, "hwnd", "hwnd")
    EndIf
EndFunc   ;==>WM_CHANGECBCHAIN

; Handle $WM_DRAWCLIPBOARD messages
Func WM_DRAWCLIPBOARD($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Display any text on clipboard
    ;Check clipboard Content type
    If _ClipBoardContentType() = 4 Then
        ; If files copied:
        $Content = _ClipBoard_GetData($CF_HDROP)
    Else
        $Content = _ClipBoard_GetData()
    EndIf
    FileWrite("ClipBoardLog.txt",_TimeStamp()&" - "&$Content&@CRLF)
    ; Pass the message to the next viewer
    If $g_hNext <> 0 Then _SendMessage($g_hNext, $WM_DRAWCLIPBOARD, $wParam, $lParam)
EndFunc   ;==>WM_DRAWCLIPBOARD

Func _ClipBoardContentType()
    DllCall("User32.dll", "int", "OpenClipboard", "hwnd", "")
    $ret = DllCall("User32.dll", "int", "EnumClipboardFormats", "int", 49158)
    DllCall("User32.dll", "int", "CloseClipboard")
    If $ret[0] = 49159 Then
        Return 4
    Else
        Return 3
    EndIf
EndFunc

Func _TimeStamp()
    Local $Ret
    $Ret = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
    If Not @error Then
        Return $Ret[0]
    Else
        Return ""
    EndIf
EndFunc

the code based on the example of _ClipBoard_SetViewer with litte modification to check the clipboard content's type, and log it into a .txt file, and its work without any problem concerning CPU ressources  consumption.

but still two problems to resolve:

  1. In case of files in clipboard _ClipBoard_GetData($CF_HDROP) returns binary data like: 0x14000000000000000000000000000000010000004.... how can i get file name from it?
  2. how can i run this without creating a new GUI: doing something like :
    $hGUI = WinGetHandle(WinGetTitle(""))

and thank you again

Edited by k4rl3on
Link to comment
Share on other sites

  1. In case of files in clipboard _ClipBoard_GetData($CF_HDROP) returns binary data like: 0x14000000000000000000000000000000010000004.... how can i get file name from it?

Everything you might want to know about this can be found here.

https://msdn.microsoft.com/en-us/library/windows/desktop/bb776902(v=vs.85).aspx#CF_HDROP

Edited by BrewManNH

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

Link to comment
Share on other sites

thank you BrewManNH for the link.

here is what i tried to do:

from the documention of DragQueryFile i created a function:

Func WM_DROPFILES_FUNC($Content)
    Local $nSize, $pFileName
    Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
    ConsoleWrite("["&$nAmt[0]&"]"&@CRLF)
    For $i = 0 To $nAmt[0] - 1
        $nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", 0, "int", 0)
        $nSize = $nSize[0] + 1
        $pFileName = DllStructCreate("char[" & $nSize & "]")
        DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $nSize)
        ReDim $gaDropFiles[$i + 1]
        $gaDropFiles[$i] = DllStructGetData($pFileName, 1)
        MemoWrite($gaDropFiles[$i])
        ;If StringInStr(FileGetAttrib($gaDropFiles[$i]), "D") Then $gaDropFiles[$i] = $gaDropFiles[$i] & "\*.*"
        $pFileName = 0
    Next
EndFunc

i gave it the return of _ClipBoard_GetData($CF_HDROP) as parameter as explained in te docuentation of DragQueryFile function:

hDrop [in]

Type: HDROP

Identifier of the structure that contains the file names of the dropped files.

is this the expected value?

DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)

which must return the number of file in clipboard, returns -1 when i'm copying a file :(

the complete code:

#include <Clipboard.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>

Global $gaDropFiles[1]
Global $g_idMemo, $g_hNext = 0

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Clipboard", 400, 100)
;~     $hGUI = WinGetHandle(WinGetTitle(""))
;~     $g_idMemo = GUICtrlCreateEdit("", 2, 2, 596, 396, $WS_VSCROLL)
;~     GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)

    ; Initialize clipboard viewer
    $g_hNext = _ClipBoard_SetViewer($hGUI)

    GUIRegisterMsg($WM_CHANGECBCHAIN, "WM_CHANGECBCHAIN")
    GUIRegisterMsg($WM_DRAWCLIPBOARD, "WM_DRAWCLIPBOARD")
;~     GUIRegisterMsg ($WM_DROPFILES, "WM_DROPFILES_FUNC")

    MemoWrite("GUI handle ....: " & $hGUI)
    MemoWrite("Viewer handle .: " & _ClipBoard_GetViewer())

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    ; Shut down clipboard viewer
    _ClipBoard_ChangeChain($hGUI, $g_hNext)
EndFunc   ;==>Example

; Write message to memo
Func MemoWrite($sMessage = "")
    ConsoleWrite($sMessage & @CRLF)
EndFunc   ;==>MemoWrite

; Handle $WM_CHANGECBCHAIN messages
Func WM_CHANGECBCHAIN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Show that message was received
    MemoWrite("***** $WM_CHANGECBCHAIN *****")

    ; If the next window is closing, repair the chain
    If $wParam = $g_hNext Then
        $g_hNext = $lParam
        ; Otherwise pass the message to the next viewer
    ElseIf $g_hNext <> 0 Then
        _SendMessage($g_hNext, $WM_CHANGECBCHAIN, $wParam, $lParam, 0, "hwnd", "hwnd")
    EndIf
EndFunc   ;==>WM_CHANGECBCHAIN

; Handle $WM_DRAWCLIPBOARD messages
Func WM_DRAWCLIPBOARD($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Display any text on clipboard
    If _ClipBoardContentType() = 4 Then
        $Bin = _ClipBoard_GetData($CF_HDROP)
        WM_DROPFILES_FUNC($Bin)
    Else
        $Content = _ClipBoard_GetData()
    EndIf
    FileWrite("ClipBoardLog.txt",_TimeStamp()&" - "&$Content&@CRLF)
    ; Pass the message to the next viewer
    If $g_hNext <> 0 Then _SendMessage($g_hNext, $WM_DRAWCLIPBOARD, $wParam, $lParam)
EndFunc   ;==>WM_DRAWCLIPBOARD

Func WM_DROPFILES_FUNC($Content)
    Local $nSize, $pFileName
    Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
    ConsoleWrite("["&$nAmt[0]&"]"&@CRLF)
    For $i = 0 To $nAmt[0] - 1
        $nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", 0, "int", 0)
        $nSize = $nSize[0] + 1
        $pFileName = DllStructCreate("char[" & $nSize & "]")
        DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $nSize)
        ReDim $gaDropFiles[$i + 1]
        $gaDropFiles[$i] = DllStructGetData($pFileName, 1)
        MemoWrite($gaDropFiles[$i])
        ;If StringInStr(FileGetAttrib($gaDropFiles[$i]), "D") Then $gaDropFiles[$i] = $gaDropFiles[$i] & "\*.*"
        $pFileName = 0
    Next
EndFunc

Func _ClipBoardContentType()
    DllCall("User32.dll", "int", "OpenClipboard", "hwnd", "")
    $ret = DllCall("User32.dll", "int", "EnumClipboardFormats", "int", 49158)
    DllCall("User32.dll", "int", "CloseClipboard")
    If $ret[0] = 49159 Then
        Return 4
    Else
        Return 3
    EndIf
EndFunc

Func _TimeStamp()
    Local $Ret
    $Ret = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
    If Not @error Then
        Return $Ret[0]
    Else
        Return ""
    EndIf
EndFunc
 

 

 

 

 

Edited by k4rl3on
Link to comment
Share on other sites

Is there something you need the list of files for or is this just something you thought you'd try and get working? If you just need a list of files in a folder, there are easier ways of going about this.

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

Link to comment
Share on other sites

my script is a clipboard monitor, it checks clipboard content every second and store it into a log file.

i tried with 

$hdrop = _ClipBoard_GetDataEx($CF_HDROP)

which returns a handle of the object in the clipboard call

Func WM_DROPFILES_FUNC($Content)
    Local $nSize, $pFileName
    Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
    ConsoleWrite("["&$nAmt[0]&"]"&@CRLF)
    For $i = 0 To $nAmt[0] - 1
        $nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", 0, "int", 0)
        $nSize = $nSize[0] + 1
        $pFileName = DllStructCreate("char[" & $nSize & "]")
        DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $Content, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $nSize)
        ReDim $gaDropFiles[$i + 1]
        $gaDropFiles[$i] = DllStructGetData($pFileName, 1)
        MemoWrite($gaDropFiles[$i])
        ;If StringInStr(FileGetAttrib($gaDropFiles[$i]), "D") Then $gaDropFiles[$i] = $gaDropFiles[$i] & "\*.*"
        $pFileName = 0
    Next
EndFunc

with it as parameter, but still returns -1 as number of files in the clipboard after copying or drag drop some files as test :(

Edit: i also tried to put the created GUI handle as parameter for the function with no result :(

Edited by k4rl3on
Link to comment
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
 Share

×
×
  • Create New...