2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Clip Command
4 * FILE: base/applications/cmdutils/clip/clip.c
5 * PURPOSE: Provides clipboard management for command-line programs.
6 * PROGRAMMERS: Ricardo Hanke
19 VOID
PrintError(DWORD dwError
)
21 if (dwError
== ERROR_SUCCESS
)
24 ConMsgPuts(StdErr
, FORMAT_MESSAGE_FROM_SYSTEM
,
25 NULL
, dwError
, LANG_USER_DEFAULT
);
28 static BOOL
IsDataUnicode(HGLOBAL hGlobal
)
33 lpBuffer
= GlobalLock(hGlobal
);
34 bReturn
= IsTextUnicode(lpBuffer
, GlobalSize(hGlobal
), NULL
);
35 GlobalUnlock(hGlobal
);
40 int wmain(int argc
, wchar_t** argv
)
50 /* Initialize the Console Standard Streams */
51 hInput
= GetStdHandle(STD_INPUT_HANDLE
);
55 if (argc
> 1 && wcsncmp(argv
[1], L
"/?", 2) == 0)
57 ConResPuts(StdOut
, IDS_HELP
);
61 if (GetFileType(hInput
) == FILE_TYPE_CHAR
)
63 ConResPuts(StdOut
, IDS_USAGE
);
67 /* Initialize a growable buffer for holding clipboard data */
68 hBuffer
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, 4096);
73 * Read data from the input stream by chunks of 4096 bytes
74 * and resize the buffer each time when needed.
78 lpBuffer
= GlobalLock(hBuffer
);
82 bStatus
= ReadFile(hInput
, lpBuffer
+ dwSize
, 4096, &dwBytesRead
, NULL
);
83 dwSize
+= dwBytesRead
;
84 GlobalUnlock(hBuffer
);
86 hTemp
= GlobalReAlloc(hBuffer
, GlobalSize(hBuffer
) + 4096, GMEM_ZEROINIT
);
92 while (bStatus
&& dwBytesRead
> 0);
95 * Resize the buffer to the total size of data read.
96 * Note that, if the call fails, we still have the old buffer valid.
97 * The old buffer would be larger than the actual size of data it contains,
98 * but this is not a problem for us.
100 hTemp
= GlobalReAlloc(hBuffer
, dwSize
+ sizeof(WCHAR
), GMEM_ZEROINIT
);
104 /* Attempt to open the clipboard */
105 if (!OpenClipboard(NULL
))
108 /* Empty it, copy our data, then close it */
112 if (IsDataUnicode(hBuffer
))
114 SetClipboardData(CF_UNICODETEXT
, hBuffer
);
118 // TODO: Convert text from current console page to standard ANSI.
119 // Alternatively one can use CF_OEMTEXT as done here.
120 SetClipboardData(CF_OEMTEXT
, hBuffer
);
127 if (hBuffer
) GlobalFree(hBuffer
);
128 PrintError(GetLastError());