fb2885ba882c8af9eaa1e08c452f383b7f6e58b7
[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-2016 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 #include "../localspl_apitest.h"
23
24 //#define NDEBUG
25 #include <debug.h>
26
27 // Test list
28 extern void func_fpEnumPrinters(void);
29 extern void func_fpGetPrintProcessorDirectory(void);
30
31 const struct test winetest_testlist[] =
32 {
33 { "fpEnumPrinters", func_fpEnumPrinters },
34 { "fpGetPrintProcessorDirectory", func_fpGetPrintProcessorDirectory },
35 { 0, 0 }
36 };
37
38 BOOL
39 GetLocalsplFuncs(LPPRINTPROVIDOR pp)
40 {
41 HMODULE hLocalspl;
42 PInitializePrintProvidor pfnInitializePrintProvidor;
43
44 // Get us a handle to the loaded localspl.dll.
45 hLocalspl = GetModuleHandleW(L"localspl");
46 if (!hLocalspl)
47 {
48 skip("GetModuleHandleW failed with error %u!\n", GetLastError());
49 return FALSE;
50 }
51
52 // Get a pointer to its InitializePrintProvidor function.
53 pfnInitializePrintProvidor = (PInitializePrintProvidor)GetProcAddress(hLocalspl, "InitializePrintProvidor");
54 if (!pfnInitializePrintProvidor)
55 {
56 skip("GetProcAddress failed with error %u!\n", GetLastError());
57 return FALSE;
58 }
59
60 // Get localspl's function pointers.
61 if (!pfnInitializePrintProvidor(pp, sizeof(PRINTPROVIDOR), NULL))
62 {
63 skip("pfnInitializePrintProvidor failed with error %u!\n", GetLastError());
64 return FALSE;
65 }
66
67 return TRUE;
68 }
69
70 // Running the tests from the injected DLL and redirecting their output to the pipe.
71 BOOL WINAPI
72 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
73 {
74 char szTestName[150];
75 DWORD cbRead;
76 FILE* fpStdout;
77 HANDLE hCommandPipe;
78 int iOldStdout;
79
80 // We only want to run our test once when the DLL is injected to the process.
81 if (fdwReason != DLL_PROCESS_ATTACH)
82 return TRUE;
83
84 // Read the test to run from the command pipe.
85 hCommandPipe = CreateFileW(COMMAND_PIPE_NAME, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
86 if (hCommandPipe == INVALID_HANDLE_VALUE)
87 {
88 DPRINT("DLL: CreateFileW failed for the command pipe with error %lu!\n", GetLastError());
89 return FALSE;
90 }
91
92 if (!ReadFile(hCommandPipe, szTestName, sizeof(szTestName), &cbRead, NULL))
93 {
94 DPRINT("DLL: ReadFile failed for the command pipe with error %lu!\n", GetLastError());
95 return FALSE;
96 }
97
98 CloseHandle(hCommandPipe);
99
100 // Check if the test name is valid.
101 if (!find_test(szTestName))
102 {
103 DPRINT("DLL: Got invalid test name \"%s\"!\n", szTestName);
104 return FALSE;
105 }
106
107 // Backup our current stdout and set it to the output pipe.
108 iOldStdout = _dup(_fileno(stdout));
109 fpStdout = _wfreopen(OUTPUT_PIPE_NAME, L"w", stdout);
110 setbuf(stdout, NULL);
111
112 // Run the test.
113 run_test(szTestName);
114
115 // Restore stdout to the previous value.
116 fclose(fpStdout);
117 _dup2(iOldStdout, _fileno(stdout));
118
119 // Return FALSE so that our DLL is immediately unloaded.
120 return FALSE;
121 }