[LOCALSPL_APITEST]
[reactos.git] / rostests / apitests / localspl / dll / main.c
1 /*
2 * PROJECT: ReactOS Local Spooler API Tests Injected DLL
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Main functions
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
6 */
7
8 #define __ROS_LONG64__
9
10 #define STANDALONE
11 #include <apitest.h>
12
13 #define WIN32_NO_STATUS
14 #include <io.h>
15 #include <windef.h>
16 #include <winbase.h>
17 #include <wingdi.h>
18 #include <winreg.h>
19 #include <winspool.h>
20 #include <winsplp.h>
21
22
23 #include "../localspl_apitest.h"
24
25 //#define NDEBUG
26 #include <debug.h>
27
28 // Test list
29 extern void func_fpEnumPrinters(void);
30
31 const struct test winetest_testlist[] =
32 {
33 { "fpEnumPrinters", func_fpEnumPrinters },
34
35 { 0, 0 }
36 };
37
38 // Running the tests from the injected DLL and redirecting their output to the pipe.
39 BOOL WINAPI
40 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
41 {
42 char szTestName[150];
43 DWORD cbRead;
44 FILE* fpStdout;
45 HANDLE hCommandPipe;
46 int iOldStdout;
47
48 // We only want to run our test once when the DLL is injected to the process.
49 if (fdwReason != DLL_PROCESS_ATTACH)
50 return TRUE;
51
52 // Read the test to run from the command pipe.
53 hCommandPipe = CreateFileW(COMMAND_PIPE_NAME, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
54 if (hCommandPipe == INVALID_HANDLE_VALUE)
55 {
56 DPRINT("DLL: CreateFileW failed for the command pipe with error %lu!\n", GetLastError());
57 return FALSE;
58 }
59
60 if (!ReadFile(hCommandPipe, szTestName, sizeof(szTestName), &cbRead, NULL))
61 {
62 DPRINT("DLL: ReadFile failed for the command pipe with error %lu!\n", GetLastError());
63 return FALSE;
64 }
65
66 CloseHandle(hCommandPipe);
67
68 // Check if the test name is valid.
69 if (!find_test(szTestName))
70 {
71 DPRINT("DLL: Got invalid test name \"%s\"!\n", szTestName);
72 return FALSE;
73 }
74
75 // Backup our current stdout and set it to the output pipe.
76 iOldStdout = _dup(_fileno(stdout));
77 fpStdout = _wfreopen(OUTPUT_PIPE_NAME, L"w", stdout);
78 setbuf(stdout, NULL);
79
80 // Run the test.
81 run_test(szTestName);
82
83 // Restore stdout to the previous value.
84 fclose(fpStdout);
85 _dup2(iOldStdout, _fileno(stdout));
86
87 // Return FALSE so that our DLL is immediately unloaded.
88 return FALSE;
89 }