ReactOS  0.4.12-dev-18-gf469aca
clip.c File Reference
#include <stdio.h>
#include <windef.h>
#include <winbase.h>
#include <winuser.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for clip.c:

Go to the source code of this file.

Functions

VOID PrintError (DWORD dwError)
 
static BOOL IsDataUnicode (HGLOBAL hGlobal)
 
int wmain (int argc, wchar_t **argv)
 

Function Documentation

◆ IsDataUnicode()

static BOOL IsDataUnicode ( HGLOBAL  hGlobal)
static

Definition at line 28 of file clip.c.

Referenced by wmain().

29 {
30  BOOL bReturn;
32 
33  lpBuffer = GlobalLock(hGlobal);
34  bReturn = IsTextUnicode(lpBuffer, GlobalSize(hGlobal), NULL);
35  GlobalUnlock(hGlobal);
36 
37  return bReturn;
38 }
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:163
smooth NULL
Definition: ftsmooth.c:416
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI IsTextUnicode(IN CONST VOID *lpv, IN INT iSize, IN OUT LPINT lpiResult OPTIONAL)
Definition: unicode.c:27
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190

◆ PrintError()

VOID PrintError ( DWORD  dwError)

Definition at line 19 of file clip.c.

Referenced by wmain().

20 {
21  if (dwError == ERROR_SUCCESS)
22  return;
23 
25  NULL, dwError, LANG_USER_DEFAULT);
26 }
#define ERROR_SUCCESS
Definition: deptool.c:10
smooth NULL
Definition: ftsmooth.c:416
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
INT ConMsgPuts(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId)
Definition: outstream.c:837
#define StdErr
Definition: stream.h:77
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50

◆ wmain()

int wmain ( int  argc,
wchar_t **  argv 
)

Definition at line 40 of file clip.c.

41 {
42  HANDLE hInput;
43  DWORD dwBytesRead;
44  BOOL bStatus;
45  HGLOBAL hBuffer;
46  HGLOBAL hTemp;
48  SIZE_T dwSize = 0;
49 
50  /* Initialize the Console Standard Streams */
53 
54  /* Check for usage */
55  if (argc > 1 && wcsncmp(argv[1], L"/?", 2) == 0)
56  {
58  return 0;
59  }
60 
61  if (GetFileType(hInput) == FILE_TYPE_CHAR)
62  {
64  return 0;
65  }
66 
67  /* Initialize a growable buffer for holding clipboard data */
68  hBuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, 4096);
69  if (!hBuffer)
70  goto Failure;
71 
72  /*
73  * Read data from the input stream by chunks of 4096 bytes
74  * and resize the buffer each time when needed.
75  */
76  do
77  {
78  lpBuffer = GlobalLock(hBuffer);
79  if (!lpBuffer)
80  goto Failure;
81 
82  bStatus = ReadFile(hInput, lpBuffer + dwSize, 4096, &dwBytesRead, NULL);
83  dwSize += dwBytesRead;
84  GlobalUnlock(hBuffer);
85 
86  hTemp = GlobalReAlloc(hBuffer, GlobalSize(hBuffer) + 4096, GMEM_ZEROINIT);
87  if (!hTemp)
88  goto Failure;
89 
90  hBuffer = hTemp;
91  }
92  while (bStatus && dwBytesRead > 0);
93 
94  /*
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.
99  */
100  hTemp = GlobalReAlloc(hBuffer, dwSize + sizeof(WCHAR), GMEM_ZEROINIT);
101  if (hTemp)
102  hBuffer = hTemp;
103 
104  /* Attempt to open the clipboard */
105  if (!OpenClipboard(NULL))
106  goto Failure;
107 
108  /* Empty it, copy our data, then close it */
109 
110  EmptyClipboard();
111 
112  if (IsDataUnicode(hBuffer))
113  {
115  }
116  else
117  {
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);
121  }
122 
123  CloseClipboard();
124  return 0;
125 
126 Failure:
127  if (hBuffer) GlobalFree(hBuffer);
129  return -1;
130 }
static int argc
Definition: ServiceArgs.c:12
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
HGLOBAL NTAPI GlobalReAlloc(HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:825
__wchar_t WCHAR
Definition: xmlstorage.h:180
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
#define IDS_USAGE
Definition: resource.h:3
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:419
HANDLE WINAPI SetClipboardData(_In_ UINT, _In_opt_ HANDLE)
static char ** argv
Definition: ServiceArgs.c:11
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
static BOOL IsDataUnicode(HGLOBAL hGlobal)
Definition: clip.c:28
unsigned char * LPBYTE
Definition: typedefs.h:52
#define ConInitStdStreams()
Definition: stream.h:122
#define FILE_TYPE_CHAR
Definition: winbase.h:257
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:163
#define CF_UNICODETEXT
Definition: constants.h:408
smooth NULL
Definition: ftsmooth.c:416
#define STD_INPUT_HANDLE
Definition: winbase.h:264
BOOL WINAPI EmptyClipboard(void)
Definition: ntwrapper.h:190
unsigned int BOOL
Definition: ntddk_ex.h:94
VOID PrintError(DWORD dwError)
Definition: clip.c:19
unsigned long DWORD
Definition: ntddk_ex.h:95
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
BOOL WINAPI CloseClipboard(void)
Definition: ntwrapper.h:178
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1087
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define CF_OEMTEXT
Definition: constants.h:402
#define IDS_HELP
Definition: resource.h:3
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
#define StdOut
Definition: stream.h:76
BOOL WINAPI OpenClipboard(_In_opt_ HWND)
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
#define GMEM_MOVEABLE
Definition: winbase.h:291
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define GMEM_ZEROINIT
Definition: winbase.h:303