ignore some warnings by gcc 4.4.2
[reactos.git] / rostests / apitests / w32knapi / w32knapi.c
index 685b464..088007d 100644 (file)
-#include "w32knapi.h"\r
-\r
-HINSTANCE g_hInstance;\r
-HMODULE g_hModule = NULL;\r
-INT g_nOs;\r
-PSYSCALL_ENTRY g_SyscallTable;\r
-\r
-BOOL\r
-InitOsVersion()\r
-{\r
-       OSVERSIONINFOW osv;\r
-       LPWSTR pszRos;\r
-\r
-       osv.dwOSVersionInfoSize = sizeof(osv);\r
-       GetVersionExW(&osv);\r
-       pszRos =  osv.szCSDVersion + wcslen(osv.szCSDVersion) + 1;\r
-       /* make sure the string is zero terminated */\r
-       osv.szCSDVersion[127] = 0;\r
-       /* Is ReactOS? */\r
-       if (wcsstr(pszRos, L"ReactOS") != NULL)\r
-       {\r
-               printf("Running on %ls\n", pszRos);\r
-               g_hModule = LoadLibraryW(L"w32kdll.dll");\r
-               if (!g_hModule)\r
-               {\r
-                       printf("w32kdll.dll not found!\n");\r
-                       return FALSE;\r
-               }\r
-               g_nOs = OS_REACTOS;\r
-               return TRUE;\r
-       }\r
-\r
-       if (osv.dwPlatformId != VER_PLATFORM_WIN32_NT)\r
-       {\r
-               printf("Unsupported OS\n");\r
-               return FALSE;\r
-       }\r
-\r
-       if (osv.dwMajorVersion == 5 && osv.dwMinorVersion == 1 && osv.dwBuildNumber == 2600)\r
-       {\r
-               printf("Running on Windows XP, build 2600\n");\r
-               g_nOs = OS_WINDOWS;\r
-               g_SyscallTable = SyscallTable_XP_2600;\r
-               return TRUE;\r
-       }\r
-\r
-       printf("Unsupported OS\n");\r
-\r
-       return FALSE;\r
-}\r
-\r
-\r
-static BOOL\r
-RosSyscall(LPWSTR lpszFunction, int cParams, void* pParams, DWORD* pResult)\r
-{\r
-       DWORD ret;\r
-       char szFunctionName[MAX_PATH];\r
-       int ParamSize = cParams * 4;\r
-\r
-       sprintf(szFunctionName, "%ls", lpszFunction);\r
-       FARPROC proc = (FARPROC)GetProcAddress(g_hModule, szFunctionName);\r
-       if (!proc)\r
-       {\r
-               printf("Couldn't find proc: %s\n", szFunctionName);\r
-               return FALSE;\r
-       }\r
-\r
-       asm volatile\r
-       (\r
-               "subl %%eax, %%esp;"    // calculate new stack pos\r
-               "movl %%esp, %%edi;"    // destination is stackpointer\r
-               "cld;"                                  // clear direction flag\r
-               "rep movsd;"                    // copy params to the stack\r
-               "call *%%edx"                   // call function\r
-               : "=a" (ret)\r
-               : "c" (cParams), "a" (ParamSize), "S"(pParams), "d"(proc)\r
-       );\r
-\r
-       *pResult = ret;\r
-       return TRUE;\r
-}\r
-\r
-static PSYSCALL_ENTRY\r
-GetSyscallEntry(LPWSTR lpszFunction)\r
-{\r
-       INT i;\r
-\r
-       for (i = 0; g_SyscallTable[i].lpszFunction != NULL; i++)\r
-       {\r
-               if (wcscmp(g_SyscallTable[i].lpszFunction, lpszFunction) == 0)\r
-               {\r
-                       return &g_SyscallTable[i];\r
-               }\r
-       }\r
-       return NULL;\r
-}\r
-\r
-static BOOL\r
-WinSyscall(LPWSTR pszFunction, void* pParams, void* pResult)\r
-{\r
-       PSYSCALL_ENTRY pEntry = GetSyscallEntry(pszFunction);\r
-       DWORD ret;\r
-\r
-       asm volatile ("int $0x2e\n" : "=a"(ret): "a" (pEntry->nSyscallNum), "d" (pParams));\\r
-       *((DWORD*)pResult) = ret;\r
-       return FALSE;\r
-}\r
-\r
-DWORD\r
-Syscall(LPWSTR pszFunction, int cParams, void* pParams)\r
-{\r
-    DWORD dwRet = 0;\r
-\r
-       if (g_nOs == OS_REACTOS)\r
-       {\r
-               RosSyscall(pszFunction, cParams, pParams, &dwRet);\r
-       }\r
-       else\r
-       {\r
-               WinSyscall(pszFunction, pParams, &dwRet);\r
-       }\r
-       return dwRet;\r
-}\r
-\r
-BOOL\r
-IsFunctionPresent(LPWSTR lpszFunction)\r
-{\r
-       if (g_nOs == OS_REACTOS)\r
-       {\r
-               char szFunctionName[MAX_PATH];\r
-               sprintf(szFunctionName, "%ls", lpszFunction);\r
-               return (GetProcAddress(g_hModule, szFunctionName) != NULL);\r
-       }\r
-\r
-       return (GetSyscallEntry(lpszFunction) != NULL);\r
-}\r
-\r
-int APIENTRY\r
-WinMain(HINSTANCE hInstance,\r
-        HINSTANCE hPrevInstance,\r
-        LPSTR     lpCmdLine,\r
-        int       nCmdShow)\r
-{\r
-       g_hInstance = hInstance;\r
-\r
-       printf("Win32k native API test\n");\r
-\r
-       /* Convert to gui thread */\r
-       IsGUIThread(TRUE);\r
-\r
-       if (!InitOsVersion())\r
-       {\r
-               return 0;\r
-       }\r
-\r
-       printf("\n");\r
-\r
-       TestMain(L"w32knapi", L"win32k.sys Nt-Api");\r
-\r
-       return 0;\r
-}\r
+#include "w32knapi.h"
+
+HINSTANCE g_hInstance;
+HMODULE g_hModule = NULL;
+PGDI_TABLE_ENTRY GdiHandleTable;
+
+static
+PGDI_TABLE_ENTRY
+MyGdiQueryTable()
+{
+       PTEB pTeb = NtCurrentTeb();
+       PPEB pPeb = pTeb->ProcessEnvironmentBlock;
+       printf("TEB::PEB=0x%lx PEB::GdiHT=0x%lx, Peb=%p, Teb=%p\n", FIELD_OFFSET(TEB, ProcessEnvironmentBlock), FIELD_OFFSET(PEB, GdiSharedHandleTable), pTeb, pPeb);
+       return pPeb->GdiSharedHandleTable;
+}
+
+BOOL
+IsHandleValid(HGDIOBJ hobj)
+{
+    USHORT Index = (ULONG_PTR)hobj;
+    PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
+
+    if (pentry->KernelData == NULL ||
+        pentry->KernelData < (PVOID)0x80000000 ||
+        (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
+    {
+        return FALSE;
+    }
+    
+    return TRUE;
+}
+
+PVOID
+GetHandleUserData(HGDIOBJ hobj)
+{
+    USHORT Index = (ULONG_PTR)hobj;
+    PGDI_TABLE_ENTRY pentry = &GdiHandleTable[Index];
+
+    if (pentry->KernelData == NULL ||
+        pentry->KernelData < (PVOID)0x80000000 ||
+        (USHORT)pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
+    {
+        return NULL;
+    }
+
+    return pentry->UserData;
+}
+
+
+static DWORD WINAPI
+IntSyscall(FARPROC proc, UINT cParams, PVOID pFirstParam)
+{
+       DWORD retval = 0;
+
+#ifdef _M_I386
+#ifdef __GNUC__
+       asm volatile
+       (
+               "pushfl;"                               // Save flags
+               "movl %%ecx, %%eax;"
+               "shl $2, %%eax;"                // Calculate param size
+               "subl %%eax, %%esp;"    // Calculate new stack pos
+               "movl %%esp, %%edi;"    // Destination is stackpointer
+               "cld;"                                  // Clear direction flag
+               "rep movsd;"                    // Copy params to the stack
+               "call *%%edx;"                  // Call function
+               "popfl;"                                // Restore flags
+               : "=a" (retval)
+               : "S" (pFirstParam), "c" (cParams), "d"(proc)
+               : "%edi"
+       );
+#else
+       __asm
+       {
+               pushf
+               mov eax, cParams
+               shl eax, 2
+               sub esp, eax
+               mov edi, esp
+               cld
+               rep movsd
+               call proc
+               mov retval, eax
+               popf
+    };
+#endif
+#endif
+
+       return retval;
+}
+
+DWORD
+Syscall(LPWSTR pszFunction, int cParams, void* pParams)
+{
+       char szFunctionName[MAX_PATH];
+       FARPROC proc;
+
+       sprintf(szFunctionName, "%ls", pszFunction);
+       proc = (FARPROC)GetProcAddress(g_hModule, szFunctionName);
+       if (!proc)
+       {
+               printf("Couldn't find proc: %s\n", szFunctionName);
+               return FALSE;
+       }
+
+       return IntSyscall(proc, cParams, pParams);
+}
+
+BOOL
+IsFunctionPresent(LPWSTR lpszFunction)
+{
+       char szFunctionName[MAX_PATH];
+       sprintf(szFunctionName, "%ls", lpszFunction);
+       return (GetProcAddress(g_hModule, szFunctionName) != NULL);
+}
+
+int APIENTRY
+WinMain(HINSTANCE hInstance,
+        HINSTANCE hPrevInstance,
+        LPSTR     lpCmdLine,
+        int       nCmdShow)
+{
+       g_hInstance = hInstance;
+
+       printf("Win32k native API test\n");
+
+       /* Convert to gui thread */
+       // IsGUIThread(TRUE); <- does not exists on win2k
+
+       InitOsVersion();
+       printf("g_OsIdx = %d\n", g_OsIdx);
+
+       g_hModule = LoadLibraryW(L"w32kdll.dll");
+       if (!g_hModule)
+       {
+               printf("w32kdll.dll not found!\n");
+               return -1;
+       }
+
+       GdiHandleTable = MyGdiQueryTable();
+       if(!GdiHandleTable)
+       {
+               FreeLibrary(g_hModule);
+               printf("GdiHandleTable not found!\n");
+               return -1;
+       }
+
+       printf("\n");
+
+       return TestMain(L"w32knapi", L"win32k.sys Nt-Api");
+}