1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * PROJECT:         ReactOS api tests
 * LICENSE:         GPL - See COPYING in the top level directory
 * PURPOSE:         Test for RegisterClassEx
 * PROGRAMMERS:     Giannis Adamopoulos
 */

#include "precomp.h"

static ATOM _RegisterClass(LPCWSTR lpwszClassName, HINSTANCE hInstance, UINT style, WNDPROC lpfnWndProc)
{
    WNDCLASSEXW wcex = {sizeof(WNDCLASSEXW), style, lpfnWndProc};
    wcex.lpszClassName  = lpwszClassName;
    wcex.hInstance      = hInstance;
    return RegisterClassExW(&wcex);
}

static ATOM _GetClassAtom(LPCWSTR lpwszClassName, HINSTANCE hInstance)
{
    WNDCLASSEXW wcex = {sizeof(WNDCLASSEXW)};
    return (ATOM)GetClassInfoExW(hInstance, lpwszClassName, &wcex);
}

static WNDPROC _GetWndproc(LPCWSTR lpwszClassName, HINSTANCE hInstance)
{
    WNDCLASSEXW wcex = {sizeof(WNDCLASSEXW)};
    BOOL ret = GetClassInfoExW(hInstance, lpwszClassName, &wcex);
    return ret ? wcex.lpfnWndProc : NULL;
}

static ATOM _RegisterClassA(LPCSTR lpzClassName, HINSTANCE hInstance, UINT style, WNDPROC lpfnWndProc)
{
    WNDCLASSEXA wcex = {sizeof(WNDCLASSEX), style, lpfnWndProc};
    wcex.lpszClassName  = lpzClassName;
    wcex.hInstance      = hInstance;
    return RegisterClassExA(&wcex);
}

static ATOM _GetClassAtomA(LPCSTR lpszClassName, HINSTANCE hInstance)
{
    WNDCLASSEXA wcex = {sizeof(WNDCLASSEX)};
    return (ATOM)GetClassInfoExA(hInstance, lpszClassName, &wcex);
}

static WNDPROC _GetWndprocA(LPCSTR lpszClassName, HINSTANCE hInstance)
{
    WNDCLASSEXA wcex = {sizeof(WNDCLASSEX)};
    BOOL ret = GetClassInfoExA(hInstance, lpszClassName, &wcex);
    return ret ? wcex.lpfnWndProc : NULL;
}

HANDLE _CreateActCtxFromFile(LPCWSTR FileName)
{
    ACTCTXW ActCtx = {sizeof(ACTCTX)};
    WCHAR buffer[MAX_PATH] , *separator;

    ok (GetModuleFileNameW(NULL, buffer, MAX_PATH), "GetModuleFileName failed\n");
    separator = wcsrchr(buffer, L'\\');
    if (separator)
        wcscpy(separator + 1, FileName);

    ActCtx.lpSource = buffer;

    return CreateActCtxW(&ActCtx);
}

