StrClb

From NSIS Wiki

Author: deguix (talk, contrib)


Description

Requires: LogicLib header file and System plug-in.

Version: 2.0.1.

Makes an action with the clipboard. This function can clean the clipboard, set or get a value to/from clipboard or swap values.

How To Use

Syntax

${StrClb} "ResultVar" "String" "Action"

or

Push "String"
Push "Action"
Call StrClb
Pop "ResultVar"

Parameters

ResultVar
Variable where the value from the clipboard is retrieved when Action is "<" or "<>". If the clipboard is empty or filled with non-recognizable data as text at the time, an empty value is returned.
WARNING: This value requires a variable, even when it's not needed. In this occasion, the return value is an empty string. You should reserve a variable for this occasion.
String
String to set to the clipboard when Action is ">" or "<>".
NOTE: The value will always be considered text.
Action
Specifies an action to do with the clipboard. The value of this parameter can be the following:
empty value
Clear clipboard.
>
Set the value from the String parameter to the clipboard.
<
Get the value from the clipboard to ResultVar parameter.
<>
Swap the value from the String parameter with the clipboard's, which is returned to ResultVar parameter.

Example

${StrClb} $0 "This is just an example" ">"
${StrClb} $0 "" "<"
;$0 = "This is just an example"
 
${StrClb} $0 "This is another example" "<>"
;$0 = "This is just an example"
 
${StrClb} $0 "" ""
;$0 = ""

Function Code

!define StrClb "!insertmacro StrClb"
 
!macro StrClb ResultVar String Action
  Push "${String}"
  Push "${Action}"
  Call StrClb
  Pop "${ResultVar}"
!macroend
 
Function StrClb
/*After this point:
  ------------------------------------------
  $0 = String (input)
  $1 = Action (input)
  $2 = Lock/Unlock (temp)
  $3 = Temp (temp)
  $4 = Temp2 (temp)*/
 
  ;Get input from user
 
  Exch $1
  Exch
  Exch $0
  Exch
  Push $2
  Push $3
  Push $4
 
  StrCpy $2 ""
  StrCpy $3 ""
  StrCpy $4 ""
 
  ;Open the clipboard to do the operations the user chose (kichik's fix)
  System::Call 'user32::OpenClipboard(i $HWNDPARENT)'
 
  ${If} $1 == ">" ;Set
 
    ;Step 1: Clear the clipboard
    System::Call 'user32::EmptyClipboard()'
 
    ;Step 2: Allocate global heap
    StrLen $2 $0
    IntOp $2 $2 + 1
    System::Call 'kernel32::GlobalAlloc(i 2, i r2) i.r2'
 
    ;Step 3: Lock the handle
    System::Call 'kernel32::GlobalLock(i r2) i.r3'
 
    ;Step 4: Copy the text to locked clipboard buffer
    System::Call 'kernel32::lstrcpyA(i r3, t r0)'
 
    ;Step 5: Unlock the handle again
    System::Call 'kernel32::GlobalUnlock(i r2)'
 
    ;Step 6: Set the information to the clipboard
    System::Call 'user32::SetClipboardData(i 1, i r2)'
 
    StrCpy $0 ""
 
  ${ElseIf} $1 == "<" ;Get
 
    ;Step 1: Get clipboard data
    System::Call 'user32::GetClipboardData(i 1) i .r2'
 
    ;Step 2: Lock and copy data (kichik's fix)
    System::Call 'kernel32::GlobalLock(i r2) t .r0'
 
    ;Step 3: Unlock (kichik's fix)
    System::Call 'kernel32::GlobalUnlock(i r2)'
 
  ${ElseIf} $1 == "<>" ;Swap
 
    ;Step 1: Get clipboard data
    System::Call 'user32::GetClipboardData(i 1) i .r2'
 
    ;Step 2: Lock and copy data (kichik's fix)
    System::Call 'kernel32::GlobalLock(i r2) t .r4'
 
    ;Step 3: Unlock (kichik's fix)
    System::Call 'kernel32::GlobalUnlock(i r2)'
 
    ;Step 4: Clear the clipboard
    System::Call 'user32::EmptyClipboard()'
 
    ;Step 5: Allocate global heap
    StrLen $2 $0
    IntOp $2 $2 + 1
    System::Call 'kernel32::GlobalAlloc(i 2, i r2) i.r2'
 
    ;Step 6: Lock the handle
    System::Call 'kernel32::GlobalLock(i r2) i.r3'
 
    ;Step 7: Copy the text to locked clipboard buffer
    System::Call 'kernel32::lstrcpyA(i r3, t r0)'
 
    ;Step 8: Unlock the handle again
    System::Call 'kernel32::GlobalUnlock(i r2)'
 
    ;Step 9: Set the information to the clipboard
    System::Call 'user32::SetClipboardData(i 1, i r2)'
 
    StrCpy $0 $4
  ${Else} ;Clear
 
    ;Step 1: Clear the clipboard
    System::Call 'user32::EmptyClipboard()'
 
    StrCpy $0 ""
  ${EndIf}
 
  ;Close the clipboard
  System::Call 'user32::CloseClipboard()'
 
/*After this point:
  ------------------------------------------
  $0 = ResultVar (output)*/
 
  ;Return result to user
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd

Versions History

2.0.1
  • Fixed the return of text when Action parameter value was equal to "<>".

Credits

Author: Diego Pedroso (deguix)
Based on CopyToClipboard and CopyFromClipboard functions.

Personal tools
donate
ads