96ae78ca2a45042bcfb6c384df0325c22ea75710
[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 retval = 0;
53
54 #ifdef _M_I386
55 #ifdef __GNUC__
56 asm volatile
57 (
58 "pushfl;" // Save flags
59 "movl %%ecx, %%eax;"
60 "shl $2, %%eax;" // Calculate param size
61 "subl %%eax, %%esp;" // Calculate new stack pos
62 "movl %%esp, %%edi;" // Destination is stackpointer
63 "cld;" // Clear direction flag
64 "rep movsd;" // Copy params to the stack
65 "call *%%edx;" // Call function
66 "popfl;" // Restore flags
67 : "=a" (retval)
68 : "S" (pFirstParam), "c" (cParams), "d"(proc)
69 : "%edi"
70 );
71 #else
72 __asm
73 {
74 pushf
75 mov eax, cParams
76 shl eax, 2
77 sub esp, eax
78 mov edi, esp
79 cld
80 rep movsd
81 call proc
82 mov retval, eax
83 popf
84 };
85 #endif
86 #endif
87
88 return retval;
89 }
90
91 DWORD
92 Syscall(LPWSTR pszFunction, int cParams, void* pParams)
93 {
94 char szFunctionName[MAX_PATH];
95 FARPROC proc;
96
97 sprintf(szFunctionName, "%ls", pszFunction);
98 proc = (FARPROC)GetProcAddress(g_hModule, szFunctionName);
99 if (!proc)
100 {
101 printf("Couldn't find proc: %s\n", szFunctionName);
102 return FALSE;
103 }
104
105 return IntSyscall(proc, cParams, pParams);
106 }
107
108 BOOL
109 IsFunctionPresent(LPWSTR lpszFunction)
110 {
111 char szFunctionName[MAX_PATH];
112 sprintf(szFunctionName, "%ls", lpszFunction);
113 return (GetProcAddress(g_hModule, szFunctionName) != NULL);
114 }
115
116 int APIENTRY
117 WinMain(HINSTANCE hInstance,
118 HINSTANCE hPrevInstance,
119 LPSTR lpCmdLine,
120 int nCmdShow)
121 {
122 g_hInstance = hInstance;
123
124 printf("Win32k native API test\n");
125
126 /* Convert to gui thread */
127 // IsGUIThread(TRUE); <- does not exists on win2k
128
129 InitOsVersion();
130 printf("g_OsIdx = %d\n", g_OsIdx);
131
132 g_hModule = LoadLibraryW(L"w32kdll.dll");
133 if (!g_hModule)
134 {
135 printf("w32kdll.dll not found!\n");
136 return -1;
137 }
138
139 GdiHandleTable = MyGdiQueryTable();
140 if(!GdiHandleTable)
141 {
142 FreeLibrary(g_hModule);
143 printf("GdiHandleTable not found!\n");
144 return -1;
145 }
146
147 printf("\n");
148
149 return TestMain(L"w32knapi", L"win32k.sys Nt-Api");
150 }