VOID TestGlobalClasses(VOID)
{
    HMODULE hmod = GetModuleHandle(NULL);
    ATOM a,b,c,d,e;

    a = _GetClassAtom(L"TestClass1", hmod);
    b = _RegisterClass(L"TestClass1", hmod, 0, DefWindowProcW);
    c = _GetClassAtom(L"TestClass1", hmod);
    UnregisterClassW(L"TestClass1", hmod);
    d = _GetClassAtom(L"TestClass1", hmod);
    ok( a == 0, "\n");
    ok( b != 0, "\n");
    ok( c != 0, "\n");
    ok( d == 0, "\n");
    ok (b == c, "\n");
   
    a = _GetClassAtom(L"TestClass2", hmod);
    b = _RegisterClass(L"TestClass2", hmod, CS_GLOBALCLASS, DefWindowProcW);
    c = _GetClassAtom(L"TestClass2", hmod);
    UnregisterClassW(L"TestClass2", hmod);
    d = _GetClassAtom(L"TestClass2", hmod);
    ok( a == 0, "\n");
    ok( b != 0, "\n");
    ok( c != 0, "\n");
    ok( d == 0, "\n");
    ok (b == c, "\n");

    a = _RegisterClass(L"TestClass3", hmod, 0, DefWindowProcW);
    b = _RegisterClass(L"TestClass3", hmod, 0, DefWindowProcW);
    c = _RegisterClass(L"TestClass3", hmod, CS_GLOBALCLASS, DefWindowProcW);
    UnregisterClassW(L"TestClass3", hmod);
    d = _GetClassAtom(L"TestClass3", hmod);
    ok( a != 0, "\n");
    ok( b == 0, "\n");
    ok( c == 0, "\n");
    ok( d == 0, "\n");

    a = _RegisterClass(L"TestClass4", hmod, CS_GLOBALCLASS, DefWindowProcW);
    b = _RegisterClass(L"TestClass4", hmod, 0, DefWindowProcW);
    c = _RegisterClass(L"TestClass4", hmod, 0, DefWindowProcW);
    UnregisterClassW(L"TestClass4", hmod);
    d = _GetClassAtom(L"TestClass4", hmod);
    UnregisterClassW(L"TestClass4", hmod);
    e = _GetClassAtom(L"TestClass4", hmod);
    ok( a != 0, "\n");
    ok( b != 0, "\n");
    ok( c == 0, "\n");
    ok( d != 0, "\n");
    ok( e == 0, "\n");

    a = _GetClassAtom(L"ComboBox", hmod);
    b = _RegisterClass(L"ComboBox", hmod, 0, DefWindowProcW);
    c = _RegisterClass(L"ComboBox", hmod, CS_GLOBALCLASS, DefWindowProcW);
    UnregisterClassW(L"ComboBox", hmod);
    d = _GetClassAtom(L"ComboBox", hmod);
    UnregisterClassW(L"TestClass4", hmod);
    e = _GetClassAtom(L"TestClass4", hmod);
    ok( a != 0, "\n");
    ok( b != 0, "\n");
    ok( c == 0, "\n");
    ok( d != 0, "\n");
    ok( e == 0, "\n");
    
    a = _GetClassAtom(L"ScrollBar", hmod);
    UnregisterClassW(L"ScrollBar", hmod);
    b = _GetClassAtom(L"ScrollBar", hmod);
    c = _RegisterClass(L"ScrollBar", hmod, CS_GLOBALCLASS, DefWindowProcW);<--- c is assigned
    d = _GetClassAtom(L"ScrollBar", hmod);<--- d is assigned
    ok( a != 0, "Wrong value for a. Expected != 0, got 0\n");
    ok( b == 0, "Wrong value for b. Expected == 0, got %d\n", b);
    //ok( c != 0, "Wrong value for c. Expected != 0, got 0\n");
    //ok( d != 0, "Wrong value for d. Expected != 0, got 0\n");
    //ok_int(a, c);
    //ok_int(a, d); /* In Windows 10 and WHS testbot the last 4 tests fail */
    
    a = _GetClassAtom(L"ListBox", (HMODULE)0xdead);
    UnregisterClassW(L"ListBox", (HMODULE)0xdead);
    b = _GetClassAtom(L"ListBox", (HMODULE)0xdead);
    ok( a != 0, "\n");
    ok( b == 0, "\n");
    
    a = _RegisterClass(L"TestClass5", (HMODULE)0xdead, CS_GLOBALCLASS, DefWindowProcW);
    b = _GetClassAtom(L"TestClass5", hmod);
    UnregisterClassW(L"TestClass5", hmod);
    c = _GetClassAtom(L"TestClass5", (HMODULE)0xdead);<--- c is overwritten
    d = _GetClassAtom(L"TestClass5", hmod);<--- d is overwritten
    ok( a != 0, "\n");
    ok( b != 0, "\n");    
    ok( c == 0, "\n");
    ok( d == 0, "\n");    
}

