win32knapi: More / improved tests for NtUserSystemParametersInfo, NtGdiSelectBrush...
[reactos.git] / rostests / apitests / w32knapi / w32knapi.c
1 #include "w32knapi.h"
2
3 HINSTANCE g_hInstance;
4 HMODULE g_hModule = NULL;
5 PGDI_TABLE_ENTRY GdiHandleTable;
6
7 static
8 PGDI_TABLE_ENTRY
9 MyGdiQueryTable()
10 {
11 PTEB pTeb = NtCurrentTeb();
12 PPEB pPeb = pTeb->ProcessEnvironmentBlock;
13 return pPeb->GdiSharedHandleTable;
14 }
15
16 BOOL
17 IsHandleValid(HGDIOBJ hobj)
18 {
19 USHORT Index = (ULONG_PTR)hobj;
20 PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
21
22 if (pentry->KernelData == NULL ||
23 pentry->KernelData < (PVOID)0x80000000 ||
24 (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
25 {
26 return FALSE;
27 }
28
29 return TRUE;
30 }
31
32 PVOID
33 GetHandleUserData(HGDIOBJ hobj)
34 {
35 USHORT Index = (ULONG_PTR)hobj;
36 PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
37
38 if (pentry->KernelData == NULL ||
39 pentry->KernelData < (PVOID)0x80000000 ||
40 (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
41 {
42 return NULL;
43 }
44
45 return pentry->UserData;
46 }
47
48
49 static DWORD WINAPI
50 IntSyscall(FARPROC proc, UINT cParams, PVOID pFirstParam)
51 {
52 DWORD ret;
53
54 asm volatile
55 (
56 "pushfl;" // Save flags
57 "movl %%ecx, %%eax;"
58 "shl $2, %%eax;" // Calculate param size
59 "subl %%eax, %%esp;" // Calculate new stack pos
60 "movl %%esp, %%edi;" // Destination is stackpointer
61 "cld;" // Clear direction flag
62 "rep movsd;" // Copy params to the stack
63 "call *%%edx;" // Call function
64 "popfl;" // Restore flags
65 : "=a" (ret)
66 : "S" (pFirstParam), "c" (cParams), "d"(proc)
67 : "%edi"
68 );
69
70 return ret;
71 }
72
73 DWORD
74 Syscall(LPWSTR pszFunction, int cParams, void* pParams)
75 {
76 char szFunctionName[MAX_PATH];
77
78 sprintf(szFunctionName, "%ls", pszFunction);
79 FARPROC proc = (FARPROC)GetProcAddress(g_hModule, szFunctionName);
80 if (!proc)
81 {
82 printf("Couldn't find proc: %s\n", szFunctionName);
83 return FALSE;
84 }
85
86 return IntSyscall(proc, cParams, pParams);
87 }
88
89 BOOL
90 IsFunctionPresent(LPWSTR lpszFunction)
91 {
92 char szFunctionName[MAX_PATH];
93 sprintf(szFunctionName, "%ls", lpszFunction);
94 return (GetProcAddress(g_hModule, szFunctionName) != NULL);
95 }
96
97 int APIENTRY
98 WinMain(HINSTANCE hInstance,
99 HINSTANCE hPrevInstance,
100 LPSTR lpCmdLine,
101 int nCmdShow)
102 {
103 g_hInstance = hInstance;
104
105 printf("Win32k native API test\n");
106
107 /* Convert to gui thread */
108 // IsGUIThread(TRUE); <- does not exists on win2k
109
110 InitOsVersion();
111 printf("g_OsIdx = %d\n", g_OsIdx);
112
113 g_hModule = LoadLibraryW(L"w32kdll.dll");
114 if (!g_hModule)
115 {
116 printf("w32kdll.dll not found!\n");
117 return -1;
118 }
119
120 GdiHandleTable = MyGdiQueryTable();
121 if(!GdiHandleTable)
122 {
123 FreeLibrary(g_hModule);
124 printf("GdiHandleTable not found!\n");
125 return -1;
126 }
127
128 printf("\n");
129
130 return TestMain(L"w32knapi", L"win32k.sys Nt-Api");
131 }