xref: /reactos/win32ss/printing/base/winspool/main.c (revision 64daf542)
1 /*
2  * PROJECT:     ReactOS Spooler API
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Main functions
5  * COPYRIGHT:   Copyright 2015-2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 // Global Variables
11 HANDLE hProcessHeap;
12 
13 
14 handle_t __RPC_USER
15 WINSPOOL_HANDLE_bind(WINSPOOL_HANDLE wszName)
16 {
17     handle_t hBinding;
18     PWSTR wszStringBinding;
19     RPC_STATUS Status;
20 
21     // Get us a string binding handle from the supplied connection information
22     Status = RpcStringBindingComposeW(NULL, L"ncalrpc", NULL, L"spoolss", NULL, &wszStringBinding);
23     if (Status != RPC_S_OK)
24     {
25         ERR("RpcStringBindingComposeW failed with status %ld!\n", Status);
26         return NULL;
27     }
28 
29     // Get a handle_t binding handle from the string binding handle
30     Status = RpcBindingFromStringBindingW(wszStringBinding, &hBinding);
31     if (Status != RPC_S_OK)
32     {
33         ERR("RpcBindingFromStringBindingW failed with status %ld!\n", Status);
34         return NULL;
35     }
36 
37     // Free the string binding handle
38     Status = RpcStringFreeW(&wszStringBinding);
39     if (Status != RPC_S_OK)
40     {
41         ERR("RpcStringFreeW failed with status %ld!\n", Status);
42         return NULL;
43     }
44 
45     return hBinding;
46 }
47 
48 void __RPC_USER
49 WINSPOOL_HANDLE_unbind(WINSPOOL_HANDLE wszName, handle_t hBinding)
50 {
51     RPC_STATUS Status;
52 
53     Status = RpcBindingFree(&hBinding);
54     if (Status != RPC_S_OK)
55     {
56         ERR("RpcBindingFree failed with status %ld!\n", Status);
57     }
58 }
59 
60 void __RPC_FAR* __RPC_USER
61 midl_user_allocate(SIZE_T len)
62 {
63     return HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, len);
64 }
65 
66 void __RPC_USER
67 midl_user_free(void __RPC_FAR* ptr)
68 {
69     HeapFree(hProcessHeap, 0, ptr);
70 }
71 
72 BOOL WINAPI
73 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
74 {
75     TRACE("DllMain(%p, %lu, %p)\n", hinstDLL, fdwReason, lpvReserved);
76 
77     switch (fdwReason)
78     {
79         case DLL_PROCESS_ATTACH:
80             DisableThreadLibraryCalls(hinstDLL);
81             hProcessHeap = GetProcessHeap();
82             break;
83     }
84 
85     return TRUE;
86 }
87 
88 BOOL WINAPI
89 SpoolerInit(VOID)
90 {
91     BOOL bReturnValue = FALSE;
92     DWORD dwErrorCode;
93 
94     TRACE("SpoolerInit()\n");
95 
96     // Nothing to initialize here yet, but pass this call to the Spool Service as well.
97     RpcTryExcept
98     {
99         dwErrorCode = _RpcSpoolerInit();
100         SetLastError(dwErrorCode);
101         bReturnValue = (dwErrorCode == ERROR_SUCCESS);
102     }
103     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
104     {
105         ERR("_RpcSpoolerInit failed with exception code %lu!\n", RpcExceptionCode());
106     }
107     RpcEndExcept;
108 
109     return bReturnValue;
110 }
111