VOID TestVersionedClasses(VOID)
{
    HMODULE hmod = GetModuleHandle(NULL);
    HANDLE h1, h2;
    ULONG_PTR cookie1, cookie2;
    ATOM a,b,c,d;
    WNDPROC proc1,proc2,proc3, proc4, proc5;
    WCHAR buffer[50];


    h1 = _CreateActCtxFromFile(L"verclasstest1.manifest");
    h2 = _CreateActCtxFromFile(L"verclasstest2.manifest");
    if (h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
    {
        skip("Loading manifests failed. Skipping TestVersionedClasses\n");
        return;
    }
    
    a = _RegisterClass(L"VersionTestClass1", hmod, 0, DefWindowProcA);
    proc1 = _GetWndproc(L"VersionTestClass1", hmod);
    b = _RegisterClass(L"VersionTestClass1", hmod, 0, DefWindowProcW);
    ActivateActCtx(h1, &cookie1);
    proc2 = _GetWndproc(L"VersionTestClass1", hmod);
    c = _RegisterClass(L"VersionTestClass1", hmod, 0, DefWindowProcW);
    d = _GetClassAtom(L"VersionTestClass1", hmod);
    proc3 = _GetWndproc(L"VersionTestClass1", hmod);
    proc4 = _GetWndproc((LPCWSTR)(DWORD_PTR)a, hmod);
    DeactivateActCtx(0, cookie1);
    proc5 = _GetWndproc(L"VersionTestClass1", hmod);
    ok( a != 0, "\n");
    ok( b == 0, "\n");
    ok( c != 0, "\n");
    ok( d != 0, "\n");
    ok( a == c, "\n");
    ok( a == d, "\n");
    ok (proc1 == DefWindowProcA, "\n");
    ok (proc2 == NULL, "Got 0x%p, expected NULL\n", proc2);
    ok (proc3 == DefWindowProcW, "Got 0x%p, expected 0x%p\n", proc3, DefWindowProcW);
    ok (proc4 == DefWindowProcW, "Got 0x%p, expected 0x%p\n", proc4, DefWindowProcW);
    ok (proc5 == DefWindowProcA, "\n");
    
    a = _GetClassAtom(L"Button", hmod);
    b = _RegisterClass(L"Button", hmod, CS_GLOBALCLASS, DefWindowProcA);
    proc1 = _GetWndproc(L"Button", (HMODULE)0xdead);
    ActivateActCtx(h2, &cookie1);
    c = _RegisterClass(L"Button", hmod, CS_GLOBALCLASS, DefWindowProcA);
    proc2 = _GetWndproc(L"Button", (HMODULE)0xdead);
    d = _GetClassAtom(L"3.3.3.3!Button", (HMODULE)0xdead);
    proc3 = _GetWndproc(L"3.3.3.3!Button", (HMODULE)0xdead);
    ok( a != 0, "\n");
    ok( b == 0, "\n");
    ok( c != 0, "\n");
    ok( d != 0, "\n");
    ok( a == c, "\n");
    ok( d == a, "\n");
    ok( proc1 != NULL, "\n");
    ok( proc1 != DefWindowProcA, "Got 0x%p, expected not 0x%p\n", proc1, DefWindowProcA);
    ok( proc2 == DefWindowProcA, "Got 0x%p, expected 0x%p\n", proc2, DefWindowProcA);
    ok( proc3 == DefWindowProcA, "Got 0x%p, expected 0x%p\n", proc3, DefWindowProcA);
    
    a = _RegisterClass(L"VersionTestClass2", hmod, CS_GLOBALCLASS, DefWindowProcW);
    proc1 = _GetWndproc(L"VersionTestClass2", (HMODULE)0xdead);
    b = _RegisterClass(L"VersionTestClass2", hmod, 0, DefWindowProcA);
    proc2 = _GetWndproc(L"VersionTestClass2", hmod);
    proc3 = _GetWndproc(L"VersionTestClass2", (HMODULE)0xdead);
    ok (a != 0, "\n");
    ok (b != 0, "\n");
    ok (a == b, "\n");
    ok (proc1 == DefWindowProcW, "Got 0x%p, expected 0x%p\n", proc1, DefWindowProcW);
    ok (proc2 == DefWindowProcA, "Got 0x%p, expected 0x%p\n", proc2, DefWindowProcA);
    ok (proc3 == DefWindowProcW, "Got 0x%p, expected 0x%p\n", proc2, DefWindowProcA);

    a = _RegisterClass(L"VersionTestClass3", hmod, 0, DefWindowProcW);
    swprintf(buffer, L"#%d", a);
    proc1 = _GetWndproc((LPCWSTR)(DWORD_PTR)a, hmod);
    proc2 = _GetWndproc(buffer, hmod);
    ok (a != 0, "\n");
    ok (proc1 == DefWindowProcW, "\n");
    ok (proc2 == 0, "Got 0x%p for %S, expected 0\n", proc2, buffer);
    DeactivateActCtx(0, cookie1);

    a = _RegisterClass(L"VersionTestClass3", hmod, 0, DefWindowProcW);
    swprintf(buffer, L"#%d", a);
    proc1 = _GetWndproc((LPCWSTR)(DWORD_PTR)a, hmod);
    proc2 = _GetWndproc(buffer, hmod);
    ok (a != 0, "\n");
    ok (proc1 == DefWindowProcW, "\n");
    ok (proc2 == 0, "Got 0x%p for %S, expected 0\n", proc2, buffer);

    ActivateActCtx(h2, &cookie1);
    a = _RegisterClassA("VersionTestClass7", hmod, 0, DefWindowProcW);
    b = _GetClassAtomA("VersionTestClass7", hmod);
    proc1 = _GetWndprocA("VersionTestClass7", hmod);
    proc2 = _GetWndprocA((LPCSTR)(DWORD_PTR)a, hmod);
    ok(a != 0, "\n");
    ok(b != 0, "\n");
    ok(a == b, "\n");
    ok (proc1 == DefWindowProcW, "\n");
    ok (proc2 == DefWindowProcW, "\n");

    DeactivateActCtx(0, cookie1);

    proc1 = _GetWndproc(L"Button", 0);
    ActivateActCtx(h2, &cookie1);
    ActivateActCtx(h1, &cookie2);
    proc2 = _GetWndproc(L"Button", 0);
    DeactivateActCtx(0, cookie2);
    ActivateActCtx(0, &cookie2);
    proc3 = _GetWndproc(L"Button", 0);
    DeactivateActCtx(0, cookie2);
    DeactivateActCtx(0, cookie1);
    ok (proc1 != 0, "\n");
    ok (proc2 != 0, "\n");
    ok (proc4 != 0, "\n");
    ok (proc1 == proc2, "\n");
    ok (proc1 == proc3, "\n");

}

START_TEST(RegisterClassEx)
{
    TestGlobalClasses();
    TestVersionedClasses();
}