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