ReactOS 0.4.16-dev-2610-ge2c92c0
NtUserConvertMemHandle.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Test for NtUserConvertMemHandle
5 * COPYRIGHT: Copyright 2025 Max Korostil (mrmks04@yandex.ru)
6 */
7
8#include "../win32nt.h"
9
11{
12 HGLOBAL hGlobalBuffer = NULL;
13 CHAR* pLockedBuffer = NULL;
14
15 hGlobalBuffer = GlobalAlloc(GMEM_DDESHARE, stringLength);
16 if (hGlobalBuffer == NULL)
17 {
18 return hGlobalBuffer;
19 }
20
21 pLockedBuffer = (CHAR*)GlobalLock(hGlobalBuffer);
22 if (pLockedBuffer)
23 {
24 memcpy(pLockedBuffer, pString, stringLength);
25 }
26
27 GlobalUnlock(hGlobalBuffer);
28
29 return hGlobalBuffer;
30}
31
32HANDLE setClipboardData(UINT uFormat, HANDLE hGlobalMem)
33{
34 DWORD dwSize = 0;
35 PVOID pMem = NULL;
36 HANDLE hRet = NULL;
37 HANDLE hMem = NULL;
38 SETCLIPBDATA scd = {FALSE, FALSE};
39
40 // Get global memory
41 pMem = GlobalLock(hGlobalMem);
42 dwSize = GlobalSize(hGlobalMem);
43
44 hMem = NtUserConvertMemHandle(pMem, dwSize);
45 GlobalUnlock(hGlobalMem);
46 if (hMem == NULL)
47 {
48 return hMem;
49 }
50
51 scd.fGlobalHandle = TRUE;
52 hRet = NtUserSetClipboardData(uFormat, hMem, &scd);
53
54 return hRet;
55}
56
58{
59 HANDLE hData = NULL;
60 HANDLE hGlobal = NULL;
62 DWORD cbData = 0;
64
65 hData = NtUserGetClipboardData(uFormat, &gcd);
66 if (gcd.fGlobalHandle)
67 {
70
71 if (hGlobal == NULL)
72 {
73 return hGlobal;
74 }
75
76 pData = GlobalLock(hGlobal);
78 GlobalUnlock(hGlobal);
79 }
80
81 return hGlobal;
82}
83
85{
86 HANDLE hMem;
87 CONST CHAR testString[] = "Test string";
88 HGLOBAL hGlobal = NULL;
89
90 hMem = NtUserConvertMemHandle(UlongToPtr(0xDEADBEEF), 0xFFFF);
91 ok_hdl(hMem, NULL);
92
93 // Alloc global memory
94 hGlobal = createGlobalMemory(testString, sizeof(testString));
95 if (hGlobal == NULL)
96 {
97 skip("hGlobal is NULL\n");
98 }
99 else
100 {
101 HANDLE hMem = NULL;
102 HANDLE hRet = NULL;
103
105 hRet = setClipboardData(CF_TEXT, hGlobal);
107
108 if (hRet == NULL)
109 {
110 skip("Set clipboard data failed\n");
111 goto cleanup;
112 }
113
117
118 if (hMem)
119 {
120 PVOID pData = GlobalLock(hMem);
121 ok_long(memcmp(pData, testString, sizeof(testString)), 0);
122 GlobalUnlock(hMem);
123 }
124 else
125 {
126 skip("Get clipboard data failed\n");
127 }
128
129 cleanup:
130 GlobalFree(hGlobal);
131 }
132}
HANDLE setClipboardData(UINT uFormat, HANDLE hGlobalMem)
HGLOBAL createGlobalMemory(const CHAR *pString, DWORD stringLength)
HANDLE getClipboardData(UINT uFormat)
#define ok_long(expression, result)
Definition: atltest.h:133
#define ok_hdl
Definition: atltest.h:136
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define CF_TEXT
Definition: constants.h:396
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static void cleanup(void)
Definition: main.c:1335
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
#define UlongToPtr(u)
Definition: config.h:106
unsigned long DWORD
Definition: ntddk_ex.h:95
FxString * pString
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
HANDLE NTAPI NtUserSetClipboardData(UINT fmt, HANDLE hMem, PSETCLIPBDATA scd)
Definition: clipboard.c:1084
HANDLE NTAPI NtUserConvertMemHandle(PVOID pData, DWORD cbData)
Definition: clipboard.c:1187
HANDLE NTAPI NtUserGetClipboardData(UINT fmt, PGETCLIPBDATA pgcd)
Definition: clipboard.c:897
NTSTATUS NTAPI NtUserCreateLocalMemHandle(HANDLE hMem, PVOID pData, DWORD cbData, DWORD *pcbData)
Definition: clipboard.c:1233
static ERESOURCE GlobalLock
Definition: sys_arch.c:8
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
unsigned int UINT
Definition: ndis.h:50
#define CONST
Definition: pedump.c:81
char CHAR
Definition: pedump.c:57
_In_opt_ _In_opt_ _In_ _In_ DWORD cbData
Definition: shlwapi.h:761
static int gcd(int, int)
Definition: getopt.c:86
BOOL fGlobalHandle
Definition: ntuser.h:1168
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830
#define GMEM_MOVEABLE
Definition: winbase.h:318
#define GMEM_DDESHARE
Definition: winbase.h:322
BOOL WINAPI CloseClipboard(void)
Definition: ntwrapper.h:178
BOOL WINAPI OpenClipboard(_In_opt_ HWND)