Ticket #1028: _ClipBoard_GetData.[MyFix].au3

File _ClipBoard_GetData.[MyFix].au3, 5.1 KB (added by Ascendant, 15 years ago)

_ClipBoard_GetData (fixed) function

Line 
1; Fixed version of _ClipBoard_GetData
2
3; #FUNCTION# ====================================================================================================================
4; Name...........: _ClipBoard_GetData
5; Description ...: Retrieves data from the clipboard in a specified format
6; Syntax.........: _ClipBoard_GetData([$iFormat = 1])
7; Parameters ....: $iFormat     - Specifies a clipboard format:
8;                  |$CF_TEXT            - Text format
9;                  |$CF_BITMAP          - Handle to a bitmap (HBITMAP)
10;                  |$CF_METAFILEPICT    - Handle to a metafile picture (METAFILEPICT)
11;                  |$CF_SYLK            - Microsoft Symbolic Link (SYLK) format
12;                  |$CF_DIF             - Software Arts' Data Interchange Format
13;                  |$CF_TIFF            - Tagged image file format
14;                  |$CF_OEMTEXT         - Text format containing characters in the OEM character set
15;                  |$CF_DIB             - BITMAPINFO structure followed by the bitmap bits
16;                  |$CF_PALETTE         - Handle to a color palette
17;                  |$CF_PENDATA         - Data for the pen extensions to Pen Computing
18;                  |$CF_RIFF            - Represents audio data in RIFF format
19;                  |$CF_WAVE            - Represents audio data in WAVE format
20;                  |$CF_UNICODETEXT     - Unicode text format
21;                  |$CF_ENHMETAFILE     - Handle to an enhanced metafile (HENHMETAFILE)
22;                  |$CF_HDROP           - Handle to type HDROP that identifies a list of files
23;                  |$CF_LOCALE          - Handle to the locale identifier associated with text in the clipboard
24;                  |$CF_DIBV5           - BITMAPV5HEADER structure followed by bitmap color and the bitmap bits
25;                  |$CF_OWNERDISPLAY    - Owner display format
26;                  |$CF_DSPTEXT         - Text display format associated with a private format
27;                  |$CF_DSPBITMAP       - Bitmap display format associated with a private format
28;                  |$CF_DSPMETAFILEPICT - Metafile picture display format associated with a private format
29;                  |$CF_DSPENHMETAFILE  - Enhanced metafile display format associated with a private format
30; Return values .: Success      - Text for text based formats or a handle for all other formats
31;                  Failure      - 0
32; Author ........: Paul Campbell (PaulIA)
33; Modified.......: Gary Frost,
34;                  Ascend4nt (now follows traditional ClipBoard 'get' code, fixed $CF_UNICODETEXT errors)
35; Remarks .......: This function performs all of the steps neccesary to get data from the clipboard. It checks to see if the data
36;                  format is available, opens the clipboard, closes the clipboard and returns the data (converting it to a string
37;                  if needed.  If you need a finer degree of control over retrieving data from the clipboard, you may want to use
38;                  the _ClipBoard_GetDataEx function.
39; Related .......: _ClipBoard_GetDataEx, _ClipBoard_SetData
40; Link ..........;
41; Example .......; Yes
42; ===============================================================================================================================
43Func _ClipBoard_GetData($iFormat = 1)
44        Local $hMemory, $tData, $pMemoryBlock, $pMemoryDest, $iDataSize, $vReturn
45
46        If Not _ClipBoard_IsFormatAvailable($iFormat) Then Return SetError(-1, 0, 0)
47        If Not _ClipBoard_Open(0) Then Return SetError(-2, 0, 0)
48        $hMemory = _ClipBoard_GetDataEx($iFormat)
49
50        ;_ClipBoard_Close()             ; moved to end: traditionally done *after* copying over the memory
51
52        If $hMemory=0 Then
53                _ClipBoard_Close()
54                Return SetError(-3, 0, 0)
55        EndIf
56       
57        Local $pMemoryBlock=_MemGlobalLock($hMemory)
58       
59        If $pMemoryBlock=0 Then
60                _ClipBoard_Close()
61                Return SetError(-4,0,0)
62        EndIf
63       
64        ; Get the actual memory size of the ClipBoard memory object (in bytes)
65        $iDataSize=_MemGlobalSize($hMemory)
66       
67        If $iDataSize = 0 Then
68                _MemGlobalUnlock($hMemory)
69                _ClipBoard_Close()
70                Return SetError(-5,0,"")
71        EndIf
72
73        Switch $iFormat
74                Case $CF_TEXT, $CF_OEMTEXT
75                        $tData = DllStructCreate("char Text[" & $iDataSize & "]", $pMemoryBlock)
76                        $vReturn = DllStructGetData($tData, "Text")
77                Case $CF_UNICODETEXT
78                        ; Round() shouldn't be necessary, as CF_UNICODETEXT should be 2-bytes wide & thus evenly-divisible
79                        $iDataSize=Round($iDataSize/2)
80                        $tData = DllStructCreate("wchar Text[" & $iDataSize & "]", $pMemoryBlock)
81                        $vReturn = DllStructGetData($tData, "Text")
82                Case Else
83                        $tData = DllStructCreate("ubyte[" & $iDataSize & "]",$pMemoryBlock)
84                        $pMemoryDest = DllStructGetPtr($tData)
85                        ; Copy the memory over to our newly created structure so that we can unlock the memory
86                        _MemMoveMemory($pMemoryBlock, $pMemoryDest , $iDataSize)
87                        $vReturn = DllStructGetPtr($tData)
88        EndSwitch
89       
90        ; Unlock the memory & Close the clipboard now that we have grabbed what we needed
91        _MemGlobalUnlock($hMemory)
92        _ClipBoard_Close()
93        ; Put size of data object in @extended
94        SetError(0,$iDataSize)
95        Return $vReturn
96EndFunc   ;==>_ClipBoard_GetData