[LOCALSPL]
[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 PVOID
71 GetSpoolssFunc(const char* FunctionName)
72 {
73 HMODULE hSpoolss;
74
75 // Get us a handle to the loaded spoolss.dll.
76 hSpoolss = GetModuleHandleW(L"spoolss");
77 if (!hSpoolss)
78 {
79 skip("GetModuleHandleW failed with error %u!\n", GetLastError());
80 return FALSE;
81 }
82
83 return GetProcAddress(hSpoolss, FunctionName);
84 }
85
86 // Running the tests from the injected DLL and redirecting their output to the pipe.
87 BOOL WINAPI
88 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
89 {
90 char szTestName[150];
91 DWORD cbRead;
92 FILE* fpStdout;
93 HANDLE hCommandPipe;
94 int iOldStdout;
95
96 // We only want to run our test once when the DLL is injected to the process.
97 if (fdwReason != DLL_PROCESS_ATTACH)
98 return TRUE;
99
100 // Read the test to run from the command pipe.
101 hCommandPipe = CreateFileW(COMMAND_PIPE_NAME, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
102 if (hCommandPipe == INVALID_HANDLE_VALUE)
103 {
104 DPRINT("DLL: CreateFileW failed for the command pipe with error %lu!\n", GetLastError());
105 return FALSE;
106 }
107
108 if (!ReadFile(hCommandPipe, szTestName, sizeof(szTestName), &cbRead, NULL))
109 {
110 DPRINT("DLL: ReadFile failed for the command pipe with error %lu!\n", GetLastError());
111 return FALSE;
112 }
113
114 CloseHandle(hCommandPipe);
115
116 // Check if the test name is valid.
117 if (!find_test(szTestName))
118 {
119 DPRINT("DLL: Got invalid test name \"%s\"!\n", szTestName);
120 return FALSE;
121 }
122
123 // Backup our current stdout and set it to the output pipe.
124 iOldStdout = _dup(_fileno(stdout));
125 fpStdout = _wfreopen(OUTPUT_PIPE_NAME, L"w", stdout);
126 setbuf(stdout, NULL);
127
128 // Run the test.
129 run_test(szTestName);
130
131 // Restore stdout to the previous value.
132 fclose(fpStdout);
133 _dup2(iOldStdout, _fileno(stdout));
134
135 // Return FALSE so that our DLL is immediately unloaded.
136 return FALSE;
137 }