ignore some warnings by gcc 4.4.2
[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 printf("TEB::PEB=0x%lx PEB::GdiHT=0x%lx, Peb=%p, Teb=%p\n", FIELD_OFFSET(TEB, ProcessEnvironmentBlock), FIELD_OFFSET(PEB, GdiSharedHandleTable), pTeb, pPeb);
14 return pPeb->GdiSharedHandleTable;
15 }
16
17 BOOL
18 IsHandleValid(HGDIOBJ hobj)
19 {
20 USHORT Index = (ULONG_PTR)hobj;
21 PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
22
23 if (pentry->KernelData == NULL ||
24 pentry->KernelData < (PVOID)0x80000000 ||
25 (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
26 {
27 return FALSE;
28 }
29
30 return TRUE;
31 }
32
33 PVOID
34 GetHandleUserData(HGDIOBJ hobj)
35 {
36 USHORT Index = (ULONG_PTR)hobj;
37 PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
38
39 if (pentry->KernelData == NULL ||
40 pentry->KernelData < (PVOID)0x80000000 ||
41 (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
42 {
43 return NULL;
44 }
45
46 return pentry->UserData;
47 }
48
49
50 static DWORD WINAPI
51 IntSyscall(FARPROC proc, UINT cParams, PVOID pFirstParam)
52 {
53 DWORD retval = 0;
54
55 #ifdef _M_I386
56 #ifdef __GNUC__
57 asm volatile
58 (
59 "pushfl;" // Save flags
60 "movl %%ecx, %%eax;"
61 "shl $2, %%eax;" // Calculate param size
62 "subl %%eax, %%esp;" // Calculate new stack pos
63 "movl %%esp, %%edi;" // Destination is stackpointer
64 "cld;" // Clear direction flag
65 "rep movsd;" // Copy params to the stack
66 "call *%%edx;" // Call function
67 "popfl;" // Restore flags
68 : "=a" (retval)
69 : "S" (pFirstParam), "c" (cParams), "d"(proc)
70 : "%edi"
71 );
72 #else
73 __asm
74 {
75 pushf
76 mov eax, cParams
77 shl eax, 2
78 sub esp, eax
79 mov edi, esp
80 cld
81 rep movsd
82 call proc
83 mov retval, eax
84 popf
85 };
86 #endif
87 #endif
88
89 return retval;
90 }
91
92 DWORD
93 Syscall(LPWSTR pszFunction, int cParams, void* pParams)
94 {
95 char szFunctionName[MAX_PATH];
96 FARPROC proc;
97
98 sprintf(szFunctionName, "%ls", pszFunction);
99 proc = (FARPROC)GetProcAddress(g_hModule, szFunctionName);
100 if (!proc)
101 {
102 printf("Couldn't find proc: %s\n", szFunctionName);
103 return FALSE;
104 }
105
106 return IntSyscall(proc, cParams, pParams);
107 }
108
109 BOOL
110 IsFunctionPresent(LPWSTR lpszFunction)
111 {
112 char szFunctionName[MAX_PATH];
113 sprintf(szFunctionName, "%ls", lpszFunction);
114 return (GetProcAddress(g_hModule, szFunctionName) != NULL);
115 }
116
117 int APIENTRY
118 WinMain(HINSTANCE hInstance,
119 HINSTANCE hPrevInstance,
120 LPSTR lpCmdLine,
121 int nCmdShow)
122 {
123 g_hInstance = hInstance;
124
125 printf("Win32k native API test\n");
126
127 /* Convert to gui thread */
128 // IsGUIThread(TRUE); <- does not exists on win2k
129
130 InitOsVersion();
131 printf("g_OsIdx = %d\n", g_OsIdx);
132
133 g_hModule = LoadLibraryW(L"w32kdll.dll");
134 if (!g_hModule)
135 {
136 printf("w32kdll.dll not found!\n");
137 return -1;
138 }
139
140 GdiHandleTable = MyGdiQueryTable();
141 if(!GdiHandleTable)
142 {
143 FreeLibrary(g_hModule);
144 printf("GdiHandleTable not found!\n");
145 return -1;
146 }
147
148 printf("\n");
149
150 return TestMain(L"w32knapi", L"win32k.sys Nt-Api");
151 }