1 /*
2  * PROJECT:         ReactOS API tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Test for loading/unloading user32.dll
5  * PROGRAMMERS:     Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <apitest.h>
9 #include <ndk/pstypes.h>
10 #include <ndk/rtlfuncs.h>
11 
12 START_TEST(load)
13 {
14     HMODULE hUser32;
15     HMODULE hUser32_2;
16     BOOL Ret;
17     DWORD Error;
18     PPEB Peb = NtCurrentPeb();
19     PTEB Teb = NtCurrentTeb();
20 
21     /* Before init */
22     hUser32 = GetModuleHandleW(L"user32");
23     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
24     ok(Peb->KernelCallbackTable == NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
25     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
26     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
27 
28     SetLastError(12345);
29     hUser32 = LoadLibraryW(L"user32");
30     Error = GetLastError();
31     ok(hUser32 != NULL, "LoadLibrary failed\n");
32     ok(Error != 12345, "Error = %lu\n", Error);
33 
34     /* Initialized */
35     hUser32_2 = GetModuleHandleW(L"user32");
36     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
37     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
38     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
39     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
40 
41     SetLastError(12345);
42     hUser32_2 = LoadLibraryW(L"user32");
43     Error = GetLastError();
44     ok(hUser32_2 == hUser32, "LoadLibrary failed\n");
45     ok(Error == 12345, "Error = %lu\n", Error);
46 
47     /* Still initialized */
48     hUser32_2 = GetModuleHandleW(L"user32");
49     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
50     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
51     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
52     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
53 
54     SetLastError(12345);
55     Ret = FreeLibrary(hUser32);
56     Error = GetLastError();
57     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
58     ok(Error == 12345, "Error = %lu\n", Error);
59 
60     /* Still initialized */
61     hUser32_2 = GetModuleHandleW(L"user32");
62     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
63     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
64     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
65     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
66 
67     SetLastError(12345);
68     Ret = FreeLibrary(hUser32);
69     Error = GetLastError();
70     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
71     ok(Error == 12345, "Error = %lu\n", Error);
72 
73     /* Somebody kept an extra reference! */
74     hUser32_2 = GetModuleHandleW(L"user32");
75     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
76     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
77     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
78     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
79 
80     SetLastError(12345);
81     Ret = FreeLibrary(hUser32);
82     Error = GetLastError();
83     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
84     ok(Error == 12345, "Error = %lu\n", Error);
85 
86     /* Uninitialized */
87     hUser32 = GetModuleHandleW(L"user32");
88     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
89     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
90     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
91     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
92 
93     SetLastError(12345);
94     hUser32 = LoadLibraryW(L"user32");
95     Error = GetLastError();
96     ok(hUser32 != NULL, "LoadLibrary failed\n");
97     ok(Error != 12345, "Error = %lu\n", Error);
98 
99     /* Initialized again */
100     hUser32_2 = GetModuleHandleW(L"user32");
101     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
102     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
103     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
104     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
105 
106     SetLastError(12345);
107     Ret = FreeLibrary(hUser32);
108     Error = GetLastError();
109     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
110     ok(Error == 12345, "Error = %lu\n", Error);
111 
112     /* Extra reference again */
113     hUser32_2 = GetModuleHandleW(L"user32");
114     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
115     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
116     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
117     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
118 
119     SetLastError(12345);
120     Ret = FreeLibrary(hUser32);
121     Error = GetLastError();
122     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
123     ok(Error == 12345, "Error = %lu\n", Error);
124 
125     /* Uninitialized again */
126     hUser32 = GetModuleHandleW(L"user32");
127     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
128     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
129     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
130     ok(Teb->Win32ClientInfo != NULL, "Win32ClientInfo = %p\n", Teb->Win32ClientInfo);
131 }
132